nagiosplugin 2.2.0 → 3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5bb7500269428bb2869fb2d1a2c072fb4211f98d
4
- data.tar.gz: 68082b3cf61bb359310cd5503b0131a403632f80
3
+ metadata.gz: af2884fed9433c21dca8ac46fbb9839fdb21828c
4
+ data.tar.gz: 19ec4db414644ca3b8182ba0219e4354bc42178d
5
5
  SHA512:
6
- metadata.gz: b0d12d8f990dd210ea2927d76c3727a5ff1d24c490a30489e8b986f3fb3dc585f6994d137fa790f291adc110897fd96800ecbc97e22bc3ed162115f70a4a2655
7
- data.tar.gz: 4e8046957f75bac17ae00b663fdfc635da11eb244339dac15874c8f820845955be581952af3953a1d06710e169f136d8517f8473c86ff176030ceb89fa696056
6
+ metadata.gz: dc862fd67b4e48ed1379654eaadf1af2056e692001d4e6fdb5170fcb96cfcbce83a5303cdd32456e267ab3ccd06317d0ddd0af8fccd8416e654c8cb616dae2f3
7
+ data.tar.gz: fbff8aba3031f4e38ed2ef6efac1a1499710b2713e239f8bb4443f3e28748e15a0607f7d2b27e3ab85ed4eb5b6cecf4fe8359b562a806c858aac45c19171f304
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Björn Albers
1
+ Copyright (c) 2011-2014 Björn Albers
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,18 +1,18 @@
1
- # NagiosPlugin
1
+ # Nagios::Plugin
2
2
 
3
- The one Nagios Plugin framework, forged in the fires of Mount Doom.
3
+ A Nagios Plugin framework that fits on a folded napkin.
4
4
 
