naplug 1.6.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/lib/naplug.rb ADDED
@@ -0,0 +1,163 @@
1
+ require 'rubygems'
2
+ require 'naplug/plugin'
3
+
4
+ module Naplug
5
+
6
+ class Error < StandardError; end
7
+
8
+ module ClassMethods
9
+
10
+ attr_reader :plugins
11
+
12
+ def plugin(tag = :main, &block)
13
+ @plugins = Hash.new unless @plugins
14
+ @plugins[tag] = create_metaplugin tag, block
15
+ end
16
+
17
+ def tags
18
+ self.plugins.keys
19
+ end
20
+
21
+ private
22
+
23
+ def create_metaplugin(tag,block)
24
+ module_eval do
25
+ define_method "#{tag}".to_sym do; plugin; end # <tag> methods for quick access to plugins
26
+ define_method "#{tag}!".to_sym do; self.exec! tag; end # <tag>! methods to involke exec! on a given plugin
27
+ end
28
+ Plugin.new tag, :meta, block
29
+ end
30
+
31
+ end
32
+
33
+ module InstanceMethods
34
+
35
+ attr_reader :plugins
36
+
37
+ def initialize(args = {})
38
+ @plugins = Hash.new
39
+ plugins!
40
+
41
+ @_args = Hash.new
42
+ args! args
43
+ end
44
+
45
+ def args
46
+ @_args
47
+ end
48
+
49
+ def args!(args)
50
+ @_args.merge! args
51
+ @plugins.each do |tag,plugin|
52
+ plugin_args = args.key?(tag) ? args[tag] : {}
53
+ shared_args = args.select { |t,a| not @plugins.keys.include? t }
54
+ plugin.args! shared_args.merge! plugin_args
55
+ end
56
+ end
57
+
58
+ def to_str(tag = default_plugin.tag)
59
+ s_format = perfdata(tag) ? '%s: %s | %s' : '%s: %s'
60
+ s_array = perfdata(tag) ? [@plugins[tag].status,@plugins[tag].output,perfdata(tag)] : [@plugins[tag].status,@plugins[tag].output]
61
+ s_format % s_array
62
+ end
63
+
64
+ def exec!(tag = default_plugin.tag)
65
+ exec tag
66
+ eval tag
67
+ exit tag
68
+ end
69
+
70
+ def exec(tag = default_plugin.tag)
71
+ rexec @plugins[tag]
72
+ end
73
+
74
+ def eval(tag = default_plugin.tag)
75
+ @plugins[tag].eval
76
+ end
77
+
78
+ def eject!(payload = nil)
79
+ o = case payload
80
+ when String then payload
81
+ when Exception then "#{payload.backtrace[1][/.+:\d+/]}: #{payload.message}"
82
+ else nil
83
+ caller[0][/.+:\d+/]
84
+ end
85
+ print "UNKNOWN: plugin eject! in %s\n" % [o]
86
+ Kernel::exit 3
87
+ end
88
+
89
+ private
90
+
91
+ def rexec(plug)
92
+ if plug.has_plugins?
93
+ plug.plugins.each_value { |p| rexec p }
94
+ else
95
+ plexec plug
96
+ end
97
+ end
98
+
99
+ def plexec(p)
100
+ begin
101
+ @_running = p.tag
102
+ instance_exec p, &p.block
103
+ @_running = nil
104
+ rescue Naplug::Error => e
105
+ p.status.unknown!
106
+ p.output! "#{e.backtrace[1][/[^\/]+:\d+/]}: #{e.message}"
107
+ rescue => e
108
+ p.status.unknown!
109
+ p.output! "#{e.backtrace[0][/[^\/]+:\d+/]}: #{e.message}"
110
+ p.payload! e
111
+ ensure
112
+ @_runinng = nil
113
+ end
114
+ end
115
+
116
+ def plugins!
117
+ self.class.plugins.each do |tag,plugin|
118
+ @plugins[tag] = Plugin.new tag, plugin.block
119
+ end
120
+ end
121
+
122
+ def default_plugin
123
+ return @plugins[:main] if @plugins.key? :main
124
+ return @plugins[@plugins.keys[0]] if @plugins.size == 1
125
+ raise Naplug::Error, 'unable to determine default plugin'
126
+ end
127
+
128
+ def perfdata(tag = default_plugin.tag)
129
+ plugin = @plugins[tag]
130
+ if plugin.has_plugins?
131
+ plugin.plugins.values.map { |plug| plug.perfdata }.join(' ').gsub(/^\s+$/,'').strip!
132
+ else
133
+ plugin.perfdata
134
+ end
135
+ end
136
+
137
+ def exit(tag = default_plugin.tag)
138
+ print "%s\n" % [to_str(tag)]
139
+ Kernel::exit @plugins[tag].status.to_i
140
+ end
141
+
142
+ def method_missing(method, *args, &block)
143
+ message = "undefined instance variable or method #{method}"
144
+ case @_runinng
145
+ when nil?
146
+ begin; raise Naplug::Error, message; rescue => e; eject! e ; end
147
+ else
148
+ raise Naplug::Error, message
149
+ end
150
+ end
151
+
152
+ def respond_to_missing?(method, *)
153
+ @plugins.keys? method || super
154
+ end
155
+
156
+ end
157
+
158
+ def self.included(klass)
159
+ klass.send :include, InstanceMethods
160
+ klass.extend ClassMethods
161
+ end
162
+
163
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: naplug
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.6.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gerardo López-Fernádez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-24 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'A Ruby library for Nagios plugins '
15
+ email: gerir@evernote.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/naplug/about.rb
21
+ - lib/naplug/helpers.rb
22
+ - lib/naplug/output.rb
23
+ - lib/naplug/performancedata.rb
24
+ - lib/naplug/plugin.rb
25
+ - lib/naplug/status.rb
26
+ - lib/naplug/version.rb
27
+ - lib/naplug.rb
28
+ - examples/almostalwaysok
29
+ - examples/alwaysok
30
+ - examples/alwaysunknown
31
+ - examples/dupplugin
32
+ - examples/exception
33
+ - examples/exception+
34
+ - examples/markerfile
35
+ - examples/markerfile++
36
+ - examples/multiplug
37
+ - examples/multiplugin
38
+ - examples/multiplugins
39
+ - examples/status
40
+ - LICENSE
41
+ - README.md
42
+ homepage: https://github.com/gerirgaudi/naplug
43
+ licenses:
44
+ - Apache License, Version 2.0
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: 1.3.5
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.23
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: A Ruby library for Nagios plugins
67
+ test_files: []