5 # Based on a perl script by
6 # John Hay -- John.Hay@icomtek.csir.co.za / jhay@FreeBSD.org
8 # awk version by Frederick Bruckman <bruckman@ntp.org>
12 print "usage: ntptrace [-nv] [-r retries] [host]"
16 function getargs( command, i, argc, argv, done) {
20 command = "getopt nr:v"
21 for (i = 2; i <= ARGC; i++)
22 command = command " " ARGV[i-1]
24 argc = split($0, argv)
25 for (i = 1; i <= argc; i++) {
26 if (argv[i] == "-n") use_numeric = 1
27 if (argv[i] == "-v") do_verbose = 1
28 if (argv[i] == "-r") retries = argv[++i]
29 if (done == 1) host = argv[i]
30 if (argv[i] == "--") done = 1
33 if (retries != retries + 0)
37 function do_one_server( command, i, nvars, vars, stratum, peer, offset,
38 rootdelay, rootdispersion, refid, srcadr) {
44 command = "ntpq -n -c rv " host
45 while (command | getline) {
47 nvars = split($0, vars)
48 for (i = 1; i <= nvars; i++) {
49 if (vars[i] ~ /stratum=/) {
51 sub(/stratum=/, "", stratum)
53 if (vars[i] ~ /peer=/) {
55 sub(/peer=/, "", peer)
57 if (vars[i] ~ /offset=/) {
59 sub(/offset=/, "", offset)
61 if (vars[i] ~ /phase=/) {
63 sub(/phase=/, "", offset)
65 if (vars[i] ~ /rootdelay=/) {
67 sub(/rootdelay=/, "", rootdelay)
69 if (vars[i] ~ /rootdispersion=/) {
70 rootdispersion = vars[i]
71 sub(/rootdispersion=/, "", rootdispersion)
73 if (vars[i] ~ /refid=/) {
75 sub(/refid=/, "", refid)
83 offset = offset / 1000
84 syncdistance = (rootdispersion + (rootdelay / 2)) / 1000
86 printf("%s: stratum %d, offset %f, synch distance %f",
87 dhost(host), stratum, offset, syncdistance)
88 if (stratum == 1) printf(", refid '\''%s'\''", refid)
91 if (stratum == 0 || stratum == 1 || stratum == 16)
93 if (refid ~ /127\.127\.[0-9]{1,3}\.[0-9]{1,3}/)
96 command = "ntpq -n -c '\''pstat " peer "'\'' " host
97 while (command | getline) {
99 nvars = split($0, vars)
100 for (i = 1; i <= nvars; i++) {
101 if (vars[i] ~ /srcadr=/) {
103 sub(/srcadr=/, "", srcadr)
108 if (srcadr ~ /127\.127\.[0-9]{1,3}\.[0-9]{1,3}/ || srcadr == "")
115 function dhost(myhost, command) {
116 command = "host " myhost
117 while (command | getline) {
119 if (/address/) return $NF
121 if (/pointer/) return $NF