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_socket; #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, $filename); $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.";".$line; } # 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; }