Welcome to WebmasterWorld Guest from 54.145.166.96

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Perl to check if a website is running

...or if the server is down

   
8:29 pm on Nov 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I imagine there must be a way (I can't find it) of checking if a website is 'active', 'running' whatever you want to call it!

Recently the WorldPay payment system has been down, and I was wondering if I could check if WorldPay was up (using Perl) and if it is forward them to WorldPay. This is simple.

But if the server is not working, I don't want it to hang for 30 seconds or a minute with the customer waiting. Is it possible to request a timeout value?

8:40 pm on Nov 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



#!/usr/bin/perl 
use LWP::Simple qw(get);
if ( get("WORLD PAY URL GOES HERE") ) {
print 'ok';
} else {
print 'error';
}

But that way you perl script will hang and wait till WorldPay will respond. Timeout feature seems to be disabled in that module (at least for windows). I've tried it before with no luck.

4:49 pm on Nov 10, 2003 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



You can always run a system command, such as ping.
Still, the timeout might be worth it...
9:27 pm on Nov 13, 2003 (gmt 0)

10+ Year Member



This is just a snippet I took out of a server monitoring script we have, but you can probably get a start this way...

use Net::Ping;
use Time::HiRes qw( gettimeofday tv_interval);

$p = Net::Ping->new("tcp", 10);
$p->{port_num} = getservbyname($ref->{'PORT'}, "tcp");

$t0 = [gettimeofday];
$serverstate = "DOWN" unless $p->ping($ref->{'HOST'});
$pingtime = tv_interval($t0) unless ($serverstate eq "DOWN");

$p->close;
undef($p);

It's not the prettiest thing in the world, but it works reasonably well.

The docs for Net::Ping should get you on your way. For what you're doing, you might not bother with the Time::HiRes stuff.

9:46 pm on Nov 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Great, I give some of the ideas a try. Thanks
6:22 am on Nov 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Pinging the server box does not mean that HTTP server is running. It can be that the actual server is on and functioning, but HTTP server is down.

That happens very often as a matter of fact. Most of the load is on the HTTP server, not on the hardware or other servers (FTP, mail, etc...)

8:44 am on Nov 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could use the Net::Telnet module if you want to be sure that the http server is running, rather than just the machine:

#!/usr/bin/perl
use Net::Telnet;
$connection=Net::Telnet->new(Timeout => 5, Host => "www.example.com", Port=>80, Errmode => sub {&error;});

sub error {
print "Connection Failed!\n";
}

3:19 pm on Nov 18, 2003 (gmt 0)

10+ Year Member



The Net::Ping module does the same thing. Just use tcp instead of icmp and tell it to use port 80. I like it for it's flexibility.

I actually hadn't thought of using Net::Telnet that way.... hmmmm...

9:35 pm on Nov 18, 2003 (gmt 0)

10+ Year Member



Many small hosting companies use nagios [nagios.org] (a sourceforge project) for monitoring their servers. I have never used it before but have read that it is a bit time consuming to setup with but is as good as paid servies like alertra.
3:37 pm on Dec 3, 2003 (gmt 0)

WebmasterWorld Senior Member trillianjedi is a WebmasterWorld Top Contributor of All Time 10+ Year Member



We've just installed nagios. It's pretty good - ideal if you're running multiple web services (http, VOIP, SIP etc).

TJ

10:04 pm on Dec 3, 2003 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



We use nagios as well and it has been quite good.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month