lib/riemann/babbler/plugin.rb in riemann-babbler-2.0.0pre10 vs lib/riemann/babbler/plugin.rb in riemann-babbler-2.0.0pre11

- old
+ new

@@ -27,15 +27,17 @@ end attr_reader :riemann, :plugin_name, :plugin, :errors def initialize(riemann) - @riemann = riemann - @storage = Hash.new - @plugin_name = name_to_underscore(self.class.name) - @plugin = opts.plugins.send(plugin_name) - @errors = opts.errors.send(plugin_name) + @riemann = riemann + @storage = Hash.new + @storage['last_state'] = Hash.new + @storage['last_metric'] = Hash.new + @plugin_name = name_to_underscore(self.class.name) + @plugin = opts.plugins.send(plugin_name) + @errors = opts.errors.send(plugin_name) set_default init end def set_default @@ -69,29 +71,33 @@ def report(event) report_with_diff(event) and return if event[:as_diff] event[:metric] = event[:metric].round(2) if event[:metric].kind_of? Float event[:state] = get_state(event) - riemann << event + riemann << event if not_minimize_sent_event(event) + set_last_event(event) end + + ### Helper for reports ### + def report_with_diff(event) current_metric = event[:metric] - old_metric = @storage[event[:service]] + old_metric = @storage['last_metric'][event[:service]] if old_metric && current_metric + old_metric < 2**64 event[:metric] = current_metric - old_metric event.delete(:as_diff) report(event) end - @storage[event[:service]] = current_metric + @storage['last_metric'][event[:service]] = current_metric end #@return state def get_state(event) return event[:state] if event[:state] - metric = event[:metric].to_f - return State::CRITICAL if metric.nil? #todo: неправильно здесь возврашать nil, это ошибка плагина!!! + return event[:state] if event[:metric].nil? + metric = event[:metric].to_f warning = plugin.states.warning.nil? ? nil : plugin.states.warning critical = plugin.states.critical.nil? ? nil : plugin.states.critical return State::OK if (warning || critical).nil? if warning && critical return case @@ -109,9 +115,25 @@ if critical return (metric >= critical) ? State::CRITICAL : State::OK end end + #@return true if event may be sended + def not_minimize_sent_event(event) + return true if !opts.riemann.minimize_event_count # нет задачи минизировать + return true if event[:metric] # если есть метрика - надо отослать graphite + return true if event[:state] != State::OK # все предупреждения отсылаем + return true if @storage['last_state'][event[:service]] != State::OK + log :debug, "Skip send event #{event}" + false + end + + def set_last_event(event) + # тут пока только last_state + @storage['last_state'][event[:service]] = event[:state] if opts.riemann.minimize_event_count + end + + ### Errors ### def plugin_error!(msg) errors.count += 1 log :error, "#{plugin.service} error num #{errors.count}:\n #{msg}" errors.has_last_error = true errors.reported = false