]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
ChangeLog, README, .del-ntpsweep~dff61aa1:
authorHarlan Stenn <stenn@ntp.org>
Wed, 29 Dec 1999 23:42:41 +0000 (23:42 -0000)
committerHarlan Stenn <stenn@ntp.org>
Wed, 29 Dec 1999 23:42:41 +0000 (23:42 -0000)
  * scripts/ntpsweep: Nifty utility
  From: Hans.Lambermont@nl.origin-it.com

bk: 386a9c71PE8dnqxqjarNVSJlbrRxmw

ChangeLog
scripts/README
scripts/ntpsweep [new file with mode: 0755]

index 76b20240f8351f07f0e9463b87dd55de1efab98f..1b141c849c552e0b3a8f46f64ba3903d63e3259c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 1999-12-29  Harlan Stenn  <stenn@whimsy.udel.edu>
 
+       * scripts/ntpsweep: Nifty utility
+       From: Hans.Lambermont@nl.origin-it.com
+
        * ntpd/refclock_fg.c: 
        * ntpd/refclock_conf.c: 
        * ntpd/ntp_control.c: 
index 37442f152998f48cf0efc1d5a6a3be5bc374a57c..417cc559c12f9b738f743a66a7de09757b8c076b 100644 (file)
@@ -20,6 +20,10 @@ ntp-groper   script useful for reaching out and rattling the cages of
 
 ntp-restart    script useful for killing and restarting the NTP daemon
 
+ntpsweep       prints per host given in <file> the NTP stratum level, the
+               clock offset in seconds, the daemon version, the operating
+               system and the processor.
+
 ntpver         What version of the NTP daemon is running?
 
 stats          directory containing awk and shell scripts useful for
