lib/zabbix-ruby-client.rb in zabbix-ruby-client-0.0.5 vs lib/zabbix-ruby-client.rb in zabbix-ruby-client-0.0.6
- old
+ new
@@ -17,18 +17,16 @@
@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
- def data
- @data ||= []
- end
-
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"))
@@ -41,11 +39,16 @@
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)
+ @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
@@ -56,23 +59,32 @@
run_plugin(plugin['name'], plugin['args'])
end
end
def show
- data.each do |line|
+ merge_discover
+ @data.each do |line|
puts line
end
end
def store
File.open(datafile, "w") do |f|
- data.each do |d|
+ @data.each do |d|
f.puts d
end
end
end
+ def merge_discover
+ @data = @discover.reduce([]) do |a,(k,v)|
+ a << "#{@config['host']} #{k} { \"data\": [ #{v.join(', ')} ] }"
+ a
+ end + @data
+ end
+
def upload
+ merge_discover
store
begin
res = `#{@config['zabbix']['sender']} -z #{@config['zabbix']['host']} -i #{datafile}`
rescue Exception => e
logger.error "Sending failed."