Class: Apolo::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/apolo/monitor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMonitor

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

#nameObject

Reader/setter for the name of this monitor



70
71
72
# File 'lib/apolo/monitor.rb', line 70

def name
  @name
end

#notifiersObject (readonly)

Returns all the registered notifiers.



67
68
69
# File 'lib/apolo/monitor.rb', line 67

def notifiers
  @notifiers
end

#readersObject (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.

Parameters:

  • name (String, #read)

    The name to be given to a Apolo-based class.



25
26
27
28
# File 'lib/apolo/monitor.rb', line 25

def name(val = nil)
  @name_template = val if val
  @name_template
end

.name_templateObject



12
13
14
# File 'lib/apolo/monitor.rb', line 12

def name_template
  @name_template || self.to_s
end

.notifier_templatesObject



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.

Parameters:

  • class (Class, #read)

    A class that will be used to issue a notification. The class must accept a configuration hash in the constructor and also implement a #notify method that will receive an outpost instance. See Notifiers::Console for an example.

  • options (Hash, #read) (defaults to: {})

    Options that will be used to configure the notification class.



53
54
55
56
# File 'lib/apolo/monitor.rb', line 53

def notify(notifier, options={})
  @notifier_templates ||= []
  @notifier_templates << {:notifier => notifier, :options => options}
end

.reader_templatesObject



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_templateObject



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.

Parameters:

  • reader_description (Hash{Scout => String}, #read)

    A hash containing Reader class as key and its description as a value.

Yields:

  • Block to be evaluated to configure the current Reader.



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

See Also:



103
104
105
# File 'lib/apolo/monitor.rb', line 103

def add_notifier(notifier_name, options)
  @notifiers[notifier_name] = options
end

#add_reader(reader_description, &block) ⇒ Object

See Also:



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

Raises:

  • (ArgumentError)


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)
  message = data[:message]
  value = data[:value]

  unless message && value
    raise ArgumentError, 'You need to set :message and :value to send notify.'
  end

  @notifiers.each do |notifier, options|
    # .dup is NOT reliable
    options_copy = Marshal.load(Marshal.dump(options))
    notifier.new(options_copy).notify(@name, message, value)
  end
end

#runObject



107
108
109
# File 'lib/apolo/monitor.rb', line 107

def run
  instance_exec &@running
end