diff --git a/scripts/ntpsweep b/scripts/ntpsweep
new file mode 100755 (executable)
index 0000000..298cf06
--- /dev/null
@@ -0,0 +1,262 @@
+#!/usr/local/bin/perl -w
+#
+# $Id$
+#
+# DISCLAIMER
+# 
+# Copyright (C) 1999 Hans Lambermont and Origin B.V.
+# 
+# Permission to use, copy, modify and distribute this software and its
+# documentation for any purpose and without fee is hereby granted,
+# provided that the above copyright notice appears in all copies and
+# that both the copyright notice and this permission notice appear in
+# supporting documentation. This software is supported as is and without
+# any express or implied warranties, including, without limitation, the
+# implied warranties of merchantability and fitness for a particular
+# purpose. The name Origin B.V. must not be used to endorse or promote
+# products derived from this software without prior written permission.
+#
+
+use Getopt::Long;       # GetOptions()
+use strict;
+
+(my $program = $0) =~ s%.*/(.+?)(.pl)?$%$1%;
+
+# Hardcoded paths/program names
+my $ntpdate = "ntpdate";
+my $ntpq = "ntpq";
+
+# no STDOUT buffering
+$| = 1;
+
+my ($help, $single_host, $showpeers, $strip);
+my $res = GetOptions("help!"   => \$help,
+                    "host=s"  => \$single_host,
+                    "peers!"  => \$showpeers,
+                    "strip=s" => \$strip);
+
+if ($help || ((@ARGV != 1) && !$single_host)) {
+    warn <<EOF;
+Usage:
+  $program [--help|--peers|--strip <string>] <file>|[--host <hostname>]
+
+Description:
+  $program prints per host given in <file> the NTP stratum level, the
+  clock offset in seconds, the daemon version, the operating system and
+  the processor.
+
+Options:
+--help
+    Print this short help text and exit.
+<file>
+    Specify the hosts file. File format is one hostname or ip number per
+    line. Lines beginning with # are considered as comment.
+--host <hostname>
+    Speficy a single host, bypassing the need for a hosts file.
+--peers
+    List all peers a host synchronizes to.
+--strip <string>
+    Strip <string> from hostnames.
+
+Example:
+    $program myhosts.txt
+    $program --host some.host --strip foo.com
+    $program --help
+EOF
+    exit 1;
+}
+
+my $hostsfile = shift;
+my (@hosts, @known_hosts);
+my (%known_host_info, %known_host_peers);
+
+sub read_hosts()
+{
+    local *HOSTS;
+    open (HOSTS, $hostsfile) ||
+       die "$program: FATAL: unable to read $hostsfile: $!\n";
+    while (<HOSTS>) {
+       next if /^\s*(#|$)/; # comment/empty
+       chomp;
+       push(@hosts, $_);
+    }
+    close(HOSTS);
+}
+
+# translate IP to hostname
+sub ip2name {
+    my($ip) = @_;
+    my($addr, $name, $aliases, $addrtype, $length, @addrs);
+    $addr = pack('C4', split(/\./, $ip));
+    ($name, $aliases, $addrtype, $length, @addrs) = gethostbyaddr($addr, 2);
+    # return lower case name
+    return("\L$name");
+}
+
+sub myip2name {
+    my($ip) = @_;
+    my $name = ip2name($ip);
+    if ($name) {
+       return $name;
+    } else {
+       return $ip;
+    }
+}
+
+# item_in_list($item, @list): returns 1 if $item is in @list, 0 if not
+sub item_in_list {
+    my($item, @list) = @_;
+    my($i);
+    foreach $i (@list) {
+       return 1 if ($item eq $i);
+    }
+    return 0;
+}
+
+sub scan_host($;$) {
+    my($host, $level) = @_;
+    my $stratum = 0;
+    my $offset = 0;
+    my $daemonversion = "";
+    my $system = "";
+    my $processor = "";
+    my @peers;
+    my $known_host = 0;
+
+    if (&item_in_list($host, @known_hosts)) {
+       $known_host = 1;
+    } else {
+       # ntpdate part
+       open(NTPDATE, "$ntpdate -bd $host 2>/dev/null |") ||
+       die "Cannot open ntpdate pipe: $!\n";
+       while (<NTPDATE>) {
+           /^stratum\s+(\d+).*$/ && do {
+               $stratum = $1;
+           };
+           /^offset\s+([0-9.-]+)$/ && do {
+               $offset = $1;
+           };
+       }
+       close(NTPDATE);
+    
+       # got answers ? If so, go on.
+       if ($stratum) {
+           # ntpq part
+           my $ntpqparams = "-c 'rv 0 processor,system,daemon_version'";
+           open(NTPQ, "$ntpq $ntpqparams $host 2>/dev/null |") ||
+               die "Cannot open ntpq pipe: $!\n";
+           while (<NTPQ>) {
+               /daemon_version="(.*)"/ && do {
+                   $daemonversion = $1;
+               };
+               /system="([^"]*)"/ && do {
+                   $system = $1;
+               };
+               /processor="([^"]*)"/ && do {
+                   $processor = $1;
+               };
+           }
+           close(NTPQ);
+           
+           # Shorten daemon_version string.
+           $daemonversion =~ s/(;|Mon|Tue|Wed|Thu|Fri|Sat|Sun).*$//;
+           $daemonversion =~ s/version=//;
+           $daemonversion =~ s/(x|)ntpd //;
+           $daemonversion =~ s/(\(|\))//g;
+           $daemonversion =~ s/beta/b/;
+           $daemonversion =~ s/multicast/mc/;
+       
+           # Shorten system string
+           $system =~ s/UNIX\///;
+           $system =~ s/RELEASE/r/;
+           $system =~ s/CURRENT/c/;
+
+           # Shorten processor string
+           $processor =~ s/unknown//;
+
+           # Shorten host string
+           if ($strip) {
+               $host =~ s/$strip//;
+           }
+       }
+    
+       # got answers ? If so, go on.
+       if ($daemonversion) {
+           # ntpq again, find out the peers this time
+           if ($showpeers) {
+               my $ntpqparams = "-pn";
+               open(NTPQ, "$ntpq $ntpqparams $host 2>/dev/null |") ||
+                   die "Cannot open ntpq pipe: $!\n";
+               while (<NTPQ>) {
+                   /^No association ID's returned$/ && do {
+                       last;
+                   };
+                   /^     remote/ && do {
+                       next;
+                   };
+                   /^==/ && do {
+                       next;
+                   };
+                   /^( |x|\.|-|\+|#|\*|o)([^ ]+)/ && do {
+                       push(@peers, $2);
+                       next;
+                   };
+                   print "ERROR: $_";
+               }
+               close(NTPQ);
+           }
+       }
+    
+       # Add scanned host to known_hosts array
+       push(@known_hosts, $host);
+       $known_host_info{$host} = sprintf("%2d %9.3f %-11s %-12s %s",
+           $stratum, $offset, substr($daemonversion,0,11),
+           substr($system,0,12), substr($processor,0,9));
+       $known_host_peers{$host} = [@peers];
+    }
+
+    if ($stratum || $known_host) {
+       my $printhost = ' ' x $level . $host;
+       printf("%-32s %s\n",
+           substr($printhost,0,32), $known_host_info{$host});
+       if ($showpeers) {
+           my $peer;
+           foreach $peer (@{$known_host_peers{$host}}) {
+               &scan_host(myip2name($peer), $level + 1);
+           }
+       }
+    } else {
+       my $printhost = ' ' x $level . $host;
+       printf("%-32s  ?\n", substr($printhost,0,32));
+    }
+}
+
+sub scan_hosts()
+{
+    my $host;
+    for $host (@hosts) {
+       scan_host($host, 0);
+    }
+}
+
+# Main program
+
+if ($single_host) {
+    push(@hosts, $single_host);
+} else {
+    &read_hosts($hostsfile);
+}
+
+# Print header
+print <<EOF;
+Host                             st offset(s) version     system       processor
+--------------------------------+--+---------+-----------+------------+---------
+EOF
+
+&scan_hosts();
+
+exit 0;
+
+# ir. Hans Lambermont, Architect/Developer, Hans.Lambermont@nl.origin-it.com
+# Origin Managed Services, IntraNet Services, tel: +31402785376 +31653252172
+#       ==>> Internet Proxies * IntraConnect * Internet Services <<==