Merge from vendor branch LIBPCAP:
[dragonfly.git] / contrib / ntp / scripts / checktime
1 #!/usr/local/bin/perl 
2 #!/usr/local/bin/perl -d
3
4 # This script compares the time of several machines with the
5 # time on the local host.
6 #
7 # Use or modify it as you wish. 
8 #
9 # As the original author is only expecting 14 minutes of fame, 
10 # leaving his name attached would be appreciated.
11 #
12 # R. Gary Cutbill <rgary@chrysalis.com>
13 # 21 April 1999
14 #
15 $tol=2.0;
16 $|=1;
17 print "Time Check";
18
19 open(HOSTS,"ypcat hosts.byaddr |");  # get a list of hosts from the yp server.
20
21 while ($line=<HOSTS>) { # loop for each host getting the offset compared to localhost
22         ($addr,$host,$aliases)=split(/\s+/,$line,3);
23         $res=`/usr/local/bin/ntptrace -m 1 -r 1 -t 1 $host`;
24         print ".";
25         chop $res;
26         push (@results,$res);
27 }
28 print "\n";
29
30
31 #
32 # Sort the list of hosts, and print out there offsets
33 # from the local host.  
34 #
35 @list=sort appropriately @results;
36 foreach $i ( @list ) {
37
38    @dargs=split(/\s+/,$i);
39    if ( $dargs[1] eq "\*Timeout\*" ) { 
40      print "$i\n";
41      chop $dargs[0];
42      push(@down,$dargs[0]);
43    } else {
44      printf "%-25s %7s %3s %6s %10s %5s %8s %8s\n",@dargs;
45      if ( ( $dargs[4] > $tol ) || ( $dargs[4] < -$tol ) ) { 
46          chop $dargs[0];
47          push(@toofarout,$dargs[0]); }
48    }
49 }
50 #
51 # When the above list finishes, hosts that are different by +/- $tol (two seconds)
52 # are in @toofarout.  Hosts that are down are in @down. They are treated the same
53 # way here, but you might want to do something different depending on your site.
54 #
55 # print a set of suggested rsh commands to run on the hosts that 
56 # don't have "good" time.   "restartntp" is left as an excersize to the reader.
57 # I usually use it to kill a running xntpd, ntpdate some server, and the start xntp
58 # again.
59
60 print "\nConsider:\n";
61 foreach $i ( (@down,@toofarout) ) {
62     print "  rsh $i sudo restartntp\n";
63 }
64
65
66 #
67 # sort the results from the list.  First by stratum, then by time deviation
68 # Put hosts that didn't respond (timed out) on the bottom.
69 #
70 sub appropriately {
71     @af=split(/\s+/,$a);
72     @bf=split(/\s+/,$b);
73     $aba= ($af[4]<0)?-$af[4]:$af[4];
74     $abb= ($bf[4]<0)?-$bf[4]:$bf[4];
75
76         ( $af[1] ne $bf[1] ) ? $bf[1] cmp $af[1] :
77            ( ( $af[2] != $bf[2] ) ? ( $bf[2] <=> $af[2] ) :
78              ( ( $aba != $abb )  ? ( $abb <=> $aba ) : ($af[0] cmp $bf[0] ) ) );
79 }