nagios_plugin_base 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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +2 -0
- data/lib/nagios_plugin_base.rb +120 -0
- data/lib/nagios_plugin_base/version.rb +5 -0
- data/nagios_plugin_base.gemspec +23 -0
- metadata +80 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 3850ff75a984f3c9398fefd80537e2a6509279ac
|
|
4
|
+
data.tar.gz: 2bb4115bd072ddd99ab9f6cce0c521a6ab4932e0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a2e2b4105e3dade5395d88d741a11d974b0437fb3fade3d6cd76628ed62f3c233ff97cd2173f3097a265adc165b1aa1d552ff8307dc2ac24186d380afd4a3a9f
|
|
7
|
+
data.tar.gz: 405b86c0556754758ac67f5c95bd880cc272784a2afba1ecc600c93854b022982b3e074bcb3f3a92b6b3f359d6eeca88d3013567afc5f2f1bec78a49a74361d3
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2014 nazoking@gmail.com
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# NagiosPluginBase
|
|
2
|
+
|
|
3
|
+
Base class of nagios plugin.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'nagios_plugin_base'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install nagios_plugin_base
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
require 'nagios_plugin_base'
|
|
25
|
+
require 'open-uri'
|
|
26
|
+
|
|
27
|
+
# check is instant check method.
|
|
28
|
+
# argumsnts is options( nagios-plugin default options )
|
|
29
|
+
# block is check block.
|
|
30
|
+
Nagios::PluginBase.check!(:timeout,:verbose,:url) do
|
|
31
|
+
# if you set verbose, you can use attr_reader :verbose
|
|
32
|
+
puts "start" if verbose
|
|
33
|
+
unless open(url).read =~ /Google/
|
|
34
|
+
puts "document has not Google" if verbose
|
|
35
|
+
# Methods critical! and warning! and ok! and unknown!,
|
|
36
|
+
# print status and exit.
|
|
37
|
+
critical!
|
|
38
|
+
end
|
|
39
|
+
puts "end" if verbose
|
|
40
|
+
ok!
|
|
41
|
+
end
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
ruby check --url=https://www.google.com
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Contributing
|
|
49
|
+
|
|
50
|
+
1. Fork it ( https://github.com/nazoking/nagios_plugin_base/fork )
|
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
54
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
require "nagios_plugin_base/version"
|
|
2
|
+
require 'optparse'
|
|
3
|
+
require 'timeout'
|
|
4
|
+
|
|
5
|
+
module Nagios
|
|
6
|
+
class PluginBase
|
|
7
|
+
# exit codes
|
|
8
|
+
CODES=[
|
|
9
|
+
"OK",
|
|
10
|
+
"WARNING",
|
|
11
|
+
"CRITICAL",
|
|
12
|
+
"UNKNOWN"
|
|
13
|
+
]
|
|
14
|
+
# well known options
|
|
15
|
+
OPTOINS = {
|
|
16
|
+
:hostname => ['-H','--hostname=VAL'],
|
|
17
|
+
:warning => ['-w','--warning', 'warning threshold',Float],
|
|
18
|
+
:critical => ['-c','--critical', 'critical threshold',Float],
|
|
19
|
+
:authentication => ['-a','--authentication=VAL'],
|
|
20
|
+
:verbose => ['-v','--[no-]verbose'],
|
|
21
|
+
:timeout => ['-t','--timeout=VAL',Float],
|
|
22
|
+
:port => ['-p','--port=VAL',Integer],
|
|
23
|
+
:logname => ['-l','--logname=VAL'],
|
|
24
|
+
:url => ['-u','--url=VAL'],
|
|
25
|
+
:community => ['-C','--community'],
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
# @method ok!
|
|
29
|
+
# Print "OK" and exit 0
|
|
30
|
+
# @method warning!
|
|
31
|
+
# Print "WARNING" and exit 1
|
|
32
|
+
# @method critical!
|
|
33
|
+
# Print "CRITICAL" and exit 2
|
|
34
|
+
# @method warning!
|
|
35
|
+
# Print "WARNING" and exit 3
|
|
36
|
+
CODES.each_with_index do |name,code|
|
|
37
|
+
eval <<-CODE
|
|
38
|
+
def #{name.downcase}!
|
|
39
|
+
exit_ #{code}
|
|
40
|
+
end
|
|
41
|
+
CODE
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Print message and option helps, and exit as UNKNOWN.
|
|
45
|
+
# this is default style of argument error.
|
|
46
|
+
def invalid_args!(message=nil)
|
|
47
|
+
puts message if message
|
|
48
|
+
puts @opt.help
|
|
49
|
+
unknown!
|
|
50
|
+
end
|
|
51
|
+
# Print status (ex: OK) and exit
|
|
52
|
+
# @param [Integer] :code
|
|
53
|
+
def exit_(code)
|
|
54
|
+
if CODES[code]
|
|
55
|
+
puts "#{CODES[code]}"
|
|
56
|
+
exit code
|
|
57
|
+
else
|
|
58
|
+
raise "unknown exit code #{code}"
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
# Add option to option parser and create attribute-reader to self
|
|
62
|
+
# @param [Symbol] arg Option name
|
|
63
|
+
# @param [Array] options Option arguments for optparse
|
|
64
|
+
def set_default_option(arg, options)
|
|
65
|
+
@opt.on(*options){|v|
|
|
66
|
+
instance_variable_set("@#{arg}".to_sym, v)
|
|
67
|
+
}
|
|
68
|
+
eval <<-CODE
|
|
69
|
+
class << self
|
|
70
|
+
define_method(:#{arg}){ @#{arg} }
|
|
71
|
+
end
|
|
72
|
+
CODE
|
|
73
|
+
end
|
|
74
|
+
# set option to option parser
|
|
75
|
+
# @param [Symbol] :arg option from OPTIONS
|
|
76
|
+
def option(arg)
|
|
77
|
+
if OPTOINS[arg]
|
|
78
|
+
set_default_option(arg, OPTOINS[arg])
|
|
79
|
+
else
|
|
80
|
+
raise "unknwon nagios plugin option #{arg}"
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
# @param [Array<Symbol>] args option parser arguments
|
|
84
|
+
def initialize(args=[])
|
|
85
|
+
@opt = OptionParser.new
|
|
86
|
+
args.each{|a| option(a)}
|
|
87
|
+
end
|
|
88
|
+
# parse option
|
|
89
|
+
# @param [Array] argv cmmand line argumetns
|
|
90
|
+
def parse!(argv)
|
|
91
|
+
begin
|
|
92
|
+
@opt.parse!(argv)
|
|
93
|
+
rescue OptionParser::InvalidOption => e
|
|
94
|
+
invalid_args!(e.to_s)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
# Run block with timeout.
|
|
98
|
+
# @yield Main check callback block.
|
|
99
|
+
# @yieldreturn [Integer] exit code.
|
|
100
|
+
def run
|
|
101
|
+
Timeout.timeout(@timeout) do
|
|
102
|
+
ret = yield
|
|
103
|
+
raise "block need exit code" unless ret
|
|
104
|
+
exit_(ret)
|
|
105
|
+
end
|
|
106
|
+
rescue Timeout::Error => e
|
|
107
|
+
puts "timeout"
|
|
108
|
+
critical!
|
|
109
|
+
end
|
|
110
|
+
# instant check
|
|
111
|
+
def self.check!(*args,&block)
|
|
112
|
+
opt = self.new(args)
|
|
113
|
+
opt.parse!(ARGV)
|
|
114
|
+
opt.run do |opta|
|
|
115
|
+
opt.instance_eval(&block)
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'nagios_plugin_base/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "nagios_plugin_base"
|
|
8
|
+
spec.version = Nagios::PluginBase::VERSION
|
|
9
|
+
spec.authors = ["nazoking"]
|
|
10
|
+
spec.email = ["nazoking@gmail.com"]
|
|
11
|
+
spec.summary = %q{nagios plugin base class.}
|
|
12
|
+
spec.description = %q{nagios plugin base class.}
|
|
13
|
+
spec.homepage = "https://github.com/nazoking/nagios_plugin_base"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
23
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: nagios_plugin_base
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- nazoking
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2014-12-24 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ~>
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.7'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ~>
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.7'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ~>
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ~>
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
description: nagios plugin base class.
|
|
42
|
+
email:
|
|
43
|
+
- nazoking@gmail.com
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- .gitignore
|
|
49
|
+
- Gemfile
|
|
50
|
+
- LICENSE.txt
|
|
51
|
+
- README.md
|
|
52
|
+
- Rakefile
|
|
53
|
+
- lib/nagios_plugin_base.rb
|
|
54
|
+
- lib/nagios_plugin_base/version.rb
|
|
55
|
+
- nagios_plugin_base.gemspec
|
|
56
|
+
homepage: https://github.com/nazoking/nagios_plugin_base
|
|
57
|
+
licenses:
|
|
58
|
+
- MIT
|
|
59
|
+
metadata: {}
|
|
60
|
+
post_install_message:
|
|
61
|
+
rdoc_options: []
|
|
62
|
+
require_paths:
|
|
63
|
+
- lib
|
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - '>='
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - '>='
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: '0'
|
|
74
|
+
requirements: []
|
|
75
|
+
rubyforge_project:
|
|
76
|
+
rubygems_version: 2.2.2
|
|
77
|
+
signing_key:
|
|
78
|
+
specification_version: 4
|
|
79
|
+
summary: nagios plugin base class.
|
|
80
|
+
test_files: []
|