Subject: Re: nightly report "network" section.
To: Perry E.Metzger <perry@piermont.com>
From: Greg A. Woods <woods@weird.com>
List: tech-userlevel
Date: 12/07/2003 14:16:04
[ On Sunday, December 7, 2003 at 11:09:24 (-0500), Perry E.Metzger wrote: ]
> Subject: nightly report "network" section.
>
> Would anyone mind if I replaced that with
> netstat -in | cut -c 1-6,44-80 | fgrep -v "*" | uniq

How about using awk instead, which would avoid the explicit use of magic
column numbers, and would also allow the output columns to be spread out
a bit more to deal with the common case where the existing netstat
report doesn't leave enough width for the numbers?  (It would also avoid
the "fgrep" and "uniq" processes :-)

	netstat -in | awk 'BEGIN {
		ifs[""] = 0;
	}
	/^[^\*]* / {
		if (NR == 1) {
			printf("%-8s %10s %6s %10s %6s %6s\n",
				$1, $(NF-4), $(NF-3), $(NF-2), $(NF-1), $NF);
			next;
		}
		if (!($1 in ifs)) {
			printf("%-8s %10d %6d %10d %6d %6d\n",
				$1, $(NF-4), $(NF-3), $(NF-2), $(NF-1), $NF);
			ifs[$1] = 1;
		}
	}'

For example on one of my systems the packet count values exceed the
width of their columns producing ugly output like this:

Name  Mtu   Network       Address              Ipkts Ierrs    Opkts Oerrs Colls
de0   1500  <Link>        00:00:c0:83:c3:e9 232673148     0 273729698     4     0


On that system the above script produces the much more readable:

	Name          Ipkts  Ierrs      Opkts  Oerrs  Colls
	de0       232673278      0  273729835      4      0
	lo0        39483013      0   39483013      0      0

It might be nice to also report the byte counts in a similar way, though
merging the output from two netstat invocations would require a somewhat
more sophisticated awk script....

-- 
						Greg A. Woods

+1 416 218-0098                  VE3TCP            RoboHack <woods@robohack.ca>
Planix, Inc. <woods@planix.com>          Secrets of the Weird <woods@weird.com>