summaryrefslogtreecommitdiffstats
path: root/lib/Nagios/Plugin/Functions.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Nagios/Plugin/Functions.pm')
-rw-r--r--lib/Nagios/Plugin/Functions.pm344
1 files changed, 344 insertions, 0 deletions
diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm
new file mode 100644
index 0000000..9c20288
--- /dev/null
+++ b/lib/Nagios/Plugin/Functions.pm
@@ -0,0 +1,344 @@
1# This module holds all exported variables
2# and base functions
3package Nagios::Plugin::Functions;
4
5use strict;
6use warnings;
7use File::Basename;
8use Params::Validate qw(validate :types);
9
10our $VERSION = "0.13";
11
12our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
13
14require Exporter;
15our @ISA = qw(Exporter);
16our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages));
17our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT);
18our %EXPORT_TAGS = (
19 all => [ @EXPORT, @EXPORT_OK ],
20 codes => [ @STATUS_CODES ],
21 functions => [ qw(nagios_exit nagios_die check_messages) ],
22);
23
24use constant OK => 0;
25use constant WARNING => 1;
26use constant CRITICAL => 2;
27use constant UNKNOWN => 3;
28use constant DEPENDENT => 4;
29
30our %ERRORS = (
31 'OK' => OK,
32 'WARNING' => WARNING,
33 'CRITICAL' => CRITICAL,
34 'UNKNOWN' => UNKNOWN,
35 'DEPENDENT' => DEPENDENT,
36);
37
38our %STATUS_TEXT = reverse %ERRORS;
39
40# _fake_exit flag and accessor/mutator, for testing
41my $_fake_exit = 0;
42sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit };
43
44sub get_shortname {
45 my %arg = @_;
46
47 return $arg{plugin}->shortname if $arg{plugin};
48
49 my $shortname = uc basename($ENV{NAGIOS_PLUGIN} || $0);
50 $shortname =~ s/^CHECK_//;
51 return $shortname;
52}
53
54# nagios_exit( $code, $message )
55sub nagios_exit {
56 my ($code, $message, $arg) = @_;
57
58 # Handle named parameters
59 if (defined $code && ($code eq 'return_code' || $code eq 'message')) {
60 # Remove last argument if odd no and last is ref
61 if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) {
62 $arg = pop @_;
63 } else {
64 undef $arg;
65 }
66 my %arg = @_;
67 $code = $arg{return_code};
68 $message = $arg{message};
69 }
70 $arg ||= {};
71
72 # Handle string codes
73 $code = $ERRORS{$code} if defined $code && exists $ERRORS{$code};
74
75 # Set defaults
76 $code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code};
77 $message = '' unless defined $message;
78 $message = join(' ', @$message) if ref $message eq 'ARRAY';
79
80 # Setup output
81 my $output = "$STATUS_TEXT{$code}";
82 $output .= " - $message" if defined $message && $message ne '';
83 my $shortname = get_shortname(plugin => $arg->{plugin});
84 $output = "$shortname $output" if $shortname;
85 if ($arg->{plugin}) {
86 my $plugin = $arg->{plugin};
87 $output .= " | ". $plugin->all_perfoutput if $plugin->perfdata;
88 }
89 $output .= "\n";
90
91 # Don't actually exit if _fake_exit set
92 if ($_fake_exit) {
93 require Nagios::Plugin::ExitResult;
94 return Nagios::Plugin::ExitResult->new($code, $output);
95 }
96
97 # Print output and exit
98 print $output;
99 exit $code;
100}
101
102# nagios_die( $message, [ $code ]) OR nagios_die( $code, $message )
103# Default $code: UNKNOWN
104sub nagios_die {
105 my ($arg1, $arg2, $rest) = @_;
106
107 # Named parameters
108 if (defined $arg1 && ($arg1 eq 'return_code' || $arg1 eq 'message')) {
109 return nagios_exit(@_);
110 }
111
112 # ($code, $message)
113 elsif (defined $arg1 && (exists $ERRORS{$arg1} || exists $STATUS_TEXT{$arg1})) {
114 return nagios_exit(@_);
115 }
116
117 # ($message, $code)
118 elsif (defined $arg2 && (exists $ERRORS{$arg2} || exists $STATUS_TEXT{$arg2})) {
119 return nagios_exit($arg2, $arg1, $rest);
120 }
121
122 # Else just assume $arg1 is the message and hope for the best
123 else {
124 return nagios_exit( UNKNOWN, $arg1, $rest );
125 }
126}
127
128# For backwards compatibility
129sub die { nagios_die(@_); }
130
131
132# ------------------------------------------------------------------------
133# check_messages - return a status and/or message based on a set of
134# message arrays.
135# Returns a nagios status code in scalar context.
136# Returns a code and a message in list context.
137# The message is join($join, @array) for the relevant array for the code,
138# or join($join_all, $message) for all arrays if $join_all is set.
139sub check_messages {
140 my %arg = validate( @_, {
141 critical => { type => ARRAYREF },
142 warning => { type => ARRAYREF },
143 ok => { type => ARRAYREF | SCALAR, optional => 1 },
144 'join' => { default => ' ' },
145 join_all => 0,
146 });
147 $arg{join} = ' ' unless defined $arg{join};
148
149 # Decide $code
150 my $code = OK;
151 $code ||= CRITICAL if @{$arg{critical}};
152 $code ||= WARNING if @{$arg{warning}};
153 return $code unless wantarray;
154
155 # Compose message
156 my $message = '';
157 if ($arg{join_all}) {
158 $message = join( $arg{join_all},
159 map { @$_ ? join( $arg{'join'}, @$_) : () }
160 $arg{critical},
161 $arg{warning},
162 $arg{ok} ? (ref $arg{ok} ? $arg{ok} : [ $arg{ok} ]) : []
163 );
164 }
165
166 else {
167 $message ||= join( $arg{'join'}, @{$arg{critical}} )
168 if $code == CRITICAL;
169 $message ||= join( $arg{'join'}, @{$arg{warning}} )
170 if $code == WARNING;
171 $message ||= ref $arg{ok} ? join( $arg{'join'}, @{$arg{ok}} ) : $arg{ok}
172 if $arg{ok};
173 }
174
175 return ($code, $message);
176}
177
178# ------------------------------------------------------------------------
179
1801;
181
182# vim:sw=4:sm:et
183
184__END__
185
186=head1 NAME
187
188Nagios::Plugin::Functions - functions to simplify the creation of
189Nagios plugins.
190
191=head1 SYNOPSIS
192
193 # Constants OK, WARNING, CRITICAL, and UNKNOWN exported by default
194 use Nagios::Plugin::Functions;
195
196 # nagios_exit( ODE, $message ) - exit with error code CODE,
197 # and message "PLUGIN CODE - $message"
198 nagios_exit( CRITICAL, $critical_error ) if $critical_error;
199 nagios_exit( WARNING, $warning_error ) if $warning_error;
200 nagios_exit( OK, $result );
201
202 # nagios_die( $message, [$CODE] ) - just like nagios_exit(),
203 # but CODE is optional, defaulting to UNKNOWN
204 do_something()
205 or nagios_die("do_something() failed horribly");
206 do_something_critical()
207 or nagios_die("do_something_critical() failed", CRITICAL);
208
209 # check_messages - check a set of message arrays, returning a
210 # CODE and/or a result message
211 $code = check_messages(critical => \@crit, warning => \@warn);
212 ($code, $message) = check_messages(
213 critical => \@crit, warning => \@warn,
214 ok => \@ok );
215
216
217=head1 DESCRIPTION
218
219This module is part of the Nagios::Plugin family, a set of modules
220for simplifying the creation of Nagios plugins. This module exports
221convenience functions for the class methods provided by
222Nagios::Plugin. It is intended for those who prefer a simpler
223functional interface, and who do not need the additional
224functionality of Nagios::Plugin.
225
226=head2 Exports
227
228Nagios status code constants are exported by default:
229
230 OK
231 WARNING
232 CRITICAL
233 UNKNOWN
234 DEPENDENT
235
236as are the following functions:
237
238 nagios_exit
239 nagios_die
240 check_messages
241
242The following variables are exported only on request:
243
244 %ERRORS
245 %STATUS_TEXT
246
247
248=head2 Functions
249
250The following functions are supported:
251
252=over 4
253
254=item nagios_exit( CODE, $message )
255
256Exit with return code CODE, and a standard nagios message of the
257form "PLUGIN CODE - $message".
258
259=item nagios_die( $message, [CODE] )
260
261Same as nagios_exit(), except that CODE is optional, defaulting
262to UNKNOWN.
263
264=item check_messages( critical => \@crit, warning => \@warn )
265
266Convenience function to check a set of message arrays and return
267an appropriate nagios return code and/or a result message. Returns
268only a return code in scalar context; returns a return code and an
269error message in list context i.e.
270
271 # Scalar context
272 $code = check_messages(critical => \@crit, warning => \@warn);
273 # List context
274 ($code, $msg) = check_messages(critical => \@crit, warning => \@warn);
275
276check_messages() accepts the following named arguments:
277
278=over 4
279
280=item critical => ARRAYREF
281
282An arrayref of critical error messages - check_messages() returns
283CRITICAL if this arrayref is non-empty. Mandatory.
284
285=item warning => ARRAYREF
286
287An arrayref of warning error messages - check_messages() returns
288WARNING if this arrayref is non-empty ('critical' is checked
289first). Mandatory.
290
291=item ok => ARRAYREF | SCALAR
292
293An arrayref of informational messages (or a single scalar message),
294used in list context if both the 'critical' and 'warning' arrayrefs
295are empty. Optional.
296
297=item join => SCALAR
298
299A string used to join the relevant array to generate the message
300string returned in list context i.e. if the 'critical' array @crit
301is non-empty, check_messages would return:
302
303 join( $join, @crit )
304
305as the result message. Optional; default: ' ' (space).
306
307=item join_all => SCALAR
308
309By default, only one set of messages are joined and returned in the
310result message i.e. if the result is CRITICAL, only the 'critical'
311messages are included in the result; if WARNING, only the 'warning'
312messages are included; if OK, the 'ok' messages are included (if
313supplied) i.e. the default is to return an 'errors-only' type
314message.
315
316If join_all is supplied, however, it will be used as a string to
317join the resultant critical, warning, and ok messages together i.e.
318all messages are joined and returned.
319
320=back
321
322=back
323
324
325=head1 SEE ALSO
326
327Nagios::Plugin; the nagios plugin developer guidelines at
328http://nagiosplug.sourceforge.net/developer-guidelines.html.
329
330
331=head1 AUTHORS
332
333Ton Voon, E<lt>ton.voon@altinity.comE<gt>
334
335
336=head1 COPYRIGHT AND LICENSE
337
338Copyright (C) 2006 by Nagios Plugin Development Team
339
340This library is free software; you can redistribute it and/or modify
341it under the same terms as Perl itself, either Perl version 5.8.4 or,
342at your option, any later version of Perl 5 you may have available.
343
344=cut