Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I've got a script that runs in the background on a linux box. It's basic job is to go log into a server and grab a list of files. What I DON'T want to happen is for the script to accidentally die when it tries to log in to a server but can't reach it (due to the server being down or having someone trip over the network cable). I'd like it to periodically check to see if the server is up and reachable, and do it's job if it is. If not, the script should just go to sleep for a while, wake up, and try again.

So my question is, what's the best way to check if a server is reachable using perl? If it matters, the server happens to be an FTP server and I'm currently using Net::FTP to get a directory list, but it might be an http server in the future. Is there a particular perl module I should use? Should I simply grab the output of a ping and parse for packets received? I can think of lots of ways, but was wondering if anyone who's done this sort of thing before had pointers.

Thanks monks!

Replies are listed 'Best First'.
Re: Checking Server Availability
by pg (Canon) on Mar 12, 2003 at 05:21 UTC
    Actually, even IO::Socket::INET is not a bad solution.

    Next, what kind of checking is good enough for you to believe the service is available? Most of the time, as long as you can create TCP connection, that service is available, as that means the server side is listening.

Re: Checking Server Availability
by Zaxo (Archbishop) on Mar 12, 2003 at 05:02 UTC

    You simply need to check the status of your Net::FTP connection object after you call the login method. If it's not ok, sleep and retry, or else run from your crontab.

    To allow different protocols for portability, you might prefer the LWP family of network modules. They hide protocol differences inside a common interface.

    After Compline,
    Zaxo

Re: Checking Server Availability
by nite_man (Deacon) on Mar 12, 2003 at 07:43 UTC
    I'm agree with pg - using of the modules IO::Socket and IO::Socket::Net is more flexible and multipurpose because you can use sockets for http, ftp, upd etc. Of course, as variant you can use specific modules for each case: LWP family and libnet family.
    
    ====>
    Mice were crying and stinging but went on nibbling the cactus ... 
                                                  ( The facts of life )
                                                                  <====
    
    
Re: Checking Server Availability
by Desdinova (Friar) on Mar 12, 2003 at 08:53 UTC
    THe above advice is the way that I would go.. Just to throw my 1/50th of dollar in.. You mention using ping, for just checking connectivity this is fine, but even if you can ping it it doesn't mean the service you are going to use is up. I have had machines that ping fine but have had the ftpd or httpd process die an untimely death.

    I think the best approach is to test the conncetion to the service you need, and like any good network code make sure there is a timeout that is monitored so that if the connection fails the program acknoledges that and moves along.
      OK, sounds like lots of folks think IO::Socket and/or IO::Socket::INET is the way to go. So is this the kind of thing you recommend?

      use IO::Socket; use IO::Socket::INET; my $socket = IO::Socket::INET->new( PeerAdr => $my_server, PeerPort => 'ftp(21)', #might be http(80) someday Proto => 'tcp'); my $is_connected = $socket->connected(); $socket->shutdown(2); if(!$is_connected){ #go back to sleep } else{ #do some stuff }