nagios_check 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.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in nagios_check.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2003, 2004 Jim Weirich
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/README.rdoc ADDED
@@ -0,0 +1,22 @@
1
+ = Nagios Check
2
+
3
+ License:: MIT license (see MIT-LICENSE file)
4
+ Copyright:: 2011 Dominique Broeglin
5
+
6
+
7
+ == DESCRIPTION
8
+
9
+ An easy to use DSL for building custom probes for the Nagios monitoring system.
10
+
11
+ == FEATURES
12
+
13
+ * Provide a simple to use DSL for building your own probes.
14
+ * Decode command line options.
15
+ * Report status data to Nagios.
16
+ * Report performance data to Nagios.
17
+
18
+
19
+ == RESOURCES
20
+
21
+ * {Website}[https://github.com/dbroeglin/nagios_check]
22
+ * {Source Code}[https://github.com/dbroeglin/nagios_check]
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,127 @@
1
+ require "nagios_check/version"
2
+ require 'optparse'
3
+ require 'ostruct'
4
+ require 'timeout'
5
+
6
+ require 'nagios_check/range'
7
+
8
+ module NagiosCheck
9
+ attr_reader :options
10
+ attr_accessor :message
11
+
12
+ def prepare
13
+ @values = {}
14
+ self.message = nil
15
+ end
16
+
17
+ def run
18
+ prepare
19
+ parse_options
20
+ return_val, status = 3, "UNKNOWN"
21
+ begin
22
+ if @timeout
23
+ check_with_timeout
24
+ else
25
+ check
26
+ end
27
+ return_val, status = finish
28
+ rescue Timeout::Error
29
+ store_message "TIMEOUT after #{@timeout}s"
30
+ rescue => e
31
+ store_message "INTERNAL ERROR: #{e}"
32
+ end
33
+ msg = status
34
+ msg += ': ' + message if message
35
+ if @values && !@values.empty?
36
+ msg += '|' + @values.map do |name, value|
37
+ "#{name}=#{value};;;;"
38
+ end.join(' ')
39
+ end
40
+ puts msg
41
+ exit return_val
42
+ end
43
+
44
+ def store_message(message)
45
+ self.message = message
46
+ end
47
+
48
+ def finish
49
+ value = @values.first.last
50
+ if @critical_range && !@critical_range.include?(value)
51
+ return [2, "CRITICAL"]
52
+ end
53
+ if @warning_range && !@warning_range.include?(value)
54
+ return [1, "WARNING"]
55
+ end
56
+ return [0, "OK"]
57
+ end
58
+
59
+ def store_value(name, value, opts = {})
60
+ @values[name] = value.to_f
61
+ end
62
+
63
+ module ClassMethods
64
+ def on(*args, &block)
65
+ @ons << [args, args.delete(:required), block]
66
+ end
67
+
68
+ def store(name, opts = {})
69
+ @defaults[name] = opts[:default] if opts.has_key?(:default)
70
+ Proc::new do |value|
71
+ self.options.send "#{name}=", value
72
+ end
73
+ end
74
+
75
+ def defaults
76
+ @defaults
77
+ end
78
+
79
+ def enable_warning(*args)
80
+ on("-w RANGE", *args) do |value|
81
+ @warning_range = NagiosCheck::Range.new(value)
82
+ end
83
+ end
84
+
85
+ def enable_critical(*args)
86
+ on("-c RANGE", *args) do |value|
87
+ @critical_range = NagiosCheck::Range.new(value)
88
+ end
89
+ end
90
+
91
+ def enable_timeout(*args)
92
+ on("-t TIMEOUT", *args) do |value|
93
+ @timeout = value.to_f
94
+ end
95
+ end
96
+ end
97
+
98
+ def self.included(base)
99
+ base.extend(ClassMethods)
100
+ base.instance_eval do
101
+ @ons = []
102
+ @defaults = {}
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def opt_parse
109
+ unless @opt_parse
110
+ opt_parse = OptionParser::new
111
+ self.class.instance_variable_get("@ons").each do |args, req, block|
112
+ opt_parse.on(*args) {|value| instance_exec(value, &block) }
113
+ end
114
+ @opt_parse = opt_parse
115
+ end
116
+ @opt_parse
117
+ end
118
+
119
+ def parse_options(argv = ARGV)
120
+ @options = OpenStruct::new(self.class.defaults)
121
+ opt_parse.parse!(argv)
122
+ end
123
+
124
+ def check_with_timeout
125
+ Timeout.timeout(@timeout) { check }
126
+ end
127
+ end
@@ -0,0 +1,37 @@
1
+ module NagiosCheck
2
+ class Range
3
+ def initialize(string_range)
4
+ if string_range.nil? || string_range.empty?
5
+ raise RuntimeError, "Pattern should not be nil"
6
+ end
7
+ @string_range = string_range
8
+ tokens = string_range.scan(/^(@)?(([-.0-9]+|~)?:)?([-.0-9]+)?$/).first
9
+ unless tokens
10
+ raise RuntimeError, "Pattern should be of form [@][~][min]:max"
11
+ end
12
+ @exclusive = true if tokens.include? "@"
13
+ case tokens[2]
14
+ when nil, "" then @min = 0
15
+ when '~' then @min = nil
16
+ else @min = tokens[2].to_f
17
+ end
18
+ @max = tokens[3].nil? || tokens[3] == "" ? nil : tokens[3].to_f
19
+ end
20
+
21
+ def include?(value)
22
+ if @exclusive
23
+ (@min.nil? || value > @min) && (@max.nil? || value < @max)
24
+ else
25
+ (@min.nil? || value >= @min) && (@max.nil? || value <= @max)
26
+ end
27
+ end
28
+
29
+ def ===(value)
30
+ include?(value)
31
+ end
32
+
33
+ def to_s
34
+ "Range[#{@reversed ? "~" : ""}#{@inclusive ? "@" : ""}#{@min}:#{@max}]"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module NagiosCheck
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "nagios_check/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "nagios_check"
7
+ s.version = NagiosCheck::VERSION
8
+ s.authors = ["Dominique Broeglin"]
9
+ s.email = ["dominique.broeglin@gmail.com"]
10
+ s.homepage = "https://github.com/dbroeglin/nagios_check"
11
+ s.summary = %q{Ruby Nagios Check Integration}
12
+ s.description = %q{An easy to use DSL for building custom probes for the Nagios monitoring system}
13
+
14
+ s.rubyforge_project = "nagios_check"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+ def before_finish_test
3
+ before(:each) do
4
+ subject.prepare
5
+ description = example.metadata[:example_group][:example_group][:description_args].first
6
+ if /^when options are '(.*)'$/ =~ description
7
+ subject.send :parse_options, $1.split
8
+ end
9
+
10
+ description = example.metadata[:example_group][:description_args].first
11
+ if /^when value is (.*)$/ =~ description
12
+ subject.store_value 'val', $1.to_f
13
+ end
14
+ end
15
+ end
16
+
17
+ class OkTestCheck
18
+ include NagiosCheck
19
+ end
20
+
21
+ class WarningTestCheck
22
+ include NagiosCheck
23
+ enable_warning
24
+ end
25
+
26
+ class CriticalTestCheck
27
+ include NagiosCheck
28
+ enable_warning
29
+ enable_critical
30
+ end
31
+
32
+ describe OkTestCheck do
33
+ before_finish_test
34
+
35
+ context "when options are ''" do
36
+ context "when value is 0" do
37
+ specify { subject.finish.should == [0, "OK"] }
38
+ end
39
+ context "when value is 5" do
40
+ specify { subject.finish.should == [0, "OK"] }
41
+ end
42
+ context "when value is 10" do
43
+ specify { subject.finish.should == [0, "OK"] }
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ describe WarningTestCheck do
50
+ before_finish_test
51
+
52
+ context "when options are '-w 10'" do
53
+ context "when value is -1" do
54
+ specify { subject.finish.should == [1, "WARNING"] }
55
+ end
56
+ context "when value is 0" do
57
+ specify { subject.finish.should == [0, "OK"] }
58
+ end
59
+ context "when value is 5" do
60
+ specify { subject.finish.should == [0, "OK"] }
61
+ end
62
+ context "when value is 10" do
63
+ specify { subject.finish.should == [0, "OK"] }
64
+ end
65
+ context "when value is 11" do
66
+ specify { subject.finish.should == [1, "WARNING"] }
67
+ end
68
+ end
69
+ end
70
+
71
+ describe CriticalTestCheck do
72
+ before_finish_test
73
+
74
+ context "when options are '-w 10 -c 20'" do
75
+ context "when value is -1" do
76
+ specify { subject.finish.should == [2, "CRITICAL"] }
77
+ end
78
+ context "when value is 0" do
79
+ specify { subject.finish.should == [0, "OK"] }
80
+ end
81
+ context "when value is 5" do
82
+ specify { subject.finish.should == [0, "OK"] }
83
+ end
84
+ context "when value is 10" do
85
+ specify { subject.finish.should == [0, "OK"] }
86
+ end
87
+ context "when value is 15" do
88
+ specify { subject.finish.should == [1, "WARNING"] }
89
+ end
90
+ context "when value is 20" do
91
+ specify { subject.finish.should == [1, "WARNING"] }
92
+ end
93
+ context "when value is 21" do
94
+ specify { subject.finish.should == [2, "CRITICAL"] }
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,141 @@
1
+ require 'spec_helper'
2
+
3
+ require 'pp'
4
+
5
+ describe NagiosCheck::Range do
6
+ subject { @range }
7
+ before(:each) do
8
+ description = example.metadata[:example_group][:description_args].first
9
+ if /^when pattern is (.*)/ =~ description
10
+ @range = NagiosCheck::Range::new($1)
11
+ end
12
+ end
13
+
14
+ context "when pattern is 10" do
15
+ it { should_not contain -1 }
16
+ it { should contain 0 }
17
+ it { should contain 0.1 }
18
+ it { should contain 1 }
19
+ it { should contain 9 }
20
+ it { should contain 9.9 }
21
+ it { should contain 10 }
22
+ it { should contain 10.0 }
23
+ it { should_not contain 10.1 }
24
+ end
25
+
26
+ context "when pattern is :10" do
27
+ it { should_not contain -1 }
28
+ it { should contain 0 }
29
+ it { should contain 0.1 }
30
+ it { should contain 1 }
31
+ it { should contain 9 }
32
+ it { should contain 9.9 }
33
+ it { should contain 10 }
34
+ it { should contain 10.0 }
35
+ it { should_not contain 10.1 }
36
+ end
37
+
38
+ context "when pattern is @:10" do
39
+ it { should_not contain -1 }
40
+ it { should_not contain 0 }
41
+ it { should contain 5 }
42
+ it { should_not contain 10 }
43
+ it { should_not contain 10.0 }
44
+ it { should_not contain 11 }
45
+ end
46
+
47
+ context "when pattern is 10:" do
48
+ it { should_not contain -1 }
49
+ it { should_not contain 1 }
50
+ it { should_not contain 9.9 }
51
+ it { should contain 10 }
52
+ it { should contain 10.0 }
53
+ it { should contain 10.1 }
54
+ it { should contain 11 }
55
+ end
56
+
57
+ context "when pattern is @10:" do
58
+ it { should_not contain -1 }
59
+ it { should_not contain 1 }
60
+ it { should_not contain 9.9 }
61
+ it { should_not contain 10 }
62
+ it { should_not contain 10.0 }
63
+ it { should contain 10.1 }
64
+ it { should contain 11 }
65
+ end
66
+
67
+ context "when pattern is 10:11" do
68
+ it { should_not contain -1 }
69
+ it { should_not contain 1 }
70
+ it { should_not contain 9.9 }
71
+ it { should contain 10 }
72
+ it { should contain 10.0 }
73
+ it { should contain 10.1 }
74
+ it { should contain 10.9 }
75
+ it { should contain 11 }
76
+ it { should_not contain 11.1 }
77
+ it { should_not contain 12 }
78
+ end
79
+
80
+ context "when pattern is @10:11" do
81
+ it { should_not contain -1 }
82
+ it { should_not contain 1 }
83
+ it { should_not contain 9.9 }
84
+ it { should_not contain 10 }
85
+ it { should_not contain 10.0 }
86
+ it { should contain 10.1 }
87
+ it { should_not contain 11 }
88
+ it { should_not contain 11.1 }
89
+ it { should_not contain 12 }
90
+ end
91
+
92
+ context "when pattern is @10.05:11.05" do
93
+ it { should_not contain -1 }
94
+ it { should_not contain 1 }
95
+ it { should_not contain 9.9 }
96
+ it { should_not contain 10 }
97
+ it { should_not contain 10.0 }
98
+ it { should contain 10.1 }
99
+ it { should contain 11 }
100
+ it { should_not contain 11.1 }
101
+ it { should_not contain 12 }
102
+ end
103
+
104
+ context "when pattern is -1:1" do
105
+ it { should_not contain -2 }
106
+ it { should contain -1 }
107
+ it { should contain -0.9 }
108
+ it { should contain 0 }
109
+ it { should contain 0.9 }
110
+ it { should contain 1 }
111
+ it { should_not contain 2 }
112
+ end
113
+
114
+ context "when pattern is ~:1" do
115
+ it { should contain -2 }
116
+ it { should contain -1 }
117
+ it { should contain 0 }
118
+ it { should contain 1 }
119
+ it { should_not contain 2 }
120
+ end
121
+
122
+ context "when pattern is @~:1" do
123
+ it { should contain -2 }
124
+ it { should contain -1 }
125
+ it { should contain 0 }
126
+ it { should_not contain 1 }
127
+ it { should_not contain 2 }
128
+ end
129
+
130
+ context "when nil pattern" do
131
+ it "raises an error" do
132
+ lambda { NagiosCheck::Range.new nil }.should raise_error
133
+ end
134
+ end
135
+
136
+ context "when empty pattern" do
137
+ it "raises an error" do
138
+ lambda { NagiosCheck::Range.new "" }.should raise_error
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,30 @@
1
+ require 'nagios_check'
2
+
3
+ module Matchers
4
+ class Contain
5
+ def initialize(expected)
6
+ @expected = expected
7
+ end
8
+
9
+ def matches?(actual)
10
+ @actual = actual
11
+ @actual.include? @expected
12
+ end
13
+
14
+ def failure_message
15
+ "expected #{@actual} to contain #{@expected}"
16
+ end
17
+
18
+ def negative_failure_message
19
+ "expected #{@actual} not to contain #{@expected}"
20
+ end
21
+ end
22
+
23
+ def contain(value)
24
+ Contain::new(value)
25
+ end
26
+ end
27
+
28
+ RSpec.configure do |config|
29
+ config.include(Matchers)
30
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nagios_check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dominique Broeglin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-22 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: An easy to use DSL for building custom probes for the Nagios monitoring
15
+ system
16
+ email:
17
+ - dominique.broeglin@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - MIT-LICENSE
25
+ - README.rdoc
26
+ - Rakefile
27
+ - lib/nagios_check.rb
28
+ - lib/nagios_check/range.rb
29
+ - lib/nagios_check/version.rb
30
+ - nagios_check.gemspec
31
+ - spec/finish_spec.rb
32
+ - spec/range_spec.rb
33
+ - spec/spec_helper.rb
34
+ homepage: https://github.com/dbroeglin/nagios_check
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project: nagios_check
54
+ rubygems_version: 1.8.6
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Ruby Nagios Check Integration
58
+ test_files:
59
+ - spec/finish_spec.rb
60
+ - spec/range_spec.rb
61
+ - spec/spec_helper.rb