Class: RRDNotifier::AlarmManager

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

Overview

Trigger/Stop the alarms based on user configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ AlarmManager

Create a new AlertManager object

Parameters:

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

    options

Options Hash (opts):

  • notification_manager (Module, Object)

    The object used when a new notification is triggered/stopped

  • fiber_pool (FiberPool)

    Fiber pool to use.

  • :send_monitoring_to (String)

    if present we can send our own stats to this <host>:<port>



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

def initialize(opts = {})
  @notification_handler = opts.delete(:notification_handler) || DefaultNotificationHandler
  send_monitoring_to = opts.delete(:send_monitoring_to)
  if send_monitoring_to
    @send_monitoring_to_host, @send_monitoring_to_port = send_monitoring_to.split(':')
    # to just data to collectd
    @collectd_socket = EM::open_datagram_socket('127.0.0.1', nil)
  end
  
  unless valid_notification_handler?(@notification_handler)
    raise "notification_handler #{@notification_handler} invalid, some callbacks are missing !"
  end
  
  @fiber_pool = opts.delete(:fiber_pool) || FiberPool.new(10)
  @triggers = []
  @active_alarms = {}
  @last_updates = {}
end

Instance Attribute Details

#fiber_poolObject (readonly)

Returns the value of attribute fiber_pool.



17
18
19
# File 'lib/rrd-grapher/notifier/alarm_manager.rb', line 17

def fiber_pool
  @fiber_pool
end

Instance Method Details

#active_alarm?(measure_id, alarm_class, *args) ⇒ Boolean

Return the active alarm matching given parameters.

Parameters:

  • measure_id (String)

    counter ID

  • alarm_class (Class)

    The alarm class

  • args (Array)

    these will be given to Alarm::is_same? so the alarm can tell us if it matchs.

Returns:

  • (Boolean)


178
179
180
181
182
183
184
185
186
# File 'lib/rrd-grapher/notifier/alarm_manager.rb', line 178

def active_alarm?(measure_id, alarm_class, *args)      
  # fetch active alarms
  active_alarms = active_alarms_for(measure_id)
  
  # check if we have an active alarm
  active_alarms.detect do |al|
    al.is_a?(alarm_class) && al.is_same?(*args)
  end
end

#active_alarms_for(measure_id) ⇒ Object



132
133
134
# File 'lib/rrd-grapher/notifier/alarm_manager.rb', line 132

def active_alarms_for(measure_id)
  @active_alarms[measure_id] ||= []
end

#last_update_for(measure_id) ⇒ Object

Used by triggers to query the time of the last update for this measure.

Parameters:

  • measure_id (String)

    ID Representing this particular counter



128
129
130
# File 'lib/rrd-grapher/notifier/alarm_manager.rb', line 128

def last_update_for(measure_id)
  @last_updates[measure_id]
end

#packet_received(p) ⇒ Object

Called by the Notifier when an event is successfully extracted from a collectd packet.

Parameters:

  • p (Packet)

    packet received



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

def packet_received(p)
  @fiber_pool.spawn do
    if p.data?
      trigger_notifications(p)
      @last_updates[p.measure_id] = Time.now
    else
      @notification_handler.dispatch_notification(p)
    end
  end
end

#raise_alarm(measure_id, alarm) ⇒ Object

Raise an alarm.

Parameters:

  • measure_id (String)

    counter ID

  • alarm (Alarm)

    An alarm object



116
117
118
119
# File 'lib/rrd-grapher/notifier/alarm_manager.rb', line 116

def raise_alarm(measure_id, alarm)
  (@active_alarms[measure_id] ||= []) << alarm
  @notification_handler.alarm_started(alarm)
end

#register_alarm(*args) ⇒ Object

Register a new alarm.

See Also:

  • AlarmTrigger::initialize


64
65
66
# File 'lib/rrd-grapher/notifier/alarm_manager.rb', line 64

def register_alarm(*args)
  @triggers << AlarmTrigger.new(self, *args)
end

#send_gauge(host, interval, plugin, plugin_instance, type, type_instance, value) ⇒ Object

Send a counter to collectd.



88
89
90
91
92
93
94
# File 'lib/rrd-grapher/notifier/alarm_manager.rb', line 88

def send_gauge(host, interval, plugin, plugin_instance, type, type_instance, value)
  data = Collectd.new(host, interval)
  data.start()
  data.gauge(plugin, plugin_instance, type, type_instance, value)
  
  @collectd_socket.send_datagram(data.to_s, @send_monitoring_to_host, @send_monitoring_to_port)
end

#stop_alarm(measure_id, alarm_class, *args) ⇒ Object

Note:

This method is just a wrapper around active_alarm? and stop_specific_alarm.

Stop an alarm matching specified params.

Parameters:

  • measure_id (String)

    counter ID

  • alarm_class (Class)

    The alarm class

  • args (Array)

    these will be given to Alarm::is_same? so the alarm can tell us if it matchs.



159
160
161
162
163
164
165
# File 'lib/rrd-grapher/notifier/alarm_manager.rb', line 159

def stop_alarm(measure_id, alarm_class, *args)
  # check if we have an active alarm
  alarm = active_alarm?(measure_id, alarm_class, *args)
  if alarm
    stop_specific_alarm(measure_id, alarm)
  end
end

#stop_specific_alarm(measure_id, alarm) ⇒ Object

Stop an alarm.

Parameters:

  • measure_id (String)

    counter ID

  • alarm (Alarm)

    An alarm object



142
143
144
145
# File 'lib/rrd-grapher/notifier/alarm_manager.rb', line 142

def stop_specific_alarm(measure_id, alarm)
  active_alarms_for(measure_id).delete(alarm)
  @notification_handler.alarm_stopped(alarm)
end

#trigger_notifications(p) ⇒ Object

Called all the triggers matching this packet to see if one or more wants to start/stop alarms.

Parameters:



103
104
105
# File 'lib/rrd-grapher/notifier/alarm_manager.rb', line 103

def trigger_notifications(p)
  @triggers.each{|t| t.check_alarms(p) }
end

#valid_notification_handler?(obj) ⇒ Boolean

Check that the object respond to the required methods.

Parameters:

  • obj (Object)

    the handler

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/rrd-grapher/notifier/alarm_manager.rb', line 54

def valid_notification_handler?(obj)
  obj.respond_to?(:dispatch_notification) &&
  obj.respond_to?(:alarm_started) &&
  obj.respond_to?(:alarm_stopped)
end