I have a script monitors a PBX that is streaming call data via specific port and saves this data to a database. The problem I am having is that the script does not notice when the PBX goes offline and will not reconnect when the PBX comes back online(some of these PBXes have programmed reboots in the night). I was wondering what would be the best way for me to add an "alive" type of check to this script to monitor if the PBX is alive and if not to keep testing until it does come alive again at which point it will reconnect to the streaming port. I assume we could use a ping type test for this maybe? See current code below: <code> use DBI; use strict; use IO::Socket; use POSIX; # initialize host and port my $host = shift || $ARGV[0]; my $port = shift || $ARGV[1]; my $proto = getprotobyname('tcp'); my $iaddr = inet_aton($host); my $paddr = sockaddr_in($port, $iaddr);

You can use the ping to check whether the data base is alive or not. The ping will return true if the connection is alive. Otherwise it will return false. Based on that we can re-connect to the database.

But in case of you, need to use the IO::Socket::INET's connected function.

So $sock->connected() you need to use. This will return the peer's address if the connection is alive. Otherwise this will return undef value.

Based on this now you can re-connect and also you can identify whether the connection is alive or not.

Otherwise you can use IO::Select's $sock->can_read and $sock->can_write sub routines.