aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/alert_group.rb
blob: fe17516029a539198465323d971d4d92ff3c0d5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# encoding: UTF-8
require 'mauve/alert'
require 'log4r'

module Mauve
  #
  # This corresponds to a alert_group clause in the configuration.  It is what
  # is used to classify alerts into levels, and thus who gets notified about it
  # and when.
  #
  class AlertGroup < Struct.new(:name, :includes, :acknowledgement_time, :level, :notifications)

    #
    # Define some constants, and the ordering.
    #
    URGENT = :urgent
    NORMAL = :normal
    LOW    = :low
    LEVELS = [LOW, NORMAL, URGENT]

    class << self

      # Finds all AlertGroups that match an alert.
      #
      # @param [Mauve::Alert] alert
      #
      # @return [Array] AlertGroups that match
      def matches(alert)
        grps = find_all { |alert_group| alert_group.includes?(alert) }

        #
        # Make sure we always match the last (and therefore default) group.
        #
        grps << all.last unless grps.include?(all.last)

        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
      end

      # All AlertGroups
      #
      # @return [Array]
      def all
        return [] if Configuration.current.nil?

        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().
      #
      # @return [Array]
      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

    # Creates a new AlertGroup
    #
    # @param name Name of alert group
    #
    # @return [AlertGroup] self
    def initialize(name)
      self.name = name
      self.level = :normal
      self.includes = Proc.new { true }
      self
    end

    # @return [String]
    def to_s
      "#<AlertGroup:#{name} (level #{level})>"
    end

    # The list of current raised alerts in this group.
    #
    # @return [Array] Array of Mauve::Alert
    def current_alerts
      Alert.all(:cleared_at => nil, :raised_at.not => nil).select { |a| includes?(a) }
    end

    # Decides whether a given alert belongs in this group according to its
    # includes { } clause
    #
    # @param [Mauve::Alert] alert
    #
    # @return [Boolean] Success or failure.
    def includes?(alert)

      unless alert.is_a?(Alert)
        logger.error "Got given a #{alert.class} instead of an Alert!"
      	logger.debug caller.join("\n")
        return false
      end

      alert.instance_eval(&self.includes) ? true : false
    end

    alias matches_alert? includes?

    # @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.
    # 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, at=Time.now)
      #
      # If there are no notifications defined.
      #
      if notifications.nil?
        logger.warn("No notifications found for #{self.inspect}")
        return false
      end

      during_runners = []

      #
      # Bail out if notifications for this alert have been suppressed.
      #
      if alert.suppressed?
        logger.info("Notifications suppressed until #{alert.suppress_until} for #{alert}")

        this_reminder = AlertChanged.first_or_new(
          :alert_id => alert.id,
          :person => self.name,
          :remind_at.not => nil,
          :was_relevant => false
        )

        this_reminder.level = level.to_s
        this_reminder.at  = at

        #
        # Set a reminder if
        #  * there was no existing reminder
        #  * the alert now is raised, but the reminder was set when the alert was cleared, or the alert was ack'd.
        #  * the alert now is cleared, but the reminder was set when the alert was raised/re-raised/ack'd
        #
        if this_reminder.update_type.nil? or
          (alert.raised? and  %w(cleared acknowledged).include?(this_reminder.update_type.to_s.downcase)) or
          (alert.cleared? and %w(re-raised raised acknowledged).include?(this_reminder.update_type.to_s.downcase))

          this_reminder.update_type = alert.update_type
          this_reminder.remind_at = alert.suppress_until
        else
          this_reminder.remind_at = nil
        end

        return this_reminder.save
      end


      #
      # This is where we set the reminder -- i.e. on a per-alert-group basis.
      #
      remind_at = nil

      these_notifications = self.resolve_notifications(at)

      these_notifications.each do |notification|
        #
        # Make sure the level is set.
        #
        notification.level = self.level

        #
        # Create a new during_runner for this notification clause, and keep it
        # handy.
        #
        during_runner = DuringRunner.new(at, alert, &notification.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

      #
      # OK got the next reminder time.
      #
      unless remind_at.nil?
        #
        # Find the last reminder, if available for the same alert, update type, and person.
        #
        this_reminder = AlertChanged.first_or_new(
          :alert_id => alert.id,
          :person => self.name,
          :update_type => alert.update_type,
          :remind_at.not => nil,
          :remind_at.gt => at
        )

        this_reminder.level = level.to_s
        this_reminder.at    = at
        this_reminder.remind_at = remind_at
        this_reminder.was_relevant = true
        this_reminder.save
      end

      #
      # The notifications are specified in the config file.
      #
      sent_to = []
      these_notifications.each do |notification|
        sent_to << notification.notify(alert, sent_to, during_runners.shift)
      end

      #
      # If the alert is ack'd or cleared, notify anyone who has contributed to
      # its history since it was raised.
      #
      alert.extra_people_to_notify.each do |person|
        person.notifications.each do |n|
          notification = Mauve::Notification.new(person)
          notification.level  = self.level
          notification.every  = n.every
          notification.during = n.during
          notification
          sent_to << notification.notify(alert, sent_to, DuringRunner.new(at, alert, &notification.during))
        end
      end if alert.acknowledged? or alert.cleared?

      return (sent_to.length > 0)
    end

    # This sorts by priority (urgent first), and then alphabetically, so the
    # first match is the most urgent.
    #
    # @param [Mauve::AlertGroup] other
    #
    # @return [Integer]
    def <=>(other)
      [LEVELS.index(self.level), self.name]  <=> [LEVELS.index(other.level), other.name]
    end

    #
    #
    #
    def resolve_notifications(at = Time.now)
      self.notifications.collect do |notification|
        notification.people.collect do |person|
          person.resolve_notifications(notification.every, notification.during, at)
        end
      end.flatten.compact
    end
  end

end