naplug 1.7.1 → 1.9.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/README.md +44 -2
- data/lib/naplug/helpers/cli.rb +44 -0
- data/lib/naplug/helpers/json_thresholds.rb +46 -0
- data/lib/naplug/helpers.rb +0 -21
- data/lib/naplug/plugin.rb +46 -4
- data/lib/naplug/version.rb +1 -1
- data/lib/naplug.rb +27 -7
- metadata +4 -2
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Naplug [](http://badge.fury.io/rb/naplug)
|
2
2
|
|
3
|
-
*Naplug* is a [Nagios plugin](http://nagiosplug.sourceforge.net/developer-guidelines.html) library for Ruby focused on plugin internals: organization, status, performance data, output and exit code handling. It does not
|
3
|
+
*Naplug* is a [Nagios plugin](http://nagiosplug.sourceforge.net/developer-guidelines.html) library for Ruby focused on plugin internals: organization, status, performance data, output and exit code handling. It contains (but does not include by default) functionality related to option and argument parsing, allowing plugin developers to use [any of the many fine CLI tools available](https://www.ruby-toolbox.com/categories/CLI_Option_Parsers) for this purpose. It aims to ease the task of writing Nagios plugins in Ruby and _handling the paperwork_, allowing the plugin developer to concentrate on the test logic of the plugin. Some of internal implementation is largely modeled after the very excellent [Worlkflow](https://github.com/geekq/workflow) library.
|
4
4
|
|
5
5
|
*Naplug* allows plugins to contain other plugins (referred to as *plugs*), which are a useful abstraction to break up significant tasks that the plugin as a whole must perform in order to determine the state of a service or host. The status and output of these plugs is thus used to determine the overall status of the plugin and build the output depending on said status.
|
6
6
|
|
@@ -499,7 +499,7 @@ Overriding these will likely cause *Naplug* to misbehave, to say the least.
|
|
499
499
|
|
500
500
|
Other methods can be defined in the class as necessary, and they can be used in the defined plugins or plugs, generally to provide helpers services. These should be defined as `private` or `protected` as necessary.
|
501
501
|
|
502
|
-
|
502
|
+
## Status
|
503
503
|
|
504
504
|
Status is a special object that represent the status of a plugin for each of the defined states in the [Nagios Plugin Guidelines](http://nagiosplug.sourceforge.net/developer-guidelines.html): `OK`, `WARNING`, `CRITICAL` and `UNKNOWN`. Each of these states is itself an instance method which sets the state, and you can obtain the string and numeric representation through the usual methods `to_s` and `to_i`. The initial (and default) status of a `Status` object is `UNKNOWN`. Statuses are comparable in that larger statuses represent worse states, a feature that will come handy shortly.
|
505
505
|
|
@@ -537,6 +537,48 @@ which produces
|
|
537
537
|
Comparing statuses:
|
538
538
|
status [OK] < status1 [WARNING] is true
|
539
539
|
|
540
|
+
## Naplug Helpers
|
541
|
+
|
542
|
+
*Naplug* includes helpers, which are not loaded by default.
|
543
|
+
|
544
|
+
### `Naplug::Helpers::CLI`
|
545
|
+
|
546
|
+
Naplug is very much focused on plugin internals, leaving the work of command-line parsing to external entities. This allows plugin developers to use their preferred choice of parsers, such as [`thor`](http://whatisthor.com), [`Slop`](https://github.com/leejarvis/slop) or `OptionsParser`.
|
547
|
+
|
548
|
+
A helper is however built-in, and uses the small and very flexible [*trollop*] library, which must be installed as a gem. To use:
|
549
|
+
|
550
|
+
require 'naplug'
|
551
|
+
require 'naplug/helpers/cli'
|
552
|
+
|
553
|
+
class FooPlugin
|
554
|
+
|
555
|
+
VERSION = '1.0.0'
|
556
|
+
|
557
|
+
include Naplug
|
558
|
+
|
559
|
+
plugin do |p|
|
560
|
+
...
|
561
|
+
end
|
562
|
+
end
|
563
|
+
|
564
|
+
class FooPluginCLI
|
565
|
+
|
566
|
+
include Naplug::Helpers::CLI
|
567
|
+
|
568
|
+
opts = options do
|
569
|
+
version FooPlugin::VERSION
|
570
|
+
banner "#{File.basename($0)}"
|
571
|
+
opt :warning, 'number of mtime WARNING seconds (required)', :type => :int, :required => true
|
572
|
+
opt :critical, 'number of mtime CRITICAL seconds (required)', :type => :int, :required => true
|
573
|
+
end
|
574
|
+
|
575
|
+
plugin = FooPlugin.new opts
|
576
|
+
plugin.exec!
|
577
|
+
|
578
|
+
end
|
579
|
+
|
580
|
+
Naplug does change the behavior of *Trollop* so that when arguments generate an error, these are handled correctly as a plugin (producing an `UNKNOWN` status).
|
581
|
+
|
540
582
|
# Futures
|
541
583
|
|
542
584
|
There following are some ideas on future Naplug features.
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'trollop'
|
2
|
+
require 'naplug'
|
3
|
+
|
4
|
+
module Naplug
|
5
|
+
|
6
|
+
module Helpers
|
7
|
+
|
8
|
+
module CLI
|
9
|
+
|
10
|
+
def self.included(klass)
|
11
|
+
klass.send :include, InstanceMethods
|
12
|
+
klass.extend ClassMethods
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
include Trollop
|
17
|
+
|
18
|
+
def with_standard_exception_handling parser
|
19
|
+
begin
|
20
|
+
yield
|
21
|
+
rescue CommandlineError => e
|
22
|
+
plugin = Naplug::Plugin.new :cli, Proc.new { true }, {}
|
23
|
+
plugin.output! e.message
|
24
|
+
print "%s: %s\n" % [plugin.status.to_s,plugin.output]
|
25
|
+
exit plugin.status.to_i
|
26
|
+
rescue HelpNeeded
|
27
|
+
parser.educate
|
28
|
+
exit
|
29
|
+
rescue VersionNeeded
|
30
|
+
puts parser.version
|
31
|
+
exit
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module InstanceMethods
|
37
|
+
def initialize
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Naplug
|
4
|
+
|
5
|
+
module Helpers
|
6
|
+
|
7
|
+
module JSON_Thresholds
|
8
|
+
|
9
|
+
def self.included(klass)
|
10
|
+
klass.extend ClassMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def hashify_json_thresholds(*threstag)
|
15
|
+
tag, thresholds_json, thresholds_hash = case threstag.size
|
16
|
+
when 0, 1
|
17
|
+
[nil, threstag[0], {}]
|
18
|
+
else
|
19
|
+
[threstag[1],threstag[0], {}]
|
20
|
+
end
|
21
|
+
plug = nil
|
22
|
+
thresholds_proc = Proc.new do |json_element|
|
23
|
+
case
|
24
|
+
when (json_element.is_a? String and json_element.match(/\d*:\d*:\d*:\d*/))
|
25
|
+
case tag.nil?
|
26
|
+
when true
|
27
|
+
thresholds_hash[plug] = Hash[Status.states.zip json_element.split(':',-1).map { |v| v.nil? ? nil : v.to_i } ]
|
28
|
+
else
|
29
|
+
thresholds_hash[tag] = Hash[plug, Hash[Status.states.zip json_element.split(':',-1).map { |v| v.nil? ? nil : v.to_i } ]]
|
30
|
+
end
|
31
|
+
when Symbol
|
32
|
+
plug = json_element
|
33
|
+
else
|
34
|
+
nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
JSON.recurse_proc(JSON.parse(thresholds_json, :symbolize_names => true),&thresholds_proc) if thresholds_json
|
38
|
+
thresholds_hash
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
data/lib/naplug/helpers.rb
CHANGED
@@ -7,27 +7,6 @@ module Naplug
|
|
7
7
|
|
8
8
|
module Helpers
|
9
9
|
|
10
|
-
module Thresholds
|
11
|
-
|
12
|
-
def hashify_json_thresholds(tag,thres_json=nil)
|
13
|
-
thresholds = { tag => {} }
|
14
|
-
plug = nil
|
15
|
-
thres_proc = Proc.new do |json_element|
|
16
|
-
case
|
17
|
-
when (json_element.is_a? String and json_element.match(/\d*:\d*:\d*:\d*/))
|
18
|
-
thresholds[:main][plug] = Hash[Status.states.zip json_element.split(':',-1).map { |v| v.nil? ? nil : v.to_i } ]
|
19
|
-
when Symbol
|
20
|
-
plug = json_element
|
21
|
-
else
|
22
|
-
nil
|
23
|
-
end
|
24
|
-
end
|
25
|
-
JSON.recurse_proc(JSON.parse(thres_json, :symbolize_names => true),&thres_proc) if thres_json
|
26
|
-
thresholds
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
10
|
module Hashes
|
32
11
|
|
33
12
|
# Thx Avdi Grimm! http://devblog.avdi.org/2009/11/20/hash-transforms-in-ruby/
|
data/lib/naplug/plugin.rb
CHANGED
@@ -13,8 +13,11 @@ module Naplug
|
|
13
13
|
class DuplicatePlugin < StandardError; end
|
14
14
|
|
15
15
|
DEFAULT_META = { :debug => false, :enabled => true }
|
16
|
+
VALID_META_OPTIONS = [ :debug, :state, :description, :parent ]
|
17
|
+
|
18
|
+
def initialize(tag, block, meta)
|
19
|
+
validate_meta_options meta
|
16
20
|
|
17
|
-
def initialize(tag, block, meta = {})
|
18
21
|
@tag = tag
|
19
22
|
@block = block
|
20
23
|
@plugins = Hash.new
|
@@ -23,7 +26,13 @@ module Naplug
|
|
23
26
|
@_data = OpenStruct.new :status => Status.new, :output => Output.new, :payload => nil, :perfdata => nil
|
24
27
|
@_meta = OpenStruct.new DEFAULT_META.merge meta
|
25
28
|
|
26
|
-
begin
|
29
|
+
begin
|
30
|
+
instance_eval &block
|
31
|
+
rescue ArgumentError => e
|
32
|
+
raise
|
33
|
+
rescue
|
34
|
+
nil
|
35
|
+
end
|
27
36
|
|
28
37
|
end
|
29
38
|
|
@@ -56,6 +65,10 @@ module Naplug
|
|
56
65
|
@_meta.parent
|
57
66
|
end
|
58
67
|
|
68
|
+
def description
|
69
|
+
@_meta.description
|
70
|
+
end
|
71
|
+
|
59
72
|
# true when a plugin contains plugs
|
60
73
|
def has_plugins?
|
61
74
|
@plugins.empty? ? false : true
|
@@ -142,9 +155,10 @@ module Naplug
|
|
142
155
|
|
143
156
|
private
|
144
157
|
|
145
|
-
def plugin(
|
158
|
+
def plugin(*tagmeta, &block)
|
159
|
+
tag,meta = tagmeta_grok(tagmeta)
|
146
160
|
raise DuplicatePlugin, "duplicate definition of #{tag}" if @plugins.key? tag
|
147
|
-
@plugins[tag] = Plugin.new tag, block, :parent => self
|
161
|
+
@plugins[tag] = Plugin.new tag, block, meta.merge({ :parent => self })
|
148
162
|
self.define_singleton_method tag do
|
149
163
|
@plugins[tag]
|
150
164
|
end
|
@@ -154,5 +168,33 @@ module Naplug
|
|
154
168
|
@_meta.debug
|
155
169
|
end
|
156
170
|
|
171
|
+
def tagmeta_grok(tagmeta)
|
172
|
+
case tagmeta.size
|
173
|
+
when 0
|
174
|
+
[:main, {}]
|
175
|
+
when 1
|
176
|
+
case tagmeta[0]
|
177
|
+
when Symbol
|
178
|
+
[tagmeta[0], {}]
|
179
|
+
when Hash
|
180
|
+
[:main,tagmeta[0]]
|
181
|
+
else
|
182
|
+
raise Naplug::Error, 'ArgumentError on Naplug#plugin'
|
183
|
+
end
|
184
|
+
when 2
|
185
|
+
raise Naplug::Error, 'ArgumentError on Naplug#plugin' unless tagmeta[0].is_a? Symbol and tagmeta[1].is_a? Hash
|
186
|
+
tagmeta[0..1]
|
187
|
+
else
|
188
|
+
raise Naplug::Error, 'ArgumentError on Naplug#plugin'
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
def validate_meta_options(options)
|
193
|
+
invalid_options = options.keys - VALID_META_OPTIONS
|
194
|
+
if invalid_options.any?
|
195
|
+
raise ArgumentError, "invalid meta option(s): #{invalid_options.join(', ')}"
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
157
199
|
end
|
158
200
|
end
|
data/lib/naplug/version.rb
CHANGED
data/lib/naplug.rb
CHANGED
@@ -11,8 +11,6 @@
|
|
11
11
|
#--
|
12
12
|
# Naplug::ClassMethods and Naplug::InstanceMethods
|
13
13
|
|
14
|
-
|
15
|
-
require 'rubygems'
|
16
14
|
require 'naplug/plugin'
|
17
15
|
|
18
16
|
module Naplug
|
@@ -30,9 +28,10 @@ module Naplug
|
|
30
28
|
# Create a metaplugin (which basically contains a tag and a block)
|
31
29
|
# @param tag [Symbol] the plugin tag
|
32
30
|
# @return [Plugin] a metaplugin
|
33
|
-
def plugin(
|
31
|
+
def plugin(*tagmeta, &block)
|
32
|
+
tag, meta = tagmeta_grok tagmeta
|
34
33
|
@plugins = Hash.new unless @plugins
|
35
|
-
@plugins[tag] = create_metaplugin tag, block
|
34
|
+
@plugins[tag] = create_metaplugin tag, meta, block
|
36
35
|
end
|
37
36
|
|
38
37
|
# A list of plugin tags
|
@@ -44,12 +43,33 @@ module Naplug
|
|
44
43
|
private
|
45
44
|
|
46
45
|
# Create a metaplugin (helper)
|
47
|
-
def create_metaplugin(tag,block)
|
46
|
+
def create_metaplugin(tag,meta,block)
|
48
47
|
module_eval do
|
49
48
|
define_method "#{tag}".to_sym do; @plugins[tag]; end # <tag> methods for quick access to plugins
|
50
49
|
define_method "#{tag}!".to_sym do; self.exec! tag; end # <tag>! methods to involke exec! on a given plugin
|
51
50
|
end
|
52
|
-
Plugin.new tag, block,
|
51
|
+
Plugin.new tag, block, meta.merge({ :parent => self })
|
52
|
+
end
|
53
|
+
|
54
|
+
def tagmeta_grok(tagmeta)
|
55
|
+
case tagmeta.size
|
56
|
+
when 0
|
57
|
+
[:main, {}]
|
58
|
+
when 1
|
59
|
+
case tagmeta[0]
|
60
|
+
when Symbol
|
61
|
+
[tagmeta[0], {}]
|
62
|
+
when Hash
|
63
|
+
[:main,tagmeta[0]]
|
64
|
+
else
|
65
|
+
raise Naplug::Error, 'ArgumentError on Naplug#plugin'
|
66
|
+
end
|
67
|
+
when 2
|
68
|
+
raise Naplug::Error, 'ArgumentError on Naplug#plugin' unless tagmeta[0].is_a? Symbol and tagmeta[1].is_a? Hash
|
69
|
+
tagmeta[0..1]
|
70
|
+
else
|
71
|
+
raise Naplug::Error, 'ArgumentError on Naplug#plugin'
|
72
|
+
end
|
53
73
|
end
|
54
74
|
|
55
75
|
end
|
@@ -168,7 +188,7 @@ module Naplug
|
|
168
188
|
|
169
189
|
def plugins!
|
170
190
|
self.class.plugins.each do |tag,plugin|
|
171
|
-
@plugins[tag] = Plugin.new tag, plugin.block
|
191
|
+
@plugins[tag] = Plugin.new tag, plugin.block, {}
|
172
192
|
end
|
173
193
|
end
|
174
194
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: naplug
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.9.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-27 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'A Ruby library for Nagios plugins '
|
15
15
|
email: gerir@evernote.com
|
@@ -18,6 +18,8 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- lib/naplug/about.rb
|
21
|
+
- lib/naplug/helpers/cli.rb
|
22
|
+
- lib/naplug/helpers/json_thresholds.rb
|
21
23
|
- lib/naplug/helpers.rb
|
22
24
|
- lib/naplug/output.rb
|
23
25
|
- lib/naplug/performancedata.rb
|