Class: Nagios::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/nagios/plugin.rb

Constant Summary collapse

VERSION =
'3.0.2'
EXIT_CODE =
{ unknown:  3,
critical: 2,
warning:  1,
ok:       0 }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run!(*args) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/nagios/plugin.rb', line 11

def self.run!(*args)
  plugin = new(*args)
  plugin.check if plugin.respond_to?(:check)
  puts plugin.output
  exit EXIT_CODE[plugin.status]
rescue => e
  puts "PLUGIN UNKNOWN: #{e.message}\n\n" << e.backtrace.join("\n")
  exit EXIT_CODE[:unknown]
end

Instance Method Details

#nameObject



34
35
36
# File 'lib/nagios/plugin.rb', line 34

def name
  self.class.name.split('::').last.upcase
end

#outputObject



21
22
23
24
25
# File 'lib/nagios/plugin.rb', line 21

def output
  s = "#{name.upcase} #{status.upcase}"
  s << ": #{message}" if ( respond_to?(:message) && !message.to_s.empty? )
  s
end

#statusObject



27
28
29
30
31
32
# File 'lib/nagios/plugin.rb', line 27

def status
  return :critical if critical?
  return :warning  if warning?
  return :ok       if ok?
  :unknown
end

#to_sObject



38
39
40
# File 'lib/nagios/plugin.rb', line 38

def to_s
  output
end