2 # $Id: summary.in,v 1.2 2003/12/04 16:23:38 drochner Exp $
3 # Perl version of (summary.sh, loop.awk, peer.awk):
4 # Create summaries from xntpd's loop and peer statistics.
6 # Copyright (c) 1997, 1999 by Ulrich Windl <Ulrich.Windl@rz.uni-regensburg.de>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 require 5.003; # "never tested with any other version of Perl"
27 my $log_date_pattern = '[12]\d{3}[01]\d[0-3]\d';
28 my $statsdir = "/var/log/ntp"; # directory with input files
29 my $outputdir = "/tmp"; # directory for output files
30 my $skip_time_steps = 3600.0; # ignore time offsets larger that this
31 my $startdate = "19700101"; # first data file to use (YYYYMMDD)
32 my $enddate=`date -u +%Y%m%d`; chomp $enddate; --$enddate;
33 my $peer_dist_limit = 400.0;
35 my %options = ("directory|input-directory=s" => \$statsdir,
36 "output-directory=s" => \$outputdir,
37 "skip-time-steps:f" => \$skip_time_steps,
38 "start-date=s" => \$startdate,
39 "end-date=s" => \$enddate,
40 "peer-dist-limit=f" => \$peer_dist_limit);
42 if ( !GetOptions(%options) )
44 print STDERR "valid options for $0 are:\n";
46 foreach $opt (sort(keys %options)) {
47 print STDERR "\t--$opt\t(default is ";
48 if ( ref($options{$opt}) eq "ARRAY" ) {
49 print STDERR join(", ", map { "'$_'" } @{$options{$opt}});
51 print STDERR "'${$options{$opt}}'";
59 # check possibly current values of options
60 die "$statsdir: no such directory" unless (-d $statsdir);
61 die "$outputdir: no such directory" unless (-d $outputdir);
62 die "$skip_time_steps: skip-time-steps must be positive"
63 unless ($skip_time_steps >= 0.0);
64 die "$startdate: invalid start date|$`|$&|$'"
65 unless ($startdate =~ m/.*$log_date_pattern$/);
66 die "$enddate: invalid end date"
67 unless ($enddate =~ m/.*$log_date_pattern$/);
69 $skip_time_steps = 0.128 if ($skip_time_steps == 0);
73 my ($result, @rest) = @_;
74 map { $result = $_ if ($_ < $result) } @rest;
80 my ($result, @rest) = @_;
81 map { $result = $_ if ($_ > $result) } @rest;
85 # calculate mean, range, and standard deviation for offset and frequency
88 my ($directory, $fname, $out_file) = @_;
89 print "$directory/$fname\n";
90 open INPUT, "$directory/$fname" or warn "can't open $directory/$fname: $!";
91 open OUTPUT, ">>$out_file" or die "can't open $out_file: $!";
92 print OUTPUT "$fname\n";
93 my ($loop_tmax, $loop_fmax) = (-1e9, -1e9);
94 my ($loop_tmin, $loop_fmin) = (1e9, 1e9);
95 my ($loop_time_rms, $loop_freq_rms) = (0, 0);
102 chop; # strip record separator
105 #NTPv3: 50529 74356.259 -0.000112 16.1230 8
106 #NTPv3: day, sec.msec, offset, drift_comp, sys_poll
107 #NTPv4: 51333 54734.582 0.000001648 16.981964 0.000001094 0.020938 6
108 #NTPv4: day, sec.msec, offset, drift_comp, sys_error, clock_stabil, sys_poll
109 if ($Fld[2] > $skip_time_steps || $Fld[2] < -$skip_time_steps) {
110 warn "ignoring loop offset $Fld[2] (file $fname, line $.)\n";
114 ($offs, $freq) = ($Fld[2], $Fld[3]);
115 $loop_tmax = max($loop_tmax, $offs);
116 $loop_tmin = min($loop_tmin, $offs);
117 $loop_fmax = max($loop_fmax, $freq);
118 $loop_fmin = min($loop_fmin, $freq);
120 $loop_time_rms += $offs * $offs;
122 $loop_freq_rms += $freq * $freq;
125 if ($loop_count > 1) {
126 $loop_time /= $loop_count;
127 $loop_time_rms = $loop_time_rms / $loop_count - $loop_time * $loop_time;
128 if ($loop_time_rms < 0) {
129 warn "loop_time_rms: $loop_time_rms < 0";
132 $loop_time_rms = sqrt($loop_time_rms);
133 $loop_freq /= $loop_count;
134 $loop_freq_rms = $loop_freq_rms / $loop_count - $loop_freq * $loop_freq;
135 if ($loop_freq_rms < 0) {
136 warn "loop_freq_rms: $loop_freq_rms < 0";
139 $loop_freq_rms = sqrt($loop_freq_rms);
141 ("loop %d, %.0f+/-%.1f, rms %.1f, freq %.2f+/-%0.3f, var %.3f\n",
142 $loop_count, ($loop_tmax + $loop_tmin) / 2 * 1e6,
143 ($loop_tmax - $loop_tmin) / 2 * 1e6, $loop_time_rms * 1e6,
144 ($loop_fmax + $loop_fmin) / 2, ($loop_fmax - $loop_fmin) / 2,
148 warn "no valid lines in $directory/$fname";
153 # calculate mean, standard deviation, maximum offset, mean dispersion,
154 # and maximum distance for each peer
157 my ($directory, $fname, $out_file) = @_;
158 print "$directory/$fname\n";
159 open INPUT, "$directory/$fname" or warn "can't open $directory/$fname: $!";
160 open OUTPUT, ">>$out_file" or die "can't open $out_file: $!";
161 print OUTPUT "$fname\n";
162 # we toss out all distances greater than one second on the assumption the
163 # peer is in initial acquisition
164 my ($n, $MAXDISTANCE) = (0, 1.0);
178 chop; # strip record separator
181 #NTPv3: 50529 83316.249 127.127.8.1 9674 0.008628 0.00000 0.00700
182 #NTPv3: day, sec.msec, addr, status, offset, delay, dispersion
183 #NTPv4: 51333 56042.037 127.127.8.1 94f5 -0.000014657 0.000000000 0.000000000 0.000013214
184 #NTPv4: day, sec.msec, addr, status, offset, delay, dispersion, skew
186 $dist = $Fld[6] + $Fld[5] / 2;
187 next if ($dist > $MAXDISTANCE);
189 if ($offs > $skip_time_steps || $offs < -$skip_time_steps) {
190 warn "ignoring peer offset $offs (file $fname, line $.)\n";
194 for ($j = 0; $j < $n; $j++) {
195 if ($Fld[2] eq $peer_ident{$j}) {
196 $i = $j; # peer found
200 if ($i == $n) { # add new peer
201 $peer_ident{$i} = $Fld[2];
202 $peer_tmax{$i} = $peer_dist{$i} = -1e9;
203 $peer_tmin{$i} = 1e9;
204 $peer_time{$i} = $peer_time_rms{$i} = 0;
205 $peer_delay{$i} = $peer_disp{$i} = 0;
210 $peer_tmax{$i} = max($peer_tmax{$i}, $offs);
211 $peer_tmin{$i} = min($peer_tmin{$i}, $offs);
212 $peer_dist{$i} = max($peer_dist{$i}, $dist);
213 $peer_time{$i} += $offs;
214 $peer_time_rms{$i} += $offs * $offs;
215 $peer_delay{$i} += $Fld[5];
216 $peer_disp{$i} += $Fld[6];
220 " ident cnt mean rms max delay dist disp\n";
222 "==========================================================================\n";
224 for ($i = 0; $i < $n; $i++) {
225 next if $peer_count{$i} < 2;
226 $peer_time{$i} /= $peer_count{$i};
227 eval { $peer_time_rms{$i} = sqrt($peer_time_rms{$i} / $peer_count{$i} -
228 $peer_time{$i} * $peer_time{$i}); };
229 $peer_time_rms{$i} = 0, warn $@ if $@;
230 $peer_delay{$i} /= $peer_count{$i};
231 $peer_disp{$i} /= $peer_count{$i};
232 $peer_tmax{$i} = $peer_tmax{$i} - $peer_time{$i};
233 $peer_tmin{$i} = $peer_time{$i} - $peer_tmin{$i};
234 if ($peer_tmin{$i} > $peer_tmax{$i}) { # can this happen at all?
235 $peer_tmax{$i} = $peer_tmin{$i};
238 "%-15s %4d %8.3f %8.3f %8.3f %8.3f %8.3f %8.3f\n",
239 $peer_ident{$i}, $peer_count{$i}, $peer_time{$i} * 1e3,
240 $peer_time_rms{$i} * 1e3, $peer_tmax{$i} * 1e3,
241 $peer_delay{$i} * 1e3, $peer_dist{$i} * 1e3, $peer_disp{$i} * 1e3;
243 print OUTPUT sort @lines;
249 my ($directory, $fname, $out_file) = @_;
250 print "$directory/$fname\n";
251 open INPUT, "$directory/$fname";
252 open OUTPUT, ">>$out_file" or die "can't open $out_file: $!";
253 print OUTPUT "$fname\n";
262 my (%peer_ident, %peer_count, %peer_mean, %peer_var, %peer_max);
263 my (%peer_1, %peer_2, %peer_3, %peer_4);
266 open INPUT, "<$in_file" or die "can't open $in_file: $!";
270 chop; # strip record separator
272 next if ($#Fld < 7 || $Fld[0] eq 'ident');
274 for ($j = 0; $j < $n; $j++) {
275 if ($Fld[0] eq $peer_ident{$j}) {
280 if ($i == $n) { # add new peer
281 $peer_count{$i} = $peer_mean{$i} = $peer_var{$i} = 0;
283 $peer_1{$i} = $peer_2{$i} = $peer_3{$i} = $peer_4{$i} = 0;
284 $peer_ident{$i} = $Fld[0];
287 $dist = $Fld[6] - $Fld[5] / 2;
288 if ($dist < $peer_dist_limit) {
290 $peer_mean{$i} += $Fld[2];
291 $peer_var{$i} += $Fld[3] * $Fld[3];
293 $peer_max{$i} = max($peer_max{$i}, $max);
308 warn "dist exceeds limit: $dist (file $in_file, line $.)\n";
314 " host days mean rms max >1 >5 >10 >50\n";
316 "==================================================================\n";
317 for ($i = 0; $i < $n; $i++) {
318 next if ($peer_count{$i} < 2);
319 $peer_mean{$i} /= $peer_count{$i};
320 eval { $peer_var{$i} = sqrt($peer_var{$i} / $peer_count{$i} -
321 $peer_mean{$i} * $peer_mean{$i}); };
322 $peer_var{$i} = 0, warn $@ if $@;
324 "%-15s %3d %9.3f% 9.3f %9.3f %3d %3d %3d %3d\n",
325 $peer_ident{$i}, $peer_count{$i}, $peer_mean{$i}, $peer_var{$i},
326 $peer_max{$i}, $peer_1{$i}, $peer_2{$i}, $peer_3{$i}, $peer_4{$i};
331 my $loop_summary="$outputdir/loop_summary";
332 my $peer_summary="$outputdir/peer_summary";
333 my $clock_summary="$outputdir/clock_summary";
334 my (@loopfiles, @peerfiles, @clockfiles);
336 print STDERR "Creating summaries from $statsdir ($startdate to $enddate)\n";
338 opendir SDIR, $statsdir or die "directory ${statsdir}: $!";
340 @loopfiles=sort grep /loop.*$log_date_pattern/, readdir SDIR;
342 @peerfiles=sort grep /peer.*$log_date_pattern/, readdir SDIR;
344 @clockfiles=sort grep /clock.*$log_date_pattern/, readdir SDIR;
347 # remove old summary files
348 map { unlink $_ if -f $_ } ($loop_summary, $peer_summary, $clock_summary);
352 $date = $_; $date =~ s/.*($log_date_pattern)$/$1/;
353 if ($date ge $startdate && $date le $enddate) {
354 do_loop $statsdir, $_, $loop_summary;
359 $date = $_; $date =~ s/.*($log_date_pattern)$/$1/;
360 if ($date ge $startdate && $date le $enddate) {
361 do_peer $statsdir, $_, $peer_summary;
366 $date = $_; $date =~ s/.*($log_date_pattern)$/$1/;
367 if ($date ge $startdate && $date le $enddate) {
368 do_clock $statsdir, $_, $clock_summary;
372 print STDERR "Creating peer summary with limit $peer_dist_limit\n";
373 peer_summary $peer_summary if (-f $peer_summary);