#!/usr/bin/perl # # NetStat Client software - for NetStat.Net # uses file formats from client.c, and is intended as a drop in # replacement for that tool. # # Copyright (C) 1998 Robert Stone # for Access Internet Communication, Inc. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # ===== # # See the included LICENSE file for licensing information, and # COPYING for copyright information. # ##### # start config section $HTML = '.'; #"/var/www/netstat"; $NSC = '.'; #"/usr/lib/netstat"; $HOSTS = "$NSC/hosts.nsc"; $RESULTS = "$HTML/results"; $pingtries = 100; # number of pings to attempt $pingsize = 12; # size of ping reqs in bytes # fping's minimum is 12 # client.c used 10 (no timestamp space?) $fping = "/usr/bin/fping -q -b$pingsize -A -C$pingtries"; # end config section ##### # input file looks like: # "%s:%s:%s\n", name, dname, ipaddr # output file looks like: # "|%s|%s|%d|%.2f|%.2f|%.2f|%d\n", name, dname, status, min, avg, max, loss open(HOSTS) || die "$0: can't open $HOSTS: $!\n"; while() { $hosts{$3} = [ $1, $2 ] if(/^\s*(.+)\s*:\s*(.+)\s*:\s*([0-9.]+)\s*$/); } close(HOSTS); open(RESULTS, "> $RESULTS.new") || die "$0: can't write $RESULTS: $!\n"; open(FPING, join(' ',$fping, keys(%hosts), '2>&1 |')) || die "$0: can't exec fping: $!\n"; open(OUT, "> output") || warn "%0: can't write output: $!\n"; while() { print OUT $_; chop; unless(/^([0-9.]+)\s+:\s+(.*)$/) { next if(/^ICMP Host Unreachable /); warn "line not understood: $_\n"; next; } printf(RESULTS "|$1|%s|%d|%.2f|%.2f|%.2f|%d\n", $hosts{$1}[0,1], getstats(split(' ', $2))); } close(FPING); close(RESULTS); close(OUT); unlink($RESULTS); link("$RESULTS.new",$RESULTS); unlink("$RESULTS.new"); exit(0); sub getstats { $min = ''; $max = ''; $tot = 0; $loss = 0; $stat = 1; # 1 means down, 0 means up while($_ = pop(@_)) { if($_ eq '-') { $loss++; next; } else { $stat=0; } $min = $_ if($min eq '' || $min > $_); $max = $_ if($max eq '' || $max < $_); $tot += $_; } if($stat == 1) { $min=0; # to make sure zeros $max=0; # are printed as in # client.c } return($stat, $min, $tot/$pingtries, $max, $loss); }