[Nagiosplug-devel] Suggested code addition to perl Nagios::Plugin module - instance methods for starting and resetting a global alarm()

Max perldork at webwizarddesign.com
Thu Jul 9 02:14:44 CEST 2009


Why make the user suddenly go out of an OO paradign and into a
procedural one when n::p already has the timeout option implemented?
I don't see the value of forcing the user to call alarm() instead of
just putting a convenience method in n::p ... It is not like there are
multiple alarm()-like calls to choose from in perl.  There is only
one, so why not wrap it in the instance and use the state (value for
timeout) that the n::p instance already knows?


On 7/8/09, Thomas Guyot-Sionnest <dermoth at aei.ca> wrote:
> -----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-----
>
> ------------------------------------------------------------------------------
> Enter the BlackBerry Developer Challenge
> This is your chance to win up to $100,000 in prizes! For a limited time,
> vendors submitting new applications to BlackBerry App World(TM) will have
> the opportunity to enter the BlackBerry Developer Challenge. See full prize
> details at: http://p.sf.net/sfu/Challenge
> _______________________________________________________
> Nagios Plugin Development Mailing List
> Nagiosplug-devel at lists.sourceforge.net
> Unsubscribe at https://lists.sourceforge.net/lists/listinfo/nagiosplug-devel
> ::: Please include plugins version (-v) and OS when reporting any issue.
> ::: Messages without supporting info will risk being sent to /dev/null
>




More information about the Devel mailing list