nagiosplugin 1.3.0 → 2.0.0

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.
data/.gitignore CHANGED
@@ -1,6 +1,18 @@
1
1
  *.gem
2
+ *.rbc
2
3
  .bundle
4
+ .config
5
+ .yardoc
3
6
  Gemfile.lock
4
- pkg/*
5
- tmp/*
6
- vendor/*
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor
data/.rbenv-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p385
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in nagiosplugin.gemspec
3
+ # Specify your gem's dependencies in ..gemspec
4
4
  gemspec
@@ -1,4 +1,6 @@
1
- Copyright (c) 2011 Björn Albers
1
+ Copyright (c) 2013 Björn Albers
2
+
3
+ MIT License
2
4
 
3
5
  Permission is hereby granted, free of charge, to any person obtaining
4
6
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,51 +1,121 @@
1
1
  # NagiosPlugin
2
2
 
3
- The one [Nagios](http://www.nagios.org/) Plugin framework, forged in the fires of Mount Doom.
3
+ The one [Nagios](http://www.nagios.org/) Plugin framework, forged in the
4
+ fires of Mount Doom.
5
+
6
+ [![Build
7
+ Status](https://secure.travis-ci.org/bjoernalbers/nagiosplugin.png)](http://travis-ci.org/bjoernalbers/nagiosplugin)
8
+
9
+ **NOTE: The API has changed since version 2.0 (see issue
10
+ [#2](https://github.com/bjoernalbers/nagiosplugin/issues/2))!!!**
4
11
 
5
- [![Build Status](https://secure.travis-ci.org/bjoernalbers/nagiosplugin.png)](http://travis-ci.org/bjoernalbers/nagiosplugin)
6
12
 
7
13
  ## Installation
8
14
 
9
- gem install nagiosplugin
15
+ Via bundler: Add this to your Gemfile and run `bundle install`:
16
+
17
+ ```Ruby
18
+ gem 'nagiosplugin'
19
+ ```
20
+
21
+ Manually via Rubygems: Run `gem install nagiosplugin`.
22
+
10
23
 
11
24
  ## Usage
12
25
 
13
- ### Step 1
26
+ Writing your custom plugin is really simple:
27
+
28
+ ### Step 1: Describe when something is critical, warning and ok.
29
+
30
+ Create your subclass from `NagiosPlugin::Plugin` and define these
31
+ instance methods to determine the status:
32
+
33
+ ```Ruby
34
+ require 'nagiosplugin'
35
+
36
+ class MyFancyPlugin < NagiosPlugin::Plugin
37
+ def critical?
38
+ # Enter your code here (returns true when critical, else false).
39
+ end
40
+
41
+ def warning?
42
+ # Enter your code here (returns true when warning, else false).
43
+ end
44
+
45
+ def ok?
46
+ # Enter your code... you get the idea.
47
+ end
48
+ end
49
+ ```
14
50
 
15
- Just subclass from `NagiosPlugin::Plugin` and define your `check` method
16
- which should figure out the status and then call the appropriate status
17
- method (`ok`, `warning`, `critical` or `unknown`) with meaningfull message.
18
- (The status methods will exit immediately by raising a corresponding StatusError.)
51
+ **Please keep in mind that the "worst" status always wins, i.e. if both
52
+ `critical?` and `warning?` are true then the status would be critical
53
+ (if none is true, then the status would be unknown of course)!**
19
54
 
20
- Take a look at a working [usage
21
- example](https://github.com/bjoernalbers/nagiosplugin/blob/master/features/nagiosplugin_usage.feature).
55
+ You may use your `initialize` method to setup check data (NagiosPlugin
56
+ doesn't use that either), parse CLI options, etc.
22
57
 
23
- ### Step 2
58
+ ### Step 2: Provide some context via status message (optionally).
24
59
 
25
- Call the `run` method on your new class, which outputs the check result
26
- and exits in compliance with the official [Nagios plug-in development
60
+ Ask yourself what might be important to know (and fits into a text
61
+ message) when Nagios just send you an alert in the middle of the night.
62
+
63
+ ```Ruby
64
+ def message
65
+ # Create an info string (this will be printed after the
66
+ # status on stdout).
67
+ # Service name, status and message should be longer than 78 chars!!!
68
+ end
69
+ ```
70
+
71
+ ### Step 3: Perform the actual check.
72
+
73
+ In your binary then call `check!` on your class and you're done:
74
+
75
+ ```Ruby
76
+ MyFancyPlugin.new.check!
77
+ ```
78
+
79
+ This will output the check result and exits in compliance with the
80
+ official
81
+ [Nagios plug-in development
27
82
  guidelines](http://nagiosplug.sourceforge.net/developer-guidelines.html)
28
83
 
29
- ### Step 3
84
+ If anything funky happens in your code: NagiosPlugin does a "blind
85
+ rescue mission" and transforms any execptions to an unknown status.
86
+
87
+
88
+ ## One more thing...
89
+
90
+ The API changed completely compared to v1.3 as well as the default command
91
+ line options (they are gone).
92
+ I suggest that you use 3rd party [CLI Option
93
+ Parsers](https://www.ruby-toolbox.com/categories/CLI_Option_Parsers)
94
+ because everyone has different preferences on this.
95
+
96
+ Please let me know if you find this usefull or if you want to contribute!
30
97
 
31
- Profit... and maybe also fun.
32
98
 
33
99
  ## Note on Patches/Pull Requests
34
100
 
35
- * Fork the project and run `bundle install` to resolve all development dependencies.
36
- * Add specs and/or features for it. This is important so I don't break it in a future version unintentionally.
101
+ * Fork the project and run `bundle install` to resolve all development
102
+ dependencies.
103
+ * Add specs and/or features for it. This is important so I don't break
104
+ it in a future version unintentionally.
37
105
  * Make your feature addition or bug fix.
38
106
  * Commit, do not mess with the Rakefile or gemspec.
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.)
107
+ (If you want to have your own version, that is fine but bump version
108
+ in a commit by itself I can ignore when I pull.)
40
109
  * Send me a pull request. Bonus points for topic branches.
41
110
 
111
+
42
112
  ## Acknowledgments
43
113
 
44
114
  Thanks to the following contributors for improving NagiosPlugin:
45
115
 
46
- * [szuecs (Sandor Szücs)](https://github.com/szuecs): Adding default
47
- options, perfdata stuff and helpful sample code.
116
+ * [szuecs (Sandor Szücs)](https://github.com/szuecs)
117
+
48
118
 
49
119
  ## Copyright
50
120
 
51
- Copyright (c) 2011-2012 Björn Albers. See LICENSE for details.
121
+ Copyright (c) 2011-2013 Björn Albers. See LICENSE for details.
data/Rakefile CHANGED
@@ -2,13 +2,13 @@ require 'bundler/gem_tasks'
2
2
 
3
3
  require 'cucumber/rake/task'
4
4
  Cucumber::Rake::Task.new(:features) do |t|
5
- t.cucumber_opts = '--format pretty'
5
+ t.cucumber_opts = '--format pretty'
6
6
  end
7
7
 
8
8
  require 'rspec/core/rake_task'
9
9
  RSpec::Core::RakeTask.new do |t|
10
- t.rspec_opts = %w[--color]
11
- t.pattern = './spec/**/*_spec.rb'
10
+ t.rspec_opts = %w[--color]
11
+ t.pattern = './spec/**/*_spec.rb'
12
12
  end
