use Time::Local; use Mail::Sendmail; use Net::Ping::External qw(ping); $pmwrap=0; while (6 ne 9){ # Loop forever... I don't mind. system("cp new.dnsf dnsfc"); # Each pass through the servers, copy dnsf again to catch changes open (DC,"dnsfc"); $previousline="does not exist"; while(){ open (PM,">> mon.txt"); chomp; if(/^##M/){ ($previousip,$previoushost)=split /\t/,$previousline; print PM $previoushost." : "; if($pmwrap eq 6){ #just to make the output pretty by printing a newline every seven servers. printtime(); $pmwrap=-1; } $pmwrap++; ckserv($previoushost,$previousip); } $previousline=$_; close PM; } close DC; } sub printtime{ open (PM,">> mon.txt"); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); $mon++; $year=$year+1900; print PM "\n".$mon."/".$mday."/".$year."-".$hour.":".$min."\n"; close PM; } sub ckserv{ #Check the current server $currentserver=@_[0]; #The first argument passed $currentip=@_[1]; $found=0; open (SP, "schedule.txt"); while (){ chomp; s/((?:\w|-)+),(\w+) (\d+)\/(\d+)\/(\d+) (\d+):(\d+):(\d+) to (\d+)\/(\d+)\/(\d+) (\d+):(\d+):(\d+)/ $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14/; # $1 - servername # $2 - ignore once|weekly|monthly # $3 - begin month # $4 - begin day # $5 - begin year # $6 - begin hour # $7 - begin minute # $8 - begin second # $9 - end month # $10 - end day # $11 - end year # $12 - end hour # $13 - end minute # $14 - end second $timeb=timelocal($8,$7,$6,$4,$3-1,$5); #full time of beginning of ignore (default time # format in seconds since 1/1/1970) $timee=timelocal($14,$13,$12,$10,$9-1,$11); #full time of end of ignore $timebdwk=timelocal($8,$7,$6,1,0,0); #hour/min/sec portion of beginning of ignore in time format $timeedwk=timelocal($14,$13,$12,1,0,0); #hour/min/sec portion of end of ignore in time format ($secb,$minb,$hourb,$mdayb,$monb,$yearb,$wdayb,$ydayb,$isdstb)=localtime($timeb); #split out the parts of the begin of ignore ($sece,$mine,$houre,$mdaye,$mone,$yeare,$wdaye,$ydaye,$isdste)=localtime($timee); #split out the parts of the end of ignore ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); #split out the parts of the current time $timeedwkn=timelocal($sec,$min,$hour,1,0,0); #hour/min/sec portion of current time in time format if ((($2 eq 'weekly' && $timeedwkn le $timeedwk && $timeedwkn ge $timebdwk && $wdayb eq $wday) || ($2 eq 'monthly' && $timeedwkn le $timeedwk && $timeedwkn ge $timebdwk && $mdayb eq $mday) || ($2 eq 'once' && time le $timee&& time ge $timeb)) && (($1 eq 'everybodydown') || ($1 eq $currentserver))){ $found=1; } } if(pf()){ # 10 pings has to fail 4 times before Mon Pages open (PM,">> mon.txt"); print PM "10 pings to server ".$currentserver.", but router passed \n"; close PM; printtime(); if(pf()){ open (PM,">> mon.txt"); print PM "20 pings to server ".$currentserver.", but router passed \n"; close PM; printtime(); if(pf()){ open (PM,">> mon.txt"); print PM "30 pings to server ".$currentserver.", but router passed \n"; close PM; printtime(); if(pf()){ if($found ne 1){ ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); $mon++; my %mail = ( To => 'agatha@nospam.com', From => 'pingmon@nospam.com', Message => $text, Subject => $currentserver." is down!", SMTP => '216.40.230.46' ); sendmail(%mail) or die $Mail::Sendmail::error; open (PM,">> mon.txt"); print PM "Server down -- Sending Page\n"; close PM; } else{ my %mail = ( To => 'agatha@nospam.com', From => 'pingmon@nospam.com', Message => $text, Subject => $currentserver." Scheduled Downtime", SMTP => '216.40.230.46' ); sendmail(%mail) or die $Mail::Sendmail::error; open (PM,">>mon.txt"); print PM "Server down -- scheduled\n"; close PM; } } printtime(); } } } } sub pf{ if(ping(hostname => $currentip, count => 10, size => 16, timeout => 3)){ return 0; } open (PM,">> mon.txt"); print PM "\n*************** Ping Fail : ".$currentserver." ***********\n"; close PM; return rf(); } sub rf{ $currentrouter=$currentip; $currentrouter=~s/(\d+\.\d+)(\.\d+\.)(\d+)/$1$2$3/; #Parse out the first two octets as $1, the third octet as $2, and the fourth octet as $3 #We need these to calculate the router to ping if($1 eq "10.40"){ $currentrouter=$1.".0.1"; } elsif($currentserver eq "pukey"){ $currentrouter="10.10.10.10"; } else{ $currentrouter=$1.$2."1"; } open (PM,">> mon.txt"); print PM "Ping failed... testing ".$currentserver." : ".$currentip." with router ".$currentrouter."\n"; printtime(); close PM; return (ping(hostname => $currentrouter, count => 10, size => 16, timeout => 3)); }