[Nagiosplug-devel] Suggested code addition to perl Nagios::Plugin module - instance methods for starting and resetting a global alarm()
Thomas Guyot-Sionnest
dermoth at aei.ca
Thu Jul 9 01:02:24 CEST 2009
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 08/07/09 12:53 PM, Max wrote:
> Hi,
>
> We use this extensively in our scripts, simple addition, but keeps
> code nice and clean. I realize this is simplistic and will break if
> the user uses a module that sets alarm() after the plugin alarm is
> set, we mostly use this in my subclass of Nagios::Plugin,
> Nagios::Plugin::SNMP, but use it for some non-SNMP plugins as well.
>
> - Max
>
> Example use:
>
> my $plugin = Nagios::Plugin->new(...);
>
> # Starts a timer only if the user provided a positive integer to --timeout;
> # If timeout is reached, will exit with UNKNOWN and error message.
>
> $plugin->start_timer();
>
> ... code
> ... code
>
> # All done, reset so alarm is not triggered.
> $plugin->reset_timer();
What you want is already implemented trough Nagios::Plugin::Getopt,
which (among others) automatically parses the -t (or -0-timeout) argument.
For example if you copy-paste this you'll trigger a timeout ("$" is your
prompt):
$ perl -MNagios::Plugin -e '$0="check_stuff"; '\
' $n = Nagios::Plugin->new(usage=>"", timeout=>1); $n->getopts(); '\
' alarm($n->opts->timeout); select(undef, undef, undef, 1.5); '\
' $n->nagios_exit(OK, "All good");'
Which return:
CHECK_STUFF UNKNOWN - plugin timed out (timeout 1s)
Here's like-by-line what it does:
> $ perl -MNagios::Plugin -e '$0="check_stuff"; '\
Execute perl from command-line, load Nagios::Plugin and trick is into
thinking the base command name is "check_stuff"
> ' $n = Nagios::Plugin->new(usage=>"", timeout=>1); $n->getopts(); '\
Instantiate a new plugin, set a blank usage (normally you'd want to at
least specify the -t option) and give it the default timeout.
> ' alarm($n->opts->timeout); select(undef, undef, undef, 1.5); '\
Set alarm to the timeout passed to the plugin (default timeout of one)
and then use select() to sleep 1.5 second)
> ' $n->nagios_exit(OK, "All good");'
This is the normal way to end the plugin but it gets never executed
because the timeout kicks in
There's likely place for enhancements (like defining the result code to
send or passing our own SIGALRM sub), but whatever you do should be
based on what I just shown.
- --
Thomas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFKVSWA6dZ+Kt5BchYRAodbAJ9zz8089H/k6BzTB5PKX6MSKYsjLwCfQ9Sh
nfFznNzW1ovwkf9m0kqTX1g=
=Wszy
-----END PGP SIGNATURE-----
More information about the Devel
mailing list