13
13
 
14
14
  task :default => :spec
@@ -5,24 +5,31 @@ Feature: NagiosPlugin Usage
5
5
  I want to use the one NagiosPlugin framework
6
6
 
7
7
  Scenario Outline: Subclass from NagiosPlugin
8
- Given a file named "check_foo.rb" with:
8
+ Given a file named "check_fancy.rb" with:
9
9
  """
10
10
  require 'nagiosplugin'
11
11
 
12
- class Foo < NagiosPlugin::Plugin
13
- def check
14
- case ARGV.first
15
- when 'UNKNOWN' then unknown 'no clue, sorry'
16
- when 'CRITICAL' then critical 'booooom!'
17
- when 'WARNING' then warning 'it could be worse'
18
- when 'OK' then ok 'all is fine'
19
- end
12
+ class FancyPlugin < NagiosPlugin::Plugin
13
+ def critical?
14
+ <critical>
15
+ end
16
+
17
+ def warning?
18
+ <warning>
19
+ end
20
+
21
+ def ok?
22
+ <ok>
23
+ end
24
+
25
+ def message
26
+ 'answer is 42'
20
27
  end
21
28
  end
22
29
 
23
- Foo.run
30
+ FancyPlugin.new.check!
24
31
  """
25
- When I run `ruby check_foo.rb <status>`
32
+ When I run `ruby check_fancy.rb`
26
33
  Then the exit status should be <code>
