diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2011-04-13 17:03:16 +0100 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2011-04-13 17:03:16 +0100 |
commit | 89a67770e66d11740948e90a41db6cee0482cf8e (patch) | |
tree | be858515fb789a89d68f94975690ab019813726c /lib/mauve/alert_group.rb |
new version.
Diffstat (limited to 'lib/mauve/alert_group.rb')
-rw-r--r-- | lib/mauve/alert_group.rb | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/lib/mauve/alert_group.rb b/lib/mauve/alert_group.rb new file mode 100644 index 0000000..d8156fa --- /dev/null +++ b/lib/mauve/alert_group.rb @@ -0,0 +1,104 @@ +# encoding: UTF-8 +require 'mauve/alert' +require 'log4r' + +module Mauve + class AlertGroup < Struct.new(:name, :includes, :acknowledgement_time, :level, :notifications) + def to_s + "#<AlertGroup:#{name} (level #{level})>" + end + + class << self + def matches(alert) + all.select { |alert_group| alert_group.matches_alert?(alert) } + end + + # If there is any significant change to a set of alerts, the Alert + # class sends the list here so that appropriate action can be taken + # for each one. We scan the list of alert groups to find out which + # alerts match which groups, then send a notification to each group + # object in turn. + # + def notify(alerts) + alerts.each do |alert| + groups = matches(alert) + + # + # Make sure we've got a matching group + # + logger.warn "no groups found for #{alert.id}" if groups.empty? + + # + # Notify each group. + # + groups.each do |grp| + logger.info("notifying group #{groups[0]} of AlertID.#{alert.id}.") + grp.notify(alert) + end + end + end + + def logger + Log4r::Logger.new self.to_s + end + + def all + Configuration.current.alert_groups + end + + # Find all alerts that match + # + # @deprecated Buggy method, use Alert.get_all(). + # + # This method returns all the alerts in all the alert_groups. Only + # the first one should be returned thus making this useless. If you want + # a list of all the alerts matching a level, use Alert.get_all(). + # + def all_alerts_by_level(level) + Configuration.current.alert_groups.map do |alert_group| + alert_group.level == level ? alert_group.current_alerts : [] + end.flatten.uniq + end + + end + + def initialize(name) + self.name = name + self.level = :normal + self.includes = Proc.new { true } + end + + # The list of current raised alerts in this group. + # + def current_alerts + Alert.all(:cleared_at => nil, :raised_at.not => nil).select { |a| matches_alert?(a) } + end + + # Decides whether a given alert belongs in this group according to its + # includes { } clause + # + # @param [Alert] alert An alert to test for belongness to group. + # @return [Boolean] Success or failure. + def matches_alert?(alert) + result = alert.instance_eval(&self.includes) + if true == result or + true == result.instance_of?(MatchData) + return true + end + return false + end + + def logger ; self.class.logger ; end + + # Signals that a given alert (which is assumed to belong in this group) + # has undergone a significant change. We resend this to every notify list. + # + def notify(alert) + notifications.each do |notification| + notification.alert_changed(alert) + end + end + + end + +end |