naplug 1.9.0 → 1.10.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2d6003ce6c362663bf1d5f692ba2a4409951167e4ed6c0396e96ea42058ffb1e
4
+ data.tar.gz: 20eade85d4b26153fafe22c5b23ee56d919cfdfddf934f007e029bcb145cdc66
5
+ SHA512:
6
+ metadata.gz: ab5a24e2f158e5aadd13d0f3a06754a51e695cc995bfc1bcbfa1e47f4caa1380f3287b200b6508cfbd96e376830201122741feecc76a63b4b960d4a22a22e3d5
7
+ data.tar.gz: e0855f074375141e46acb067b9b1a94f44b15111bde193bdee9d76c664b0bffe00b4a4bcfc6648fc258656ddc014c5492148028f07c3003f194601a75a766623
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Naplug [![Gem Version](https://badge.fury.io/rb/naplug.png)](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 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.
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 the choice to use the built-in parser or [any of the many other fine CLI tools available](https://www.ruby-toolbox.com/categories/CLI_Option_Parsers) for this purpose.
4
4
 
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.
5
+ **Naplug** aims to ease the task of writing Nagios plugins in Ruby by _handling the paperwork_, allowing the plugin developer to concentrate on the test logic of the plugin. Some of the internal implementation is largely modeled after the very excellent [Worlkflow](https://github.com/geekq/workflow) library.
6
+
7
+ **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
8
 
7
9
  While *Naplug* handles the nitty-gritty of Nagios plugins, it is important to have familiarity with the [Nagios Plugin Developer Guidelines](http://nagiosplug.sourceforge.net/developer-guidelines.html).
8
10
 
@@ -15,20 +17,20 @@ While *Naplug* handles the nitty-gritty of Nagios plugins, it is important to ha
15
17
 
16
18
  Naplug approaches Nagios plugins as Ruby classes (note that `plugin` is a reserved keyword at both the class and instance levels). To use *Naplug*, install the gem and:
17
19
 
18
- #!/usr/bin/end ruby -rubygems
20
+ #!/usr/bin/env ruby
19
21
  require 'naplug'
20
22
 
21
23
  class MyPlugin
22
24
  include Naplug
23
25
  plugin do |p|
24
- ...
26
+ ... <do plugin work> ...
25
27
  end
26
28
  end
27
29
 
28
30
  MyPlugin.new.exec!
31
+
29
32
 
30
-
31
- All examples will omit the `require`s for readability.
33
+ All examples in this document will omit the `require` for readability.
32
34
 
33
35
  A very simple plugin that always returns an OK status:
34
36
 
@@ -44,7 +46,7 @@ A very simple plugin that always returns an OK status:
44
46
 
45
47
  AlwaysOkPlugin.new.exec!
46
48
 
47
- In the above example, a new class `AlwaysOkPlugin` is defined (the class name is arbitrary), and within this class, a plugin is created, which performs some work to set the status and output of the plugin. Once the class is defined, a new instance of the plugin is created and executed. The `exec!` method executes the plugin, evaluates status, produces correctly formatted output, and exits with the appropriate exit code:
49
+ In the above example, a new class, `AlwaysOkPlugin`, is defined (the class name is arbitrary), and within this class, a plugin is created, which performs some work to set the status and output of the plugin. Once the class is defined, a new instance of the plugin is created and executed. The `exec!` method executes the plugin, evaluates status, produces correctly formatted output, and exits with the appropriate exit code:
48
50
 
49
51
  naplug@plugin:~: alwaysok
50
52
  OK: Optimism level: 100%
@@ -577,7 +579,7 @@ A helper is however built-in, and uses the small and very flexible [*trollop*] l
577
579
 
578
580
  end
579
581
 
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).
582
+ 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).r
581
583
 
582
584
  # Futures
583
585
 
@@ -4,13 +4,12 @@ lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
4
4
  $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
5
5
 
6
6
  require 'naplug'
7
- require 'awesome_print'
8
7
 
9
8
  class PerfDataPlugPlugin
10
9
 
11
10
  include Naplug
12
11
 
13
- plugin :p do |p|
12
+ plugin :p, :benchmark => true do |p|
14
13
 
15
14
  plugin :p1 do |p1|
16
15
  p1.status.ok!
@@ -5,7 +5,6 @@ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
5
5
 
6
6
  require 'rubygems'
7
7
  require 'naplug'
8
- #require 'awesome_print'
9
8
 
10
9
  class PerfDataPlugin
11
10
 
@@ -11,7 +11,9 @@
11
11
  #--
12
12
  # Naplug::ClassMethods and Naplug::InstanceMethods
13
13
 
14
+ require 'naplug/meta'
14
15
  require 'naplug/plugin'
16
+ require 'naplug/helpers/grokkers'
15
17
 
16
18
  module Naplug
17
19
 
@@ -19,19 +21,28 @@ module Naplug
19
21
 
20
22
  module ClassMethods
21
23
 
24
+ include Naplug::Helpers::Grokkers
25
+
22
26
  # @!scope class
23
27
 
24
28
  # @!attribute [r] plugins
25
29
  # @return [Hash<Symbol, Plugin>] metaplugins
26
- attr_reader :plugins
30
+ attr_reader :plugins, :_time
27
31
 
28
32
  # Create a metaplugin (which basically contains a tag and a block)
29
33
  # @param tag [Symbol] the plugin tag
30
34
  # @return [Plugin] a metaplugin
31
35
  def plugin(*tagmeta, &block)
32
36
  tag, meta = tagmeta_grok tagmeta
37
+ @metas = Hash.new unless @metas
38
+ @metas[tag] = Meta.new meta.merge :meta => true
33
39
  @plugins = Hash.new unless @plugins
34
40
  @plugins[tag] = create_metaplugin tag, meta, block
41
+ @_time = { :start => Time.now } # if m[:benchmark]
42
+ end
43
+
44
+ def meta(m)
45
+ @_time = { :start => Time.now } # if m[:benchmark]
35
46
  end
36
47
 
37
48
  # A list of plugin tags
@@ -48,28 +59,7 @@ module Naplug
48
59
  define_method "#{tag}".to_sym do; @plugins[tag]; end # <tag> methods for quick access to plugins
49
60
  define_method "#{tag}!".to_sym do; self.exec! tag; end # <tag>! methods to involke exec! on a given plugin
50
61
  end
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
62
+ Plugin.new tag, block, meta.merge(:parent => self, :meta => true)
73
63
  end
74
64
 
75
65
  end
@@ -120,8 +110,11 @@ module Naplug
120
110
  # Execute, evaluate and exit the plugin according to the plugin status, outputting the plugin's text output (and performance data, if applicable)
121
111
  # @param tag [Symbol] a plugin tag
122
112
  def exec!(tag = default_plugin.tag)
123
- exec tag
124
- eval tag
113
+ t = Benchmark.realtime do
114
+ exec tag
115
+ eval tag
116
+ end
117
+ # @plugins[tag].perfdata! "monitoring.#{File.basename($0)}.#{tag}", t if @plugins[tag].meta.benchmark
125
118
  exit tag
126
119
  end
127
120
 
@@ -158,12 +151,7 @@ module Naplug
158
151
 
159
152
  # @return [Array<PerformanceData>] a list of performance data objects
160
153
  def perfdata(tag = default_plugin.tag)
161
- plugin = @plugins[tag]
162
- if plugin.has_plugins?
163
- plugin.plugins.values.select { |plug| plug.perfdata }.map { |plug| plug.perfdata }
164
- else
165
- plugin.perfdata ? [plugin.perfdata] : []
166
- end
154
+ @plugins[tag].perfdata(:deep).flatten.select { |pd| pd}
167
155
  end
168
156
 
169
157
  private
@@ -188,7 +176,7 @@ module Naplug
188
176
 
189
177
  def plugins!
190
178
  self.class.plugins.each do |tag,plugin|
191
- @plugins[tag] = Plugin.new tag, plugin.block, {}
179
+ @plugins[tag] = Plugin.new tag, plugin.block, plugin.meta.to_h.merge(:meta => false)
192
180
  end
193
181
  end
194
182
 
@@ -0,0 +1,29 @@
1
+ module Naplug
2
+
3
+ module Helpers
4
+
5
+ module Grokkers
6
+
7
+ def tagmeta_grok(tagmeta)
8
+ case tagmeta.size
9
+ when 0
10
+ [:main, {}]
11
+ when 1
12
+ case tagmeta[0]
13
+ when Symbol
14
+ [tagmeta[0], {}]
15
+ when Hash
16
+ [:main,tagmeta[0]]
17
+ else
18
+ raise Naplug::Error, 'ArgumentError on Naplug#plugin'
19
+ end
20
+ when 2
21
+ raise Naplug::Error, 'ArgumentError on Naplug#plugin' unless tagmeta[0].is_a? Symbol and tagmeta[1].is_a? Hash
22
+ tagmeta[0..1]
23
+ else
24
+ raise Naplug::Error, 'ArgumentError on Naplug#plugin'
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,45 @@
1
+ require 'benchmark'
2
+
3
+ module Naplug
4
+
5
+ class Meta
6
+
7
+ DEFAULT = { :debug => false, :state => true, :description => '', :parent => nil, :benchmark => nil, :meta => true }
8
+ OPTIONS = DEFAULT.keys
9
+
10
+ def initialize(meta = DEFAULT)
11
+ validate meta
12
+ @meta = DEFAULT.merge meta
13
+ @meta[:benchmark] = Benchmark::Tms.new if @meta[:benchmark]
14
+ end
15
+
16
+ OPTIONS.each do |option|
17
+ define_method option do
18
+ @meta[option]
19
+ end
20
+ define_method "#{option}!".to_sym do |m|
21
+ @meta[option] = m
22
+ end
23
+ end
24
+
25
+ def to_h
26
+ @meta
27
+ end
28
+
29
+ private
30
+
31
+ def validate(meta)
32
+ invalid_options = meta.keys - OPTIONS
33
+ raise Naplug::Error, "invalid meta option(s): #{invalid_options.join(', ')}" if invalid_options.any?
34
+
35
+ # benchmark is allowed to be nil, false, true, or a Benchmark::Tms object
36
+ case meta[:benchmark]
37
+ when nil, true, false, Benchmark::Tms
38
+ true
39
+ else
40
+ raise Naplug::Error, "invalid benchmark metadata: #{meta[:benchmark].class.to_s}"
41
+ end
42
+ end
43
+ end
44
+
45
+ end
@@ -22,7 +22,7 @@ module Naplug
22
22
  def to_s(output = :text_output)
23
23
  case output
24
24
  when :text_output then @text_output
25
- when :long_text then @long_text.joing "\n"
25
+ when :long_text then @long_text.join "\n"
26
26
  else nil
27
27
  end
28
28
  end
@@ -1,30 +1,27 @@
1
1
  require 'ostruct'
2
2
 
3
+ require 'naplug/meta'
3
4
  require 'naplug/status'
4
5
  require 'naplug/output'
5
6
  require 'naplug/performancedata'
7
+ require 'naplug/helpers/grokkers'
6
8
 
7
9
  module Naplug
8
10
 
9
11
  class Plugin
10
12
 
11
- attr_reader :block, :plugins, :tag, :meta
13
+ include Naplug::Helpers::Grokkers
12
14
 
13
- class DuplicatePlugin < StandardError; end
14
-
15
- DEFAULT_META = { :debug => false, :enabled => true }
16
- VALID_META_OPTIONS = [ :debug, :state, :description, :parent ]
15
+ attr_reader :block, :plugins, :tag
17
16
 
18
17
  def initialize(tag, block, meta)
19
- validate_meta_options meta
20
-
21
18
  @tag = tag
22
19
  @block = block
23
20
  @plugins = Hash.new
24
21
 
25
22
  @_args = Hash.new
26
23
  @_data = OpenStruct.new :status => Status.new, :output => Output.new, :payload => nil, :perfdata => nil
27
- @_meta = OpenStruct.new DEFAULT_META.merge meta
24
+ @_meta = Meta.new meta
28
25
 
29
26
  begin
30
27
  instance_eval &block
@@ -36,29 +33,8 @@ module Naplug
36
33
 
37
34
  end
38
35
 
39
- # @return [True, False] true if this plugin is a metaplugin, false otherwise
40
- def is_meta?
41
- @_meta.status
42
- end
43
-
44
- # enable execution of the plugin; metaplugins are always enabled
45
- def enable!
46
- is_meta? ? nil : @_meta.enabled = true
47
- end
48
-
49
- # disable execution of the plugin; metaplugins cannot be disabled
50
- def disable!
51
- is_meta? ? nil : @_meta.enabled = false
52
- end
53
-
54
- # true when plugin is enabled; false otherwise
55
- def is_enabled?
56
- @_meta.enabled
57
- end
58
-
59
- # true when the plugin is disabled; false otherwise
60
- def is_disabled?
61
- not @_meta.enabled
36
+ def meta
37
+ @_meta
62
38
  end
63
39
 
64
40
  def parent
@@ -102,8 +78,13 @@ module Naplug
102
78
  end
103
79
 
104
80
  # returns the performance data of the plugin as a PerformanceData object
105
- def perfdata
106
- @_data.perfdata
81
+ def perfdata(mode = nil)
82
+ case mode
83
+ when :deep
84
+ plugins.values.map { |p| p.perfdata :deep }.push @_data.perfdata
85
+ else
86
+ @_data.perfdata
87
+ end
107
88
  end
108
89
 
109
90
  def perfdata!(label,value,f = {})
@@ -157,7 +138,7 @@ module Naplug
157
138
 
158
139
  def plugin(*tagmeta, &block)
159
140
  tag,meta = tagmeta_grok(tagmeta)
160
- raise DuplicatePlugin, "duplicate definition of #{tag}" if @plugins.key? tag
141
+ raise Naplug::Error, "duplicate definition of #{tag}" if @plugins.key? tag
161
142
  @plugins[tag] = Plugin.new tag, block, meta.merge({ :parent => self })
162
143
  self.define_singleton_method tag do
163
144
  @plugins[tag]
@@ -168,33 +149,5 @@ module Naplug
168
149
  @_meta.debug
169
150
  end
170
151
 
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
-
199
152
  end
200
153
  end
@@ -1,3 +1,3 @@
1
1
  module Naplug
2
- VERSION = '1.9.0'
2
+ VERSION = '1.10.1'
3
3
  end
metadata CHANGED
@@ -1,32 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: naplug
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0
5
- prerelease:
4
+ version: 1.10.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Gerardo López-Fernádez
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-02-27 00:00:00.000000000 Z
11
+ date: 2018-04-17 00:00:00.000000000 Z
13
12
  dependencies: []
14
- description: ! 'A Ruby library for Nagios plugins '
13
+ description: 'A Ruby library for Nagios plugins '
15
14
  email: gerir@evernote.com
16
15
  executables: []
17
16
  extensions: []
18
17
  extra_rdoc_files: []
19
18
  files:
20
- - lib/naplug/about.rb
21
- - lib/naplug/helpers/cli.rb
22
- - lib/naplug/helpers/json_thresholds.rb
23
- - lib/naplug/helpers.rb
24
- - lib/naplug/output.rb
25
- - lib/naplug/performancedata.rb
26
- - lib/naplug/plugin.rb
27
- - lib/naplug/status.rb
28
- - lib/naplug/version.rb
29
- - lib/naplug.rb
19
+ - LICENSE
20
+ - README.md
30
21
  - examples/almostalwaysok
31
22
  - examples/dupplugin
32
23
  - examples/exception
@@ -41,32 +32,40 @@ files:
41
32
  - examples/perfdata_uniplug
42
33
  - examples/status
43
34
  - examples/u_alwaysunknown
44
- - LICENSE
45
- - README.md
35
+ - lib/naplug.rb
36
+ - lib/naplug/about.rb
37
+ - lib/naplug/helpers.rb
38
+ - lib/naplug/helpers/cli.rb
39
+ - lib/naplug/helpers/grokkers.rb
40
+ - lib/naplug/helpers/json_thresholds.rb
41
+ - lib/naplug/meta.rb
42
+ - lib/naplug/output.rb
43
+ - lib/naplug/performancedata.rb
44
+ - lib/naplug/plugin.rb
45
+ - lib/naplug/status.rb
46
+ - lib/naplug/version.rb
46
47
  homepage: https://github.com/gerirgaudi/naplug
47
48
  licenses:
48
49
  - Apache License, Version 2.0
50
+ metadata: {}
49
51
  post_install_message:
50
52
  rdoc_options: []
51
53
  require_paths:
52
54
  - lib
53
55
  required_ruby_version: !ruby/object:Gem::Requirement
54
- none: false
55
56
  requirements:
56
- - - ! '>='
57
+ - - ">="
57
58
  - !ruby/object:Gem::Version
58
59
  version: '0'
59
60
  required_rubygems_version: !ruby/object:Gem::Requirement
60
- none: false
61
61
  requirements:
62
- - - ! '>='
62
+ - - ">="
63
63
  - !ruby/object:Gem::Version
64
64
  version: 1.3.5
65
65
  requirements: []
66
66
  rubyforge_project:
67
- rubygems_version: 1.8.23
67
+ rubygems_version: 2.7.6
68
68
  signing_key:
69
- specification_version: 3
69
+ specification_version: 4
70
70
  summary: A Ruby library for Nagios plugins
71
71
  test_files: []
72
- has_rdoc: