Feb/091
Handing timeouts in perl scripts
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.




























20:54 on November 1st, 2011
This is okay, but not exactly what I’m looking for. I need the equivalent of:
my $timeout = 30 * 100; # milliseconds my $start = GetTickCount(); #this is what I need equivalent of
while ($m_CmdResponse eq “”) { usleep(100000); #100ms PollData(); #this querys server for data and if any, stores in $m_CmdResponse if (GetTickCount() > ($start + $timeout)) { die(“Timeout.”); } }