Class: Sensu::Extension::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu/extension.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Initialize the extension, call post_init() when the eventmachine reactor starts up, stop() when it stops.


19
20
21
22
23
24
25
26
# File 'lib/sensu/extension.rb', line 19

def initialize
  EM.next_tick do
    post_init
  end
  EM.add_shutdown_hook do
    stop
  end
end

Instance Attribute Details

#loggerArray

Returns logger provided by Sensu.

Returns:

  • (Array)

    logger provided by Sensu.


11
12
13
# File 'lib/sensu/extension.rb', line 11

def logger
  @logger
end

#settingsArray

Returns settings hash provided by Sensu.

Returns:

  • (Array)

    settings hash provided by Sensu.


15
16
17
# File 'lib/sensu/extension.rb', line 15

def settings
  @settings
end

Class Method Details

.descendantsArray<object>

Determine classes that have inherited this class, used by the extension loader. Do not override this method!

Returns:

  • (Array<object>)

120
121
122
123
124
# File 'lib/sensu/extension.rb', line 120

def self.descendants
  ObjectSpace.each_object(Class).select do |klass|
    klass < self
  end
end

Instance Method Details

#[](key) ⇒ Object

Retrieve the definition object corresponding to a key, acting like a Hash object. Do not override this method!

Parameters:

  • key (String, Symbol)

Returns:

  • (Object)

    value for key.


79
80
81
# File 'lib/sensu/extension.rb', line 79

def [](key)
  definition[key.to_sym]
end

#definitionObject

Override this method to change the extension's definition, a hash. You probably don't need to touch this. The hash must contain :type (“extension”) and :name.


41
42
43
44
45
46
# File 'lib/sensu/extension.rb', line 41

def definition
  {
    :type => "extension",
    :name => name
  }
end

#descriptionObject

Override this method to set the extension's description.


34
35
36
# File 'lib/sensu/extension.rb', line 34

def description
  "extension description (change me)"
end

#has_key?(key) ⇒ TrueClass, FalseClass

Check to see if the definition has a key. Do not override this method!

Parameters:

  • key (String, Symbol)

Returns:

  • (TrueClass, FalseClass)

88
89
90
# File 'lib/sensu/extension.rb', line 88

def has_key?(key)
  definition.has_key?(key.to_sym)
end

#nameObject

Override this method to set the extension's name.


29
30
31
# File 'lib/sensu/extension.rb', line 29

def name
  "base"
end

#post_initObject

Override this method to do something immediately after the eventmachine reactor is started. This method is great for setting up connections etc.


51
52
53
# File 'lib/sensu/extension.rb', line 51

def post_init
  true
end

#run(data = nil, options = {}, &callback) ⇒ Object

Override this method to do something when the extension is run, you must yield or call the callback with two parameters, an output string and exit code.

Parameters:

  • data (Object, nil) (defaults to: nil)

    provided by Sensu.

  • options (Hash) (defaults to: {})

    provided by Sensu, may contain connection objects, eg. redis.

  • callback (Proc)

    provided by Sensu, expecting to be called with two parameters, an output string and exit code.


64
65
66
# File 'lib/sensu/extension.rb', line 64

def run(data=nil, options={}, &callback)
  callback.call("noop", 0)
end

#safe_run(data = nil, options = {}, &callback) ⇒ Object

Run the extension with a few safeties. This method wraps run() with a begin;rescue, and duplicates data before passing it to ensure the extension doesn't mutate the original. Do not override this method!

Parameters:

  • data (Object, nil) (defaults to: nil)

    to dup() and pass to run(), if run() has an absolue arity of 1 or more.

  • options (Hash) (defaults to: {})

    to pass to run(), if run() has an absolute arity of 2.

  • callback (Proc)

    to pass to run().


102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sensu/extension.rb', line 102

def safe_run(data=nil, options={}, &callback)
  begin
    @run_arity ||= method(:run).arity.abs
    arguments = []
    arguments << (data ? data.dup : data) if @run_arity >= 1
    arguments << options if @run_arity == 2
    run(*arguments, &callback)
  rescue => error
    klass = error.class.name
    backtrace = error.backtrace.map { |line| "\s\s#{line}" }.join("\n")
    callback.call("#{klass}: #{error}\n#{backtrace}", 2)
  end
end

#stopObject

Override this method to do something when the eventmachine reactor stops, such as connection or file cleanup.


70
71
72
# File 'lib/sensu/extension.rb', line 70

def stop
  true
end