27
34
  And the stdout should contain exactly:
28
35
  """
@@ -31,8 +38,8 @@ Feature: NagiosPlugin Usage
31
38
  """
32
39
 
33
40
  Examples:
34
- | status | code | stdout |
35
- | UNKNOWN | 3 | FOO UNKNOWN: no clue, sorry |
36
- | CRITICAL | 2 | FOO CRITICAL: booooom! |
37
- | WARNING | 1 | FOO WARNING: it could be worse |
38
- | OK | 0 | FOO OK: all is fine |
41
+ | critical | warning | ok | status | code | stdout |
42
+ | true | true | true | CRITICAL | 2 | FANCY CRITICAL: answer is 42 |
43
+ | false | true | true | WARNING | 1 | FANCY WARNING: answer is 42 |
44
+ | false | false | true | OK | 0 | FANCY OK: answer is 42 |
45
+ | false | false | false | UNKNOWN | 3 | FANCY UNKNOWN: answer is 42 |
data/lib/nagiosplugin.rb CHANGED
@@ -1,3 +1,2 @@
1
1
  require 'nagiosplugin/version'
2
2
  require 'nagiosplugin/plugin'
3
- require 'nagiosplugin/status_error'
@@ -1,106 +1,45 @@
1
- require "optparse"
2
-
3
1
  module NagiosPlugin
4
2
  class Plugin
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
40
-
41
- # Create new instance and run it.
42
- def run(*args)
43
- self.new(*args).run
44
- end
45
-
46
- private
47
-
48
- # @macro [status] message
49
- # @method $1(message)
50
- # Raise $1 StatusError with message
51
- # @param [String] message the exeption message
52
- def make(status)
53
- define_method(status) do |message|
54
- raise StatusError.new(status, message)
55
- end
56
- end
3
+ NAGIOS_PLUGIN_EXIT_CODES = {
4
+ :unknown => 3,
5
+ :critical => 2,
6
+ :warning => 1,
7
+ :ok => 0
8
+ }
9
+
10
+ def check!
11
+ puts nagios_plugin_output
12
+ exit nagios_plugin_exit_code
13
+ rescue => e
14
+ puts "PLUGIN UNKNOWN: #{e.to_s}\n\n#{e.backtrace}"
15
+ exit NAGIOS_PLUGIN_EXIT_CODES[:unknown]
57
16
  end
58
17
 
59
- make :ok
60
- make :warning
61
- make :critical
62
- make :unknown
18
+ private
63
19
 
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]
20
+ def nagios_plugin_status
21
+ @nagios_plugin_status ||=
22
+ case
23
+ when critical? then :critical
24
+ when warning? then :warning
25
+ when ok? then :ok
26
+ else :unknown
27
+ end
68
28
  end
69
29
 
70
-
71
- # Overwrite this check method and call a status method within.
72
- def check
73
- unknown 'please overwrite the `check` method in your class'
30
+ def nagios_plugin_exit_code
31
+ NAGIOS_PLUGIN_EXIT_CODES[nagios_plugin_status]
74
32
  end
75
33
 
