Class: RRDNotifier::AlarmTrigger

Inherits:
Object
  • Object
show all
Defined in:
lib/rrd-grapher/notifier/alarm_trigger.rb

Overview

Represents the conditions to raise an alarm.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manager, host, plugin, type, opts = {}) ⇒ AlarmTrigger

Create a new AlarmTrigger object.

Parameters:

  • manager (AlertManager)

    The manager (used to raise/stop alarms and query informations)

  • host (String, nil)

    the hostname (nil: any host)

  • plugin (String)

    plugin name ex: something (any plugin_instance matched) ex: something/inst (match both plugin and plugin_instance)

  • type (String)

    type name (see plugin for format)

  • opts (Hash) (defaults to: {})

    Alarm option

Options Hash (opts):

  • min (Number)

    minimal allowed value

  • max (Number)

    maximum allowed value

  • monitor_presence (Number)

    raise an alarm if data is missing for x seconds

  • monitor_drift (Boolean)

    Ensure the clock of the host is not drifting too far from our clock (default: false)

  • index (Integer)

    for multi-values counters this parameter allow you to select what you want to monitor (default: 0)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rrd-grapher/notifier/alarm_trigger.rb', line 32

def initialize(manager, host, plugin, type, opts = {})
  @host = self.class.load_param(host)
  @plugin, @plugin_instance = self.class.load_param(plugin)
  @type, @type_instance = self.class.load_param(type)

  @manager = manager
  
  @index = opts.delete(:index) || 0
  @min = opts.delete(:min)
  @max = opts.delete(:max)
  @monitor_drift = opts.has_key?(:monitor_drift) ? opts.delete(:monitor_drift) : false
  @monitor_presence = opts.has_key?(:monitor_presence) ? opts.delete(:monitor_presence) : false

  raise "Unknown arguments: #{opts.inspect}" unless opts.empty?

  @timers_register = {}
end

Class Method Details

.load_param(str) ⇒ Array

Split if arg contains a “/” and return nil if a “*” is found.

Parameters:

  • str (String)

    source string

Returns:

  • (Array)

    result



57
58
59
60
61
62
63
64
65
# File 'lib/rrd-grapher/notifier/alarm_trigger.rb', line 57

def self.load_param(str)
  raise "nil is not a valid value" unless str

  ret = str.split("/").map! do |s|
    (s == '*') ? nil : s
  end

  (ret.size == 1) ? ret[0] : ret
end

Instance Method Details

#check_alarm_drift(p, value) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rrd-grapher/notifier/alarm_trigger.rb', line 136

def check_alarm_drift(p, value)
  return unless @monitor_drift
  now = Time.now
  
  drift = (now - p.time).to_f.abs
  # convert the time to ms before sending it
  @manager.send_gauge(p.host, p.interval, 'monitoring', nil, 'gauge', 'clock_drift', drift * 1000)
  
  # check the absolute value of the difference between
  # current server time and time included in the collectd
  # packet.
  if drift > @monitor_drift
    unless @manager.active_alarm?(p.measure_id, AlarmClockDrift, @monitor_drift)
      @manager.raise_alarm( p.measure_id, AlarmClockDrift.new(p, @monitor_drift) )
    end
  else
    @manager.stop_alarm(p.measure_id, AlarmClockDrift, @monitor_drift)
  end
end

#check_alarm_high(p, value) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rrd-grapher/notifier/alarm_trigger.rb', line 111

def check_alarm_high(p, value)
  return unless @max
  
  if value > @max
    unless @manager.active_alarm?(p.measure_id, AlarmTooHigh, @max)
      @manager.raise_alarm( p.measure_id, AlarmTooHigh.new(p, @max) )
    end
  else
    @manager.stop_alarm(p.measure_id, AlarmTooHigh, @max)
  end
end

#check_alarm_low(p, value) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rrd-grapher/notifier/alarm_trigger.rb', line 123

def check_alarm_low(p, value)
  return unless @min
  
  if value < @min
    unless @manager.active_alarm?(p.measure_id, AlarmTooLow, @min)
      @manager.raise_alarm( p.measure_id, AlarmTooLow.new(p, @min) )
    end
  else
    @manager.stop_alarm(p.measure_id, AlarmTooLow, @min)
  end
end

#check_alarm_presence(p, value) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rrd-grapher/notifier/alarm_trigger.rb', line 157

def check_alarm_presence(p, value)
  return unless @monitor_presence
  
  # stop any already active alarm
  last_update = @manager.last_update_for(p.measure_id)
  @manager.stop_alarm(p.measure_id, AlarmMissingData, @monitor_presence, last_update)

  # reset any active timer
  timer_id = @timers_register[p.measure_id]
  EM::cancel_timer(timer_id) if timer_id

  # and create a new one
  @timers_register[p.measure_id] = EM::add_timer(@monitor_presence) do
    presence_timeout(p)
  end
end

#check_alarms(p) ⇒ Object

Called by the manager when a packet is received.

Parameters:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rrd-grapher/notifier/alarm_trigger.rb', line 92

def check_alarms(p)
  # if the packet is not intersting for me, stop here
  unless match?(p)
    return
  end
  
  if @index >= p.values.size
    puts "index #{@index} was given but only #{p.values.size} found, check disabled"
    return
  end
  
  value = p.value(@index)
  
  check_alarm_high(p, value)
  check_alarm_low(p, value)
  check_alarm_presence(p, value)
  check_alarm_drift(p, value)
end

#inspectObject



83
84
85
# File 'lib/rrd-grapher/notifier/alarm_trigger.rb', line 83

def inspect
  "#{@host}-#{@plugin}/#{@plugin_instance}-#{@type}/#{@type_instance}"
end

#match?(p) ⇒ Boolean

Checks if this packet is interesting for the trigger.

Parameters:

Returns:

  • (Boolean)

    true if the packet is interesting



74
75
76
77
78
79
80
# File 'lib/rrd-grapher/notifier/alarm_trigger.rb', line 74

def match?(p)
  (@host.nil? || (@host == p.host)) &&
  (@plugin.nil? || (@plugin == p.plugin)) &&
  (@plugin_instance.nil? || (@plugin_instance == p.plugin_instance)) &&
  (@type.nil? || (@type == p.type)) &&
  (@type_instance.nil? || (@type_instance == p.type_instance))
end

#presence_timeout(p) ⇒ Object

Raise a presence alarm.

Parameters:

  • p (Packet)

    the data point



179
180
181
182
183
184
# File 'lib/rrd-grapher/notifier/alarm_trigger.rb', line 179

def presence_timeout(p)
  @manager.fiber_pool.spawn do
    last_update = @manager.last_update_for(p.measure_id)
    @manager.raise_alarm( p.measure_id, AlarmMissingData.new(p, @monitor_presence, last_update) )
  end
end