NAME

Nagios::Plugin - a family of perl modules to streamline writing Nagios plugins

SYNOPSIS

# Constants OK, WARNING, CRITICAL, and UNKNOWN are exported by default
# See also Nagios::Plugin::Functions for a functional interface
use Nagios::Plugin;

# Constructor
$np = Nagios::Plugin->new;            # OR
$np = Nagios::Plugin->new( shortname => "PAGESIZE" );

# Exit methods - nagios_exit( CODE, MESSAGE ), 
#                nagios_die( MESSAGE, [CODE])
$page = retrieve_page($page1)
    or $np->nagios_exit( UNKNOWN, "Could not retrieve page" );
    # Return code: 3; 
    #   output: PAGESIZE UNKNOWN - Could not retrieve page 
test_page($page)
    or $np->nagios_exit( CRITICAL, "Bad page found" );

# nagios_die() is just like nagios_exit(), but return code defaults 
#   to UNKNOWN
$page = retrieve_page($page2)
    or $np->nagios_die( "Could not retrieve page" );
    # Return code: 3; 
    #   output: PAGESIZE UNKNOWN - Could not retrieve page

# Threshold methods (NOT YET IMPLEMENTED - use 
#    Nagios::Plugin::Threshold for now)
$code = $np->check_threshold(
    check => $value,
    warning => $warning_threshold,
    critical => $critical_threshold,
);
$np->nagios_exit( $code, "Threshold check failed" ) if $code != OK;

# Message methods (EXPERIMENTAL AND SUBJECT TO CHANGE) -
#   add_message( CODE, $message ); check_messages()
for (@collection) {
    if (m/Error/) {
        $np->add_message( CRITICAL, $_ );
    } else {
        $np->add_message( OK, $_ );
    }
}
($code, $message) = $np->check_message();
nagios_exit( $code, $message );
# If any items in collection matched m/Error/, returns CRITICAL and 
#   the joined set of Error messages; otherwise returns OK and the 
#   joined set of ok messages

# Perfdata methods
$np->add_perfdata( 
    label => "size",
    value => $value,
    uom => "kB",
    threshold => $threshold,
);
$np->add_perfdata( label => "time", ... );
$np->nagios_exit( OK, "page size at http://... was ${value}kB" );
# Return code: 0; 
#   output: PAGESIZE OK - page size at http://... was 36kB \
#   | size=36kB;10:25;25: time=...

# Option handling methods (NOT YET IMPLEMENTED - use 
#   Nagios::Plugin::Getopt for now)

DESCRIPTION

Nagios::Plugin and its associated Nagios::Plugin::* modules are a family of perl modules to streamline writing Nagios plugins. The main end user modules are Nagios::Plugin, providing an object-oriented interface to the entire Nagios::Plugin::* collection, and Nagios::Plugin::Functions, providing a simpler functional interface to a useful subset of the available functionality.

