1
# Functional interface to basic Nagios::Plugin constants, exports,
4
package Nagios::Plugin::Functions;
11
use Params::Validate qw(:types validate);
12
use Math::Calc::Units;
14
# Remember to update Nagios::Plugins as well
15
our $VERSION = "0.34";
17
our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
20
our @ISA = qw(Exporter);
21
our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages));
22
our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state max_state_alt convert $value_re);
24
all => [ @EXPORT, @EXPORT_OK ],
25
codes => [ @STATUS_CODES ],
26
functions => [ qw(nagios_exit nagios_die check_messages max_state max_state_alt convert) ],
30
use constant WARNING => 1;
31
use constant CRITICAL => 2;
32
use constant UNKNOWN => 3;
33
use constant DEPENDENT => 4;
38
'CRITICAL' => CRITICAL,
40
'DEPENDENT' => DEPENDENT,
43
our %STATUS_TEXT = reverse %ERRORS;
45
my $value = qr/[-+]?[\d\.]+/;
46
our $value_re = qr/$value(?:e$value)?/;
48
# _fake_exit flag and accessor/mutator, for testing
50
sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit };
52
# _use_die flag and accessor/mutator, so exceptions can be raised correctly
54
sub _use_die { @_ ? $_use_die = shift : $_use_die };
59
my $shortname = undef;
61
return $arg->{shortname} if (defined($arg->{shortname}));
62
$shortname = $arg->{plugin} if (defined( $arg->{plugin}));
64
$shortname = uc basename($shortname || $ENV{NAGIOS_PLUGIN} || $0);
65
$shortname =~ s/^CHECK_(?:BY_)?//; # Remove any leading CHECK_[BY_]
66
$shortname =~ s/\..*$//; # Remove any trailing suffix
71
return CRITICAL if grep { $_ == CRITICAL } @_;
72
return WARNING if grep { $_ == WARNING } @_;
73
return OK if grep { $_ == OK } @_;
74
return UNKNOWN if grep { $_ == UNKNOWN } @_;
75
return DEPENDENT if grep { $_ == DEPENDENT } @_;
80
return CRITICAL if grep { $_ == CRITICAL } @_;
81
return WARNING if grep { $_ == WARNING } @_;
82
return UNKNOWN if grep { $_ == UNKNOWN } @_;
83
return DEPENDENT if grep { $_ == DEPENDENT } @_;
84
return OK if grep { $_ == OK } @_;
88
# nagios_exit( $code, $message )
90
my ($code, $message, $arg) = @_;
92
# Handle named parameters
93
if (defined $code && ($code eq 'return_code' || $code eq 'message')) {
94
# Remove last argument if odd no and last is ref
95
if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) {
101
$code = $arg{return_code};
102
$message = $arg{message};
106
# Handle string codes
107
$code = $ERRORS{$code} if defined $code && exists $ERRORS{$code};
110
$code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code};
111
$message = '' unless defined $message;
112
if (ref $message && ref $message eq 'ARRAY') {
113
$message = join(' ', map { chomp; $_ } @$message);
120
my $output = "$STATUS_TEXT{$code}";
121
$output .= " - $message" if defined $message && $message ne '';
122
my $shortname = ($arg->{plugin} ? $arg->{plugin}->shortname : undef);
123
$shortname ||= get_shortname(); # Should happen only if funnctions are called directly
124
$output = "$shortname $output" if $shortname;
125
if ($arg->{plugin}) {
126
my $plugin = $arg->{plugin};
127
$output .= " | ". $plugin->all_perfoutput
128
if $plugin->perfdata && $plugin->all_perfoutput;
132
# Don't actually exit if _fake_exit set
134
require Nagios::Plugin::ExitResult;
135
return Nagios::Plugin::ExitResult->new($code, $output);
138
_nagios_exit($code, $output);
142
my ($code, $output) = @_;
143
# Print output and exit; die if flag set and called via a die in stack backtrace
145
for (my $i = 0;; $i++) {
148
if ($_[3] =~ m/die/) {
158
# nagios_die( $message, [ $code ]) OR nagios_die( $code, $message )
159
# Default $code: UNKNOWN
161
my ($arg1, $arg2, $rest) = @_;
164
if (defined $arg1 && ($arg1 eq 'return_code' || $arg1 eq 'message')) {
165
return nagios_exit(@_);
169
elsif (defined $arg1 && (exists $ERRORS{$arg1} || exists $STATUS_TEXT{$arg1})) {
170
return nagios_exit(@_);
174
elsif (defined $arg2 && (exists $ERRORS{$arg2} || exists $STATUS_TEXT{$arg2})) {
175
return nagios_exit($arg2, $arg1, $rest);
178
# Else just assume $arg1 is the message and hope for the best
180
return nagios_exit( UNKNOWN, $arg1, $arg2 );
184
# For backwards compatibility
185
sub die { nagios_die(@_); }
188
# ------------------------------------------------------------------------
191
# Simple wrapper around Math::Calc::Units::convert
194
my ($value, $from, $to) = @_;
195
my ($newval) = Math::Calc::Units::convert("$value $from", $to, 'exact');
199
# ------------------------------------------------------------------------
200
# check_messages - return a status and/or message based on a set of
202
# Returns a nagios status code in scalar context.
203
# Returns a code and a message in list context.
204
# The message is join($join, @array) for the relevant array for the code,
205
# or join($join_all, $message) for all arrays if $join_all is set.
207
my %arg = validate( @_, {
208
critical => { type => ARRAYREF },
209
warning => { type => ARRAYREF },
210
ok => { type => ARRAYREF | SCALAR, optional => 1 },
211
'join' => { default => ' ' },
214
$arg{join} = ' ' unless defined $arg{join};
218
$code ||= CRITICAL if @{$arg{critical}};
219
$code ||= WARNING if @{$arg{warning}};
220
return $code unless wantarray;
224
if ($arg{join_all}) {
225
$message = join( $arg{join_all},
226
map { @$_ ? join( $arg{'join'}, @$_) : () }
229
$arg{ok} ? (ref $arg{ok} ? $arg{ok} : [ $arg{ok} ]) : []
234
$message ||= join( $arg{'join'}, @{$arg{critical}} )
235
if $code == CRITICAL;
236
$message ||= join( $arg{'join'}, @{$arg{warning}} )
238
$message ||= ref $arg{ok} ? join( $arg{'join'}, @{$arg{ok}} ) : $arg{ok}
242
return ($code, $message);
245
# ------------------------------------------------------------------------
255
Nagios::Plugin::Functions - functions to simplify the creation of
260
# Constants OK, WARNING, CRITICAL, and UNKNOWN exported by default
261
use Nagios::Plugin::Functions;
263
# nagios_exit( CODE, $message ) - exit with error code CODE,
264
# and message "PLUGIN CODE - $message"
265
nagios_exit( CRITICAL, $critical_error ) if $critical_error;
266
nagios_exit( WARNING, $warning_error ) if $warning_error;
267
nagios_exit( OK, $result );
269
# nagios_die( $message, [$CODE] ) - just like nagios_exit(),
270
# but CODE is optional, defaulting to UNKNOWN
272
or nagios_die("do_something() failed horribly");
273
do_something_critical()
274
or nagios_die("do_something_critical() failed", CRITICAL);
276
# check_messages - check a set of message arrays, returning a
277
# CODE and/or a result message
278
$code = check_messages(critical => \@crit, warning => \@warn);
279
($code, $message) = check_messages(
280
critical => \@crit, warning => \@warn,
283
# get_shortname - return the default short name for this plugin
284
# (as used by nagios_exit/die; not exported by default)
285
$shortname = get_shortname();
290
This module is part of the Nagios::Plugin family, a set of modules
291
for simplifying the creation of Nagios plugins. This module exports
292
convenience functions for the class methods provided by
293
Nagios::Plugin. It is intended for those who prefer a simpler
294
functional interface, and who do not need the additional
295
functionality of Nagios::Plugin.
299
Nagios status code constants are exported by default:
307
as are the following functions:
313
The following variables and functions are exported only on request:
324
The following functions are supported:
328
=item nagios_exit( <CODE>, $message )
330
Exit with return code CODE, and a standard nagios message of the
331
form "PLUGIN CODE - $message".
333
=item nagios_die( $message, [CODE] )
335
Same as nagios_exit(), except that CODE is optional, defaulting
336
to UNKNOWN. NOTE: exceptions are not raised by default to calling code.
337
Set C<$_use_die> flag if this functionality is required (see test code).
339
=item check_messages( critical => \@crit, warning => \@warn )
341
Convenience function to check a set of message arrays and return
342
an appropriate nagios return code and/or a result message. Returns
343
only a return code in scalar context; returns a return code and an
344
error message in list context i.e.
347
$code = check_messages(critical => \@crit, warning => \@warn);
349
($code, $msg) = check_messages(critical => \@crit, warning => \@warn);
351
check_messages() accepts the following named arguments:
355
=item critical => ARRAYREF
357
An arrayref of critical error messages - check_messages() returns
358
CRITICAL if this arrayref is non-empty. Mandatory.
360
=item warning => ARRAYREF
362
An arrayref of warning error messages - check_messages() returns
363
WARNING if this arrayref is non-empty ('critical' is checked
366
=item ok => ARRAYREF | SCALAR
368
An arrayref of informational messages (or a single scalar message),
369
used in list context if both the 'critical' and 'warning' arrayrefs
374
A string used to join the relevant array to generate the message
375
string returned in list context i.e. if the 'critical' array @crit
376
is non-empty, check_messages would return:
380
as the result message. Optional; default: ' ' (space).
382
=item join_all => SCALAR
384
By default, only one set of messages are joined and returned in the
385
result message i.e. if the result is CRITICAL, only the 'critical'
386
messages are included in the result; if WARNING, only the 'warning'
387
messages are included; if OK, the 'ok' messages are included (if
388
supplied) i.e. the default is to return an 'errors-only' type
391
If join_all is supplied, however, it will be used as a string to
392
join the resultant critical, warning, and ok messages together i.e.
393
all messages are joined and returned.
397
Return the default shortname used for this plugin i.e. the first
398
token reported by nagios_exit/nagios_die. The default is basically
400
uc basename( $ENV{NAGIOS_PLUGIN} || $0 )
402
with any leading 'CHECK_' and trailing file suffixes removed.
404
get_shortname is not exported by default, so must be explicitly
409
Returns the worst state in the array. Order is: CRITICAL, WARNING, OK, UNKNOWN,
412
The typical usage of max_state is to initialise the state as UNKNOWN and use
413
it on the result of various test. If no test were performed successfully the
414
state will still be UNKNOWN.
416
=item max_state_alt(@a)
418
Returns the worst state in the array. Order is: CRITICAL, WARNING, UNKNOWN,
421
This is a true definition of a max state (OK last) and should be used if the
422
internal tests performed can return UNKNOWN.
428
Nagios::Plugin; the nagios plugin developer guidelines at
429
http://nagiosplug.sourceforge.net/developer-guidelines.html.
434
This code is maintained by the Nagios Plugin Development Team: http://nagiosplug.sourceforge.net
437
=head1 COPYRIGHT AND LICENSE
439
Copyright (C) 2006 by Nagios Plugin Development Team
441
This library is free software; you can redistribute it and/or modify
442
it under the same terms as Perl itself.