76
- # Return the service name, which will be displayed on stdout.
77
- #
78
- # By default this will be the upcased class name. If you want
79
- # to name your service different than the class then overwrite
80
- # this method.
81
- #
82
- # @return [String] the service name
83
- def service
84
- self.class.name.upcase
34
+ def nagios_plugin_service
35
+ self.class.name.split('::').last.gsub(/plugin$/i, '').upcase
85
36
  end
86
37
 
87
- # Run check and return result in a nagios-compatible format.
88
- #
89
- # It will...
90
- # - execute your check method
91
- # - output the result in a nagios-compatible format (SERVICE STATUS: Message)
92
- # - exit according to the status
93
- def run
94
- check
95
- rescue StatusError => e
96
- rescue => e
97
- e = StatusError.new(:unknown, ([e.to_s, nil] + e.backtrace).join("\n"))
98
- else
99
- e = StatusError.new(:unknown, 'no status method was called')
100
- ensure
101
- puts [service, e.to_s].join(' ')
102
- exit e.to_i
38
+ def nagios_plugin_output
39
+ output = [nagios_plugin_service] << ' '
40
+ output << nagios_plugin_status.to_s.upcase
41
+ output << ': ' + message if respond_to?(:message)
42
+ output.join
103
43
  end
104
-
105
44
  end
106
45
  end
@@ -1,3 +1,3 @@
1
1
  module NagiosPlugin
2
- VERSION = '1.3.0'
2
+ VERSION = '2.0.0'
3
3
  end
data/nagiosplugin.gemspec CHANGED
@@ -1,25 +1,27 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path('../lib', __FILE__)
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
4
  require 'nagiosplugin/version'
4
5
 
5
- Gem::Specification.new do |s|
6
- s.name = 'nagiosplugin'
7
- s.version = NagiosPlugin::VERSION
8
- s.authors = ['Björn Albers']
9
- s.email = ['bjoernalbers@googlemail.com']
10
- s.homepage = 'https://github.com/bjoernalbers/nagiosplugin'
11
- s.summary = "#{s.name}-#{s.version}"
12
- s.description = 'The one Nagios Plugin framework, forged in the fires of Mount Doom.'
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'nagiosplugin'
8
+ spec.version = NagiosPlugin::VERSION
9
+ spec.authors = ['Björn Albers']
10
+ spec.email = ['bjoernalbers@googlemail.com']
11
+ spec.homepage = 'https://github.com/bjoernalbers/nagiosplugin'
12
+ spec.summary = "#{spec.name}-#{spec.version}"
13
+ spec.description = 'The one Nagios Plugin framework, forged in the fires of Mount Doom.'
14
+ spec.license = 'MIT'
13
15
 
14
- s.files = `git ls-files`.split("\n")
15
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
- s.require_paths = ['lib']
16
+ spec.files = `git ls-files`.split("\n")
17
+ spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
18
20
 
19
- s.add_development_dependency 'rake'
20
- s.add_development_dependency 'cucumber'
21
- s.add_development_dependency 'aruba'
22
- s.add_development_dependency 'guard-cucumber'
23
- s.add_development_dependency 'rspec'
24
- s.add_development_dependency 'guard-rspec'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'cucumber'
23
+ spec.add_development_dependency 'aruba'
24
+ spec.add_development_dependency 'guard-cucumber'
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'guard-rspec'
25
27
  end
@@ -1,119 +1,137 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe NagiosPlugin::Plugin do
4
- before do
5
- class MyPlugin < NagiosPlugin::Plugin; end
6
- @plugin = MyPlugin.new
3
+ module NagiosPlugin
4
+ describe Plugin do
5
+ let(:plugin) { Plugin.new }
7
6
 
8
- class MergedOptionsPlugin < NagiosPlugin::Plugin
9
- def initialize(options, &blk)
10
- super(options, &blk)
11
- @foo = options[:foo]
7
+ describe '#check!' do
8
+ before do
9
+ plugin.stub(:puts => nil,
10
+ :exit => nil,
11
+ :nagios_plugin_exit_code => nil,
12
+ :nagios_plugin_output => nil)
12
13
  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
