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

William Leibzon william at leibzon.org
Wed Jul 8 21:03:42 CEST 2009


I don't use Nagios::Plugin library but I have this type of code in half of
my plugins. I suggest not only adding to the library but actually making a
default or at least recommended in the docs. It comes real handy when your
plugin has to call external commands that may hang - you don't want any
extra zombie processes, etc.

On Wed, Jul 8, 2009 at 9:53 AM, Max <perldork at webwizarddesign.com> 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();
>
> - 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);
> }
>
>
> ------------------------------------------------------------------------------
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.monitoring-plugins.org/archive/devel/attachments/20090708/1f23df28/attachment.html>


More information about the Devel mailing list