Module: NagiosCheck

Includes:
Chronometer
Defined in:
lib/nagios_check.rb,
lib/nagios_check/range.rb,
lib/nagios_check/version.rb,
lib/nagios_check/chronometer.rb

Defined Under Namespace

Modules: Chronometer, ClassMethods Classes: MissingOption, Range, Result

Constant Summary collapse

VERSION =
"0.4.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Chronometer

#time

Instance Attribute Details

#messageObject

Returns the value of attribute message.



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

def message
  @message
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/nagios_check.rb', line 12

def options
  @options
end

Class Method Details

.included(base) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/nagios_check.rb', line 157

def self.included(base)
  base.extend(ClassMethods)
  base.instance_eval do 
    @option_specs = {} 
    @defaults = {}
  end
end

Instance Method Details

#finishObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/nagios_check.rb', line 81

def finish
  raise "No value was provided" if @values.empty?
  value = @values.first.last
  return [3, "UNKNOWN"] if value.nil?
  if @options.c && !@options.c.include?(value)
    return [2, "CRITICAL"]
  end
  if @options.w && !@options.w.include?(value)
    return [1, "WARNING"]
  end
  return [0, "OK"]
end

#perform(argv) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nagios_check.rb', line 48

def perform(argv)
  prepare
  return_val, status = 3, "UNKNOWN"
  begin
    parse_options(argv)
    if @options.t
      check_with_timeout
    else
      check
    end
    return_val, status = finish
  rescue Timeout::Error
    store_message "TIMEOUT after #{@options.t}s"
  rescue OptionParser::InvalidArgument, NagiosCheck::MissingOption => e
    store_message "CLI ERROR: #{e}"
  rescue => e
    store_message "INTERNAL ERROR: #{e.class.name}: #{(e.to_s || '').gsub(/[\r\n]+/, ' ')}"
  end
  msg = status
  msg += ': ' + message if message
  if @values && !@values.empty?
    msg += '|' + @values.map do |name, value|
      "#{name}=#{value};;;;"
    end.join(' ')
  end

  Result.new(return_val, msg, @values)
end

#prepareObject



37
38
39
40
# File 'lib/nagios_check.rb', line 37

def prepare
  @values = {}
  self.message = nil
end

#runObject



42
43
44
45
46
# File 'lib/nagios_check.rb', line 42

def run
  result = perform(ARGV)
  puts result.output
  exit result.exit_status
end

#store_message(message) ⇒ Object



77
78
79
# File 'lib/nagios_check.rb', line 77

def store_message(message)
  self.message = message
end

#store_value(name, value, opts = {}) ⇒ Object



94
95
96
# File 'lib/nagios_check.rb', line 94

def store_value(name, value, opts = {})
  @values[name] = (value.nil? ? nil : value.to_f)
end