xref: /freebsd/contrib/ntp/scripts/stats/peer.awk (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
1# awk program to scan peerstats files and report errors/statistics
2#
3# usage: awk -f peer.awk peerstats
4#
5# format of peerstats record
6#  MJD    sec    ident   stat  offset (s)  delay (s)  disp (s)
7# 49235 11.632 128.4.2.7 f414  -0.000041    0.21910   0.00084
8#
9# format of output dataset (time values in milliseconds)
10# peerstats.19960706
11#        ident     cnt     mean     rms      max     delay     dist     disp
12# ==========================================================================
13# 140.173.112.2     85   -0.509    1.345    4.606   80.417   49.260    1.092
14# 128.4.1.20      1364    0.058    0.364    4.465    3.712   10.540    1.101
15# 140.173.16.1    1415   -0.172    0.185    1.736    3.145    5.020    0.312
16#...
17#
18BEGIN {
19	n = 0
20	MAXDISTANCE = 1.0
21}
22#
23# scan all records in file
24#
25# we toss out all distances greater than one second on the assumption the
26# peer is in initial acquisition
27#
28{
29	if (NF >= 7 && ($7 + $6 / 2) < MAXDISTANCE) {
30		i = n
31		for (j = 0; j < n; j++) {
32			if ($3 == peer_ident[j])
33				i = j
34		}
35		if (i == n) {
36			peer_ident[i] = $3
37			peer_tmax[i] = peer_dist[i] = -1e9
38			peer_tmin[i] = 1e9
39			n++
40		}
41		peer_count[i]++
42		if ($5 > peer_tmax[i])
43			peer_tmax[i] = $5
44		if ($5 < peer_tmin[i])
45			peer_tmin[i] = $5
46		dist = $7 + $6 / 2
47		if (dist > peer_dist[i])
48			peer_dist[i] = dist
49		peer_time[i] += $5
50		peer_time_rms[i] += $5 * $5
51		peer_delay[i] += $6
52		peer_disp[i] +=  $7
53	}
54} END {
55	printf "       ident     cnt     mean     rms      max     delay     dist     disp\n"
56	printf "==========================================================================\n"
57	for (i = 0; i < n; i++) {
58		peer_time[i] /= peer_count[i]
59                peer_time_rms[i] = sqrt(peer_time_rms[i] / peer_count[i] - peer_time[i] * peer_time[i])
60		peer_delay[i] /= peer_count[i]
61		peer_disp[i] /= peer_count[i]
62		peer_tmax[i] = peer_tmax[i] - peer_time[i]
63		peer_tmin[i] = peer_time[i] - peer_tmin[i]
64		if (peer_tmin[i] > peer_tmax[i])
65			peer_tmax[i] = peer_tmin[i]
66		printf "%-15s%5d%9.3f%9.3f%9.3f%9.3f%9.3f%9.3f\n", peer_ident[i], peer_count[i], peer_time[i] * 1e3, peer_time_rms[i] * 1e3, peer_tmax[i] * 1e3, peer_delay[i] * 1e3, peer_dist[i] * 1e3, peer_disp[i] * 1e3
67	}
68}
69