14
+
15
+ it 'displays the plugin output on stdout' do
16
+ plugin.should_receive(:nagios_plugin_output).and_return('chunky bacon')
17
+ plugin.should_receive(:puts).with('chunky bacon')
18
+ plugin.check!
18
19
  end
19
- end
20
- end
21
20
 
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
28
- end
21
+ it 'exits with the status exit code' do
22
+ plugin.should_receive(:nagios_plugin_exit_code).and_return(42)
23
+ plugin.should_receive(:exit).with(42)
24
+ plugin.check!
25
+ end
29
26
 
30
- describe '#run' do
31
- before do
32
- @plugin.stub(:puts)
33
- @plugin.stub(:exit)
34
- end
27
+ context 'when an exception was raised' do
28
+ let(:exception) { StandardError.new }
35
29
 
36
- it 'should run the check method' do
37
- @plugin.should_receive(:check).with(no_args)
38
- @plugin.run
39
- end
30
+ before do
31
+ plugin.stub(:nagios_plugin_output).and_return { raise }
32
+ end
40
33
 
41
- it 'should output the servie name' do
42
- @plugin.stub(:service).and_return('MY_FUNKY_PLUGIN')
43
- @plugin.should_receive(:puts).with(/MY_FUNKY_PLUGIN/)
44
- @plugin.run
45
- end
34
+ it 'rescues the exception' do
35
+ expect { plugin.check! }.to_not raise_error
36
+ end
46
37
 
47
- it 'should output an appropriate message when check was not overwritten' do
48
- @plugin.should_receive(:puts).with(/please overwrite the `check` method in your class/i)
49
- @plugin.run
50
- end
38
+ it 'exits with exit code unknown' do
39
+ plugin.should_receive(:exit).with(3)
40
+ plugin.check!
41
+ end
51
42
 
52
- context 'when an unknown exception was raised' do
53
- before do
54
- StandardError.any_instance.stub(:backtrace).and_return(%w[foo bar baz])
55
- def @plugin.check
56
- raise StandardError, 'Oops!'
43
+ it 'displays the exception and backtrace on stdout' do
44
+ plugin.stub(:nagios_plugin_output).and_return { raise 'Oops!' }
45
+ StandardError.any_instance.stub(:backtrace).
46
+ and_return('Chunky Bacon')
47
+ plugin.should_receive(:puts).
48
+ with("PLUGIN UNKNOWN: Oops!\n\nChunky Bacon")
49
+ plugin.check!
57
50
  end
58
51
  end
52
+ end
59
53
 
60
- it 'should output the execptions message' do
61
- @plugin.should_receive(:puts).with(/Oops!/)
62
- @plugin.run
54
+ describe '#nagios_plugin_service' do
55
+ it 'returns the upcased class name' do
56
+ class Foo < Plugin; end
57
+ plugin = Foo.new
58
+ expect(plugin.send(:nagios_plugin_service)).to eq('FOO')
63
59
  end
64
60
 
65
- it 'should output the exceptions backtrace' do
66
- @plugin.should_receive(:puts).with(/foo\nbar\nbaz/)
67
- @plugin.run
61
+ it 'strips "Plugin" from the class name' do
62
+ class BarPlugin < Plugin; end
63
+ plugin = BarPlugin.new
64
+ expect(plugin.send(:nagios_plugin_service)).to eq('BAR')
68
65
  end
66
+ end
69
67
 
70
- it 'should exit unknown' do
71
- @plugin.should_receive(:exit).with(3)
72
- @plugin.run
68
+ describe '#nagios_plugin_status' do
69
+ it 'returns unknown when not critical, warning or ok' do
70
+ plugin.stub(:critical? => false, :warning? => false, :ok? => false)
71
+ expect(plugin.send(:nagios_plugin_status)).to eq(:unknown)
73
72
  end
74
- end
75
73
 
