All of the interesting technological, artistic or just plain fun subjects I'd investigate if I had an infinite number of lifetimes. In other words, a dumping ground...

Thursday, 12 June 2008

NTP scripts

http://www.david-taylor.myby.co.uk/ntp/NTPandMRTG.txt

People have asked how I get the NTP statistics plotted by
MRTG. Well I got the idea from Piotr Trojanek who had the
following code for Unix:

#!/bin/sh

ntpq -c rv \
|tail -n 1 \
|sed 's/[a-z=,]//g' \
|mawk '{printf "%d\n%d\n",
int(($1 > 0) ? $1 : -$1),
int(($2 > 0) ? $2 : -$2)}'

ntpdc -c sysstats \
|mawk '{TOTSEC = $3;
H = int(TOTSEC/3600);
TOTSEC -= H*3600;
M = int(TOTSEC/60);
TOTSEC -= M*60;
printf "%02d:%02d:%02d\n", H, M, TOTSEC;
exit 0}'
echo "ntp_host"

as the command to be run from MRTG, but as I am running
Windows, I substituted a Perl script for the Unix script.
The first step is to get MRTG to take its data from a
command line rather than an SNMP query. You do this by
placing the command to run in back-quotes as the argument
to an MRTG "Target" statement. Then you need to write the
command or script to write the four values MRTG is expecting
to standard output.
---------------------------------------------------------------


Extract from mrtg.cfg
---------------------------------------------------------------

Target[odin_ntp]: `perl GetNTP.pl odin`
MaxBytes[odin_ntp]: 200
MaxBytes2[odin_ntp]: 200
Unscaled[odin_ntp]: dwmy
Timezone[odin_ntp]: GMT
Title[odin_ntp]: NTP statistics for Odin - offset from NTP
Options[odin_ntp]: integer, gauge, nopercent, growright
YLegend[odin_ntp]: offset+100 ms
ShortLegend[odin_ntp]: ms
LegendI[odin_ntp]:
LegendO[odin_ntp]: offset: 
Legend1[odin_ntp]: n/a
Legend2[odin_ntp]: time offset in ms, with 100ms offset added to ensure it's positive!
PageTop[odin_ntp]: <H1>NTP -- PC Odin</H1>


Perl script run to manipulate NTPQ output - file: GetNTP.pl
-----------------------------------------------------------

$ntp_str = `ntpq -c rv $ARGV[0]`;
$val = (split(/\,/,$ntp_str))[20];
$val =~ s/offset=//i;
$val = int ($val + 100);
if ($val < 0) {
$val = 0;
}
print "0\n";
print "$val\n";
print "0\n";
print "0\n";


-----------------------------------------------------------
Update 2004-Mar-13:

Thanks to Jim O'Boyle, who mentioned that I could use parameter-passing from
the mrtg.cfg to avoid hard-coding the node name into GetNTP.pl. The version
above includes his update.

Thanks, Jim!


-----------------------------------------------------------
Update 2006-Jul-10:

In February, I added a simple stratum 1 server, and added a different version
of the Perl script to cover the more limited range of +/-20 microseconds
(displayed as 0..40us). By July, the GPS was failing more often (tree leaf
growth?), so I modified the script to limit on both positive and negative
excursions (as without the GPS the server could be hundreds of microseconds
out).

$ntp_str = `ntpq -c rv $ARGV[0]`;
$val = (split(/\,/,$ntp_str))[20];
$val =~ s/offset=//i;
$val = 1000.0 * $val; # convert to microseconds
$report = int ($val + 20);
if ($report < 0) {
$report = 0;
}
if ($report > 40) {
$report = 40;
}
print "0\n";
print "$report\n";
print "0\n";
print "0\n";


Thanks, Jim!

No comments:

tim's shared items

Add to Google Reader or Homepage