nagios-plugin 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,123 @@
1
+ require 'optparse'
2
+
3
+ $:.unshift(File.dirname(__FILE__)) unless
4
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
5
+
6
+ module Nagios
7
+ class Plugin
8
+ VERSION = '0.0.1'
9
+ OK_STATE = 0
10
+ WARNING_STATE = 1
11
+ CRITICAL_STATE = 2
12
+ UNKNOWN_STATE = 3
13
+
14
+ def initialize(args = {})
15
+ @shortname = args[:shortname] || "Nagios::Plugin"
16
+ @print = args[:print] || true
17
+ @exit = args[:exit] || true
18
+ @perfdata = Hash.new
19
+
20
+ # now, handle the arguments...
21
+ @opts = OptionParser.new
22
+ @opts.on("-w", "--warning WARNING", "WARNING THRESHOLD") do |w|
23
+ @warning = w
24
+ end
25
+ @opts.on("-c", "--critical CRITICAL", "CRITICAL THRESHOLD") do |c|
26
+ @critical = c
27
+ end
28
+ end
29
+
30
+ def warning
31
+ @warning
32
+ end
33
+
34
+ def critical
35
+ @critical
36
+ end
37
+
38
+ def parse_argv()
39
+ @opts.parse(ARGV)
40
+ end
41
+
42
+ def exit=(enabled)
43
+ @exit = enabled
44
+ end
45
+
46
+ def print=(enabled)
47
+ @print = enabled
48
+ end
49
+
50
+ # actually does the check
51
+ def run
52
+ raise "You must implement the run method!"
53
+ end
54
+
55
+ def state
56
+ @state
57
+ end
58
+
59
+ def output
60
+ @output
61
+ end
62
+
63
+ def add_perfdata(args)
64
+ field = args[:field] or raise "must supply field!"
65
+ data = args[:data] or raise "must supply data!"
66
+ @perfdata[field] = {:data => data}
67
+ @perfdata[field][:uom] = args[:uom] if args[:uom]
68
+ @perfdata[field][:warning] = args[:warning] if args[:warning]
69
+ @perfdata[field][:critical] = args[:critical] if args[:critical]
70
+ end
71
+
72
+ private
73
+
74
+ def critical(message)
75
+ @state = :CRITICAL
76
+ do_exit(CRITICAL_STATE, message)
77
+ end
78
+
79
+ def ok(message)
80
+ @state = :OK
81
+ do_exit(OK_STATE, message)
82
+ end
83
+
84
+ def warning(message)
85
+ @state = :WARNING
86
+ do_exit(WARNING_STATE, message)
87
+ end
88
+
89
+ def unknown(message)
90
+ @state = :UNKNOWN
91
+ do_exit(UNKNOWN_STATE, message)
92
+ end
93
+
94
+ def do_exit(status, message)
95
+ @output = @shortname + " "
96
+ case status
97
+ when OK_STATE
98
+ @output += "OK - "
99
+ when WARNING_STATE
100
+ @output += "WARNING - "
101
+ when CRITICAL_STATE
102
+ @output += "CRITICAL - "
103
+ when UNKNOWN_STATE
104
+ @output += "UNKNOWN - "
105
+ end
106
+ @output += message
107
+ # see if we have keys...
108
+ if @perfdata.keys.length > 0
109
+ @output += " | "
110
+ outputs = []
111
+ @perfdata.each_pair do |key, value|
112
+ outputs << "#{key}=#{value[:data]}#{value[:uom]};#{value[:warning]};#{value[:critical]}"
113
+ end
114
+ @output += outputs.join(", ")
115
+ end
116
+ @output.strip!
117
+ puts @output if @print
118
+ exit(status) if @exit
119
+ return @output
120
+ end
121
+
122
+ end
123
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/nagios-plugin'
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+
4
+ class TestPlugin < Nagios::Plugin
5
+ def run
6
+ critical("waffles are getting really low...")
7
+ end
8
+ end
9
+
10
+ class TestNagiosPlugin < Test::Unit::TestCase
11
+
12
+ def setup
13
+ @tp = TestPlugin.new(:shortname => "Waffles")
14
+ @tp.exit = false
15
+ @tp.print = false
16
+ end
17
+
18
+ def test_critical
19
+ output = @tp.run
20
+ assert_equal("Waffles CRITICAL - waffles are getting really low...", output)
21
+ end
22
+
23
+ def test_perf
24
+ @tp.add_perfdata(:field => "waffles_per_second", :data => 10, :warning => 2, :critical => 5, :uom => "wps")
25
+ @tp.run
26
+ output = @tp.output
27
+ assert_equal("Waffles CRITICAL - waffles are getting really low... | waffles_per_second=10wps;2;5", output)
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nagios-plugin
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Joshua Harding
13
+ - Daniel Zajic
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-27 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email:
24
+ - danielzajic@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - lib/nagios-plugin.rb
33
+ - test/test_helper.rb
34
+ - test/test_nagios-plugin.rb
35
+ has_rdoc: true
36
+ homepage: https://github.com/analog-analytics/ruby-nagios-plugin
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.6
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Provides a base class for writing Nagios Plugins in Ruby
65
+ test_files: []
66
+