Class: ZabbixRubyClient::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbix-ruby-client/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, tasks) ⇒ Runner

Returns a new instance of Runner.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/zabbix-ruby-client/runner.rb', line 10

def initialize(config, tasks)
  @config = config
  @tasks = tasks

  @store = ZabbixRubyClient::Store.new(
    @config['datadir'],
    @config['zabbix']['host'],
    @config['taskfile'],
    @config['keepdata']
  )

  @data = ZabbixRubyClient::Data.new(@config['host'])
  @logsdir = makedir(@config['logsdir'], 'logs')
  ZabbixRubyClient::Plugins.scan_dirs([ PLUGINDIR ] + @config['plugindirs'])
  ZabbixRubyClient::Log.set_logger(File.join(@logsdir, 'zrc.log'), @config['loglevel'])
  ZabbixRubyClient::Log.debug @config.inspect
  @is_22 = /v2\.[24]\./.match zabbix_sender_version
  ZabbixRubyClient::Log.debug "zabbix sender version #{zabbix_sender_version}"
end

Instance Method Details

#collectObject



30
31
32
33
34
# File 'lib/zabbix-ruby-client/runner.rb', line 30

def collect
  @tasks.each do |plugin|
    @data.run_plugin(plugin['name'], plugin['args'])
  end
end

#showObject



36
37
38
39
40
# File 'lib/zabbix-ruby-client/runner.rb', line 36

def show
  @data.merge.each do |line|
    puts line
  end
end

#uploadObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/zabbix-ruby-client/runner.rb', line 42

def upload
  file = @store.record(@data.merge)
  command = "#{@config['zabbix']['sender']} -z #{@config['zabbix']['host']} -p #{@config['zabbix']['port']} -T -i #{file}"
  ZabbixRubyClient::Log.debug command
  begin
    res = `#{command}`
    if @is_22
      case $?.to_i
      when 0
        ZabbixRubyClient::Log.debug "zabbix-sender: Data Sent (#{$?})"
      when 1
        @store.keepdata(file)
        ZabbixRubyClient::Log.error "zabbix-sender: Sending failed (#{$?})"
        ZabbixRubyClient::Log.error res
      when 256
        @store.keepdata(file)
        ZabbixRubyClient::Log.warn "zabbix-sender: shit happens (unknown code 256)"
        ZabbixRubyClient::Log.warn res
      when 512
        ZabbixRubyClient::Log.warn "zabbix-sender: Some values failed"
        ZabbixRubyClient::Log.warn res
      else
        ZabbixRubyClient::Log.error "zabbix-sender: Something failed. #{$i}. code #{$?.to_i} was returned"
        ZabbixRubyClient::Log.error res
      end
    else
      if $?.to_i != 0
        ZabbixRubyClient::Log.warn "v2.0"
        @store.keepdata(file)
        ZabbixRubyClient::Log.error "zabbix-sender: Sending failed"
        ZabbixRubyClient::Log.error res
      else
        ZabbixRubyClient::Log.debug "zabbix-sender: Data Sent"
      end
    end
  rescue Exception => e
    @store.keepdata(file)
    ZabbixRubyClient::Log.error "zabbix-sender: broken."
    ZabbixRubyClient::Log.error e.message
  end
end