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.



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

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.



9
10
11
# File 'lib/sensu/extension.rb', line 9

def logger
  @logger
end

#settingsArray

Returns settings hash provided by Sensu.

Returns:

  • (Array)

    settings hash provided by Sensu.



13
14
15
# File 'lib/sensu/extension.rb', line 13

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>)


132
133
134
135
136
# File 'lib/sensu/extension.rb', line 132

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.



84
85
86
# File 'lib/sensu/extension.rb', line 84

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.



45
46
47
48
49
50
# File 'lib/sensu/extension.rb', line 45

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

#descriptionObject

Override this method to set the extension’s description.



38
39
40
# File 'lib/sensu/extension.rb', line 38

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)


93
94
95
# File 'lib/sensu/extension.rb', line 93

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

#nameObject

Override this method to set the extension’s name.



27
28
29
# File 'lib/sensu/extension.rb', line 27

def name
  "base"
end

#name_aliasObject

Override this method to set a name alias for the extension. The extention can be referenced by the name alias. This method is used to aid in the transition to the extension from a Sensu component sharing the same name, e.g. pipe handler.



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

def name_alias; 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.



55
56
57
# File 'lib/sensu/extension.rb', line 55

def post_init
  true
end

#run(data = nil) {|output, status| ... } ⇒ 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.

Yields:

  • (output, status)

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

Yield Parameters:

  • output (String)
  • status (Integer)


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

def run(data=nil)
  yield("noop", 0)
end

#safe_run(data = nil) {|output, status| ... } ⇒ Object

Run the extension with a few safeties. This method wraps run() with a begin;rescue and determines if the extension utilizes the provided data (i.e. event data). Do not override this method!

Parameters:

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

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

Yields:

  • (output, status)

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

Yield Parameters:

  • output (String)
  • status (Integer)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/sensu/extension.rb', line 109

def safe_run(data=nil)
  begin
    @run_arity ||= method(:run).arity.abs
    if @run_arity >= 1
      run(data) do |output, status|
        yield(output, status)
      end
    else
      run do |output, status|
        yield(output, status)
      end
    end
  rescue => error
    klass = error.class.name
    backtrace = error.backtrace.map { |line| "\s\s#{line}" }.join("\n")
    yield("#{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.



75
76
77
# File 'lib/sensu/extension.rb', line 75

def stop
  true
end