Nagios::Plugin(3)
okay bestie, welcome to Nagios::Plugin(3) from the User Contributed Perl Documentation β manual name folded in near the top like a little name tag, because we're polite. this is part 1, and it's giving βPerl modules that make writing Nagios plugins so much less of a nightmare.β π
β¨
π NAME π
Nagios::Plugin is literally a whole family of perl modules β a squad β whose entire thing is streamlining the writing of Nagios plugins. no cap, that's the whole name section. π
β¨ SYNOPSIS β¨
The synopsis is a full mood board: constants, constructor, command-line processing, exit/return methods, threshold checks, message checks, and perfdata. quick vibe check first, then the exact code β every option, argument, URL, return code, and example stays exactly as it was. π«Ά
- default exports β
OK,WARNING,CRITICAL, andUNKNOWNare exported by default. for the functional interface, see alsoNagios::Plugin::Functions. - constructor β
$np = Nagios::Plugin->new;or$np = Nagios::Plugin->new( shortname => "PAGESIZE" );. - Getopt β
Nagios::Plugin::Getoptprocesses the@ARGVcommand line options;--verbose,--help,--usage,--timeout, and--hostare defined automatically. - adding args β
$np->add_arg( spec => 'warning|w=s', help => ... )adds valid command line options and builds them into your usage/help documentation; the help example points tohttp://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMATfor the threshold format. - parsing β
$np->getopts;parses@ARGVand processes standard arguments (e.g. usage, help, version). - exit/return β
nagios_exit( CODE, MESSAGE )andnagios_die( MESSAGE, [CODE]);nagios_die()is just likenagios_exit(), but the return code defaults toUNKNOWN. - thresholds β
$np->check_threshold( check => $value, warning => $warning_threshold, critical => $critical_threshold );returns$code; exit with$np->nagios_exit( $code, "Threshold check failed" ) if $code != OK;. - messages β EXPERIMENTAL AND SUBJECT TO CHANGE:
add_message( CODE, $message )andcheck_messages(). for each item in@collection,m/Error/getsadd_message( CRITICAL, $_ ), otherwiseadd_message( OK, $_ ).($code, $message) = $np->check_messages();thennagios_exit( $code, $message );if any items matchedm/Error/, returnsCRITICALand the joined set of Error messages; otherwise returnsOKand the joined set of ok messages. - perfdata β
add_perfdatawithlabel => "size",value => $value,uom => "kB",threshold => $threshold; you can add more with$np->add_perfdata( label => "time", ... );; then$np->nagios_exit( OK, "page size at http://... was ${value}kB" );gives return code0and output includesPAGESIZE OK - page size at http://... was 36kBplus the perfdata line| size=36kB;10:25;25: time=....
# 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" ); # OR
# use Nagios::Plugin::Getopt to process the @ARGV command line options:
# --verbose, --help, --usage, --timeout and --host are defined automatically.
$np = Nagios::Plugin->new(
usage => "Usage: %s [ -v|--verbose ] [-H <host>] [-t <timeout>] "
. "[ -c|--critical=<threshold> ] [ -w|--warning=<threshold> ]",
);
# add valid command line options and build them into your usage/help documentation.
$np->add_arg(
spec => 'warning|w=s',
help => '-w, --warning=INTEGER:INTEGER . See '
. 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT '
. 'for the threshold format. ',
);
# Parse @ARGV and process standard arguments (e.g. usage, help, version)
$np->getopts;
# Exit/return value 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
$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_messages();
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=...
π DESCRIPTION π
Okay so bestie, Nagios::Plugin and its whole Nagios::Plugin::* family are a set of perl modules made to streamline writing Nagios plugins. The main end user modules are Nagios::Plugin, which gives you an object-oriented interface to the entire Nagios::Plugin::* collection, and Nagios::Plugin::Functions, which gives you a simpler functional interface to a useful subset of the available functionality.
The whole point 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). slay β¨
β¨ EXPORTS β¨
Nagios status code constants are exported by default:
OKWARNINGCRITICALUNKNOWNDEPENDENT
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' );
Nagios::Plugin->new(
usage => "Usage: %s [ -v|--verbose ] [-H <host>] [-t <timeout>]
[ -c|--critical=<critical threshold> ] [ -w|--warning=<warning threshold> ] ",
version => $VERSION,
blurb => $blurb,
extra => $extra,
url => $url,
license => $license,
plugin => basename $0,
timeout => 15,
);
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.usageβ thinkusage ("Usage: %s --foo --bar"). Passing a value for theusage()argument makesNagios::Plugininstantiate its ownNagios::Plugin::Getoptobject so you can start doing command line argument processing. See"CONSTRUCTOR"inNagios::Plugin::Getoptfor more about"usage"and the following options:versionurlblurblicenseextraplugintimeout
ποΈ OPTION HANDLING METHODS ποΈ
Nagios::Plugin provides these methods for accessing the functionality in Nagios::Plugin::Getopt.
add_arg
Examples:
# Define --hello argument (named parameters)
$plugin->add_arg(
spec => 'hello=s',
help => "--hello\n Hello string",
required => 1,
);
# Define --hello argument (positional parameters)
# Parameter order is 'spec', 'help', 'default', 'required?'
$plugin->add_arg('hello=s', "--hello\n Hello string", undef, 1);
See "ARGUMENTS" in Nagios::Plugin::Getopt for more details.
getopts()
Parses and processes the command line options you've defined, automatically doing the right thing with help/usage/version arguments.
See "GETOPTS" in Nagios::Plugin::Getopt for more details.
opts()
Assuming you've instantiated it by passing 'usage' to new(), opts() returns the Nagios::Plugin object's Nagios::Plugin::Getopt object, with which you can do lots of great things. main character energy π
E.g.
if ( $plugin->opts->verbose ) {
print "yah yah YAH YAH YAH!!!";
}
# start counting down to timeout
alarm $plugin->opts->timeout;
your_long_check_step_that_might_time_out();
# access any of your custom command line options,
# assuming you've done these steps above:
# $plugin->add_arg('my_argument=s', '--my_argument [STRING]');
# $plugin->getopts;
print $plugin->opts->my_argument;
Again, see Nagios::Plugin::Getopt.
π 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.
β οΈ NOTE: exceptions are not raised by default to calling code. Set $_use_die flag if this functionality is required (see test code).
die( $message, [<CODE>] )
Alias for nagios_die(). Deprecated.
max_state, max_state_alt
These are wrapper function for Nagios::Plugin::Functions::max_state and Nagios::Plugin::Functions::max_state_alt.
π THRESHOLD METHODS π
These provide a top level interface to the Nagios::Plugin::Threshold module; for more details, see Nagios::Plugin::Threshold and Nagios::Plugin::Range.
check_threshold( $value )
check_threshold( check => $value, warning => $warn, critical => $crit )
Evaluates $value against the thresholds and returns OK, CRITICAL, or WARNING constant. The thresholds may be:
- explicitly set by passing
'warning'and/or'critical'parameters tocheck_threshold(), or, - explicitly set by calling
set_thresholds()beforecheck_threshold(), or, - implicitly set by command-line parameters
-w,-c,--criticalor--warning, if you have run"$plugin->getopts()".
You can specify $value as an array of values and each will be checked against the thresholds.
The return value is ready to pass to nagios_exit, e.g.,
$p->nagios_exit(
return_code => $p->check_threshold($result),
message => " sample result was $result"
);
set_thresholds(warning => "10:25", critical => "~:25")
Sets the acceptable ranges and creates the plugin's Nagios::Plugins::Threshold object. See http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT for details and examples of the threshold format.
threshold()
Returns the object's Nagios::Plugin::Threshold object, if it has been defined by calling set_thresholds(). You can pass a new Threshold object to it to replace the old one too, but you shouldn't need to do that from a plugin script.
π¬ 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. They are equivalent to maintaining a set of @critical, @warning, and and @ok message arrays (add_message), and then doing a final if test (check_messages) like this:
if (@critical) {
nagios_exit( CRITICAL, join(' ', @critical) );
}
elsif (@warning) {
nagios_exit( WARNING, join(' ', @warning) );
}
else {
nagios_exit( OK, join(' ', @ok) );
}
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@critis non-empty,check_messageswould 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 isCRITICAL, only the'critical'messages are included in the result; ifWARNING, only the'warning'messages are included; ifOK, 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 viaadd_message().warning => ARRAYREFβ additional warning messages to supplement any passed in viaadd_message().ok => ARRAYREF | SCALARβ additional ok messages to supplement any passed in viaadd_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). obsessed with a tidy perfdata moment π¦
πΈ EXAMPLES πΈ
"Enough talk! Show me some examples!"
See the file 'check_stuff.pl' in the 't' directory included with the Nagios::Plugin distribution 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 π
Nagios::Plugin::Functionsβ simple functional interface to a subset of the availableNagios::Pluginfunctionality.Nagios::Plugin::GetoptNagios::Plugin::RangeNagios::Plugin::PerformanceNagios::Plugin::RangeNagios::Plugin::Threshold
The Nagios Plugin project page is at http://nagiosplug.sourceforge.net. iykyk π
π BUGS π
Please report bugs in these modules to the Nagios Plugin development team: nagiosplug-devel@lists.sourceforge.net.
β¨ AUTHOR β¨
Okay so who do we thank for this masterpiece? π The page is maintained by the Nagios Plugin development team β you can find them over at http://nagiosplug.sourceforge.net. Bestie, that's the link, go say hi π«Ά
And the original author, the one who started it all, the icon, the legend π β that's Ton Voon, <ton.voon@altinity.com>. Show some respect, fr fr π
π COPYRIGHT AND LICENSE π
Copyright (C) 2006 by Nagios Plugin Development Team. Periodt, that's the copyright line, no cap π
Now the good part β 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. So yeah, lowkey generous of them π
Ngl, if you're gonna pass this around or remix it, just keep those Perl terms in mind β it's literally the whole deal, iykyk πΈ
And that's a wrap on the whole page, bestie! π This gorgeous little doc is perl v5.42.0 energy, dated 2024-03-04 β so it's giving vintage-but-still-slaying πβ¨ Now go forth and write some iconic plugins πβ
π¦ where this page comes from
- opensuse-leap 16.0: perl-Nagios-Plugin 0.36-bp160.1.8 Β· Artistic-1.0 or GPL-2.0+
- opensuse-leap 16.0: perl-Nagios-Plugin 0.36-bp160.1.8 Β· Artistic-1.0 or GPL-2.0+
- opensuse-leap 16.0: perl-Nagios-Plugin 0.36-bp160.1.8 Β· Artistic-1.0 or GPL-2.0+
- opensuse-leap 16.0: perl-Nagios-Plugin 0.36-bp160.1.8 Β· Artistic-1.0 or GPL-2.0+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms