Class: Nagios::Check::Base
- Inherits:
-
Object
- Object
- Nagios::Check::Base
- Defined in:
- lib/nagios-check/base.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#thresholds ⇒ Object
readonly
Returns the value of attribute thresholds.
Class Method Summary collapse
Instance Method Summary collapse
- #add_threshold(name, values = {}) ⇒ Object
- #check_threshold(options) ⇒ Object
- #default_options ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #nagios_exit(code, message) ⇒ Object
- #parse(args) ⇒ Object
- #perfoutput ⇒ Object
- #run(args = []) ⇒ Object
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
14 15 16 17 |
# File 'lib/nagios-check/base.rb', line 14 def initialize @thresholds = {} @options = {}.merge!() end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/nagios-check/base.rb', line 5 def @options end |
#thresholds ⇒ Object (readonly)
Returns the value of attribute thresholds.
5 6 7 |
# File 'lib/nagios-check/base.rb', line 5 def thresholds @thresholds end |
Class Method Details
.shortname(value = nil) ⇒ Object
8 9 10 11 |
# File 'lib/nagios-check/base.rb', line 8 def shortname(value = nil) @shortname = value if value @shortname end |
Instance Method Details
#add_threshold(name, values = {}) ⇒ Object
66 67 68 69 70 |
# File 'lib/nagios-check/base.rb', line 66 def add_threshold(name, values = {}) @thresholds[name] ||= Threshold.new @thresholds[name].send(:warn, values[:warn]) if values[:warn] @thresholds[name].send(:crit, values[:crit]) if values[:crit] end |
#check_threshold(options) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/nagios-check/base.rb', line 55 def check_threshold() if .is_a?(Hash) name, value = .to_a.flatten else name = :default value = end raise Exception, "threshold '#{name}' does not exit" unless self.thresholds[name] self.thresholds[name].get_status(value) end |
#default_options ⇒ Object
31 32 33 |
# File 'lib/nagios-check/base.rb', line 31 def {} end |
#nagios_exit(code, message) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/nagios-check/base.rb', line 39 def nagios_exit(code, ) exit_code = code.is_a?(Symbol) ? Nagios::EXIT_CODE[code] : code reversed_exit_codes = Nagios::EXIT_CODE.to_a.reduce([]) { |r,v| r << v.reverse } exit_status = Hash[reversed_exit_codes][exit_code] unless exit_status puts "UNKNOWN - exit code '#{code}' is not found." exit UNKNOWN end output = exit_status.to_s.upcase output << " - #{}" output = self.class.shortname.nil? ? output : "#{self.clas.shortname.shortname} #{output}" output << " | #{perfoutput}" if perfoutput puts output exit exit_code end |
#parse(args) ⇒ Object
35 36 37 |
# File 'lib/nagios-check/base.rb', line 35 def parse(args) option_parser(args) end |
#perfoutput ⇒ Object
28 29 |
# File 'lib/nagios-check/base.rb', line 28 def perfoutput end |
#run(args = []) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/nagios-check/base.rb', line 19 def run(args = []) parse(args) begin check rescue => e nagios_exit(:unknown, "#{e.class}: #{e.to_s}\n#{e.backtrace.join("\n")}") end end |