Class: Nagios::PluginBase

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ PluginBase

Returns a new instance of PluginBase.

Parameters:

  • args (Array<Symbol>) (defaults to: [])

    option parser arguments



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

Parameters:

  • :code (Integer)


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!(message=nil)
  puts message if message
  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

Parameters:

  • :arg (Symbol)

    option from OPTIONS



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

Parameters:

  • argv (Array)

    cmmand line argumetns



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.

Yields:

  • Main check callback block.

Yield Returns:

  • (Integer)

    exit code.



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

Parameters:

  • arg (Symbol)

    Option name

  • options (Array)

    Option arguments for optparse



64
65
66
67
68
69
70
71
72
73
# File 'lib/nagios_plugin_base.rb', line 64

def set_default_option(arg, options)
  @opt.on(*options){|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