76
- context 'when no exception was raised' do
77
- before { def @plugin.check; end }
74
+ it 'returns critical when critical' do
75
+ plugin.stub(:critical? => true, :warning? => true, :ok? => true)
76
+ expect(plugin.send(:nagios_plugin_status)).to eq(:critical)
77
+ end
78
78
 
79
- it 'should display a hint for the developer' do
80
- @plugin.should_receive(:puts).with(/no status method was called/i)
81
- @plugin.run
79
+ it 'returns warning when warning but not critical' do
80
+ plugin.stub(:critical? => false, :warning? => true, :ok? => true)
81
+ expect(plugin.send(:nagios_plugin_status)).to eq(:warning)
82
82
  end
83
83
 
84
- it 'should exit unknown' do
85
- @plugin.should_receive(:exit).with(3)
86
- @plugin.run
84
+ it 'returns ok when ok but not critical or warning' do
85
+ plugin.stub(:critical? => false, :warning? => false, :ok? => true)
86
+ expect(plugin.send(:nagios_plugin_status)).to eq(:ok)
87
87
  end
88
- end
89
88
 
90
- context 'when a status error was raised' do
91
- before do
92
- def @plugin.check
93
- raise NagiosPlugin::StatusError.new(:ok, 'hello, world.')
89
+ it 'saves the status' do
90
+ [:critical?, :warning?, :ok?].each do |check|
91
+ plugin.should_receive(check).once.and_return(false)
94
92
  end
93
+ expect(plugin.send(:nagios_plugin_status)).to eq(:unknown)
94
+ expect(plugin.send(:nagios_plugin_status)).to eq(:unknown)
95
95
  end
96
+ end
96
97
 
97
- it 'should output the status type' do
98
- @plugin.should_receive(:puts).with(/OK/)
99
- @plugin.run
98
+ describe '#nagios_plugin_exit_code' do
99
+ it 'returns 3 when unknown' do
100
+ plugin.should_receive(:nagios_plugin_status).and_return(:unknown)
101
+ expect(plugin.send(:nagios_plugin_exit_code)).to eq(3)
100
102
  end
101
103
 
102
- it 'should output the status message' do
103
- @plugin.should_receive(:puts).with(/hello, world\./)
104
- @plugin.run
104
+ it 'returns 2 when critical' do
105
+ plugin.should_receive(:nagios_plugin_status).and_return(:critical)
106
+ expect(plugin.send(:nagios_plugin_exit_code)).to eq(2)
105
107
  end
106
108
 
107
- it 'should exit with the exit status from status error' do
108
- @plugin.should_receive(:exit).with(0)
109
- @plugin.run
109
+ it 'returns 1 when warning' do
110
+ plugin.should_receive(:nagios_plugin_status).and_return(:warning)
111
+ expect(plugin.send(:nagios_plugin_exit_code)).to eq(1)
112
+ end
113
+
114
+ it 'returns 0 when ok' do
115
+ plugin.should_receive(:nagios_plugin_status).and_return(:ok)
116
+ expect(plugin.send(:nagios_plugin_exit_code)).to eq(0)
110
117
  end
111
118
  end
112
- end
113
119
 
114
- describe '#service' do
115
- it 'should return the upcased class name' do
116
- @plugin.service.should eql('MYPLUGIN')
120
+ describe '#nagios_plugin_output' do
121
+ before do
122
+ plugin.stub(:nagios_plugin_status)
123
+ end
124
+
125
+ it 'joins the service name and the upcased status' do
126
+ plugin.should_receive(:nagios_plugin_service).and_return('FRIED')
127
+ plugin.should_receive(:nagios_plugin_status).and_return(:chicken)
128
+ expect(plugin.send(:nagios_plugin_output)).to match(/^FRIED CHICKEN$/)
129
+ end
130
+
131
+ it 'includes a custom plugin message if present' do
132
+ plugin.should_receive(:message).and_return('ALL U CAN EAT!')
133
+ expect(plugin.send(:nagios_plugin_output)).to match(/: ALL U CAN EAT!$/)
134
+ end
117
135
  end
