9
Feb/09
0

Handing timeouts in perl scripts

Perl Writing some Nagios check plugins I wrote some code to catch timeouts. These timeouts can occur for several reasons. Mostly some network connections via SSH, SNMP or HTTP-Gets which run longer than the default check timeout in Nagios. I usually use a global check timeout of 15 seconds in Nagios. This means a check script which runs more than 15 seconds will be killed. In this case Nagios returns a CRITICAL state with a “(Service check timed out)” message. This is bad cause I don’t want those timeouts to result in CRITICAL state and some CRITICAL notifications.

Now read how to catch such timeouts…

I use the alarm(13) function in my Perl script. This functions sets a timer to the given number of seconds – in this case 13 seconds. When the timer reached zero it will throw a SIGALRM. Thankfully you can customize the event on SIGALRM. This gives my script the possibility to print some output and exit the script regular with a UNKNOWN exit code.

Here my snippet to catch timeouts in perl scripts:

# Handle SIGALRM (timeout triggered by alarm() call)
$SIG{ALRM} = sub { 
  print "UNKNOWN: Script timed out\n";
  exit -1;
};
 
# Start the timer to script timeout
alarm(13);

This should really be included in all Perl Nagios check script.

Filed under: Perl
Comments (0) Trackbacks (0)

No comments yet.

No trackbacks yet.