#!/bin/sh
# usage: netsolmon DEV [INTERVAL]
# works only on solaris
# ganked from http://www.brendangregg.com/Perf/network.html
case "$#" in
	1|2)	:;;
	*)	echo "usage: netsolmon DEV [INTERVAL]" 1>&2; exit 1;;
esac
DEV=$1
IVAL=5
if [ "$#" -eq 2 ]; then
	IVAL=$2
fi

# helpfully, these stats results have input and output swapped between
# the byte counts and the packet counts. The order is:
#	bge:0:bge0:obytes64     13577366389844
#	bge:0:bge0:rbytes64     429293127522
#	bge:0:bge0:ipackets64   2295473708
#	bge:0:bge0:opackets64   10051285406
#	
getrxtx() {
	kstat -p "*:*:$1:*bytes64" "*:*:$1:*packets64" | awk '{print $2}'
}

rxtx=`getrxtx $DEV`
while sleep $IVAL; do
	nrxtx=`getrxtx $DEV`
	# IVAL otxb orxb orxp otbp ntxb nrxb nrxp ntxp
	(echo $IVAL $rxtx $nrxtx) |
	awk '{rxd = ($7 - $3) / (1024*1024*$1);
	      txd = ($6 - $2) / (1024*1024*$1);
	      rxp = ($8 - $4) / $1
	      txp = ($9 - $5) / $1
	      printf "%6.2f MB/s RX %6.2f MB/s TX   packets/sec: %5d RX %5d TX\n", rxd, txd, rxp, txp}'
	rxtx="$nrxtx"
done
