aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/alert_group.rb
diff options
context:
space:
mode:
authorPatrick J Cherry <patrick@bytemark.co.uk>2011-06-15 19:44:07 +0100
committerPatrick J Cherry <patrick@bytemark.co.uk>2011-06-15 19:44:07 +0100
commit42f14de6ab50d0e05f49f038e40b94b686701bf1 (patch)
tree33dc459cf3ae32de8956690a990f080321a67a83 /lib/mauve/alert_group.rb
parent92e5d4e652aae161640359b41c0ba8dcaedac0ad (diff)
* Added Alert#subject to return source when subject unspecified
* Added extra arg to acknowledge! * Alert#all_raised / all_cleared does not returned ack'd alerts * Added sanitising for source/subject * Catch empty alert_group notifications in AlertChanged * Tidied up alert_group * Added skip bytemark_auth when RACK_ENV is development * Tidied up logging in MauveThread, Timer, UdpServer * Added extra convenience methods to Time and Date * Moved working/daylight/dead_zone hours into Time
Diffstat (limited to 'lib/mauve/alert_group.rb')
-rw-r--r--lib/mauve/alert_group.rb58
1 files changed, 46 insertions, 12 deletions
diff --git a/lib/mauve/alert_group.rb b/lib/mauve/alert_group.rb
index adb9909..a8b1482 100644
--- a/lib/mauve/alert_group.rb
+++ b/lib/mauve/alert_group.rb
@@ -4,13 +4,26 @@ require 'log4r'
module Mauve
class AlertGroup < Struct.new(:name, :includes, :acknowledgement_time, :level, :notifications)
- def to_s
- "#<AlertGroup:#{name} (level #{level})>"
- end
+
+ #
+ # Define some constants, and the ordering.
+ #
+ URGENT = :urgent
+ NORMAL = :normal
+ LOW = :low
+ LEVELS = [LOW, NORMAL, URGENT]
class << self
+
def matches(alert)
- all.select { |alert_group| alert_group.matches_alert?(alert) }
+ grps = all.select { |alert_group| alert_group.matches_alert?(alert) }
+
+ #
+ # Make sure we always match the last (and therefore default) group.
+ #
+ grps << all.last unless grps.include?(all.last)
+
+ grps
end
# If there is any significant change to a set of alerts, the Alert
@@ -26,18 +39,19 @@ module Mauve
#
# Make sure we've got a matching group
#
- logger.warn "no groups found for #{alert.id}" if groups.empty?
+ if groups.empty?
+ logger.warn "no groups found for #{alert}"
+ next
+ end
#
# Notify just the group that thinks this alert is the most urgent.
#
- %w(urgent normal low).each do |lvl|
- this_group = groups.find{|grp| grp.level.to_s == lvl}
- next if this_group.nil?
- logger.info("notifying group #{this_group} of AlertID.#{alert.id} (matching #{lvl})")
- this_group.notify(alert)
- break
- end
+ logger.warn "Found #{groups.length} matching groups for #{alert}" if groups.length > 1
+
+ this_group = groups.first
+ logger.info("notifying group #{this_group} of #{alert} (matching #{this_group.level})")
+ this_group.notify(alert)
end
end
@@ -70,6 +84,10 @@ module Mauve
self.level = :normal
self.includes = Proc.new { true }
end
+
+ def to_s
+ "#<AlertGroup:#{name} (level #{level})>"
+ end
# The list of current raised alerts in this group.
#
@@ -98,6 +116,14 @@ module Mauve
#
def notify(alert)
#
+ # If there are no notifications defined.
+ #
+ if notifications.nil?
+ logger.warn("No notifications found for #{alert}")
+ return
+ end
+
+ #
# The notifications are specified in the config file.
#
notifications.each do |notification|
@@ -105,6 +131,14 @@ module Mauve
end
end
+ #
+ # This sorts by priority (urgent first), and then alphabetically, so the
+ # first match is the most urgent.
+ #
+ def <=>(other)
+ [LEVELS.index(self.level), self.name] <=> [LEVELS.index(other.level), other.name]
+ end
+
end
end