CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Telnet session disconnect testing

 



bajangerry
New User

Jan 13, 2010, 6:46 AM

Post #1 of 2 (1001 views)
Telnet session disconnect testing Can't Post

Dear Gurus,

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);

for(;;){

my $sock = new IO::Socket::INET(PeerAddr => $host, PeerPort => $port,Proto => "tcp",)
or die "Cannot connect to PBX at address: $host port: $port: $!";

while (<$sock>) {
s/^\0+//; # Remove leading null characters
print $_;
chomp ($_);
#$_ =~ s/^[^ ]+//;
if ($_ =~m"/") {
&TXTout; #send data to the data logging subroutine
&DBconnect; #send data to the database subroutine
}
} #Close While loop

sub TXTout {
my $dir = "/var/log/smdr/"; # please ensure you create this directory to get your log files
my $filename = strftime("%B%d%Y",localtime(time));
my $fileexpression = $dir.$filename;
if (-e $fileexpression){
open(FILE, ">>$fileexpression");
print FILE $_;
close (FILE);
}
else {
open FILE, >$fileexpression;
print FILE $_;
close (FILE);
}
}

sub DBconnect {
# MySQL Connection parameters
my $dbuser= "root";
my $dbpassword= "Adm1n!";
my $dbhost= "localhost";
my ($line, $mon, $day, $stime, $pm, $hrs, $mins, $sec, $callp, $leaddigit, $callno, $speed, $callp2, $transf, $acccode, $sysid, $tester);

$mon = substr ($_, 1,2);
$day = substr ($_, 4,2);
$stime = substr($_, 7,5);
$pm = substr($_, 12,1);
$hrs = substr($_, 14,2);
$mins = substr($_, 17,2);
$sec = substr($_, 20,2);
$callp = substr($_, 23,4);
$leaddigit = substr($_, 29,3);
$callno = substr($_, 33,26);
$speed = substr($_, 60,1);
$callp2 = substr($_, 61,5);
$transf = substr($_, 67,5);
$acccode = substr($_, 72,12);
$sysid = substr($_, 85,3);
$tester = strftime( "%Y",localtime(time));
if ($acccode == ""){$acccode = 0}
# Establish the connection which returns a DB handle
my $dbh= DBI->connect("dbi:mysql:database=smdr;host=$dbhost",$dbuser,$dbpassword) or die $DBI::errstr;
# Prepare the SQL statement
my $sth= $dbh->prepare("INSERT INTO import VALUE(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)") or die $DBI::errstr;
# Send the statement to the server
$sth->execute($mon,$day,$stime,$pm,$hrs,$mins,$sec,$callp,$leaddigit,$callno,$speed,$callp2,$transf,$acccode,$sysid,$tester,'');
# Close the database connection
$dbh->disconnect or die $DBI::errstr;
} #Close DBconnect subroutine

#close the socket
close $sock or die "close: $!";
print "socket closed";
print "<br />";
}
</code>


thillai_selvan
Novice

Mar 8, 2010, 10:50 PM

Post #2 of 2 (856 views)
Re: [bajangerry] Telnet session disconnect testing [In reply to] Can't Post

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.


(This post was edited by thillai_selvan on Mar 8, 2010, 10:51 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives