I have a client/server setup of two perl scripts. One sits on a box and reads in a log file, and on the other box, it connects to the server and tails the file through the socket. This has worked ok for a while, but it seems to have started acting weird lately. Just to put this out there, there is no connection problem between the two machines outside the scripts, latency is almost nothing, and there is only one hop in a traceroute. It's I have come to the conclusion it is the script. So here we have the script on the server side that is reading the log file and passing data thru the socket:
use strict; use lib '/opt/nextone/lib/perl5/site_perl/5.8.3/'; use IO::Socket; use LogFile; use File::Tail; use Sys::Hostname; use Time::Local; use POSIX qw(strftime); use constant PORT => 9204; use constant PIDFILE => '/var/run/cdr_hup.pid'; use constant USER => 'root'; use constant GROUP => 'root'; use constant CDR_HOME => '/var/cdrs'; # signal handler for child die events $SIG{TERM} = $SIG{INT} = \&do_term; $SIG{HUP} = \&do_hup; our $connection; my $quit=0; my $port = $ARGV[0] || PORT; my @allow=('IP ADDRESS OF INCOMING CONNECTION'); my ($ip_addr, $valid, $log_file); $log_file ='/var/log/log_server'; init_log($log_file); log_notice "Start Server\n"; my $listen_socket = IO::Socket::INET->new(LocalPort => $port, Listen => 20, Proto => 'tcp', ReuseAddr => 1) or die "Can't bind : $@\n";; #log_notice "Can't create a listening socket: $@\n" unless $listen_soc +ket; #my $pid = init_server(PIDFILE,USER,GROUP,$port); log_notice "Server accepting connections on port $port\n"; while (!$quit) { next unless $connection = $listen_socket->accept; log_notice "Client connecting\n"; my $host = $connection->peerhost; foreach $ip_addr (@allow) { $valid=0; if($host == $ip_addr) { $valid=$ip_addr; last; } } if(!$valid) { $connection->close; log_warn "Connection attempt from invalid host : $host\n"; next; } $connection->setsockopt(SOL_SOCKET, SO_KEEPALIVE=>1); log_notice("Accepting a connection from $host\n"); interact($connection); log_notice("Connection from $host closed\n"); $connection->close; } # End While !quit sub interact { my $sock = shift; STDIN->fdopen($sock,"r") or die "Can't reopen STDIN: $!"; STDOUT->fdopen($sock,"w") or die "Can't reopen STDOUT: $!"; STDERR->fdopen($sock,"w") or die "Can't reopen STDERR: $!"; $| = 1; my ($cdr_path, $cdr_file, $record, $line, $file, $thishost, $filenam +e); $cdr_path="/var/cdrs"; $cdr_file=gen_name(); $record=0; $thishost=hostname; $filename=$cdr_path.'/'.$cdr_file; while ( <$sock> ) { until (-e $filename) { sleep 10; } next unless /\S/; # blank line log_notice "Tailing $filename\n"; $file=File::Tail->new(name=>$filename, maxinterval=>5, interval= +>1, tail=>-1, errmode=>\&do_exit); while ( defined($line=$file->read) ) { $record++; print $sock $record.";".$cdr_file.";".$thishost.";".$l +ine; } # End While # 2 &do_term($sock); } # End While sock } #End interact sub do_exit { log_notice("End Tail ... Closing Socket\n"); if (defined $connection) { $connection->shutdown(2); } sleep 2; exec '/usr/local/bin/logserver' || log_warn ("LOGSERVER : Could not exec $0\n"); exit 9; # Should not get here } # End do_exit sub do_term { log_notice("TERM signal received, terminating\n"); exit 0; }
And on the client side, here is just the connection part, most everything else is just how to handle the data it gets:
CONNECT: $socket = IO::Socket::INET->new("$host:$port"); unless ($socket) { # Potential Problem here $retry++; if($retry==3) { log_die ("Client : Error connecting to server ... $@\n +"); } log_notice ("Client : Retry $retry : Sleeping $timeout Second +s\n"); sleep($timeout); # wait a minute; goto CONNECT; } $retry=0; #Reset in case we disconnect early $connected=1; #So that we know... #Start from beginning, uncomment if you don't want too $recover_from=is_recovery($dbh); $recover_from++; $rec_num=0; $stCnt->finish; if ($recover_mode) { log_notice ("Client : Begin recovery mode from record number $ +recover_from\n"); } $SIG{TERM}=$SIG{INT}=$SIG{PIPE}=$SIG{CHLD} = sub { my $sig=shift; if (defined $sth) { $sth->finish; } if (defined $dbh ) { $dbh->disconnect; } if (defined $socket) { $socket->shutdown(2); } sleep(5); do { $pid = waitpid(-1,&WNOHANG); } until $pid == -1; log_notice ("Client : Caught signal SIG$sig\n"); if($sig eq 'PIPE') { log_notice ("Connection Ended\n"); } if($sig eq 'TERM' || $sig eq 'HUP' || $sig eq 'INT') { log_notice ("User Cancelling?\n"); exit 1; #User cancelling job ? } if ($connected) { ############################################################# # First check to see if this is early termination # Should only terminate at midnight. If this is # an early termination the assumption is that the # system we were connected to has croaked. If the # current time is less than $midnight then we try # to exec to ourself (RESTART..) where, hopefully # we will reconnect and recover where we left off # or give up after three times the $timeout. ############################################################# $timestamp=time; if($timestamp < $midnight) { log_notice ("Client : Restarting $0\n"); log_error("End Processing $src_cdr_file\n"); exec '/home/$0' || log_warn ("Client : Could not exec $0\n"); exit 6; # Something is wrong if this exit is taken } # End if $timestamp log_notice ("Client : Normal Termination\n"); log_error("End Processing $src_cdr_file\n\n"); exec '/usr/bin/perl', '/home/client' || log_warn ("Client : Could not exec $0\n"); exit 7; # Something is wrong if this exit is taken } # End if $connected }; # End anonymous sub print $socket "tail\n"; # Rock n Roll
Now I commented out the part where it does the is_recovery subroutine. The reason I did this is because the pipe between the client and server is never broken, but it just stops updating. If I restart the process, the recovery subroutine checks to see what line I was just on when the process was shut down and continues on from there. Because there seems to be some kind of timeout, or problem, I have it start from the beginning of the file to make sure it didn't miss anything. Also, I kept restarting the process and with the recovery part in there, and it wouldn't continue reading in files. May be a flaw there. Any ideas? I really need help on this one. Just doesn't make sense why few times a week now it just stops reading, but none of the processes die...

In reply to Weird IO::Socket problem by hallikpapa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.