Sha256: e45b48f69fa26169a75d7609876986d5e2f63f6e4a568d381431522dc6707db1

Contents?: true

Size: 1.97 KB

Versions: 5

Compression:

Stored size: 1.97 KB

Contents

module Kurchatov
  module Mixin
    module Event

      EVENT_FIELDS = [
        :time, :state, :service, :host,
        :description, :tags, :ttl, :metric
      ]

      def event(hash = {})
        normilize_event(hash)
        Log.info("Mock message for test plugin: #{hash.inspect}") if Kurchatov::Config[:test_plugin]
        events << hash
      end

      protected

      def normilize_event(hash = {})
        hash[:description] = hash[:desc] if hash[:description].nil? && hash[:desc]
        if hash[:metric].kind_of?(Float)
          hash[:metric] = 0.0 if hash[:metric].nan?
          hash[:metric] = hash[:metric].round(2)
        end
        set_diff_metric(hash)
        set_event_state(hash)
        hash.each {|k,_| hash.delete(k) unless EVENT_FIELDS.include?(k)}
        hash[:service] ||= name
        hash[:tags] ||= Kurchatov::Config[:tags]
        hash[:host] ||= Kurchatov::Config[:host]
      end

      def set_event_state(hash = {})
        return if hash[:state]
        return if hash[:critical].nil? && hash[:warning].nil?
        return if hash[:metric].nil?
        if hash[:state] == true || hash[:state] == false
          hash[:state] = hash[:state] ? 'ok' : 'critical'
          return
        end
        hash[:state] = 'ok'
        hash[:state] = 'warning' if hash[:warning] && hash[:metric] >= hash[:warning]
        hash[:state] = 'critical' if hash[:critical] && hash[:metric] >= hash[:critical]
      end

      def set_diff_metric(hash ={})
        hash[:diff] ||= hash[:as_diff] if hash[:as_diff]
        return if hash[:diff].nil? && !hash[:diff]
        return if hash[:metric].nil?
        return if hash[:service].nil?
        @history ||= {}
        if @history[hash[:service]]
          old_metric = @history[hash[:service]]
          @history[hash[:service]] = hash[:metric]
          hash[:metric] = hash[:metric] - old_metric
        else
          @history[hash[:service]] = hash[:metric]
          hash[:metric] = nil
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
kurchatov-0.0.3b lib/kurchatov/mixin/event.rb
kurchatov-0.0.3a lib/kurchatov/mixin/event.rb
kurchatov-0.0.2 lib/kurchatov/mixin/event.rb
kurchatov-0.0.2b lib/kurchatov/mixin/event.rb
kurchatov-0.0.1 lib/kurchatov/mixin/event.rb