[Nagiosplug-devel] Suggested code addition to perl Nagios::Plugin module - instance methods for starting and resetting a global alarm()
Max
perldork at webwizarddesign.com
Wed Jul 8 18:53:55 CEST 2009
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();
- Max
Code:
# -------------------------------------------------------------------------
# NP - time out helpers
# Start a global timer for the plugin
sub start_timer {
my $self = shift;
my $timeout = $self->opts->get('timeout');
return if ((! defined $timeout) || ($timeout < 1));
$SIG{'ALRM'} = sub {
$self->nagios_exit(UNKNOWN, "Plugin timeout (${timeout}s) exceeded!");
};
alarm($timeout);
}
# Reset the global counter
sub reset_timer {
my $self = shift;
alarm(0);
}
More information about the Devel
mailing list