Class: Sensu::Extension::Base
- Inherits:
-
Object
- Object
- Sensu::Extension::Base
- Defined in:
- lib/sensu/extension.rb
Instance Attribute Summary collapse
-
#logger ⇒ Array
Logger provided by Sensu.
-
#settings ⇒ Array
Settings hash provided by Sensu.
Class Method Summary collapse
-
.descendants ⇒ Array<object>
Determine classes that have inherited this class, used by the extension loader.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Retrieve the definition object corresponding to a key, acting like a Hash object.
-
#definition ⇒ Object
Override this method to change the extension’s definition, a hash.
-
#description ⇒ Object
Override this method to set the extension’s description.
-
#has_key?(key) ⇒ TrueClass, FalseClass
Check to see if the definition has a key.
-
#initialize ⇒ Base
constructor
Initialize the extension, call post_init() when the eventmachine reactor starts up, stop() when it stops.
-
#name ⇒ Object
Override this method to set the extension’s name.
-
#name_alias ⇒ Object
Override this method to set a name alias for the extension.
-
#post_init ⇒ Object
Override this method to do something immediately after the eventmachine reactor is started.
-
#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.
-
#safe_run(data = nil) {|output, status| ... } ⇒ Object
Run the extension with a few safeties.
-
#stop ⇒ Object
Override this method to do something when the eventmachine reactor stops, such as connection or file cleanup.
Constructor Details
#initialize ⇒ Base
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
#logger ⇒ Array
Returns logger provided by Sensu.
9 10 11 |
# File 'lib/sensu/extension.rb', line 9 def logger @logger end |
#settings ⇒ Array
Returns settings hash provided by Sensu.
13 14 15 |
# File 'lib/sensu/extension.rb', line 13 def settings @settings end |
Class Method Details
.descendants ⇒ Array<object>
Determine classes that have inherited this class, used by the extension loader. Do not override this method!
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!
84 85 86 |
# File 'lib/sensu/extension.rb', line 84 def [](key) definition[key.to_sym] end |
#definition ⇒ Object
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 |
#description ⇒ Object
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!
93 94 95 |
# File 'lib/sensu/extension.rb', line 93 def has_key?(key) definition.has_key?(key.to_sym) end |
#name ⇒ Object
Override this method to set the extension’s name.
27 28 29 |
# File 'lib/sensu/extension.rb', line 27 def name "base" end |
#name_alias ⇒ Object
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_init ⇒ Object
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.
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!
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 |
#stop ⇒ Object
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 |