5 # directory to hold conditions and their corresponding metric
6 # {condition => metric}
7 attr_accessor :directory
12 def self.attach(condition, metric)
13 self.directory[condition] = metric
17 Timer.get.schedule(condition, 0)
18 when EventCondition, TriggerCondition
23 def self.detach(condition)
24 self.directory.delete(condition)
28 Timer.get.unschedule(condition)
29 when EventCondition, TriggerCondition
34 def self.trigger(condition)
37 self.handle_poll(condition)
38 when EventCondition, TriggerCondition
39 self.handle_event(condition)
43 def self.handle_poll(condition)
46 metric = self.directory[condition]
48 # it's possible that the timer will trigger an event before it can be cleared
49 # by an exiting metric, in which case it should be ignored
53 watch.mutex.synchronize do
55 result = condition.test
58 msg = watch.name + ' ' + condition.class.name + " [#{result}] " + self.dest_desc(metric, condition)
60 LOG.log(watch, :info, msg)
64 self.notify(condition, msg)
72 if result && condition.transition
77 metric.destination && metric.destination[result]
80 # transition or reschedule
85 rescue EventRegistrationFailedError
86 msg = watch.name + ' Event registration failed, moving back to previous state'
88 LOG.log(watch, :info, msg)
95 Timer.get.schedule(condition)
100 message = format("Unhandled exception (%s): %s\n%s",
101 e.class, e.message, e.backtrace.join("\n"))
108 def self.handle_event(condition)
110 metric = self.directory[condition]
115 watch.mutex.synchronize do
116 msg = watch.name + ' ' + condition.class.name + " [true] " + self.dest_desc(metric, condition)
118 LOG.log(watch, :info, msg)
122 self.notify(condition, msg)
125 # get the destination
127 if condition.transition
132 metric.destination && metric.destination[true]
145 def self.dest_desc(metric, condition)
146 if metric.destination
147 metric.destination.inspect
149 if condition.transition
150 {true => condition.transition}.inspect
157 def self.notify(condition, message)
159 spec = Contact.normalize(condition.notify)
163 spec[:contacts].inject([]) do |acc, contact_name_or_group|
164 acc += Array(God.contacts[contact_name_or_group] || God.contact_groups[contact_name_or_group])
170 resolved_contacts.each do |c|
171 c.notify(message, Time.now, spec[:priority], spec[:category])
175 puts e.backtrace.join("\n")