#!/usr/bin/perl # # Network Monitor v1.0b # jwa@nbs.nau.edu - 25 July 1995 # # this program looks best in a colour xterm w/ -bg black -fg white. # require "flush.pl"; require "getopts.pl"; $hostcount=1; $responding=1; $down=1; $hosts=0; $xspacing=25; $yspacing=2; # -t timeout # -f hosts file # -u update delay (secs) # -m monochrome mode &Getopts('t:f:u:m'); # # handle -t option (timeout) # if ($opt_t ne "") { if ($opt_t < 251) { print "timeout value (-t) must be above 250!\n"; &usage(); } $timeout = $opt_t; } else { $timeout = 251; } # # handle -f option (hostfile name) # if ($opt_f ne "") { $hostfile = $opt_f; open(SANITY, $hostfile) || die "Can't open $hostfile: $!\n"; } else { $hostfile = "netmon.hosts"; open(SANITY, $hostfile) || die "Can't open $hostfile: $!\n"; } # # handle -u option (update delay in secs) # if ($opt_u ne "") { $delay = $opt_u; } else { $delay = 5; } # # handle -m option (monochrome mode) # if ($opt_m eq "1") { $monochrome = 1; } $fping = "fping -t $timeout -r0 -f - "; &init(); while (1) { $x = 1; # starting X coord for display $y = 4; # starting Y coord &loadlist(); &showstatus(); &showhosts(); sleep($delay); } sub init { &normal(); &clearscreen(); } sub showstatus { &moveto(1,1); &normal(); &bold(); print "Network Monitor"; &moveto(31,1); print "$delay/s dly $timeout/ms tmout File: $hostfile"; &moveto(1,2); print "$responding hosts responding "; printf "(%0.0f%%) ", ($responding / $hostcount ) * 100; &moveto(32,2); print "$down hosts down "; printf "(%0.0f%%) ", ($down / $hostcount ) * 100; &moveto(58,2); print "$hostcount hosts total "; #print "Worst host: $worsthost"; &normal(); &moveto(1,1); } sub loadlist { $hosts=0; @status_list=(); $longest=1; $hostcount=0; $responding=$down=0; open (PING, "cut -f2 -d: < $hostfile | $fping | sort -u |"); # || die "Couldn't run fping: $!\n"; FILE: while ($status = ) { ($hostip, $is, $status) = split(/ /, $status, 3); chop $status; # match ip to hostname in $hostfile open(HOSTFILE, $hostfile); while () { $data = $_; chop $data; ($hostname, $testip) = split(/:/, $data, 2); if ($testip eq $hostip) { # We have a match .. First calculate spacing.. if (length ($hostname) > $longest) { $longest = length($hostname); $xspacing = $longest + 3; } # then update the list if ($status eq "alive") { @status_list = (@status_list, $hostname, "alive"); $responding++; } else { @status_list = (@status_list, $hostname, "dead"); $down++; } # inc host count $hosts = $hosts + 2; $hostcount++; } } } # {FILE} # now figure out the yspacing $blah = int (( (80 / $xspacing) / ($hostcount) ) * 20 ) - 1; if ($blah == 0) { $yspacing = 1; } else { $yspacing = 2; # you could change this to $blah, but i think it's oogly } } sub showhosts { $i=0; while ($i != $hosts) { if ( $status_list[$i+1] eq "alive" ) { if ($monochrome eq "1") { &normal(); } else { &green(); } } else { if ($monochrome eq "1") { &bold(); } else { &red(); } } &updatestatus($status_list[$i]); &normal(); $i = $i + 2; } } sub updatestatus { $thing = shift(@_); &moveto($x, $y); print "$thing"; &moveto(1,1); &flush(STDOUT); $x = $x + $xspacing; if ($x > 80-$xspacing) { $x = 1; $y = $y + $yspacing; if ($y > 25) { &normal(); &clearscreen(); print "too many hosts in list; shorten the hostnames or decrease the amount of hosts.\n"; die; } } } sub moveto { $movey = @_; $movex = shift(@_); $movey = shift(@_); print "\x1b[$movey;${movex}f"; &flush(STDOUT); } sub clearscreen { print "\x1b[2J"; } sub red { print "\x1b[31;1m"; } sub green { print "\x1b[32;1m"; } sub normal { print "\x1b[0;0m"; } sub bold { print "\x1b[1;1m"; } sub beep { print "\x07"; &flush(STDOUT); } sub usage { print "netmon v1.0 jwa\@nbs.nau.edu\n"; print "usage: netmon [-t timeout] [-u update delay] [-f hostfile] [-m mono mode]\n"; print " -t timeout for fping - must be > 250 (default: 251)\n"; print " -u sleep time between updates (default: 5)\n"; print " -f name of host file (1 host per line, default \"netmon.hosts\")\n"; print " -m monochrome display (default: off)\n"; print " -h usage (help)\n"; print "\n"; die }