The purpose of the collection is to make it as simple as possible for developers to create plugins that conform the Nagios Plugin guidelines (http://nagiosplug.sourceforge.net/developer-guidelines.html).

EXPORTS

Nagios status code constants are exported by default:

OK
WARNING
CRITICAL
UNKNOWN
DEPENDENT

The following variables are also exported on request:

%ERRORS

A hash mapping error strings ("CRITICAL", "UNKNOWN", etc.) to the corresponding status code.

%STATUS_TEXT

A hash mapping status code constants (OK, WARNING, CRITICAL, etc.) to the corresponding error string ("OK", "WARNING, "CRITICAL", etc.) i.e. the reverse of %ERRORS.

CONSTRUCTOR

Nagios::Plugin->new;

Nagios::Plugin->new( shortname => 'PAGESIZE' );

Instantiates a new Nagios::Plugin object. Accepts the following named arguments:

shortname

The 'shortname' for this plugin, used as the first token in the plugin output by the various exit methods. Default: uc basename $0.

EXIT METHODS

nagios_exit( <CODE>, $message )

Exit with return code CODE, and a standard nagios message of the form "SHORTNAME CODE - $message".

nagios_die( $message, [<CODE>] )

Same as nagios_exit(), except that CODE is optional, defaulting to UNKNOWN.

die( $message, [<CODE>] )

Alias for nagios_die(). Deprecated.

THRESHOLD METHODS

NOT YET IMPLEMENTED - use Nagios::Plugin::Threshold directly for now.

check_threshold( check => $value, warning => $warn, critical => $crit )

MESSAGE METHODS

EXPERIMENTAL AND SUBJECT TO CHANGE

add_messages and check_messages are higher-level convenience methods to add and then check a set of messages, returning an appropriate return code and/or result message.

add_message( <CODE>, $message )

Add a message with CODE status to the object. May be called multiple times. The messages added are checked by check_messages, following.

Only CRITICAL, WARNING, and OK are accepted as valid codes.

check_messages()

Check the current set of messages and return an appropriate nagios return code and/or a result message. In scalar context, returns only a return code; in list context returns both a return code and an output message, suitable for passing directly to nagios_exit() e.g.

$code = $np->check_messages;
($code, $message) = $np->check_messages;

check_messages returns CRITICAL if any critical messages are found, WARNING if any warning messages are found, and OK otherwise. The message returned in list context defaults to the joined set of error messages; this may be customised using the arguments below.

check_messages accepts the following named arguments (none are required):

join => SCALAR

A string used to join the relevant array to generate the message string returned in list context i.e. if the 'critical' array @crit is non-empty, check_messages would return:

join( $join, @crit )

as the result message. Default: ' ' (space).

join_all => SCALAR

By default, only one set of messages are joined and returned in the result message i.e. if the result is CRITICAL, only the 'critical' messages are included in the result; if WARNING, only the 'warning' messages are included; if OK, the 'ok' messages are included (if supplied) i.e. the default is to return an 'errors-only' type message.

If join_all is supplied, however, it will be used as a string to join the resultant critical, warning, and ok messages together i.e. all messages are joined and returned.

critical => ARRAYREF

Additional critical messages to supplement any passed in via add_message().

warning => ARRAYREF

Additional warning messages to supplement any passed in via add_message().

ok => ARRAYREF | SCALAR

Additional ok messages to supplement any passed in via add_message().

PERFORMANCE DATA METHODS

add_perfdata( label => "size", value => $value, uom => "kB", threshold => $threshold )

Add a set of performance data to the object. May be called multiple times. The performance data is included in the standard plugin output messages by the various exit methods.

See the Nagios::Plugin::Performance documentation for more information on performance data and the various field definitions, as well as the relevant section of the Nagios Plugin guidelines (http://nagiosplug.sourceforge.net/developer-guidelines.html#AEN202).

OPTION HANDLING METHODS

NOT YET IMPLEMENTED - use Nagios::Plugin::Getopt directly for now.

EXAMPLES

"Enough talk! Show me some examples!"

See the file 'check_stuff.pl' in the 't' directory for a complete working example of a plugin script.

VERSIONING

The Nagios::Plugin::* modules are currently experimental and so the interfaces may change up until Nagios::Plugin hits version 1.0, although every attempt will be made to keep them as backwards compatible as possible.

SEE ALSO

See Nagios::Plugin::Functions for a simple functional interface to a subset of the available Nagios::Plugin functionality.

See also Nagios::Plugin::Getopt, Nagios::Plugin::Range, Nagios::Plugin::Performance, Nagios::Plugin::Range, and Nagios::Plugin::Threshold.

The Nagios Plugin project page is at http://nagiosplug.sourceforge.net.

BUGS

Please report bugs in these modules to the Nagios Plugin development team: nagiosplug-devel@lists.sourceforge.net.

AUTHOR

Maintained by the Nagios Plugin development team - http://nagiosplug.sourceforge.net.

Originally by Ton Voon, <ton.voon@altinity.com>.

Nathan Vonnahme added extra tests and subsequent fixes.

COPYRIGHT AND LICENSE

Copyright (C) 2006 by Nagios Plugin Development Team

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.