Class: Apolo::Monitor
- Inherits:
-
Object
- Object
- Apolo::Monitor
- Defined in:
- lib/apolo/monitor.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Reader/setter for the name of this monitor.
-
#notifiers ⇒ Object
readonly
Returns all the registered notifiers.
-
#readers ⇒ Object
readonly
Returns all the registered readers.
Class Method Summary collapse
-
.name(val = nil) ⇒ Object
Set the name of the app.
- .name_template ⇒ Object
- .notifier_templates ⇒ Object
-
.notify(notifier, options = {}) ⇒ Object
Register a notifier class in the list of notifications.
- .reader_templates ⇒ Object
- .run(&block) ⇒ Object
- .running_template ⇒ Object
-
.using(reader_description) { ... } ⇒ Object
Register a reader in the list of readers.
Instance Method Summary collapse
- #add_notifier(notifier_name, options) ⇒ Object
- #add_reader(reader_description, &block) ⇒ Object
- #get_data(reader_exec) ⇒ Object
-
#initialize ⇒ Monitor
constructor
New instance of a Application-based class.
- #notify(data) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize ⇒ Monitor
New instance of a Application-based class.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/apolo/monitor.rb', line 73 def initialize @readers = Hash.new { |h, k| h[k] = [] } @notifiers = {} @name = self.class.name_template @running = self.class.running_template # Register readers self.class.reader_templates.each do |template| add_reader(template[:reader_description], &template[:block]) end self.class.notifier_templates.each do |template| add_notifier(template[:notifier], template[:options]) end end |
Instance Attribute Details
#name ⇒ Object
Reader/setter for the name of this monitor
70 71 72 |
# File 'lib/apolo/monitor.rb', line 70 def name @name end |
#notifiers ⇒ Object (readonly)
Returns all the registered notifiers.
67 68 69 |
# File 'lib/apolo/monitor.rb', line 67 def notifiers @notifiers end |
#readers ⇒ Object (readonly)
Returns all the registered readers.
64 65 66 |
# File 'lib/apolo/monitor.rb', line 64 def readers @readers end |
Class Method Details
.name(val = nil) ⇒ Object
Set the name of the app. Can be used by notifiers in order to have a better description of the service in question.
25 26 27 28 |
# File 'lib/apolo/monitor.rb', line 25 def name(val = nil) @name_template = val if val @name_template end |
.name_template ⇒ Object
12 13 14 |
# File 'lib/apolo/monitor.rb', line 12 def name_template @name_template || self.to_s end |
.notifier_templates ⇒ Object
8 9 10 |
# File 'lib/apolo/monitor.rb', line 8 def notifier_templates @notifier_templates || [] end |
.notify(notifier, options = {}) ⇒ Object
Register a notifier class in the list of notifications.
53 54 55 56 |
# File 'lib/apolo/monitor.rb', line 53 def notify(notifier, ={}) @notifier_templates ||= [] @notifier_templates << {:notifier => notifier, :options => } end |
.reader_templates ⇒ Object
4 5 6 |
# File 'lib/apolo/monitor.rb', line 4 def reader_templates @reader_templates || [] end |
.run(&block) ⇒ Object
58 59 60 |
# File 'lib/apolo/monitor.rb', line 58 def run(&block) @running_template = block end |
.running_template ⇒ Object
16 17 18 |
# File 'lib/apolo/monitor.rb', line 16 def running_template @running_template end |
.using(reader_description) { ... } ⇒ Object
Register a reader in the list of readers.
36 37 38 39 40 41 42 |
# File 'lib/apolo/monitor.rb', line 36 def using(reader_description, &block) @reader_templates ||= [] @reader_templates << { :reader_description => reader_description, :block => block } end |
Instance Method Details
#add_notifier(notifier_name, options) ⇒ Object
103 104 105 |
# File 'lib/apolo/monitor.rb', line 103 def add_notifier(notifier_name, ) @notifiers[notifier_name] = end |
#add_reader(reader_description, &block) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/apolo/monitor.rb', line 90 def add_reader(reader_description, &block) config = ReaderConfig.new config.instance_exec(&block) if block reader_description.each do |reader, description| @readers[reader] << { :description => description, :config => config } end end |
#get_data(reader_exec) ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/apolo/monitor.rb', line 126 def get_data(reader_exec) @readers.each do |reader, configurations| if configurations.first[:description] == reader_exec return run_reader(reader, configurations.last[:config]) end end raise ArgumentError, "Can't found #{reader_exec} reader." end |
#notify(data) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/apolo/monitor.rb', line 111 def notify(data) = data[:message] value = data[:value] unless && value raise ArgumentError, 'You need to set :message and :value to send notify.' end @notifiers.each do |notifier, | # .dup is NOT reliable = Marshal.load(Marshal.dump()) notifier.new().notify(@name, , value) end end |
#run ⇒ Object
107 108 109 |
# File 'lib/apolo/monitor.rb', line 107 def run instance_exec &@running end |