lib/riemann/babbler/plugin.rb in riemann-babbler-0.5.9 vs lib/riemann/babbler/plugin.rb in riemann-babbler-0.6.0

- old
+ new

@@ -18,19 +18,20 @@ require 'open3' require 'timeout' require 'rest_client' attr_reader :logger + attr_reader :riemann + alias :r :riemann attr_reader :hostname - def initialize( configatron, logger ) + def initialize( configatron, logger, riemann ) @configatron = configatron @logger = logger + @riemann = riemann @storage = Hash.new - @configatron.riemann.cache_host = Configatron::Dynamic.new{ riemann_ip } - @configatron.riemann.last_cache_time = Time.now - @riemann_ip = riemann_random_ip + @hostname = get_hostname init run end # Доступ к конфигу определенного плагина @@ -42,52 +43,28 @@ def options @configatron end alias :opts :options - def riemann_ip - if Time.now - options.riemann.last_cache_time > options.riemann.dns_ttl - options.riemann.last_cache_time = Time.now - @riemann_ip = riemann_random_ip - end - @riemann_ip - end - - def riemann_random_ip - ipaddress = Resolv.new.getaddresses(configatron.riemann.host) - ipaddress[rand(ipaddress.length)] - end - - def riemann - @riemann ||= Riemann::Client.new( - :host => options.riemann.cache_host, - :port => options.riemann.port - ) - end - alias :r :riemann - def report(event) report_with_diff(event) and return if event[:as_diff] # если нет event[:state] то попробовать его добавить unless event[:state] event[:state] = state(event[:metric]) unless plugin.states.critical.nil? end event[:tags] = options.riemann.tags unless options.riemann.tags.nil? - event[:host] = host + event[:host] = hostname logger.debug "Report status: #{event.inspect}" + logger.error "Riemann: #{riemann.inspect}" riemann << event end def report_with_diff(event) current_metric = event[:metric] event[:metric] = current_metric - @storage[ event[:service] ] if @storage.has_key? event[:service] @storage[ event[:service] ] = current_metric event.delete(:as_diff) report(event) - end - - def host - @hostname ||= get_hostname end def get_hostname hostname = File.read('/proc/sys/kernel/hostname').strip.downcase hostname += options.riemann.suffix unless options.riemann.suffix.nil?