#!/usr/bin/perl # © 2001 Ronald Florence # last modified ron@rosie.18james.com, 14 Jun 2002 use Socket; my %d; my $conf = "/etc/ntp.conf"; my $ntpq = "/usr/bin/ntpq"; if ($#ARGV == 1) { for (@ARGV) { $_ = join ".", unpack('C4', (gethostbyname $_)[4]) if /[a-zA-Z]/; $d{$_} = 0; } } elsif (open (NTPCONF, $conf)) { while () { if (/^server/) { ($s, $server) = split; $ip = ($server =~ /[A-Za-z]/) ? join ".", unpack('C4', (gethostbyname $server)[4]) : $server; $d{$ip} = 0; last if keys(%d) == 2; } } } else { die "usage: $0 [ntp-server-1 ntp-server-2]\n" ; } foreach (`$ntpq -pn`) { ($remote,$ref,$st,$t,$w,$p,$reach,$delay,$o,$d) = split; for (keys %d) { $d{$_} = $delay*1000 if $remote =~ /$_/ && $reach > 0 && $delay > 0; } if ($remote =~ /^\*/) { $peer = substr $remote, 1; $id = $ref; $stratum = $st; } } foreach ($peer, $id) { if (/[a-zA-Z]/) { s/^\.|\.$//g; } else { $_ = gethostbyaddr pack('C4', split /\./, $_), AF_INET || $_; } } if ($#ARGV == 1) { print map "$d{$_}\n", @ARGV; } else { print map "$_\n", values %d; } print "stratum $stratum\n", "$peer ($id)\n"; __END__ =head1 NAME delay.pl - charts and logs network delays for mrtg =head1 SYNOPSIS Requires a working NTP installation. Use some variation of the following in the mrtg.cfg file: Target[delay]: `./delay.pl ntp-server1 ntp-server2` Options[delay]: growright, gauge, nopercent MaxBytes[delay]: 500000 Title[delay]: Network Delay PageTop[delay]:

NTP Network Delay

kMG[delay]: µ,m,, YLegend[delay]: delay (ms) ShortLegend[delay]: sec LegendI[delay]: NTP-1: LegendO[delay]: NTP-2: Legend1[delay]: NTP network delay to ntp-server-1 Legend2[delay]: NTP network delay to ntp-server-2 If the first two servers in your /etc/inet/ntp.conf file are the servers you want to use to measure delays, you don't need to specify the servers on the command line. =head1 DESCRIPTION A simple perl script to read and log the delays from the ntpq -p command on a system running NTP, and massage the data into a format suitable for mrtg. Outputs delays from two NTP-servers in µsec, and the stratum, server and refid of the server with which the local clocks are synced -- in mrtg format. =head1 LICENSE You may distribute this script under the same license as Perl itself. =head1 AUTHOR Ronald Florence =cut