Using alarm: Timeout to do something else.

P

Prabh

Hello all,
I need to monitor availability of a webserver for x no. of sec.s and
trigger
an alert mechanism after the time elapses and webserver still doesnt
respond.

Am thinking of using the 'alarm' function to do this, along the lines,

==========================================
use LWP::Simple ;
alarm(60) ;
my $url = head("http://abc.xyz.com") ;
==========================================

When the webserver doesnt respond after 60 sec.s, I want to shoot off
a mail to the webadmins that the webserver isnt responding. So, where
do I get the "at this point 60 sec.s have elapsed and theres no
reponse from webserver, its ok to send mail now" point.

Paraphrasing the alarm example from the Camel book:

===========================================
print "Answer in 60 sec.s\n" ;
alarm(60) ;
$answer = <STDIN> ;
$timeleft = alarm(0) ;
print "You had $timeleft sec.s remaining.\n" ;
===========================================

I here, where do I print "Hey, 60 sec.s elapsed and you still didnt
answer. Goodbye." All I get after 60 sec.s is the message "Alarm
clock". I dont know where its coming from.

Thanks for your time,
Prab
 
A

A. Sinan Unur

(e-mail address removed) (Prabh) wrote in @posting.google.com:
Am thinking of using the 'alarm' function to do this, along the lines,

Then do read the documentation for the alarm function.
Paraphrasing the alarm example from the Camel book:

I do not have the Camel book here with me so I cannot comment on the
extent of the 'paraphrasing' you have done.
===========================================
print "Answer in 60 sec.s\n" ;
alarm(60) ;
$answer = <STDIN> ;
$timeleft = alarm(0) ;
print "You had $timeleft sec.s remaining.\n" ;
===========================================

I here, where do I print "Hey, 60 sec.s elapsed and you still didnt
answer. Goodbye." All I get after 60 sec.s is the message "Alarm
clock". I dont know where its coming from.

What part of the documentation for the alarm function is not clear?

#! /usr/bin/perl

use strict;
use warnings;

my $timeleft = 60;

eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm $timeleft;
my $input = <STDIN>;
$timeleft = alarm 0;
};

if ($@) {
die unless $@ eq "alarm\n";
print "You took too long to respond!\n";
} else {
print "You had $timeleft seconds remaining.\n"
}
 
C

Charles DeRykus

Hello all,
I need to monitor availability of a webserver for x no. of sec.s and
trigger
an alert mechanism after the time elapses and webserver still doesnt
respond.

Am thinking of using the 'alarm' function to do this, along the lines,

==========================================
use LWP::Simple ;
alarm(60) ;
my $url = head("http://abc.xyz.com") ;
==========================================

When the webserver doesnt respond after 60 sec.s, I want to shoot off
a mail to the webadmins that the webserver isnt responding. So, where
do I get the "at this point 60 sec.s have elapsed and theres no
reponse from webserver, its ok to send mail now" point.

Paraphrasing the alarm example from the Camel book:

===========================================
print "Answer in 60 sec.s\n" ;
alarm(60) ;
$answer = <STDIN> ;
$timeleft = alarm(0) ;
print "You had $timeleft sec.s remaining.\n" ;
===========================================

I here, where do I print "Hey, 60 sec.s elapsed and you still didnt
answer. Goodbye." All I get after 60 sec.s is the message "Alarm
clock". I dont know where its coming from.

I seem to recall you can do something like this to
access the underlying agent:

use LWP::Simple qw($ua head);

$ua->timeout(60);
foreach my $url (@list_of_urls) {
head($url) or send_mail(...);
}

Of course, head() might fail because of something
other than a timeout but you might want to report
those problems too.

hth,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top