Class: Nagios::Check::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/nagios-check/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



14
15
16
17
# File 'lib/nagios-check/base.rb', line 14

def initialize
  @thresholds = {}
  @options = {}.merge!(default_options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/nagios-check/base.rb', line 5

def options
  @options
end

#thresholdsObject (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

Raises:

  • (Exception)


55
56
57
58
59
60
61
62
63
64
# File 'lib/nagios-check/base.rb', line 55

def check_threshold(options)
  if options.is_a?(Hash)
    name, value = options.to_a.flatten
  else
    name = :default
    value = options
  end
  raise Exception, "threshold '#{name}' does not exit" unless self.thresholds[name]
  self.thresholds[name].get_status(value)
end

#default_optionsObject



31
32
33
# File 'lib/nagios-check/base.rb', line 31

def default_options
  {}
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, message)
  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 << " - #{message}"
  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

#perfoutputObject



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