diff options
Diffstat (limited to 'lib/mauve')
-rw-r--r-- | lib/mauve/alert.rb | 23 | ||||
-rw-r--r-- | lib/mauve/alert_changed.rb | 6 | ||||
-rw-r--r-- | lib/mauve/alert_group.rb | 76 | ||||
-rw-r--r-- | lib/mauve/configuration_builders/alert_group.rb | 6 | ||||
-rw-r--r-- | lib/mauve/configuration_builders/notification_method.rb | 4 | ||||
-rw-r--r-- | lib/mauve/notification.rb | 36 | ||||
-rw-r--r-- | lib/mauve/notifier.rb | 14 | ||||
-rw-r--r-- | lib/mauve/notifiers/debug.rb | 58 | ||||
-rw-r--r-- | lib/mauve/notifiers/email.rb | 1 | ||||
-rw-r--r-- | lib/mauve/version.rb | 2 | ||||
-rw-r--r-- | lib/mauve/web_interface.rb | 2 |
11 files changed, 154 insertions, 74 deletions
diff --git a/lib/mauve/alert.rb b/lib/mauve/alert.rb index 9d6ab85..28079a2 100644 --- a/lib/mauve/alert.rb +++ b/lib/mauve/alert.rb @@ -146,14 +146,15 @@ module Mauve # # Find the AlertGroup by name if we've got a cached value # - alert_group = AlertGroup.all.find{|a| self.cached_alert_group == a.name} if self.cached_alert_group + alert_group = AlertGroup.find{|a| self.cached_alert_group == a.name} if self.cached_alert_group if alert_group.nil? # # If we've not found the alert group by name look for it again, the # proper way. # - alert_group = AlertGroup.matches(self).first + alert_group = AlertGroup.find{|a| a.includes?(self)} + alert_group = AlertGroup.all.last if alert_group.nil? self.cached_alert_group = alert_group.name unless alert_group.nil? end @@ -399,13 +400,8 @@ module Mauve # Send a notification for this alert. # # @return [Boolean] Showing if an alert has been sent. - def notify - if self.alert_group.nil? - logger.warn "Could not notify for #{self} since there are no matching alert groups" - false - else - self.alert_group.notify(self) - end + def notify(at = Time.now) + Server.notification_push([self, at]) end # Acknowledge an alert @@ -485,12 +481,6 @@ module Mauve self.will_unacknowledge_at = postpone_until end - # - # Re-cache the alert group. - # - self.cached_alert_group = nil - self.alert_group - logger.info("Postponing raise of #{self} until #{postpone_until} as it was last updated in a prior run of Mauve.") else self.acknowledged_by = nil @@ -503,8 +493,9 @@ module Mauve self.update_type = "raised" if self.update_type.nil? or self.update_type != "changed" or self.original_attributes[Alert.properties[:update_type]] == "cleared" # - # Cache the alert group, but only if not already set. + # Find the alert group to allow it to be cached. # + self.cached_alert_group = nil self.alert_group end diff --git a/lib/mauve/alert_changed.rb b/lib/mauve/alert_changed.rb index 9b2bea4..0e0d257 100644 --- a/lib/mauve/alert_changed.rb +++ b/lib/mauve/alert_changed.rb @@ -98,7 +98,11 @@ module Mauve return save end - alert_group.notify(alert) + # + # Push this notifitcation onto the queue. + # + Server.notification_push([alert, Time.now]) + # # Need to make sure this reminder is cleared. # diff --git a/lib/mauve/alert_group.rb b/lib/mauve/alert_group.rb index 7ea7ffb..31d9cc8 100644 --- a/lib/mauve/alert_group.rb +++ b/lib/mauve/alert_group.rb @@ -26,7 +26,7 @@ module Mauve # # @return [Array] AlertGroups that match def matches(alert) - grps = all.select { |alert_group| alert_group.includes?(alert) } + grps = find_all { |alert_group| alert_group.includes?(alert) } # # Make sure we always match the last (and therefore default) group. @@ -36,6 +36,28 @@ module Mauve grps end + # + # + # + def find_all(&block) + return all unless block_given? + + all.find_all do |alert_group| + yield(alert_group) + end + end + + # + # + # + def find(&block) + return nil unless block_given? + + all.find do |alert_group| + yield(alert_group) + end + end + # @return [Log4r::Logger] def logger Log4r::Logger.new self.to_s @@ -113,13 +135,16 @@ module Mauve # @return [Log4r::Logger] 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. + # 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. + # The time is used to determine the time to be used when evaluating + # "during" blocks in the notifier clauses. # # @param [Mauve::Alert] alert + # @param [Time] at # # @return [Boolean] indicates success or failure of alert. - def notify(alert) + def notify(alert, at=Time.now) # # If there are no notifications defined. # @@ -128,16 +153,39 @@ module Mauve return false end + during_runners = [] + # # This is where we set the reminder -- i.e. on a per-alert-group basis. - # - remind_at = notifications.inject(nil) do |reminder_time, notification| - this_time = notification.remind_at_next(alert) - if reminder_time.nil? or (!this_time.nil? and reminder_time > this_time) - this_time - else - reminder_time - end + + remind_at = nil + notifications.each do |notification| + # + # Create a new during_runner for this notification clause, and keep it + # handy. + # + during_runner = DuringRunner.new(at, alert, ¬ification.during) + during_runners << during_runner + + # + # Work out the next reminder time + # + this_remind_at = notification.remind_at_next(alert, during_runner) + + # + # Skip this one if no reminder time can be found + # + next if this_remind_at.nil? + + # + # Set the next reminder time if we've not had one already. + # + remind_at = this_remind_at if remind_at.nil? + + # + # We need the next soonest reminder time. + # + remind_at = this_remind_at if remind_at > this_remind_at end # @@ -148,7 +196,7 @@ module Mauve :level => level.to_s, :alert_id => alert.id, :person => self.name, - :at => Time.now, + :at => at, :update_type => alert.update_type, :remind_at => remind_at, :was_relevant => true) @@ -161,7 +209,7 @@ module Mauve # sent_to = [] notifications.each do |notification| - sent_to << notification.notify(alert, sent_to) + sent_to << notification.notify(alert, sent_to, during_runners.shift) end return (sent_to.length > 0) diff --git a/lib/mauve/configuration_builders/alert_group.rb b/lib/mauve/configuration_builders/alert_group.rb index c652de8..56c446d 100644 --- a/lib/mauve/configuration_builders/alert_group.rb +++ b/lib/mauve/configuration_builders/alert_group.rb @@ -20,15 +20,15 @@ module Mauve # @return [Mauve::Notification] New notification instance. def builder_setup(*who) who = who.map do |username| - #raise BuildException.new("You haven't declared who #{username} is") unless - # @context.people[username] - #@context.people[username] if @context.people[username] @context.people[username] + elsif @context.people_lists[username] @context.people_lists[username] + else raise ArgumentError.new("You have not declared who #{username} is") + end end @result = Mauve::Notification.new(who, @context.last_alert_group.level) diff --git a/lib/mauve/configuration_builders/notification_method.rb b/lib/mauve/configuration_builders/notification_method.rb index 9596587..3f9283e 100644 --- a/lib/mauve/configuration_builders/notification_method.rb +++ b/lib/mauve/configuration_builders/notification_method.rb @@ -35,6 +35,10 @@ module Mauve def result @result ||= @provider_class.new(@name) end + + def debug! + result.extend(Mauve::Notifiers::Debug) + end # This catches all methods available for a provider, as needed. # diff --git a/lib/mauve/notification.rb b/lib/mauve/notification.rb index 920f1b1..3c902a2 100644 --- a/lib/mauve/notification.rb +++ b/lib/mauve/notification.rb @@ -42,6 +42,7 @@ module Mauve @alert = alert @during = during || Proc.new { true } @logger = Log4r::Logger.new "Mauve::DuringRunner" + @now_cache = Hash.new end # This evaluates the +during+ block, returning the result. @@ -49,8 +50,21 @@ module Mauve # @param [Time] t Set the time at which to evaluate the +during+ block. # @return [Boolean] def now?(t=@time) + # + # Use the cache if we've already worked out what happens at time t. + # + return @now_cache[t] if @now_cache.has_key?(t) + + # + # Store the test time in an instance variable so the test knows what time + # we're testing against. + # @test_time = t - instance_eval(&@during) ? true : false + + # + # Store the answer in our cache and return. + # + @now_cache[t] = (instance_eval(&@during) ? true : false) end # This finds the next occurance of the +during+ block evaluating to true. @@ -195,15 +209,18 @@ module Mauve # @param [Array] already_sent_to A list of people that have already received this alert. # # @return [Array] The list of people that have received this alert. - def notify(alert, already_sent_to = []) + def notify(alert, already_sent_to = [], during_runner = nil) if people.nil? or people.empty? logger.warn "No people found in for notification #{list}" return end + # Set up a during_runner + during_runner ||= DuringRunner.new(Time.now, self.alert, &self.during) + # Should we notify at all? - return already_sent_to unless DuringRunner.new(Time.now, alert, &during).now? + return already_sent_to unless during_runner.now? people.collect do |person| case person @@ -222,7 +239,7 @@ module Mauve if already_sent_to.include?(person.username) logger.info("Already sent notification of #{alert} to #{person.username}") else - Server.notification_push([person, level, alert]) + person.send_alert(level, alert) already_sent_to << person.username end end @@ -236,13 +253,16 @@ module Mauve # @param [Mauve::Alert] alert The alert in question # @return [Time or nil] The time a reminder should get sent, or nil if it # should never get sent again. - def remind_at_next(alert) + def remind_at_next(alert, during_runner = nil) return nil unless alert.raised? - if DuringRunner.new(Time.now, alert, &during).now? - return DuringRunner.new(Time.now, alert, &during).find_next(every) + # Set up a during_runner + during_runner ||= DuringRunner.new(Time.now, self.alert, &self.during) + + if during_runner.now? + return during_runner.find_next(every) else - return DuringRunner.new(Time.now, alert, &during).find_next() + return during_runner.find_next() end end diff --git a/lib/mauve/notifier.rb b/lib/mauve/notifier.rb index bf36162..8a26b2c 100644 --- a/lib/mauve/notifier.rb +++ b/lib/mauve/notifier.rb @@ -86,14 +86,12 @@ module Mauve # Empty the buffer, one notification at a time. # sz.times do - person, *args = Server.notification_pop - - # - # Nil person.. that's craaazy too! - # - next if person.nil? - - person.send_alert(*args) + alert, at = Server.notification_pop + if alert.alert_group.nil? + logger.warn "Could not notify for #{alert} since there are no matching alert groups" + else + alert.alert_group.notify(alert, at) + end end end diff --git a/lib/mauve/notifiers/debug.rb b/lib/mauve/notifiers/debug.rb index 775364d..a9afc52 100644 --- a/lib/mauve/notifiers/debug.rb +++ b/lib/mauve/notifiers/debug.rb @@ -12,48 +12,62 @@ module Mauve base.class_eval do alias_method :send_alert_without_debug, :send_alert alias_method :send_alert, :send_alert_to_debug_channels - - # Specifying deliver_to_file allows the administrator to ask for alerts - # to be delivered to a particular file, which is assumed to be perused - # by a person rather than a machine. - # - attr :deliver_to_file, true - - # Specifying deliver_to_queue allows a tester to ask for the send_alert - # parameters to be appended to a Queue object (or anything else that - # responds to <<). - # - attr :deliver_to_queue, true end end + + def extended(base) + base.instance_eval do + alias :send_alert_without_debug :send_alert + alias :send_alert :send_alert_to_debug_channels + end + end end - + + + # Specifying deliver_to_file allows the administrator to ask for alerts + # to be delivered to a particular file, which is assumed to be perused + # by a person rather than a machine. + # + def deliver_to_file + @deliver_to_file + end + + def deliver_to_file=(fn) + @deliver_to_file = fn + end + + # Specifying deliver_to_queue allows a tester to ask for the send_alert + # parameters to be appended to a Queue object (or anything else that + # responds to <<). + # + def deliver_to_queue + @deliver_to_queue + end + + def deliver_to_queue=(q) + @deliver_to_queue = q + end + def disable_normal_delivery! @disable_normal_delivery = true end def send_alert_to_debug_channels(destination, alert, all_alerts, conditions = nil) - message = if respond_to?(:prepare_message) + message = if self.respond_to?(:prepare_message) prepare_message(destination, alert, all_alerts, conditions) else [destination, alert, all_alerts].inspect end if deliver_to_file - #lock_file = "#{deliver_to_file}.lock" - #while File.exists?(lock_file) - # sleep 0.1 - #end - #FileUtils.touch(lock_file) File.open("#{deliver_to_file}", "a+") do |fh| fh.flock(File::LOCK_EX) - fh.print("#{Time.now} from #{self.class}: " + message + "\n") + fh.print YAML.dump([Time.now, self.class, destination, message]) fh.flush() end - #FileUtils.rm(lock_file) end - deliver_to_queue << [destination, alert, all_alerts, conditions] if deliver_to_queue + deliver_to_queue << [Time.now, self.class, destination, message] if deliver_to_queue if @disable_normal_delivery true # pretend it happened OK if we're just testing diff --git a/lib/mauve/notifiers/email.rb b/lib/mauve/notifiers/email.rb index 8efdbec..5b8e9a0 100644 --- a/lib/mauve/notifiers/email.rb +++ b/lib/mauve/notifiers/email.rb @@ -97,7 +97,6 @@ module Mauve m.to_s end - include Debug end end end diff --git a/lib/mauve/version.rb b/lib/mauve/version.rb index 43befb4..d7af3a9 100644 --- a/lib/mauve/version.rb +++ b/lib/mauve/version.rb @@ -5,6 +5,6 @@ module Mauve # # Current version - VERSION="3.7.3" + VERSION="3.7.7" end diff --git a/lib/mauve/web_interface.rb b/lib/mauve/web_interface.rb index 225cc33..16e7da2 100644 --- a/lib/mauve/web_interface.rb +++ b/lib/mauve/web_interface.rb @@ -165,6 +165,8 @@ EOF if Authentication.authenticate(usr, pwd) session['username'] = usr + # Clear the flash. + flash['error'] = nil redirect next_page else flash['error'] = "Authentication failed." |