]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ntp/scripts/ntptrace.in
This commit was generated by cvs2svn to compensate for changes in r172683,
[FreeBSD/FreeBSD.git] / contrib / ntp / scripts / ntptrace.in
1 #! @PATH_PERL@ -w
2
3 # John Hay -- John.Hay@icomtek.csir.co.za / jhay@FreeBSD.org
4
5 use Socket;
6 use Getopt::Std;
7 use vars qw($opt_n);
8
9 $ntpq = "ntpq";
10
11 getopts('n');
12
13 $dodns = 1;
14 $dodns = 0 if (defined($opt_n));
15
16 $host = shift;
17 $host ||= "127.0.0.1";
18
19 for (;;) {
20         $stratum = 255;
21         $cmd = "$ntpq -n -c rv $host";
22         open(PH, $cmd . "|") || die "failed to start command $cmd: $!";
23         while (<PH>) {
24                 $stratum = $1 if (/stratum=(\d+)/);
25                 $peer = $1 if (/peer=(\d+)/);
26                 # Very old servers report phase and not offset.
27                 $offset = $1 if (/(?:offset|phase)=([^\s,]+)/);
28                 $rootdelay = $1 if (/rootdelay=([^\s,]+)/);
29                 $refid = $1 if (/refid=([^\s,]+)/);
30         }
31         close(PH) || die "$cmd failed";
32         last if ($stratum == 255);
33         $offset /= 1000;
34         $rootdelay /= 1000;
35         $dhost = $host;
36         # Only do lookups of IPv4 addresses. The standard lookup functions
37         # of perl only do IPv4 and I don't know if we should require extras.
38         if ($dodns && $host =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/) {
39                 $iaddr = inet_aton($host);
40                 $name = (gethostbyaddr($iaddr, AF_INET))[0];
41                 $dhost = $name if (defined($name));
42         }
43         printf("%s: stratum %d, offset %f, root distance %f",
44             $dhost, $stratum, $offset, $rootdelay);
45         printf(", refid '%s'", $refid) if ($stratum == 1);
46         printf("\n");
47         last if ($stratum == 0 || $stratum == 1 || $stratum == 16);
48         last if ($refid =~ /^127\.127\.\d{1,3}\.\d{1,3}$/);
49
50         $cmd = "$ntpq -n -c \"pstat $peer\" $host";
51         open(PH, $cmd . "|") || die "failed to start command $cmd: $!";
52         $thost = "";
53         while (<PH>) {
54                 $thost = $1, last if (/srcadr=(\S+),/);
55         }
56         close(PH) || die "$cmd failed";
57         last if ($thost eq "");
58         $host = $thost;
59 }
60