checktime.in   [plain text]


#! @PATH_PERL@
#! @PATH_PERL@ -d
# 
# This script compares the time of several machines with the
# time on the local host.
#
# Use or modify it as you wish. 
#
# As the original author is only expecting 14 minutes of fame, 
# leaving his name attached would be appreciated.
#
# R. Gary Cutbill <rgary@chrysalis.com>
# 21 April 1999
#
$tol=2.0;
$|=1;
print "Time Check";

open(HOSTS,"ypcat hosts.byaddr |");  # get a list of hosts from the yp server.

while ($line=<HOSTS>) { # loop for each host getting the offset compared to localhost
	($addr,$host,$aliases)=split(/\s+/,$line,3);
        $res=`/usr/local/bin/ntptrace -m 1 -r 1 -t 1 $host`;
	print ".";
	chop $res;
        push (@results,$res);
}
print "\n";


#
# Sort the list of hosts, and print out there offsets
# from the local host.  
#
@list=sort appropriately @results;
foreach $i ( @list ) {

   @dargs=split(/\s+/,$i);
   if ( $dargs[1] eq "\*Timeout\*" ) { 
     print "$i\n";
     chop $dargs[0];
     push(@down,$dargs[0]);
   } else {
     printf "%-25s %7s %3s %6s %10s %5s %8s %8s\n",@dargs;
     if ( ( $dargs[4] > $tol ) || ( $dargs[4] < -$tol ) ) { 
	 chop $dargs[0];
         push(@toofarout,$dargs[0]); }
   }
}
#
# When the above list finishes, hosts that are different by +/- $tol (two seconds)
# are in @toofarout.  Hosts that are down are in @down. They are treated the same
# way here, but you might want to do something different depending on your site.
#
# print a set of suggested rsh commands to run on the hosts that 
# don't have "good" time.   "restartntp" is left as an excersize to the reader.
# I usually use it to kill a running xntpd, ntpdate some server, and the start xntp
# again.
# 
print "\nConsider:\n";
foreach $i ( (@down,@toofarout) ) {
    print "  rsh $i sudo restartntp\n";
}


#
# sort the results from the list.  First by stratum, then by time deviation
# Put hosts that didn't respond (timed out) on the bottom.
#
sub appropriately {
    @af=split(/\s+/,$a);
    @bf=split(/\s+/,$b);
    $aba= ($af[4]<0)?-$af[4]:$af[4];
    $abb= ($bf[4]<0)?-$bf[4]:$bf[4];

        ( $af[1] ne $bf[1] ) ? $bf[1] cmp $af[1] :
	   ( ( $af[2] != $bf[2] ) ? ( $bf[2] <=> $af[2] ) :
	     ( ( $aba != $abb )  ? ( $abb <=> $aba ) : ($af[0] cmp $bf[0] ) ) );
}