5
5
  [![Gem Version](https://badge.fury.io/rb/nagiosplugin.svg)](http://badge.fury.io/rb/nagiosplugin)
6
6
  [![Build Status](https://secure.travis-ci.org/bjoernalbers/nagiosplugin.png)](http://travis-ci.org/bjoernalbers/nagiosplugin)
7
7
 
8
+
8
9
  ## Introduction
9
10
 
10
- NagiosPlugin is an
11
- [embarrassingly simple](https://github.com/bjoernalbers/nagiosplugin/blob/master/lib/nagiosplugin/plugin.rb)
12
- framework to write custom monitoring plugins for
13
- [Nagios](http://www.nagios.org/).
14
- It was born out of duplication, because all nagios plugins share some
15
- common behaviour...
11
+ *Nagios::Plugin* helps you write [Nagios](http://www.nagios.org/) Plugins with Ruby:
12
+ It ensures that your plugin returns a [compliant](http://nagiosplug.sourceforge.net/developer-guidelines.html) exit code and status output.
13
+
14
+ Besides, it is [easy to understand](https://github.com/bjoernalbers/nagiosplugin/blob/master/lib/nagiosplugin/plugin.rb) and comes with automated tests.
15
+
16
16
 
17
17
  ## Installation
18
18
 
@@ -25,94 +25,71 @@ gem 'nagiosplugin'
25
25
  Manually via Rubygems: Run `gem install nagiosplugin`.
26
26
 
27
27
 
28
- ## Usage
29
-
30
- Writing your custom plugin is really simple:
28
+ ## Quick Start
31
29
 
32
- ### Step 1: Describe when something is critical, warning and ok.
33
-
34
- Create your subclass from `NagiosPlugin::Plugin` and define these
35
- instance methods to determine the status:
30
+ Here, a full working (but totally useless) example plugin named `check_fancy`:
36
31
 
37
32
  ```Ruby
33
+ #!/usr/bin/env ruby
34
+
38
35
  require 'nagiosplugin'
39
36
 
40
- class MyFancyPlugin < NagiosPlugin::Plugin
37
+ # Create your custom plugin as subclass from Nagios::Plugin
38
+ class Fancy < Nagios::Plugin
39
+ # Required method: Is the status critical?
41
40
  def critical?
42
- # Enter your code here (returns true when critical, else false).
41
+ @number < 3
43
42
  end
44
43
 
44
+ # Required method: Is the status warning?
45
45
  def warning?
46
- # Enter your code here (returns true when warning, else false).
46
+ @number < 5
47
47
  end
48
48
 
49
+ # Required method: Is the... I see, you got it.
49
50
  def ok?
50
- # Enter your code... you get the idea.
51
+ @number > 6
51
52
  end
52
- end
53
- ```
54
-
55
- **Please keep in mind that the "worst" status always wins, i.e. if both
56
- `critical?` and `warning?` are true then the status would be critical
57
- (if none is true, then the status would be unknown of course)!**
58
-
59
- You may use your `initialize` method to setup check data (NagiosPlugin
60
- doesn't use that either), parse CLI options, etc.
61
53
 
62
- ### Step 2: Provide some context via status message (optionally).
63
-
64
- Ask yourself what might be important to know (and fits into a text
65
- message) when Nagios send you an alert in the middle of the night.
54
+ # Optional method that is executed once before determining the status.
55
+ def chec
56
+ @number = rand(10)
57
+ end
66
58
 
67
- ```Ruby
68
- def message
69
- # Create an info string (this will be printed after the
70
- # status on stdout).
71
- # Service name, status and message should be longer than 78 chars!!!
59
+ # Optional method: The returned stuff will be appended to plugin output.
60
+ def message
61
+ "#{@number} was picked"
62
+ end
72
63
  end
73
- ```
74
-
75
- ### Step 3: Perform the actual check.
76
64
 
77
- In your binary then just call the build-in class method `check` and you're done:
78
-
79
- ```Ruby
80
- MyFancyPlugin.check(options)
65
+ # Call the build-in class method to display the status and exit properly:
66
+ Fancy.run!
81
67
  ```
82
68
 
83
- (Optional arguments, i.e. from your favorite [CLI option
84
- parser](https://www.ruby-toolbox.com/categories/CLI_Option_Parsers), will be
85
- forwarded to you initializer method.)
86
-
87
- This will output the check result and exits in compliance with the
88
- official
89
- [Nagios plug-in development
90
- guidelines](http://nagiosplug.sourceforge.net/developer-guidelines.html)
91
-
92
- If anything funky happens in your code: NagiosPlugin does a "blind
93
- rescue mission" and transforms any execptions to an unknown status.
94
-
95
-
96
- ## Note on Patches/Pull Requests
97
-
98
- * Fork the project and run `bundle install` to resolve all development
99
- dependencies.
100
- * Add specs and/or features for it. This is important so I don't break
101
- it in a future version unintentionally.
102
- * Make your feature addition or bug fix.
103
- * Commit, do not mess with the Rakefile or gemspec.
104
- (If you want to have your own version, that is fine but bump version
105
- in a commit by itself I can ignore when I pull.)
106
- * Send me a pull request. Bonus points for topic branches.
69
+ When you run it you'd something like this:
107
70
 
71
+ ```
72
+ $ check_fancy; echo $?
73
+ FANCY CRITICAL: 2 was picked
74
+ 2
75
+ $ check_fancy; echo $?
76
+ FANCY UNKNOWN: 6 was picked
77
+ 3
78
+ $ check_fancy; echo $?
79
+ FANCY OK: 7 was picked
80
+ 0
81
+ ```
108
82
 
109
- ## Acknowledgments
110
83
 
111
- Thanks to the following contributors for improving NagiosPlugin:
84
+ ## Thinks to remember
112
85
 
113
- * [szuecs (Sandor Szücs)](https://github.com/szuecs)
86
+ - the "worst" status always wins, for example if both `critical?` and
87
+ `warning?` return true then the status would be critical
88
+ - `Nagios::Plugin.run!` does a "blind rescue mission" and transforms any
89
+ execptions to an unknown status.
90
+ - drink more water!
114
91
 
115
92
 
116
93
  ## Copyright
117
94
 
118
- Copyright (c) 2011-2013 Björn Albers. See LICENSE for details.
95
+ Copyright (c) 2011-2014 Björn Albers
@@ -1,15 +1,15 @@
1
1
  Feature: NagiosPlugin Usage
2
2
 
3
- In order to write awesome Nagios Plugins with minimal effort
3
+ In order to write Nagios Plugins with Ruby
4
4
  As a busy (developer|sysadmin|devop|hacker|superhero|rockstar)
5
- I want to use the one NagiosPlugin framework
5
+ I want to use the one Nagios Plugin framework.
6
6
 
7
7
  Scenario Outline: Subclass from NagiosPlugin
8
- Given a file named "check_fancy.rb" with:
8
+ Given a file named "check_chunky_bacon.rb" with:
9
9
  """
10
10
  require 'nagiosplugin'
11
11
 
12
- class FancyPlugin < NagiosPlugin::Plugin
12
+ class ChunkyBacon < Nagios::Plugin
13
13
  def critical?
14
14
  <critical>
15
15
  end
@@ -23,14 +23,14 @@ Feature: NagiosPlugin Usage
23
23
  end
24
24
 
25
25
  def message
26
- 'answer is 42'
26
+ 42
27
27
  end
28
28
  end
29
29
 
30
- FancyPlugin.check
30
+ ChunkyBacon.run!
31
31
  """
32
- When I run `ruby check_fancy.rb`
33
- Then the exit status should be <code>
32
+ When I run `ruby check_chunky_bacon.rb`
33
+ Then the exit status should be <es>
34
34
  And the stdout should contain exactly:
35
35
  """
36
36
  <stdout>
@@ -38,8 +38,8 @@ Feature: NagiosPlugin Usage
38
38
  """
39
39
 
40
40
  Examples:
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 |
41
+ | critical | warning | ok | status | es | stdout |
42
+ | true | true | true | CRITICAL | 2 | CHUNKYBACON CRITICAL: 42 |
43
+ | false | true | true | WARNING | 1 | CHUNKYBACON WARNING: 42 |
44
+ | false | false | true | OK | 0 | CHUNKYBACON OK: 42 |
45
+ | false | false | false | UNKNOWN | 3 | CHUNKYBACON UNKNOWN: 42 |
@@ -0,0 +1,42 @@
1
+ module Nagios
2
+ class Plugin
3
+ VERSION = '3.0.0'
4
+
5
+ EXIT_CODE =
6
+ { unknown: 3,
7
+ critical: 2,
8
+ warning: 1,
9
+ ok: 0 }
10
+
11
+ def self.run!(*args)
12
+ plugin = new(*args)
13
+ plugin.check if plugin.respond_to?(:check)
14
+ puts plugin.output
15
+ exit EXIT_CODE[plugin.status]
16
+ rescue => e
17
+ puts "PLUGIN UNKNOWN: #{e.message}\n\n" << e.backtrace.join("\n")
18
+ exit EXIT_CODE[:unknown]
19
+ end
20
+
21
+ def output
22
+ s = "#{name.upcase} #{status.upcase}"
23
+ s << ": #{message}" if ( respond_to?(:message) && !message.to_s.empty? )
24
+ s
25
+ end
26
+
27
+ def status
28
+ return :critical if critical?
29
+ return :warning if warning?
30
+ return :ok if ok?
31
+ :unknown
32
+ end
33
+
34
+ def name
35
+ self.class.name.split('::').last.upcase
36
+ end
37
+
38
+ def to_s
39
+ output
40
+ end
41
+ end
42
+ end
@@ -1,2 +1 @@
1
- require 'nagiosplugin/version'
2
- require 'nagiosplugin/plugin'
1
+ require 'nagios/plugin'
@@ -1,19 +1,19 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'nagiosplugin/version'
4
+ require 'nagiosplugin'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'nagiosplugin'
8
- spec.version = NagiosPlugin::VERSION
8
+ spec.version = Nagios::Plugin::VERSION
9
9
  spec.authors = ['Björn Albers']
10
10
  spec.email = ['bjoernalbers@gmail.com']
11
11
  spec.homepage = 'https://github.com/bjoernalbers/nagiosplugin'
12
12
  spec.summary = "#{spec.name}-#{spec.version}"
13
- spec.description = 'The one Nagios Plugin framework, forged in the fires of Mount Doom.'
13
+ spec.description = 'A Nagios Plugin framework that fits on a folded napkin.'
14
14
  spec.license = 'MIT'
15
15
 
16
- spec.files = `git ls-files`.split("\n") - %w(.gitignore .rbenv-version .travis.yml)
16
+ spec.files = `git ls-files`.split("\n") - %w(.gitignore .travis.yml)
17
17
  spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
18
  spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  spec.require_paths = ['lib']
@@ -21,7 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency 'rake'
22
22
  spec.add_development_dependency 'cucumber'
23
23
  spec.add_development_dependency 'aruba'
24
- spec.add_development_dependency 'guard-cucumber'
25
24
  spec.add_development_dependency 'rspec', '~> 3.1'
26
- spec.add_development_dependency 'guard-rspec'
27
25
  end
@@ -0,0 +1,171 @@
1
+ require 'spec_helper'
2
+
3
+ module Nagios
4
+ describe Plugin do
5
+ let(:plugin) { Plugin.new }
6
+
7
+ describe '.run!' do
8
+ before do
9
+ allow(plugin).to receive_messages(
10
+ output: '',
11
+ check: nil)
12
+ allow(Plugin).to receive_messages(
13
+ puts: nil,
14
+ exit: nil,
15
+ new: plugin)
16
+ end
17
+
18
+ it 'displays the plugin output on stdout' do
19
+ allow(plugin).to receive(:output).and_return('chunky bacon')
20
+ Plugin.run!
21
+ expect(Plugin).to have_received(:puts).with('chunky bacon')
22
+ end
23
+
24
+ it 'exits with exit code 3 when plugin status unknown' do
25
+ allow(plugin).to receive(:status).and_return(:unknown)
26
+ Plugin.run!
27
+ expect(Plugin).to have_received(:exit).with(3)
28
+ end
29
+
30
+ it 'exits with exit code 2 when plugin status critical' do
31
+ allow(plugin).to receive(:status).and_return(:critical)
32
+ Plugin.run!
33
+ expect(Plugin).to have_received(:exit).with(2)
34
+ end
35
+
36
+ it 'exits with exit code 1 when plugin status warning' do
37
+ allow(plugin).to receive(:status).and_return(:warning)
38
+ Plugin.run!
39
+ expect(Plugin).to have_received(:exit).with(1)
40
+ end
41
+
42
+ it 'exits with exit code 0 when plugin status ok' do
43
+ allow(plugin).to receive(:status).and_return(:ok)
44
+ Plugin.run!
45
+ expect(Plugin).to have_received(:exit).with(0)
46
+ end
47
+
48
+ it 'forwards any arguments to the plugin initializer' do
49
+ Plugin.run!(42, fancy: true)
50
+ expect(Plugin).to have_received(:new).with(42, fancy: true)
51
+ end
52
+
53
+ it 'calls the plugin check callback first' do
54
+ Plugin.run!
55
+ expect(plugin).to have_received(:check).ordered
56
+ expect(plugin).to have_received(:output).ordered
57
+ end
58
+
59
+ context 'when an exception was raised' do
60
+ let(:exception) { StandardError.new('Oops!') }
61
+
62
+ before do
63
+ allow(Plugin).to receive(:new).and_raise(exception)
64
+ end
65
+
66
+ it 'rescues the exception' do
67
+ expect { Plugin.run! }.to_not raise_error
68
+ end
69
+
70
+ it 'exits with status 3' do
71
+ Plugin.run!
72
+ expect(Plugin).to have_received(:exit).with(3)
73
+ end
74
+
75
+ it 'displays the exception and backtrace on stdout' do
76
+ allow(StandardError).to receive(:new).and_return(exception)
77
+ allow(exception).to receive(:backtrace).and_return(%w(Chunky Bacon))
78
+ Plugin.run!
79
+ expect(Plugin).to have_received(:puts).
80
+ with("PLUGIN UNKNOWN: Oops!\n\nChunky\nBacon")
81
+ end
82
+ end
83
+ end
84
+
85
+ describe '#name' do
86
+ it 'returns the upcased class name' do
87
+ class Foo < Plugin; end
88
+ plugin = Foo.new
89
+ expect(plugin.name).to eq('FOO')
90
+ end
91
+
92
+ it 'strips "Nagios" prefix from the class name' do
93
+ class Nagios::Bar < Plugin; end
94
+ plugin = Nagios::Bar.new
95
+ expect(plugin.name).to eq('BAR')
96
+ end
97
+ end
98
+
99
+ describe '#status' do
100
+ it 'returns :unknown when status not critical, not warning and not ok' do
101
+ allow(plugin).to receive_messages(
102
+ critical?: false,
103
+ warning?: false,
104
+ ok?: false)
105
+ expect(plugin.status).to eq(:unknown)
106
+ end
107
+
108
+ it 'returns :critical when status critical' do
109
+ allow(plugin).to receive_messages(
110
+ critical?: true,
111
+ warning?: true,
112
+ ok?: true)
113
+ expect(plugin.status).to eq(:critical)
114
+ end
115
+
116
+ it 'returns :warning when status not critical but warning' do
117
+ allow(plugin).to receive_messages(
118
+ critical?: false,
119
+ warning?: true,
120
+ ok?: true)
121
+ expect(plugin.status).to eq(:warning)
122
+ end
123
+
124
+ it 'returns :ok when when status not critical, not warning but ok' do
125
+ allow(plugin).to receive_messages(
126
+ critical?: false,
127
+ warning?: false,
128
+ ok?: true)
129
+ expect(plugin.status).to eq(:ok)
130
+ end
131
+ end
132
+
133
+ describe '#output' do
134
+ before do
135
+ allow(plugin).to receive_messages(
136
+ name: 'chunkybacon',
137
+ status: 'tasty')
138
+ end
139
+
140
+ context 'without a message' do
141
+ before do
142
+ allow(plugin).to receive(:message).and_return(nil)
143
+ end
144
+
145
+ it 'returns the name and status' do
146
+ expect(plugin.output).to eq('CHUNKYBACON TASTY')
147
+ end
148
+ end
149
+
150
+ context 'with an empty message' do
151
+ before do
152
+ allow(plugin).to receive(:message).and_return('')
153
+ end
154
+
155
+ it 'returns the name and status' do
156
+ expect(plugin.output).to eq('CHUNKYBACON TASTY')
157
+ end
158
+ end
159
+
160
+ context 'with a message' do
161
+ before do
162
+ allow(plugin).to receive(:message).and_return(42)
163
+ end
164
+
165
+ it 'returns the name, status and message' do
166
+ expect(plugin.output).to eq('CHUNKYBACON TASTY: 42')
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nagiosplugin
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Björn Albers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-09 00:00:00.000000000 Z
11
+ date: 2014-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: guard-cucumber
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: rspec
71
57
  requirement: !ruby/object:Gem::Requirement
@@ -80,21 +66,7 @@ dependencies:
80
66
  - - "~>"
81
67
  - !ruby/object:Gem::Version
82
68
  version: '3.1'
83
- - !ruby/object:Gem::Dependency
84
- name: guard-rspec
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
- description: The one Nagios Plugin framework, forged in the fires of Mount Doom.
69
+ description: A Nagios Plugin framework that fits on a folded napkin.
98
70
  email:
99
71
  - bjoernalbers@gmail.com
100
72
  executables: []
@@ -102,17 +74,15 @@ extensions: []
102
74
  extra_rdoc_files: []
103
75
  files:
104
76
  - Gemfile
105
- - Guardfile
106
77
  - LICENSE.txt
107
78
  - README.md
108
79
  - Rakefile
109
80
  - features/nagiosplugin_usage.feature
110
81
  - features/support/env.rb
82
+ - lib/nagios/plugin.rb
111
83
  - lib/nagiosplugin.rb
112
- - lib/nagiosplugin/plugin.rb
113
- - lib/nagiosplugin/version.rb
114
84
  - nagiosplugin.gemspec
115
- - spec/nagiosplugin/plugin_spec.rb
85
+ - spec/nagios/plugin_spec.rb
116
86
  - spec/spec_helper.rb
117
87
  homepage: https://github.com/bjoernalbers/nagiosplugin
118
88
  licenses:
@@ -137,9 +107,9 @@ rubyforge_project:
137
107
  rubygems_version: 2.2.2
138
108
  signing_key:
139
109
  specification_version: 4
140
- summary: nagiosplugin-2.2.0
110
+ summary: nagiosplugin-3.0.0
141
111
  test_files:
142
112
  - features/nagiosplugin_usage.feature
143
113
  - features/support/env.rb
144
- - spec/nagiosplugin/plugin_spec.rb
114
+ - spec/nagios/plugin_spec.rb
145
115
  - spec/spec_helper.rb
data/Guardfile DELETED
@@ -1,15 +0,0 @@
1
- # A sample Guardfile
2
- # More info at https://github.com/guard/guard#readme
3
-
4
- guard 'rspec', :cli => '--color' do
5
- watch(%r{^spec/.+_spec\.rb$})
6
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
- watch('spec/spec_helper.rb') { "spec" }
8
- end
9
-
10
- guard 'cucumber' do
11
- watch(%r{^lib/.+$}) { 'features' }
12
- watch(%r{^features/.+\.feature$})
13
- watch(%r{^features/support/.+$}) { 'features' }
14
- watch(%r{^features/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
15
- end
@@ -1,49 +0,0 @@
1
- module NagiosPlugin
2
- class Plugin
3
- NAGIOS_PLUGIN_EXIT_CODES = {
4
- :unknown => 3,
5
- :critical => 2,
6
- :warning => 1,
7
- :ok => 0
8
- }
9
-
10
- class << self
11
- def check(*args)
12
- plugin = new(*args)
13
- puts plugin.nagios_plugin_output
14
- exit plugin.nagios_plugin_exit_code
15
- rescue => e
16
- pretty_error = ([e.to_s, nil] + e.backtrace).join("\n")
17
- puts "PLUGIN UNKNOWN: #{pretty_error}"
18
- exit NAGIOS_PLUGIN_EXIT_CODES[:unknown]
19
- end
20
- end
21
-
22
- def nagios_plugin_exit_code
23
- NAGIOS_PLUGIN_EXIT_CODES[nagios_plugin_status]
24
- end
25
-
26
- def nagios_plugin_output
27
- output = [nagios_plugin_service] << ' '
28
- output << nagios_plugin_status.to_s.upcase
29
- output << ': ' + message if ( respond_to?(:message) && !message.empty? )
30
- output.join
31
- end
32
-
33
- private
34
-
35
- def nagios_plugin_status
36
- @nagios_plugin_status ||=
37
- case
38
- when critical? then :critical
39
- when warning? then :warning
40
- when ok? then :ok
41
- else :unknown
42
- end
43
- end
44
-
45
- def nagios_plugin_service
46
- self.class.name.split('::').last.gsub(/plugin$/i, '').upcase
47
- end
48
- end
49
- end
@@ -1,3 +0,0 @@
1
- module NagiosPlugin
2
- VERSION = '2.2.0'
3
- end
@@ -1,163 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module NagiosPlugin
4
- describe Plugin do
5
- let(:plugin) { Plugin.new }
6
-
7
- describe '.check' do
8
- before do
9
- allow(plugin).to receive_messages(
10
- nagios_plugin_exit_code: nil,
11
- nagios_plugin_output: nil)
12
- allow(Plugin).to receive_messages(
13
- puts: nil,
14
- exit: nil,
15
- new: plugin)
16
- end
17
-
18
- it 'displays the plugin output on stdout' do
19
- allow(plugin).to receive(:nagios_plugin_output).
20
- and_return('chunky bacon')
21
- Plugin.check
22
- expect(Plugin).to have_received(:puts).with('chunky bacon')
23
- end
24
-
25
- it 'exits with the status exit code' do
26
- allow(plugin).to receive(:nagios_plugin_exit_code).and_return(42)
27
- Plugin.check
28
- expect(Plugin).to have_received(:exit).with(42)
29
- end
30
-
31
- it 'passed all arguments to the initializer' do
32
- Plugin.check(42, fancy: true)
33
- expect(Plugin).to have_received(:new).with(42, fancy: true)
34
- end
35
-
36
- context 'when an exception was raised' do
37
- let(:exception) { StandardError.new('Oops!') }
38
-
39
- before do
40
- allow(Plugin).to receive(:new).and_raise(exception)
41
- end
42
-
43
- it 'rescues the exception' do
44
- expect { Plugin.check }.to_not raise_error
45
- end
46
-
47
- it 'exits with exit code unknown' do
48
- Plugin.check
49
- expect(Plugin).to have_received(:exit).with(3)
50
- end
51
-
52
- it 'displays the exception and backtrace on stdout' do
53
- allow(StandardError).to receive(:new).and_return(exception)
54
- allow(exception).to receive(:backtrace).and_return(%w(Chunky Bacon))
55
- Plugin.check
56
- expect(Plugin).to have_received(:puts).
57
- with("PLUGIN UNKNOWN: Oops!\n\nChunky\nBacon")
58
- end
59
- end
60
- end
61
-
62
- describe '#nagios_plugin_service' do
63
- it 'returns the upcased class name' do
64
- class Foo < Plugin; end
65
- plugin = Foo.new
66
- expect(plugin.send(:nagios_plugin_service)).to eq('FOO')
67
- end
68
-
69
- it 'strips "Plugin" from the class name' do
70
- class BarPlugin < Plugin; end
71
- plugin = BarPlugin.new
72
- expect(plugin.send(:nagios_plugin_service)).to eq('BAR')
73
- end
74
- end
75
-
76
- describe '#nagios_plugin_status' do
77
- it 'returns unknown when not critical, warning or ok' do
78
- allow(plugin).to receive_messages(
79
- critical?: false,
80
- warning?: false,
81
- ok?: false)
82
- expect(plugin.send(:nagios_plugin_status)).to eq(:unknown)
83
- end
84
-
85
- it 'returns critical when critical' do
86
- allow(plugin).to receive_messages(
87
- critical?: true,
88
- warning?: true,
89
- ok?: true)
90
- expect(plugin.send(:nagios_plugin_status)).to eq(:critical)
91
- end
92
-
93
- it 'returns warning when warning but not critical' do
94
- allow(plugin).to receive_messages(
95
- critical?: false,
96
- warning?: true,
97
- ok?: true)
98
- expect(plugin.send(:nagios_plugin_status)).to eq(:warning)
99
- end
100
-
101
- it 'returns ok when ok but not critical or warning' do
102
- allow(plugin).to receive_messages(
103
- critical?: false,
104
- warning?: false,
105
- ok?: true)
106
- expect(plugin.send(:nagios_plugin_status)).to eq(:ok)
107
- end
108
-
109
- it 'caches the status' do
110
- [:critical?, :warning?, :ok?].each do |check|
111
- allow(plugin).to receive(check).once.and_return(false)
112
- end
113
- expect(plugin.send(:nagios_plugin_status)).to eq(:unknown)
114
- expect(plugin.send(:nagios_plugin_status)).to eq(:unknown)
115
- end
116
- end
117
-
118
- describe '#nagios_plugin_exit_code' do
119
- it 'returns 3 when unknown' do
120
- allow(plugin).to receive(:nagios_plugin_status).and_return(:unknown)
121
- expect(plugin.send(:nagios_plugin_exit_code)).to eq(3)
122
- end
123
-
124
- it 'returns 2 when critical' do
125
- allow(plugin).to receive(:nagios_plugin_status).and_return(:critical)
126
- expect(plugin.send(:nagios_plugin_exit_code)).to eq(2)
127
- end
128
-
129
- it 'returns 1 when warning' do
130
- allow(plugin).to receive(:nagios_plugin_status).and_return(:warning)
131
- expect(plugin.send(:nagios_plugin_exit_code)).to eq(1)
132
- end
133
-
134
- it 'returns 0 when ok' do
135
- allow(plugin).to receive(:nagios_plugin_status).and_return(:ok)
136
- expect(plugin.send(:nagios_plugin_exit_code)).to eq(0)
137
- end
138
- end
139
-
140
- describe '#nagios_plugin_output' do
141
- before do
142
- allow(plugin).to receive(:nagios_plugin_status)
143
- end
144
-
145
- it 'joins the service name and the upcased status' do
146
- allow(plugin).to receive(:nagios_plugin_service).and_return('FRIED')
147
- allow(plugin).to receive(:nagios_plugin_status).and_return(:chicken)
148
- expect(plugin.send(:nagios_plugin_output)).to match(/^FRIED CHICKEN$/)
149
- end
150
-
151
- it 'includes a custom plugin message if present' do
152
- allow(plugin).to receive(:message).and_return('ALL U CAN EAT!')
153
- expect(plugin.send(:nagios_plugin_output)).to match(/: ALL U CAN EAT!$/)
154
- end
155
-
156
- it 'does not append message if message is empty' do
157
- allow(plugin).to receive(:message).and_return('')
158
- expect(plugin.send(:nagios_plugin_output)).not_to match(/:/)
159
- end
160
-
161
- end
162
- end
163
- end