118
136
  end
119
137
  end
metadata CHANGED
@@ -1,175 +1,168 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: nagiosplugin
3
- version: !ruby/object:Gem::Version
4
- hash: 27
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 3
9
- - 0
10
- version: 1.3.0
11
6
  platform: ruby
12
- authors:
13
- - "Bj\xC3\xB6rn Albers"
7
+ authors:
8
+ - Björn Albers
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-08-30 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- requirement: &id001 !ruby/object:Gem::Requirement
22
- none: false
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- hash: 3
27
- segments:
28
- - 0
29
- version: "0"
30
- prerelease: false
31
- type: :development
12
+ date: 2013-09-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
32
15
  name: rake
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- requirement: &id002 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
36
17
  none: false
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- hash: 3
41
- segments:
42
- - 0
43
- version: "0"
44
- prerelease: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
45
22
  type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
46
31
  name: cucumber
47
- version_requirements: *id002
48
- - !ruby/object:Gem::Dependency
49
- requirement: &id003 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
50
33
  none: false
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- hash: 3
55
- segments:
56
- - 0
57
- version: "0"
58
- prerelease: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
59
38
  type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
60
47
  name: aruba
61
- version_requirements: *id003
62
- - !ruby/object:Gem::Dependency
63
- requirement: &id004 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
64
49
  none: false
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- hash: 3
69
- segments:
70
- - 0
71
- version: "0"
72
- prerelease: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
73
54
  type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
74
63
  name: guard-cucumber
75
- version_requirements: *id004
76
- - !ruby/object:Gem::Dependency
77
- requirement: &id005 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
78
65
  none: false
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- hash: 3
83
- segments:
84
- - 0
85
- version: "0"
86
- prerelease: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
87
70
  type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
88
79
  name: rspec
89
- version_requirements: *id005
90
- - !ruby/object:Gem::Dependency
91
- requirement: &id006 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
92
81
  none: false
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- hash: 3
97
- segments:
98
- - 0
99
- version: "0"
100
- prerelease: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
101
86
  type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
102
95
  name: guard-rspec
103
- version_requirements: *id006
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
104
110
  description: The one Nagios Plugin framework, forged in the fires of Mount Doom.
105
- email:
111
+ email:
106
112
  - bjoernalbers@googlemail.com
107
113
  executables: []
108
-
109
114
  extensions: []
110
-
111
115
  extra_rdoc_files: []
112
-
113
- files:
116
+ files:
114
117
  - .gitignore
118
+ - .rbenv-version
115
119
  - .travis.yml
116
120
  - Gemfile
117
121
  - Guardfile
118
- - LICENSE
122
+ - LICENSE.txt
119
123
  - README.md
120
124
  - Rakefile
121
- - examples/check_file_exists.rb
122
- - examples/check_perfdata.rb
123
125
  - features/nagiosplugin_usage.feature
124
- - features/steps/dev_steps.rb
125
126
  - features/support/env.rb
126
127
  - lib/nagiosplugin.rb
127
- - lib/nagiosplugin/default_options.rb
128
- - lib/nagiosplugin/perfdata.rb
129
128
  - lib/nagiosplugin/plugin.rb
130
- - lib/nagiosplugin/status_error.rb
131
129
  - lib/nagiosplugin/version.rb
132
130
  - nagiosplugin.gemspec
133
131
  - spec/nagiosplugin/plugin_spec.rb
134
- - spec/nagiosplugin/status_error_spec.rb
135
132
  - spec/spec_helper.rb
136
133
  homepage: https://github.com/bjoernalbers/nagiosplugin
137
- licenses: []
138
-
134
+ licenses:
135
+ - MIT
139
136
  post_install_message:
140
137
  rdoc_options: []
141
-
142
- require_paths:
138
+ require_paths:
143
139
  - lib
144
- required_ruby_version: !ruby/object:Gem::Requirement
140
+ required_ruby_version: !ruby/object:Gem::Requirement
145
141
  none: false
