summaryrefslogtreecommitdiffstats
path: root/lib/Nagios
diff options
context:
space:
mode:
authorGavin Carr <gonzai@users.sourceforge.net>2006-09-26 04:10:37 +0000
committerGavin Carr <gonzai@users.sourceforge.net>2006-09-26 04:10:37 +0000
commit3a58d586f935a35390196bbbb65b36d5c651fe93 (patch)
tree4ed74dec16c5e8d4a223f5aa80bb4e7a55f016d2 /lib/Nagios
parente548a22a92b3aa345f4a952fddb39cc693c35a70 (diff)
downloadmonitoring-plugin-perl-3a58d586f935a35390196bbbb65b36d5c651fe93.tar.gz
Update Nagios::Plugin with NP::Function wrapper methods, and extras.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1483 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib/Nagios')
-rw-r--r--lib/Nagios/Plugin.pm386
-rw-r--r--lib/Nagios/Plugin/Functions.pm15
2 files changed, 319 insertions, 82 deletions
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm
index 5ff6709..025880d 100644
--- a/lib/Nagios/Plugin.pm
+++ b/lib/Nagios/Plugin.pm
@@ -1,17 +1,17 @@
1# This is only because Class::Struct doesn't allow subclasses 1# This is only because Class::Struct doesn't allow subclasses
2# Trick stolen from Class::DBI 2# Trick stolen from Class::DBI
3package Nagios::__::Plugin; 3###package Nagios::__::Plugin;
4 4
5use 5.008004;
6use Class::Struct; 5use Class::Struct;
7struct "Nagios::__::Plugin" => { 6struct "Nagios::__::Plugin" => {
8 perfdata => '@', 7 perfdata => '@',
9 shortname => '$', 8 shortname => '$',
10 }; 9 messages => '%',
10 };
11 11
12package Nagios::Plugin; 12package Nagios::Plugin;
13 13
14use Nagios::Plugin::Functions; 14use Nagios::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES);
15use Nagios::Plugin::Performance; 15use Nagios::Plugin::Performance;
16use Nagios::Plugin::Threshold; 16use Nagios::Plugin::Threshold;
17 17
@@ -22,149 +22,385 @@ use Carp;
22 22
23use Exporter; 23use Exporter;
24our @ISA = qw(Exporter Nagios::__::Plugin); 24our @ISA = qw(Exporter Nagios::__::Plugin);
25our @EXPORT = (@STATUS_CODES);
25our @EXPORT_OK = qw(%ERRORS); 26our @EXPORT_OK = qw(%ERRORS);
26 27
27our $VERSION = $Nagios::Plugin::Functions::VERSION; 28our $VERSION = $Nagios::Plugin::Functions::VERSION;
28 29
29sub add_perfdata { 30sub add_perfdata {
30 my ($self, %args) = @_; 31 my ($self, %args) = @_;
31 my $perf = Nagios::Plugin::Performance->new(%args); 32 my $perf = Nagios::Plugin::Performance->new(%args);
32 push @{$self->perfdata}, $perf; 33 push @{$self->perfdata}, $perf;
33} 34}
34
35sub all_perfoutput { 35sub all_perfoutput {
36 my $self = shift; 36 my $self = shift;
37 return join(" ", map {$_->perfoutput} (@{$self->perfdata})); 37 return join(" ", map {$_->perfoutput} (@{$self->perfdata}));
38} 38}
39 39
40sub set_thresholds { shift; Nagios::Plugin::Threshold->set_thresholds(@_); } 40sub set_thresholds { shift; Nagios::Plugin::Threshold->set_thresholds(@_); }
41 41
42# NP::Functions wrappers
43sub nagios_exit {
44 my $self = shift;
45 Nagios::Plugin::Functions::nagios_exit(@_, { plugin => $self });
46}
47sub nagios_die {
48 my $self = shift;
49 Nagios::Plugin::Functions::nagios_die(@_, { plugin => $self });
50}
42sub die { 51sub die {
43 my $self = shift; 52 my $self = shift;
44 Nagios::Plugin::Functions::die(@_, { plugin => $self }); 53 Nagios::Plugin::Functions::nagios_die(@_, { plugin => $self });
54}
55
56# -------------------------------------------------------------------------
57# NP::Functions::check_messages helpers and wrappers
58
59sub add_message {
60 my $self = shift;
61 my ($code, @messages) = @_;
62
63 croak "Invalid error code '$code'"
64 unless defined($ERRORS{uc $code}) || defined($STATUS_TEXT{$code});
65
66 # Store messages using strings rather than numeric codes
67 $code = $STATUS_TEXT{$code} if $STATUS_TEXT{$code};
68 $code = lc $code;
69 croak "Error code '$code' not supported by add_message"
70 if $code eq 'unknown' || $code eq 'dependent';
71
72 $self->messages($code, []) unless $self->messages($code);
73 push @{$self->messages($code)}, @messages;
45} 74}
46 75
76sub check_messages {
77 my $self = shift;
78 my %args = @_;
79
80 # Add object messages to any passed in as args
81 for my $code (qw(critical warning ok)) {
82 my $messages = $self->messages($code) || [];
83 if ($args{$code}) {
84 unless (ref $args{$code} eq 'ARRAY') {
85 if ($code eq 'ok') {
86 $args{$code} = [ $args{$code} ];
87 } else {
88 croak "Invalid argument '$code'"
89 }
90 }
91 push @{$args{$code}}, @$messages;
92 }
93 else {
94 $args{$code} = $messages;
95 }
96
97
98 Nagios::Plugin::Functions::check_messages(%args);
99}
100
101# -------------------------------------------------------------------------
102
471; 1031;
48__END__
49 104
50=head1 NAME 105#vim:et:sw=4
106
107__END__
51 108
52Nagios::Plugin - Object oriented helper routines for your Nagios plugin
53 109
54=head1 SYNOPSIS 110=head1 NAME
55 111
56 use Nagios::Plugin qw(%ERRORS); 112Nagios::Plugin - a family of perl modules to streamline writing Nagios plugins
57 $p = Nagios::Plugin->new( shortname => "PAGESIZE" );
58 113
59 $threshold = $p->set_thresholds( warning => "10:25", critical => "~:25" );
60 # Critical if outside -INF to 25, ie > 25. Warn if outside 10-25, ie < 10
61 114
62 # ... collect current metric into $value 115=head1 SYNOPSIS
63 if ($trouble_getting_metric) {
64 $p->die( return_code => $ERRORS{UNKNOWN}, message => "Could not retrieve page");
65 # Output: PAGESIZE UNKNOWN Could not retrieve page
66 # Return code: 3
67 }
68 116
69 $p->add_perfdata( label => "size", 117 # Constants OK, WARNING, CRITICAL, and UNKNOWN are exported by default
70 value => $value, 118 # See also Nagios::Plugin::Functions for a functional interface
71 uom => "kB", 119 use Nagios::Plugin;
72 threshold => $threshold, 120
121 # Constructor
122 $np = Nagios::Plugin->new; # OR
123 $np = Nagios::Plugin->new( shortname => "PAGESIZE" );
124
125 # Exit methods - nagios_exit( CODE, MESSAGE ), nagios_die( MESSAGE, [CODE])
126 $page = retrieve_page($page1)
127 or $np->nagios_exit( UNKNOWN, "Could not retrieve page" );
128 # Return code: 3; output: PAGESIZE UNKNOWN - Could not retrieve page
129 test_page($page)
130 or $np->nagios_exit( CRITICAL, "Bad page found" );
131
132 # nagios_die() is just like nagios_exit(), but return code defaults to UNKNOWN
133 $page = retrieve_page($page2)
134 or $np->nagios_die( "Could not retrieve page" );
135 # Return code: 3; output: PAGESIZE UNKNOWN - Could not retrieve page
136
137 # Threshold methods (NOT YET IMPLEMENTED - use Nagios::Plugin::Threshold for now)
138 $code = $np->check_threshold(
139 check => $value,
140 warning => $warning_threshold,
141 critical => $critical_threshold,
142 );
143 $np->nagios_exit( $code, "Threshold check failed" ) if $code != OK;
144
145 # Message methods (EXPERIMENTAL AND SUBJECT TO CHANGE) -
146 # add_message( CODE, $message ); check_messages()
147 for (@collection) {
148 if (m/Error/) {
149 $np->add_message( CRITICAL, $_ );
150 } else {
151 $np->add_message( OK, $_ );
152 }
153 }
154 ($code, $message) = $np->check_message();
155 nagios_exit( $code, $message );
156 # If any items in collection matched m/Error/, returns CRITICAL and the joined
157 # set of Error messages; otherwise returns OK and the joined set of ok messages
158
159 # Perfdata methods
160 $np->add_perfdata(
161 label => "size",
162 value => $value,
163 uom => "kB",
164 threshold => $threshold,
73 ); 165 );
74 $p->add_perfdata( label => "time", ... ); 166 $np->add_perfdata( label => "time", ... );
167 $np->nagios_exit( OK, "page size at http://... was ${value}kB" );
168 # Return code: 0; output:
169 # PAGESIZE OK - page size at http://... was 36kB | size=36kB;10:25;25: time=...
170
171 # Option handling methods (NOT YET IMPLEMENTED - use Nagios::Plugin::Getopt for now)
172
75 173
76 $p->die( return_code => $threshold->get_status($value), message => "page size at http://... was ${value}kB" );
77 # Output: PAGESIZE OK: page size at http://... was 36kB | size=36kB;10:25;25: time=...
78 # Return code: 0
79 174
80=head1 DESCRIPTION 175=head1 DESCRIPTION
81 176
82This is the place for common routines when writing Nagios plugins. The idea is to make it as 177Nagios::Plugin and its associated Nagios::Plugin::* modules are a family of perl modules
83easy as possible for developers to conform to the plugin guidelines 178to streamline writing Nagios plugins. The main end user modules are Nagios::Plugin,
179providing an object-oriented interface to the entire Nagios::Plugin::* collection, and
180Nagios::Plugin::Functions, providing a simpler functional interface to a useful subset of
181the available functionality.
182
183The purpose of the collection is to make it as simple as possible for developers to
184create plugins that conform the Nagios Plugin guidelines
84(http://nagiosplug.sourceforge.net/developer-guidelines.html). 185(http://nagiosplug.sourceforge.net/developer-guidelines.html).
85 186
86=head1 EXAMPLE SCRIPT
87 187
88"Enough talk! Show me where to start!" 188=head2 EXPORTS
89 189
90See the file 'check_stuff.pl' in the 't' directory for a complete working example of a plugin script. 190Nagios status code constants are exported by default:
91 191
92=head1 DESIGN 192 OK
193 WARNING
194 CRITICAL
195 UNKNOWN
196 DEPENDENT
93 197
94To facilitate object oriented classes, there are multiple perl modules, each reflecting a type of data 198The following variables are also exported on request:
95(ie, thresholds, ranges, performance). However, a plugin developer does not need to know about the
96different types - a "use Nagios::Plugin" should be sufficient.
97 199
98There is a Nagios::Plugin::Export. This holds all internals variables. You can specify these variables 200=over 4
99when use'ing Nagios::Plugin
100 201
101 use Nagios::Plugin qw(%ERRORS) 202=item %ERRORS
102 print $ERRORS{WARNING} # prints 1
103 203
104=head1 VERSIONING 204A hash mapping error strings ("CRITICAL", "UNKNOWN", etc.) to the corresponding
205status code.
105 206
106Only methods listed in the documentation for each module is public. 207=item %STATUS_TEXT
107 208
108These modules are experimental and so the interfaces may change up until Nagios::Plugin 209A hash mapping status code constants (OK, WARNING, CRITICAL, etc.) to the
109hits version 1.0, but every attempt will be made to make backwards compatible. 210corresponding error string ("OK", "WARNING, "CRITICAL", etc.) i.e. the reverse
211of %ERRORS.
110 212
111=head1 STARTING 213=back
214
215
216=head2 CONSTRUCTOR
217
218 Nagios::Plugin->new;
219
220 Nagios::Plugin->new( shortname => 'PAGESIZE' );
221
222Instantiates a new Nagios::Plugin object. Accepts the following named arguments:
112 223
113=over 4 224=over 4
114 225
115=item use Nagios::Plugin qw(%ERRORS) 226=item shortname
116 227
117Imports the %ERRORS hash. This is currently the only symbol that can be imported. 228The 'shortname' for this plugin, used as the first token in the plugin output
229by the various exit methods. Default: uc basename $0.
118 230
119=back 231=back
120 232
121=head1 CLASS METHODS 233
234=head2 EXIT METHODS
122 235
123=over 4 236=over 4
124 237
125=item Nagios::Plugin->new( shortname => $$ ) 238=item nagios_exit( <CODE>, $message )
239
240Exit with return code CODE, and a standard nagios message of the
241form "SHORTNAME CODE - $message".
242
243=item nagios_die( $message, [<CODE>] )
244
245Same as nagios_exit(), except that CODE is optional, defaulting
246to UNKNOWN.
247
248=item die( $message, [<CODE>] )
249
250Alias for nagios_die(). Deprecated.
126 251
127Initializes a new Nagios::Plugin object. Can specify the shortname here. 252=back
253
254
255=head2 THRESHOLD METHODS
256
257NOT YET IMPLEMENTED - use Nagios::Plugin::Threshold directly for now.
258
259=over 4
260
261=item check_threshold( check => $value, warning => $warn, critical => $crit )
128 262
129=back 263=back
130 264
131=head1 OBJECT METHODS 265
266=head2 MESSAGE METHODS
267
268EXPERIMENTAL AND SUBJECT TO CHANGE
269
270add_messages and check_messages are higher-level convenience methods to add
271and then check a set of messages, returning an appropriate return code and/or
272result message.
132 273
133=over 4 274=over 4
134 275
135=item set_thresholds( warning => "10:25", critical => "~:25" ) 276=item add_message( <CODE>, $message )
136 277
137Sets the thresholds, based on the range specification at 278Add a message with CODE status to the object. May be called multiple times. The messages
138http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT. 279added are checked by check_messages, following.
139Returns a Nagios::Plugin::Threshold object, which can be used to input a value to see which threshold 280
140is crossed. 281Only CRITICAL, WARNING, and OK are accepted as valid codes.
141 282
142=item add_perfdata( label => "size", value => $value, uom => "kB", threshold => $threshold )
143 283
144Adds to an array a Nagios::Plugin::Performance object with the specified values. 284=item check_messages()
145 285
146This needs to be done separately to allow multiple perfdata output. 286Check the current set of messages and return an appropriate nagios return code and/or a
287result message. In scalar context, returns only a return code; in list context returns
288both a return code and an output message, suitable for passing directly to nagios_exit()
289e.g.
147 290
148=item die( return_code => $ERRORS{CRITICAL}, message => "Output" ) 291 $code = $np->check_messages;
292 ($code, $message) = $np->check_messages;
149 293
150Exits the plugin and prints to STDOUT a Nagios plugin compatible output. Exits with the specified 294check_messages returns CRITICAL if any critical messages are found, WARNING if any
151return code. Also prints performance data if any have been added in. 295warning messages are found, and OK otherwise. The message returned in list context defaults
296to the joined set of error messages; this may be customised using the arguments below.
297
298check_messages accepts the following named arguments (none are required):
299
300=over 4
301
302=item join => SCALAR
303
304A string used to join the relevant array to generate the message
305string returned in list context i.e. if the 'critical' array @crit
306is non-empty, check_messages would return:
307
308 join( $join, @crit )
309
310as the result message. Default: ' ' (space).
311
312=item join_all => SCALAR
313
314By default, only one set of messages are joined and returned in the
315result message i.e. if the result is CRITICAL, only the 'critical'
316messages are included in the result; if WARNING, only the 'warning'
317messages are included; if OK, the 'ok' messages are included (if
318supplied) i.e. the default is to return an 'errors-only' type
319message.
320
321If join_all is supplied, however, it will be used as a string to
322join the resultant critical, warning, and ok messages together i.e.
323all messages are joined and returned.
324
325=item critical => ARRAYREF
326
327Additional critical messages to supplement any passed in via add_message().
328
329=item warning => ARRAYREF
330
331Additional warning messages to supplement any passed in via add_message().
332
333=item ok => ARRAYREF | SCALAR
334
335Additional ok messages to supplement any passed in via add_message().
336
337=back
152 338
153=back 339=back
154 340
341
342=head2 PERFORMANCE DATA METHODS
343
344=over 4
345
346=item add_perfdata( label => "size", value => $value, uom => "kB", threshold => $threshold )
347
348Add a set of performance data to the object. May be called multiple times. The performance
349data is included in the standard plugin output messages by the various exit methods.
350
351See the Nagios::Plugin::Performance documentation for more information on performance data
352and the various field definitions, as well as the relevant section of the Nagios Plugin
353guidelines (http://nagiosplug.sourceforge.net/developer-guidelines.html#AEN202).
354
355=back
356
357
358=head2 OPTION HANDLING METHODS
359
360NOT YET IMPLEMENTED - use Nagios::Plugin::Getopt directly for now.
361
362
363=head1 EXAMPLES
364
365"Enough talk! Show me some examples!"
366
367See the file 'check_stuff.pl' in the 't' directory for a complete working example of a
368plugin script.
369
370
371=head1 VERSIONING
372
373The Nagios::Plugin::* modules are currently experimental and so the interfaces may
374change up until Nagios::Plugin hits version 1.0, although every attempt will be made to
375keep them as backwards compatible as possible.
376
377
155=head1 SEE ALSO 378=head1 SEE ALSO
156 379
157http://nagiosplug.sourceforge.net 380See Nagios::Plugin::Functions for a simple functional interface to a subset
381of the available Nagios::Plugin functionality.
382
383See also Nagios::Plugin::Getopt, Nagios::Plugin::Range, Nagios::Plugin::Performance,
384Nagios::Plugin::Range, and Nagios::Plugin::Threshold.
385
386The Nagios Plugin project page is at http://nagiosplug.sourceforge.net.
387
388
389=head1 BUGS
390
391Please report bugs in these modules to the Nagios Plugin development team:
392nagiosplug-devel@lists.sourceforge.net.
393
158 394
159=head1 AUTHOR 395=head1 AUTHOR
160 396
161Maintained by the Nagios Plugin development team - http://nagiosplug.sourceforge.net 397Maintained by the Nagios Plugin development team - http://nagiosplug.sourceforge.net.
162 398
163Originally by Ton Voon, E<lt>ton.voon@altinity.comE<gt> 399Originally by Ton Voon, E<lt>ton.voon@altinity.comE<gt>.
164 400
165Nathan Vonnahme added extra tests and subsequent fixes. 401Nathan Vonnahme added extra tests and subsequent fixes.
166 402
167Gavin Carr contributed the Nagios::Plugin::GetOpt module. 403
168 404
169=head1 COPYRIGHT AND LICENSE 405=head1 COPYRIGHT AND LICENSE
170 406
diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm
index 9c20288..e77bc4f 100644
--- a/lib/Nagios/Plugin/Functions.pm
+++ b/lib/Nagios/Plugin/Functions.pm
@@ -14,7 +14,7 @@ our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
14require Exporter; 14require Exporter;
15our @ISA = qw(Exporter); 15our @ISA = qw(Exporter);
16our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); 16our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages));
17our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT); 17our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES);
18our %EXPORT_TAGS = ( 18our %EXPORT_TAGS = (
19 all => [ @EXPORT, @EXPORT_OK ], 19 all => [ @EXPORT, @EXPORT_OK ],
20 codes => [ @STATUS_CODES ], 20 codes => [ @STATUS_CODES ],
@@ -28,7 +28,7 @@ use constant UNKNOWN => 3;
28use constant DEPENDENT => 4; 28use constant DEPENDENT => 4;
29 29
30our %ERRORS = ( 30our %ERRORS = (
31 'OK' => OK, 31 'OK' => OK,
32 'WARNING' => WARNING, 32 'WARNING' => WARNING,
33 'CRITICAL' => CRITICAL, 33 'CRITICAL' => CRITICAL,
34 'UNKNOWN' => UNKNOWN, 34 'UNKNOWN' => UNKNOWN,
@@ -84,7 +84,8 @@ sub nagios_exit {
84 $output = "$shortname $output" if $shortname; 84 $output = "$shortname $output" if $shortname;
85 if ($arg->{plugin}) { 85 if ($arg->{plugin}) {
86 my $plugin = $arg->{plugin}; 86 my $plugin = $arg->{plugin};
87 $output .= " | ". $plugin->all_perfoutput if $plugin->perfdata; 87 $output .= " | ". $plugin->all_perfoutput
88 if $plugin->perfdata && $plugin->all_perfoutput;
88 } 89 }
89 $output .= "\n"; 90 $output .= "\n";
90 91
@@ -193,7 +194,7 @@ Nagios plugins.
193 # Constants OK, WARNING, CRITICAL, and UNKNOWN exported by default 194 # Constants OK, WARNING, CRITICAL, and UNKNOWN exported by default
194 use Nagios::Plugin::Functions; 195 use Nagios::Plugin::Functions;
195 196
196 # nagios_exit( ODE, $message ) - exit with error code CODE, 197 # nagios_exit( CODE, $message ) - exit with error code CODE,
197 # and message "PLUGIN CODE - $message" 198 # and message "PLUGIN CODE - $message"
198 nagios_exit( CRITICAL, $critical_error ) if $critical_error; 199 nagios_exit( CRITICAL, $critical_error ) if $critical_error;
199 nagios_exit( WARNING, $warning_error ) if $warning_error; 200 nagios_exit( WARNING, $warning_error ) if $warning_error;
@@ -223,7 +224,7 @@ Nagios::Plugin. It is intended for those who prefer a simpler
223functional interface, and who do not need the additional 224functional interface, and who do not need the additional
224functionality of Nagios::Plugin. 225functionality of Nagios::Plugin.
225 226
226=head2 Exports 227=head2 EXPORTS
227 228
228Nagios status code constants are exported by default: 229Nagios status code constants are exported by default:
229 230
@@ -245,13 +246,13 @@ The following variables are exported only on request:
245 %STATUS_TEXT 246 %STATUS_TEXT
246 247
247 248
248=head2 Functions 249=head2 FUNCTIONS
249 250
250The following functions are supported: 251The following functions are supported:
251 252
252=over 4 253=over 4
253 254
254=item nagios_exit( CODE, $message ) 255=item nagios_exit( <CODE>, $message )
255 256
256Exit with return code CODE, and a standard nagios message of the 257Exit with return code CODE, and a standard nagios message of the
257form "PLUGIN CODE - $message". 258form "PLUGIN CODE - $message".