25.4 Implementing Timeouts

To implement a hard timeout, the function alarm() is available in C, Perl, and other programming languages, which normally calls the system functon of the same name (see man 2 alarm). For the Perl alarm() function, the timeout is specified in seconds:

# ... GetOptions ...
alarm($timeout);
# ... core code ...
alarm(0);
# ... end

The alarm($timeout) call starts the alarm function, the argument 0 for the second call stops it again. The first call should be used before time-intensive processing steps, in network-based plugins before opening sockets, and similar situations where a long delay may occur. A good place is after the command line has been processed via GetOptions.

At the end of the plugin it is recommended that you ...

Get Nagios, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.