146
- requirements:
147
- - - ">="
148
- - !ruby/object:Gem::Version
149
- hash: 3
150
- segments:
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ segments:
151
147
  - 0
152
- version: "0"
153
- required_rubygems_version: !ruby/object:Gem::Requirement
148
+ hash: 1579051430854649941
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
150
  none: false
155
- requirements:
156
- - - ">="
157
- - !ruby/object:Gem::Version
158
- hash: 3
159
- segments:
151
+ requirements:
152
+ - - ! '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ segments:
160
156
  - 0
161
- version: "0"
157
+ hash: 1579051430854649941
162
158
  requirements: []
163
-
164
159
  rubyforge_project:
165
- rubygems_version: 1.8.24
160
+ rubygems_version: 1.8.23
166
161
  signing_key:
167
162
  specification_version: 3
168
- summary: nagiosplugin-1.3.0
169
- test_files:
163
+ summary: nagiosplugin-2.0.0
164
+ test_files:
170
165
  - features/nagiosplugin_usage.feature
171
- - features/steps/dev_steps.rb
172
166
  - features/support/env.rb
173
167
  - spec/nagiosplugin/plugin_spec.rb
174
- - spec/nagiosplugin/status_error_spec.rb
175
168
  - spec/spec_helper.rb
@@ -1,62 +0,0 @@
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)
@@ -1,30 +0,0 @@
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
@@ -1,17 +0,0 @@
1
- When /^I run a plugin with the following methods:$/ do |methods|
2
- steps %Q{
3
- Given a file named "check_foo" with:
4
- """
5
- require 'nagiosplugin'
6
- class Foo < NagiosPlugin::Plugin
7
- #{methods}
8
- end
9
- Foo.new.run
10
- """
11
- When I run `ruby check_foo`
12
- }
13
- end
14
-
15
- Then /^the plugin should print "([^"]*)"$/ do |stdout|
16
- steps %Q{Then the stdout should contain "FOO #{stdout}"}
17
- end
@@ -1,78 +0,0 @@
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
@@ -1,40 +0,0 @@
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,32 +0,0 @@
1
- module NagiosPlugin
2
-
3
- # A custom status error which will be raised through the status methods.
4
- class StatusError < StandardError
5
-
6
- # All allowed statuses sorted by their exit code
7
- STATUS = [:ok, :warning, :critical, :unknown]
8
-
9
- # @param [Symbol] status the status (must be {NagiosPlugin::Plugin::StatusError::STATUS a valid status})
10
- # @param [String] message the message you want to display
11
- def initialize(status, message)
12
- @status, @message = status.to_sym, message
13
- end
14
-
15
- # @return [String] the status and message
16
- def to_s
17
- "#{status.to_s.upcase}: #{@message}"
18
- end
19
-
20
- # @return [Fixnum] the status converted into an exit code
21
- def to_i
22
- STATUS.find_index(status)
23
- end
24
-
25
- private
26
-
27
- # @return [Symbol] the status (:unknown if invalid)
28
- def status
29
- (STATUS.include?(@status) && @status) || STATUS.last
30
- end
31
- end
32
- end
@@ -1,33 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe NagiosPlugin::StatusError do
4
- def create_status(status, msg = '')
5
- NagiosPlugin::StatusError.new(status, msg)
6
- end
7
-
8
- %w[ok warning critical unknown].each_with_index do |s,i|
9
- context "when #{s}" do
10
- before { @status = create_status(s.to_sym) }
11
-
12
- it 'should include status in the exception message' do
13
- @status.to_s.should include(s.upcase)
14
- end
15
-
16
- it "should convert to #{i}" do
17
- @status.to_i.should eql(i)
18
- end
19
- end
20
- end
21
-
22
- context 'when initialized with invalid status' do
23
- before { @status = create_status(:invalid) }
24
-
25
- it 'should include unknown status in the exception message' do
26
- @status.to_s.should include('UNKNOWN')
27
- end
28
-
29
- it 'should convert to 3' do
30
- @status.to_i.should eql(3)
31
- end
32
- end
33
- end