Class: ZabbixRubyClient

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbix-ruby-client/plugins/cpu.rb,
lib/zabbix-ruby-client.rb,
lib/zabbix-ruby-client/cli.rb,
lib/zabbix-ruby-client/logger.rb,
lib/zabbix-ruby-client/plugins.rb,
lib/zabbix-ruby-client/version.rb,
lib/zabbix-ruby-client/plugins/disk.rb,
lib/zabbix-ruby-client/plugins/apache.rb,
lib/zabbix-ruby-client/plugins/memory.rb,
lib/zabbix-ruby-client/plugins/network.rb

Overview

Defined Under Namespace

Modules: Logger, Plugins Classes: Cli

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ ZabbixRubyClient



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

def initialize(config_file)
  begin
    @config ||= YAML::load_file(config_file)
  rescue Exception => e
    puts "Configuration file cannot be read"
    puts e.message
    return
  end
  @logsdir = makedir(@config['logsdir'],'logs')
  @datadir = makedir(@config['datadir'],'data')
  @plugindirs = [ File.expand_path("../zabbix-ruby-client/plugins", __FILE__) ]
  if @config["plugindirs"]
    @plugindirs = @plugindirs + @config["plugindirs"]
  end
  @discover = {}
  @data = []
  Plugins.load_dirs @plugindirs
  logger.debug @config.inspect
end

Instance Method Details

#collectObject



57
58
59
60
61
# File 'lib/zabbix-ruby-client.rb', line 57

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

#datafileObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/zabbix-ruby-client.rb', line 28

def datafile
  now = Time.now
  @datafile ||= if @config['keepdata']
    unless Dir.exists? File.join(@datadir,Time.now.strftime("%Y-%m-%d")) 
      FileUtils.mkdir File.join(@datadir,Time.now.strftime("%Y-%m-%d")) 
    end
    File.join(@datadir,Time.now.strftime("%Y-%m-%d"),"data_"+Time.now.strftime("%H%M%S"))
  else
    File.join(@datadir,"data")
  end
end

#merge_discoverObject



78
79
80
81
82
83
84
# File 'lib/zabbix-ruby-client.rb', line 78

def merge_discover
  time = Time.now.to_i
  @data = @discover.reduce([]) do |a,(k,v)|
    a << "#{@config['host']} #{k} #{time} { \"data\": [ #{v.join(', ')} ] }"
    a
  end + @data
end

#run_plugin(plugin, args = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zabbix-ruby-client.rb', line 40

def run_plugin(plugin, args = nil)
  Plugins.load(plugin) || logger.error( "Plugin #{plugin} not found.")
  if Plugins.loaded[plugin]
    begin
      @data = @data + Plugins.loaded[plugin].send(:collect, @config['host'], *args)
      if Plugins.loaded[plugin].respond_to?(:discover)
        key, value = Plugins.loaded[plugin].send(:discover, *args)
        @discover[key] ||= []
        @discover[key] << [ value ]
      end
    rescue Exception => e
      logger.fatal "Oops"
      logger.fatal e.message
    end
  end
end

#showObject



63
64
65
66
67
68
# File 'lib/zabbix-ruby-client.rb', line 63

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

#storeObject



70
71
72
73
74
75
76
# File 'lib/zabbix-ruby-client.rb', line 70

def store
  File.open(datafile, "w") do |f|
    @data.each do |d|
      f.puts d
    end
  end
end

#uploadObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/zabbix-ruby-client.rb', line 86

def upload
  merge_discover
  store
  begin
    res = `#{@config['zabbix']['sender']} -z #{@config['zabbix']['host']} -T -i #{datafile}`
  rescue Exception => e
    logger.error "Sending failed."
    logger.error e.message
  end
end