Wandering Thoughts archives

2008-05-20

Getting live network bandwidth numbers on Solaris

After I wrote netvolmon for Linux, I started getting curious about how much bandwidth our current Solaris NFS servers were using. Unfortunately, Solaris's version of ifconfig does not report byte counts; fortunately, the kernel does keep this information and you can dig it out with kstat (information courtesy of here, which has a bunch of more sophisticated programs to report on this stuff).

The magic kstat incantation is 'kstat -p "*:*:<DEV>:*bytes64"', which gets you the obytes64 and rbytes64 counters for the device; this works on at least Solaris 8 and Solaris 10. (In this Solaris does Linux one better, since a 32-bit Linux machines use 32-bit network counters and on a saturated gigabit link they can roll over in under 20 seconds.)

Armed with this we can write the obvious Solaris version of netvolmon:

#!/bin/sh
# usage: netvolmon DEV [INTERVAL]
DEV=$1
IVAL=${2:-5}

getrxtx() {
    kstat -p "*:*:$1:*bytes64" |
        awk '{print $2}'
}

rxtx=`getrxtx $DEV`
while sleep $IVAL; do
    nrxtx=`getrxtx $DEV`
    (echo $IVAL $rxtx $nrxtx) |
    awk 'BEGIN {
          msg = "%6.2f MB/s RX %6.2f MB/s TX\n"}
         {rxd = ($4 - $2) / (1024*1024*$1);
          txd = ($5 - $3) / (1024*1024*$1);
          printf msg, rxd, txd}'
    rxtx="$nrxtx"
done

Vaguely to my surprise, it turns out that Solaris 8 awk doesn't allow you to split printf (and presumably print) statements over multiple lines. The Solaris shell is backwards and doesn't support the POSIX shell $(...) syntax, even in Solaris 10, so this version uses the less pleasant backquote syntax.

(This can easily be extended to report packets per second as well; the device counters you want are 'opackets64' and 'rpackets64'. I didn't put it in this version for a petty reason, namely that it would make this entry too wide, but you can get the full versions for both Solaris and Linux here.)

solaris/SolarisNetworkBandwidth written at 22:43:43; Add Comment

By day for May 2008: 1 2 3 4 5 6 7 8 9 11 12 13 15 17 18 19 20 21 23 24 25 26 28 29 30 31; before May; after May.

Page tools: See As Normal.
Search:
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.