nagiosplugin 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -39,6 +39,13 @@ Profit... and maybe also fun.
39
39
  (If you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull.)
40
40
  * Send me a pull request. Bonus points for topic branches.
41
41
 
42
+ ## Acknowledgments
43
+
44
+ Thanks to the following contributors for improving NagiosPlugin:
45
+
46
+ * [szuecs (Sandor Szücs)](https://github.com/szuecs): Adding default
47
+ options, perfdata stuff and helpful sample code.
48
+
42
49
  ## Copyright
43
50
 
44
51
  Copyright (c) 2011-2012 Björn Albers. See LICENSE for details.
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift(File.dirname(__FILE__) + "/lib")
4
+
5
+ require "nagiosplugin"
6
+ require "nagiosplugin/default_options"
7
+
8
+ class MyPlugin < NagiosPlugin::Plugin
9
+ include NagiosPlugin::DefaultOptions
10
+
11
+ VERSION = 1.0
12
+
13
+ class << self
14
+ def run(*args)
15
+ self.new(*args).run
16
+ end
17
+ end
18
+
19
+ def parse_options(*args)
20
+ @options = {}
21
+ OptionParser.new do |opts|
22
+ opts.banner = "% #{File.basename($0)} --filename <file>"
23
+ opts.separator ""
24
+ opts.separator "Specific options:"
25
+ opts.separator ""
26
+
27
+ opts.on('-f', '--filename <name>', 'Filename that should exist.') do |s|
28
+ @options[:filename] = s
29
+ end
30
+
31
+ yield(opts) if block_given?
32
+
33
+ begin
34
+ opts.parse!(args)
35
+ @options
36
+ rescue => e
37
+ puts "#{e}\n\n#{opts}"
38
+ exit(3)
39
+ end
40
+ end
41
+ end
42
+
43
+ def initialize(*args)
44
+ parse_options(*args, &default_options)
45
+ @warn = @options[:warn] if @options[:warn]
46
+ @crit = @options[:crit] if @options[:crit]
47
+ @reverse = @options[:reverse] if @options[:reverse]
48
+ @filename = @options[:filename]
49
+ end
50
+
51
+ def check
52
+ if File.executable?(@filename)
53
+ ok("#{@filename} is excutable")
54
+ elsif File.exists?(@filename)
55
+ warning("#{@filename} is not excutable")
56
+ else
57
+ critical("#{@filename} does not exist")
58
+ end
59
+ end
60
+ end
61
+
62
+ MyPlugin.run(*ARGV)
@@ -0,0 +1,30 @@
1
+ require "nagiosplugin"
2
+ require "nagiosplugin/plugin"
3
+ require "nagiosplugin/perfdata"
4
+
5
+ class CheckPerfdata < NagiosPlugin::Plugin
6
+ include NagiosPlugin::Perfdata
7
+
8
+ def check
9
+ sleep 0.4
10
+ pd = PerfData.new
11
+ pd.val = 40
12
+ pd.warn = 25
13
+ pd.crit = 50
14
+ pd.min = 1
15
+ pd.max = 90
16
+ pd.uom = "%"
17
+
18
+ @perfdata = {
19
+ :pd => pd,
20
+ :baz => PerfData.new(1),
21
+ :foo => PerfData.new(1,2,3),
22
+ :bar => PerfData.new(4,5,6,7,8),
23
+ }
24
+ ok('super perfdata')
25
+ sleep 0.2
26
+ end
27
+
28
+ end
29
+
30
+ CheckPerfdata.run
@@ -0,0 +1,78 @@
1
+ require "optparse"
2
+
3
+ module NagiosPlugin
4
+ module DefaultOptions
5
+ def parse_num(s)
6
+ if s.include? "."
7
+ s.to_f
8
+ else
9
+ s.to_i
10
+ end
11
+ end
12
+
13
+ def parse_start_num(s)
14
+ if s.include? '~'
15
+ s.delete!('~')
16
+ Float::MIN
17
+ else
18
+ parse_num(s)
19
+ end
20
+ end
21
+ def parse_reverse(s)
22
+ if s.include? '@'
23
+ s.delete!('@')
24
+ @options[:invert] = true
25
+ end
26
+ end
27
+
28
+ def parse_nagios_style(s)
29
+ parse_reverse(s)
30
+
31
+ if not s.include? ':'
32
+ s = ":" + s
33
+ end
34
+
35
+ start, stop = s.split(':')
36
+ if start.empty?
37
+ # :m
38
+ start = 0
39
+ stop = parse_num(stop)
40
+ elsif stop.empty?
41
+ # n:
42
+ start = parse_start_num(start)
43
+ stop = Float::MAX
44
+ else
45
+ # n:m
46
+ start = parse_start_num(start)
47
+ stop = parse_num(stop)
48
+ end
49
+ start..stop
50
+ end
51
+
52
+ def default_options
53
+ lambda do |opts|
54
+ opts.separator ""
55
+ opts.separator "Default options:"
56
+ opts.separator ""
57
+
58
+ opts.on('-h', '--help', 'Display this help.') do
59
+ puts "#{opts}"
60
+ exit(3)
61
+ end
62
+
63
+ opts.on('-V', '--version', 'Print version.') do |s|
64
+ puts "#{File.basename($0)} #{VERSION}"
65
+ exit(3)
66
+ end
67
+
68
+ opts.on('-w', '--warn <n:m>', 'Warning threshold.') do |s|
69
+ @options[:warn] = parse_nagios_style(s)
70
+ end
71
+
72
+ opts.on('-c', '--crit <n:m>', 'Critical threshold.') do |s|
73
+ @options[:crit] = parse_nagios_style(s)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,40 @@
1
+ module NagiosPlugin
2
+ module Perfdata
3
+ _perfdata = Struct.new :val, :warn, :crit, :min, :max, :uom
4
+ PerfData = _perfdata
5
+
6
+ def get_perfdata
7
+ if @perfdata.class == PerfData
8
+ @perfdata.select {|e| e}.join(';')
9
+ else
10
+ @perfdata.map do |k,v|
11
+ "#{k}=" + v.select {|e| e}.join(';')
12
+ end.join(' ')
13
+ end
14
+ end
15
+
16
+ # Run check and return result in a nagios-compatible format.
17
+ #
18
+ # It will...
19
+ # - execute your check method
20
+ # - output the result in a nagios-compatible format (SERVICE STATUS: Message)
21
+ # - exit according to the status
22
+ def run
23
+ t1 = Time.now
24
+ check
25
+ rescue StatusError => e
26
+ rescue => e
27
+ e = StatusError.new(:unknown, ([e.to_s, nil] + e.backtrace).join("\n"))
28
+ else
29
+ e = StatusError.new(:unknown, 'no status method was called')
30
+ ensure
31
+ t2 = Time.now
32
+ msg = [service, e.to_s].join(' ')
33
+ perfdata = get_perfdata
34
+ t = t2 - t1
35
+ puts "#{msg}|time=#{t} #{perfdata}"
36
+ exit e.to_i
37
+ end
38
+
39
+ end
40
+ end
@@ -1,6 +1,42 @@
1
+ require "optparse"
2
+
1
3
  module NagiosPlugin
2
4
  class Plugin
3
5
  class << self
6
+ def parse_options(options, &blk2)
7
+ OptionParser.new do |opts|
8
+ opts.banner = "% #{$0} [options]"
9
+ opts.separator ""
10
+ opts.separator "Specific options:"
11
+ opts.separator ""
12
+
13
+ opts.on('-h', '--help', 'Display this help.') do
14
+ abort "#{opts}"
15
+ end
16
+
17
+ opts.on('-r', '--reverse', 'Reverse thresholds.') do |b|
18
+ options[:reverse] = b
19
+ end
20
+
21
+ opts.on('-w', '--warn <n>', 'Warning threshold.') do |s|
22
+ options[:warn] = s.to_i
23
+ end
24
+
25
+ opts.on('-c', '--crit <n>', 'Critical threshold.') do |s|
26
+ options[:crit] = s.to_i
27
+ end
28
+
29
+ blk2.call(opts)
30
+
31
+ begin
32
+ opts.parse!
33
+ rescue => e
34
+ abort "#{e}\n\n#{opts}"
35
+ end
36
+ end
37
+
38
+ options
39
+ end
4
40
 
5
41
  # Create new instance and run it.
6
42
  def run(*args)
@@ -25,6 +61,13 @@ module NagiosPlugin
25
61
  make :critical
26
62
  make :unknown
27
63
 
64
+ def initialize(options={})
65
+ @warn = options[:warn] if options[:warn]
66
+ @crit = options[:crit] if options[:crit]
67
+ @reverse = options[:reverse] if options[:reverse]
68
+ end
69
+
70
+
28
71
  # Overwrite this check method and call a status method within.
29
72
  def check
30
73
  unknown 'please overwrite the `check` method in your class'
@@ -58,5 +101,6 @@ module NagiosPlugin
58
101
  puts [service, e.to_s].join(' ')
59
102
  exit e.to_i
60
103
  end
104
+
61
105
  end
62
106
  end
@@ -1,3 +1,3 @@
1
1
  module NagiosPlugin
2
- VERSION = '1.2.0'
2
+ VERSION = '1.3.0'
3
3
  end
@@ -4,6 +4,27 @@ describe NagiosPlugin::Plugin do
4
4
  before do
5
5
  class MyPlugin < NagiosPlugin::Plugin; end
6
6
  @plugin = MyPlugin.new
7
+
8
+ class MergedOptionsPlugin < NagiosPlugin::Plugin
9
+ def initialize(options, &blk)
10
+ super(options, &blk)
11
+ @foo = options[:foo]
12
+ end
13
+ attr_reader :warn, :crit, :reverse, :foo
14
+ end
15
+ @plugin_with_options = MergedOptionsPlugin.new({:warn => 5, :crit => 1, :reverse => true, :foo => "bar"}) do |opts|
16
+ opts.on('--foo <s>', 'A test option.') do |s|
17
+ options[:foo] = s
18
+ end
19
+ end
20
+ end
21
+
22
+ describe '#initialize' do
23
+ it "should have attributes" do
24
+ @plugin_with_options.should satisfy { |o|
25
+ o.warn == 5 and o.crit == 1 and o.reverse and o.foo == "bar"
26
+ }
27
+ end
7
28
  end
8
29
 
9
30
  describe '#run' do
metadata CHANGED
@@ -1,89 +1,116 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: nagiosplugin
3
- version: !ruby/object:Gem::Version
4
- version: 1.2.0
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
5
  prerelease:
6
+ segments:
7
+ - 1
8
+ - 3
9
+ - 0
10
+ version: 1.3.0
6
11
  platform: ruby
7
- authors:
8
- - Björn Albers
12
+ authors:
13
+ - "Bj\xC3\xB6rn Albers"
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-04-02 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: rake
16
- requirement: &70139107324480 !ruby/object:Gem::Requirement
17
+
18
+ date: 2012-08-30 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ requirement: &id001 !ruby/object:Gem::Requirement
17
22
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ hash: 3
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ prerelease: false
22
31
  type: :development
32
+ name: rake
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ hash: 3
41
+ segments:
42
+ - 0
43
+ version: "0"
23
44
  prerelease: false
24
- version_requirements: *70139107324480
25
- - !ruby/object:Gem::Dependency
45
+ type: :development
26
46
  name: cucumber
27
- requirement: &70139107323900 !ruby/object:Gem::Requirement
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ requirement: &id003 !ruby/object:Gem::Requirement
28
50
  none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
- version: '0'
33
- type: :development
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
34
58
  prerelease: false
35
- version_requirements: *70139107323900
36
- - !ruby/object:Gem::Dependency
59
+ type: :development
37
60
  name: aruba
38
- requirement: &70139107323300 !ruby/object:Gem::Requirement
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ requirement: &id004 !ruby/object:Gem::Requirement
39
64
  none: false
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: '0'
44
- type: :development
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
45
72
  prerelease: false
46
- version_requirements: *70139107323300
47
- - !ruby/object:Gem::Dependency
73
+ type: :development
48
74
  name: guard-cucumber
49
- requirement: &70139107322640 !ruby/object:Gem::Requirement
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ requirement: &id005 !ruby/object:Gem::Requirement
50
78
  none: false
51
- requirements:
52
- - - ! '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- type: :development
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
56
86
  prerelease: false
57
- version_requirements: *70139107322640
58
- - !ruby/object:Gem::Dependency
87
+ type: :development
59
88
  name: rspec
60
- requirement: &70139107322100 !ruby/object:Gem::Requirement
89
+ version_requirements: *id005
90
+ - !ruby/object:Gem::Dependency
91
+ requirement: &id006 !ruby/object:Gem::Requirement
61
92
  none: false
62
- requirements:
63
- - - ! '>='
64
- - !ruby/object:Gem::Version
65
- version: '0'
66
- type: :development
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
67
100
  prerelease: false
68
- version_requirements: *70139107322100
69
- - !ruby/object:Gem::Dependency
70
- name: guard-rspec
71
- requirement: &70139107321640 !ruby/object:Gem::Requirement
72
- none: false
73
- requirements:
74
- - - ! '>='
75
- - !ruby/object:Gem::Version
76
- version: '0'
77
101
  type: :development
78
- prerelease: false
79
- version_requirements: *70139107321640
102
+ name: guard-rspec
103
+ version_requirements: *id006
80
104
  description: The one Nagios Plugin framework, forged in the fires of Mount Doom.
81
- email:
105
+ email:
82
106
  - bjoernalbers@googlemail.com
83
107
  executables: []
108
+
84
109
  extensions: []
110
+
85
111
  extra_rdoc_files: []
86
- files:
112
+
113
+ files:
87
114
  - .gitignore
88
115
  - .travis.yml
89
116
  - Gemfile
@@ -91,10 +118,14 @@ files:
91
118
  - LICENSE
92
119
  - README.md
93
120
  - Rakefile
121
+ - examples/check_file_exists.rb
122
+ - examples/check_perfdata.rb
94
123
  - features/nagiosplugin_usage.feature
95
124
  - features/steps/dev_steps.rb
96
125
  - features/support/env.rb
97
126
  - lib/nagiosplugin.rb
127
+ - lib/nagiosplugin/default_options.rb
128
+ - lib/nagiosplugin/perfdata.rb
98
129
  - lib/nagiosplugin/plugin.rb
99
130
  - lib/nagiosplugin/status_error.rb
100
131
  - lib/nagiosplugin/version.rb
@@ -104,35 +135,38 @@ files:
104
135
  - spec/spec_helper.rb
105
136
  homepage: https://github.com/bjoernalbers/nagiosplugin
106
137
  licenses: []
138
+
107
139
  post_install_message:
108
140
  rdoc_options: []
109
- require_paths:
141
+
142
+ require_paths:
110
143
  - lib
111
- required_ruby_version: !ruby/object:Gem::Requirement
144
+ required_ruby_version: !ruby/object:Gem::Requirement
112
145
  none: false
113
- requirements:
114
- - - ! '>='
115
- - !ruby/object:Gem::Version
116
- version: '0'
117
- segments:
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ hash: 3
150
+ segments:
118
151
  - 0
119
- hash: -1071120004356893242
120
- required_rubygems_version: !ruby/object:Gem::Requirement
152
+ version: "0"
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
154
  none: false
122
- requirements:
123
- - - ! '>='
124
- - !ruby/object:Gem::Version
125
- version: '0'
126
- segments:
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ hash: 3
159
+ segments:
127
160
  - 0
128
- hash: -1071120004356893242
161
+ version: "0"
129
162
  requirements: []
163
+
130
164
  rubyforge_project:
131
- rubygems_version: 1.8.10
165
+ rubygems_version: 1.8.24
132
166
  signing_key:
133
167
  specification_version: 3
134
- summary: nagiosplugin-1.2.0
135
- test_files:
168
+ summary: nagiosplugin-1.3.0
169
+ test_files:
136
170
  - features/nagiosplugin_usage.feature
137
171
  - features/steps/dev_steps.rb
138
172
  - features/support/env.rb