#!/usr/bin/perl
require 'ctime.pl';
use POSIX;
$!=1;
sub convmon {
my ($monin)=@_;
if ($mon==0) { $monout="Jan"; }
if ($mon==1) { $monout="Feb"; }
if ($mon==2) { $monout="Mar"; }
if ($mon==3) { $monout="Apr"; }
if ($mon==4) { $monout="May"; }
if ($mon==5) { $monout="Jun"; }
if ($mon==6) { $monout="Jul"; }
if ($mon==7) { $monout="Aug"; }
if ($mon==8) { $monout="Sep"; }
if ($mon==9) { $monout="Oct"; }
if ($mon==10) { $monout="Nov"; }
if ($mon==11) { $monout="Dec"; }
return($monout);
}
sub convwday {
my ($wdayin)=@_;
if ($wdayin==0) { $wdayout="Sun"; }
if ($wdayin==1) { $wdayout="Mon"; }
if ($wdayin==2) { $wdayout="Tue"; }
if ($wdayin==3) { $wdayout="Wed"; }
if ($wdayin==4) { $wdayout="Thu"; }
if ($wdayin==5) { $wdayout="Fri"; }
if ($wdayin==6) { $wdayout="Sat"; }
return($wdayout);
}
sub logmsg {
my ($msg)=@_;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$esecs=mktime($sec,$min,$hour,$mday,$mon,$year,0,0,0);
$month=convmon($mon);
if ($min<10) {$min="0".$min;}
if ($sec<10) {$sec="0".$sec;}
print "$month $mday at $hour:$min:$sec - $msg\n";
return($esecs);
}
sub web_print {
open(F,">/usr/local/www/data/check_connection.htm") || die "Unable to open web file";
select (F);
if ($failed==0) {print "
UP - Escher House web connection\n";}
if ($failed==1) {print "DOWN - Escher House web connection\n";}
print "\n";
print "";
print "Connection Currently ";
if ($failed==0) { print "UP" };
if ($failed==1) { print "DOWN" };
print " since ";
logmsg("\n
");
printf "Down $downcount times for a total of %5.2f $tsmh for the last %5.2f days (%2.1f\%).\n
",$disptotal,$days,$percdown;
select STDOUT;
close(F);
}
$downcount=0;
$failed=0;
$port = 80; #use httpd
$them = '47324.com';
$AF_INET = 2;
$SOCK_STREAM = 1;
$SIG{'INT'} = 'dokill';
sub dokill { kill 9,$child if $child; }
$sockaddr = 'S n a4 x8';
chop($hostname = `hostname`);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$startsecs= mktime($sec,$min,$hour,$mday,$mon,$year,0,0,0);
if ($startmin<10) {$startmin="0".$startmin;}
$mon=convmon($mon);
$wday=convwday($wday);
printf "Started $wday $mon $mday at $hour:$min.\n";
web_print;
retry:
($name,$aliases, $proto) = getprotobyname('tcp');
($name,$aliases,$port) = getservbyname($port,'tcp')
unless $port =~ /^\d+$/;;
($name,$aliases,$type,$len,$thisaddr) = gethostbyname($hostname);
($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
$this = pack($sockaddr, $AF_INET, 0, $thisaddr);
$that = pack($sockaddr, $AF_INET,$port,$thataddr);
#Make the socket filehandle
if (!socket(S, $AF_INET, $SOCK_STREAM, $proto)) { die $!; }
#Give the socket an address
if (!bind(S, $this)) { die $!; }
#Call up the server
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$esecs=mktime($sec,$min,$hour,$mday,$mon,$year,0,0,0);
$days=($esecs-$startsecs)/60/60/24;
if (!connect(S,$that)) {
if ($failed==0) {
$failed=1;
$downcount++;
web_print;
$startfail=logmsg("Connection failed");
}
sleep 10;
goto retry;
}
if ($failed==1) {
$failed=0;
$endfail=logmsg("Connection Restored");
$elapsed=$endfail-$startfail;
$disptotal=$totalelapsed=$totalelapsed+$elapsed;
$esmh="seconds";
if ($elapsed>60) {$elapsed=$elapsed/60; $esmh="minutes";}
if ($elapsed>60) {$elapsed=$elapsed/60; $esmh="hours";}
$tsmh="seconds";
if ($totalelapsed>3600) {$disptotal=$disptotal/3600; $tsmh="hours";} else {
if ($totalelapsed>60) {$disptotal=$disptotal/60; $tsmh="minutes";} }
printf " Down for %5.2f $esmh.\n",$elapsed;
$days=($endfail-$startsecs)/60/60/24;
$percdown=($totalelapsed/($endfail-$startsecs))*100;
web_print;
printf " $downcount times for a total of %5.2f $tsmh for the last %5.2f days (%2.1f\%).\n",$disptotal,$days,$percdown;
}
#Set socket to be command buffered
if (!open(F, '>/tmp/checkconn')) { die $!; }
select(S); $| = 1; select(STDOUT);
#Avoid deadlock by forking
if ($child = fork) {
# print "sending strings\n";
# print S "GET\n\r";
sleep 3;
do dokill();
select(S); $| = 1; select(STDOUT);
# print "sent GET\n";
close(S);
wait();
} else {
# while () {
# $line=$_;
# chop($line);
# $lc++;
# if ($lc==1) {
# if ($line=~'\') {} else { logmsg("Get Failed");}
# }
# }
}
select(STDOUT);
close(F);
sleep 10;
goto retry;