[Nagiosplug-devel] Nagios::Plugin::Getopt api

Gavin Carr gavin at openfusion.com.au
Tue Aug 29 14:16:07 CEST 2006


Hi all,

As mentioned a little while ago, I've broken out the argument handling
logic from my original nagios module into a new class currently called 
Nagios::Plugin::Getopt. Here's the current api:

 SYNOPSIS
   use Nagios::Plugin::Getopt;

   # Instantiate object (usage and version are mandatory)
   $ng = Nagios::Plugin::Getopt->new(
     usage => "Usage: %s -H <host> -w <warning_threshold>
   -c <critical threshold>",
     version => '0.01',
     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=s',
     help => qq(-c, --critical=INTEGER\n   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=s',
     qq(-w, --warning=INTEGER\n   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');


N::P::G implements standard argument handling automatically (--help, 
--version, --usage, --timeout, --verbose, and short forms), and exits
with the appropriate messages from getopts for --help, --version, and
--usage. 

Any comments/critique on the api? Would people like to see an actual
example and example output, or shall I just drop it into cvs and you
can have a play?

I believe Ton would like it actually composed into the top-level
Nagios::Plugin object, so a further item for discussion would be how
this api gets mapped up there. We could probably defer that into the 
more general Nagios::Plugin api discussion to come, however.

Cheers,
Gavin






More information about the Devel mailing list