Class: Nagios::PluginBase
- Inherits:
-
Object
- Object
- Nagios::PluginBase
- Defined in:
- lib/nagios_plugin_base.rb,
lib/nagios_plugin_base/version.rb
Constant Summary collapse
- CODES =
exit codes
[ "OK", "WARNING", "CRITICAL", "UNKNOWN" ]
- OPTOINS =
well known options
{ :hostname => ['-H','--hostname=VAL'], :warning => ['-w','--warning', 'warning threshold',Float], :critical => ['-c','--critical', 'critical threshold',Float], :authentication => ['-a','--authentication=VAL'], :verbose => ['-v','--[no-]verbose'], :timeout => ['-t','--timeout=VAL',Float], :port => ['-p','--port=VAL',Integer], :logname => ['-l','--logname=VAL'], :url => ['-u','--url=VAL'], :community => ['-C','--community'], }
- VERSION =
"0.0.1"
Class Method Summary collapse
-
.check!(*args, &block) ⇒ Object
instant check.
Instance Method Summary collapse
-
#critical! ⇒ Object
Print “CRITICAL” and exit 2.
-
#exit_(code) ⇒ Object
Print status (ex: OK) and exit.
-
#initialize(args = []) ⇒ PluginBase
constructor
A new instance of PluginBase.
-
#invalid_args!(message = nil) ⇒ Object
Print message and option helps, and exit as UNKNOWN.
-
#ok! ⇒ Object
Print “OK” and exit 0.
-
#option(arg) ⇒ Object
set option to option parser.
-
#parse!(argv) ⇒ Object
parse option.
-
#run { ... } ⇒ Object
Run block with timeout.
-
#set_default_option(arg, options) ⇒ Object
Add option to option parser and create attribute-reader to self.
-
#warning! ⇒ Object
Print “WARNING” and exit 3.
Constructor Details
#initialize(args = []) ⇒ PluginBase
Returns a new instance of PluginBase.
84 85 86 87 |
# File 'lib/nagios_plugin_base.rb', line 84 def initialize(args=[]) @opt = OptionParser.new args.each{|a| option(a)} end |
Class Method Details
.check!(*args, &block) ⇒ Object
instant check
111 112 113 114 115 116 117 |
# File 'lib/nagios_plugin_base.rb', line 111 def self.check!(*args,&block) opt = self.new(args) opt.parse!(ARGV) opt.run do |opta| opt.instance_eval(&block) end end |
Instance Method Details
#critical! ⇒ Object
Print “CRITICAL” and exit 2
36 37 38 39 40 41 42 |
# File 'lib/nagios_plugin_base.rb', line 36 CODES.each_with_index do |name,code| eval <<-CODE def #{name.downcase}! exit_ #{code} end CODE end |
#exit_(code) ⇒ Object
Print status (ex: OK) and exit
53 54 55 56 57 58 59 60 |
# File 'lib/nagios_plugin_base.rb', line 53 def exit_(code) if CODES[code] puts "#{CODES[code]}" exit code else raise "unknown exit code #{code}" end end |
#invalid_args!(message = nil) ⇒ Object
Print message and option helps, and exit as UNKNOWN. this is default style of argument error.
46 47 48 49 50 |
# File 'lib/nagios_plugin_base.rb', line 46 def invalid_args!(=nil) puts if puts @opt.help unknown! end |
#ok! ⇒ Object
Print “OK” and exit 0
36 37 38 39 40 41 42 |
# File 'lib/nagios_plugin_base.rb', line 36 CODES.each_with_index do |name,code| eval <<-CODE def #{name.downcase}! exit_ #{code} end CODE end |
#option(arg) ⇒ Object
set option to option parser
76 77 78 79 80 81 82 |
# File 'lib/nagios_plugin_base.rb', line 76 def option(arg) if OPTOINS[arg] set_default_option(arg, OPTOINS[arg]) else raise "unknwon nagios plugin option #{arg}" end end |
#parse!(argv) ⇒ Object
parse option
90 91 92 93 94 95 96 |
# File 'lib/nagios_plugin_base.rb', line 90 def parse!(argv) begin @opt.parse!(argv) rescue OptionParser::InvalidOption => e invalid_args!(e.to_s) end end |
#run { ... } ⇒ Object
Run block with timeout.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/nagios_plugin_base.rb', line 100 def run Timeout.timeout(@timeout) do ret = yield raise "block need exit code" unless ret exit_(ret) end rescue Timeout::Error => e puts "timeout" critical! end |
#set_default_option(arg, options) ⇒ Object
Add option to option parser and create attribute-reader to self
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/nagios_plugin_base.rb', line 64 def set_default_option(arg, ) @opt.on(*){|v| instance_variable_set("@#{arg}".to_sym, v) } eval <<-CODE class << self define_method(:#{arg}){ @#{arg} } end CODE end |
#warning! ⇒ Object
Print “WARNING” and exit 3
36 37 38 39 40 41 42 |
# File 'lib/nagios_plugin_base.rb', line 36 CODES.each_with_index do |name,code| eval <<-CODE def #{name.downcase}! exit_ #{code} end CODE end |