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

Max perldork at webwizarddesign.com
Fri Jul 10 05:33:18 CEST 2009


On Thu, Jul 9, 2009 at 10:34 PM, Thomas Guyot-Sionnest<dermoth at aei.ca> wrote:

> $plugin->start_timer();

Sure, but I don't get why making such an issue about it... There would
be nothing else in this function than a call to alarm($self->opts->timeout).

Well, there is the wrapper to ensure that it is only called if timeout
is defined and is greater than 0 as well

if ((defined $self->opts->get('timeout') && ($self->opts->get('timeout') > 0) {
 ...
}

So that if timeout is for some reason not-defined the script will not
throw an undefined value warning and cause ePN to kill the script.

>> $plugin->start_timer(
>>     sub { my $secs = $plugin->opts->get('timeout');
>>              $plugin->exit(CRITICAL, "Timeout of ${secs} seconds breached!");  }
>> );
>
> I'm not a perl OO guru, but you'll probably need to pass $plugin (or any
> variable you need to access) to the sub somehow. Making is global will
> likely break ePN...

I am not a guru either.  I have been doing OO perl since 2000 or thereabouts.

If you define the subroutine within the scope of the method, $plugin
is passed in within the scope it is defined, so it is not global, it
is lexical to the plugin script file and will be marked for freeing
when the $plugin instance is destroyed as it would only be referenced
within the plugin itself, not stored in any module-level structure
that would persist after the plugin instance is gone.

Every perl plugin I write is meant to run clean under ePN, this code as well :).

- Max




More information about the Devel mailing list