|
NAMENagios::Plugin::Getopt - OO perl module providing standardised argument processing for Nagios pluginsSYNOPSISuse Nagios::Plugin::Getopt; # Instantiate object (usage is mandatory) $ng = Nagios::Plugin::Getopt->new( usage => "Usage: %s -H <host> -w <warning> -c <critical>", version => '0.1', url => 'http://www.openfusion.com.au/labs/nagios/', blurb => 'This plugin tests various stuff.', ); # Add argument - named parameters (spec and help are mandatory) $ng->arg( spec => 'critical|c=i', help => q(Exit with CRITICAL status if fewer than INTEGER foobars are free), required => 1, default => 10, ); # Add argument - positional parameters - arg spec, help text, # default value, required? (first two mandatory) $ng->arg( 'warning|w=i', q(Exit with WARNING status if fewer than INTEGER foobars are free), 5, 1); # Parse arguments and process standard ones (e.g. usage, help, version) $ng->getopts; # Access arguments using named accessors or or via the generic get() print $ng->warning; print $ng->get('critical'); DESCRIPTIONNagios::Plugin::Getopt is an OO perl module providing standardised and simplified argument processing for Nagios plugins. It implements a number of standard arguments itself (--help, --version, --usage, --timeout, --verbose, and their short form counterparts), produces standardised nagios plugin help output, and allows additional arguments to be easily defined.CONSTRUCTOR# Instantiate object (usage is mandatory) $ng = Nagios::Plugin::Getopt->new( usage => 'Usage: %s --hello', version => '0.01', ); The Nagios::Plugin::Getopt constructor accepts the following named arguments:
The full --help output has the following form: version string license string blurb usage string options list extra text The 'blurb' and 'extra text' sections are omitted if not supplied. For example: $ ./check_tcp_range -h check_tcp_range 0.2 [http://www.openfusion.com.au/labs/nagios/] This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY. It may be used, redistributed and/or modified under the terms of the GNU General Public Licence (see http://www.fsf.org/licensing/licenses/gpl.txt). This plugin tests arbitrary ranges/sets of tcp ports for a host. Usage: check_tcp_range -H <hostname> -p <ports> [-v] Options: -h, --help Print detailed help screen -V, --version Print version information -H, --hostname=ADDRESS Host name or IP address -p, --ports=STRING Port numbers to check. Format: comma-separated, colons for ranges, no spaces e.g. 8700:8705,8710:8715,8760 -t, --timeout=INTEGER Seconds before plugin times out (default: 15) -v, --verbose Show details for command-line debugging (can repeat up to 3 times) ARGUMENTSYou can define arguments for your plugin using the arg() method, which supports both named and positional arguments. In both cases the "spec" and "help" arguments are required, while the "label", "default", and "required" arguments are optional:# Define --hello argument (named parameters) $ng->arg( spec => 'hello|h=s', help => "Hello string", required => 1, ); # Define --hello argument (positional parameters) # Parameter order is 'spec', 'help', 'default', 'required?', 'label' $ng->arg('hello|h=s', "Hello parameter (default %s)", 5, 1);
Multi-line help is useful in cases where an argument can be of different types and you want to make this explicit in your help output e.g. $ng->arg( spec => 'warning|w=s', help => [ 'Exit with WARNING status if less than BYTES bytes of disk are free', 'Exit with WARNING status if less than PERCENT of disk is free', ], label => [ 'BYTES', 'PERCENT%' ], ); would be displayed in the help output as: -w, --warning=BYTES Exit with WARNING status if less than BYTES bytes of disk are free -w, --warning=PERCENT% Exit with WARNING status if less than PERCENT of disk space is free Note that in this case we've also specified explicit labels in another arrayref corresponding to the "help" one - if this had been omitted the types would have defaulted to 'STRING', instead of 'BYTES' and 'PERCENT%'.
Note that --help lists your arguments in the order they are defined, so you should order your "arg()" calls accordingly. GETOPTSThe main parsing and processing functionality is provided by the getopts() method, which takes no arguments:# Parse and process arguments $ng->getopts; This parses the command line arguments passed to your plugin using Getopt::Long and the builtin and provided argument specifications. Flags and argument values are recorded within the object, and can be accessed either using the generic get() accessor, or using named accessors corresponding to your argument names. For example: print $ng->get('hello'); print $ng->hello(); if ($ng->verbose) { # ... } if ($ng->get('ports') =~ m/:/) { # ... } Note that where you have defined alternate argument names, the first is considered the citation form. All the builtin arguments are available using their long variant names. BUILTIN PROCESSINGThe "getopts()" method also handles processing of the immediate builtin arguments, namely --usage, --version, --help, as well as checking all required arguments have been supplied, so you don't have to handle those yourself. This means that your plugin will exit from the getopts() call in these cases - if you want to catch that you can run getopts() within an eval{}."getopts()" also sets up a default ALRM timeout handler so you can use an alarm $ng->timeout; around any blocking operations within your plugin (which you are free to override if you want to use a custom timeout message). SEE ALSONagios::Plugin, Getopt::LongAUTHORGavin Carr <gavin@openfusion.com.au>COPYRIGHT AND LICENSECopyright (C) 2006-2007 by the Nagios Plugin Development Team.This module is free software. It may be used, redistributed and/or modified under either the terms of the Perl Artistic License (see http://www.perl.com/perl/misc/Artistic.html) or the GNU General Public Licence (see http://www.fsf.org/licensing/licenses/gpl.txt).
Visit the GSP FreeBSD Man Page Interface. |