Class: Outpost::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/outpost/application.rb

Overview

This class is the basic structure of an Outpost application. It provides the basic DSL for you to configure the monitoring of your services. Example:

class ExampleSuccess < Outpost::Application
  name "Example"
  using Outpost::Scouts::Http => 'master http server' do
    options :host => 'localhost', :port => 9595
    report :up, :response_code => 200
  end
end

The above example will set templates and every new instance of ExampleSuccess will have the same behavior. But all the methods are available to instances, so it is also possible to create Outpost applications as the following example:

my_outpost = Outpost::Application.new
my_outpost.name = 'Example'

my_outpost.using(Outpost::Scouts::Http => 'master http server') do
  options :host => 'localhost', :port => 9595
  report :up, :response_code => 200
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

New instance of a Outpost-based class.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/outpost/application.rb', line 95

def initialize
  @reports     = {}
  @last_status = nil
  @scouts      = Hash.new { |h, k| h[k] = [] }
  @notifiers   = {}
  @name        = self.class.name_template

  # Register scouts
  self.class.scout_templates.each do |template|
    add_scout(template[:scout_description], &template[:block])
  end

  self.class.notifier_templates.each do |template|
    add_notifier(template[:notifier], template[:options])
  end
end

Instance Attribute Details

#last_statusObject (readonly)

Returns the status of the last service check or nil if it didn’t happen.



80
81
82
# File 'lib/outpost/application.rb', line 80

def last_status
  @last_status
end

#nameObject

Reader/setter for the name of this scout



92
93
94
# File 'lib/outpost/application.rb', line 92

def name
  @name
end

#notifiersObject (readonly)

Returns all the registered notifiers.



89
90
91
# File 'lib/outpost/application.rb', line 89

def notifiers
  @notifiers
end

#reportsObject (readonly)

Returns a list of Report containing the last results of the last check.



83
84
85
# File 'lib/outpost/application.rb', line 83

def reports
  @reports
end

#scoutsObject (readonly)

Returns all the registered scouts.



86
87
88
# File 'lib/outpost/application.rb', line 86

def scouts
  @scouts
end

Class Method Details

.name(val = nil) ⇒ Object

Set the name of the scout. 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 Outpost-based class.



73
74
75
76
# File 'lib/outpost/application.rb', line 73

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

.name_templateObject



36
37
38
# File 'lib/outpost/application.rb', line 36

def name_template
  @name_template || self.to_s
end

.notifier_templatesObject



32
33
34
# File 'lib/outpost/application.rb', line 32

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::Email for an example.

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

    Options that will be used to configure the notification class.



63
64
65
66
# File 'lib/outpost/application.rb', line 63

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

.scout_templatesObject



28
29
30
# File 'lib/outpost/application.rb', line 28

def scout_templates
  @scout_templates || []
end

.using(scout_description) { ... } ⇒ Object

Register a scout in the list of scouts.

Parameters:

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

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

Yields:

  • Block to be evaluated to configure the current Scout.



46
47
48
49
50
51
52
# File 'lib/outpost/application.rb', line 46

def using(scout_description, &block)
  @scout_templates ||= []
  @scout_templates << {
    :scout_description => scout_description,
    :block => block
  }
end

Instance Method Details

#add_notifier(notifier_name, options) ⇒ Object

See Also:



126
127
128
# File 'lib/outpost/application.rb', line 126

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

#add_scout(scout_description, &block) ⇒ Object

See Also:



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/outpost/application.rb', line 113

def add_scout(scout_description, &block)
  config = ScoutConfig.new
  config.instance_eval(&block)

  scout_description.each do |scout, description|
    @scouts[scout] << {
      :description => description,
      :config => config
    }
  end
end

#down?Boolean

Returns true if the last status is :down

Returns:

  • (Boolean)


166
167
168
# File 'lib/outpost/application.rb', line 166

def down?
  @last_status == :down
end

#messagesArray<String>

Returns the messages of the latest service check.

Returns:

  • (Array<String>)

    An array containing all report messages.



173
174
175
# File 'lib/outpost/application.rb', line 173

def messages
  reports.map { |_, r| r.to_s }
end

#notifyObject

Runs all notifications associated with an Outpost-based class.



145
146
147
148
149
150
151
152
153
# File 'lib/outpost/application.rb', line 145

def notify
  if reports.any?
    @notifiers.each do |notifier, options|
      # .dup is NOT reliable
      options_copy = Marshal.load(Marshal.dump(options))
      notifier.new(options_copy).notify(self)
    end
  end
end

#runObject

Execute all the scouts associated with an Outpost-based class and returns either :up or :down, depending on the results.



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/outpost/application.rb', line 132

def run
  scouts.map do |scout, configurations|
    configurations.each do |options|
      @reports[options[:description]] = run_scout(scout, options)
    end
  end

  statuses = @reports.map { |_, r| r.status }

  @last_status = Report.summarize(statuses)
end

#up?Boolean

Returns true if the last status is :up

Returns:

  • (Boolean)


156
157
158
# File 'lib/outpost/application.rb', line 156

def up?
  @last_status == :up
end

#warning?Boolean

Returns true if the last status is :warning

Returns:

  • (Boolean)


161
162
163
# File 'lib/outpost/application.rb', line 161

def warning?
  @last_status == :warning
end