Getting live network bandwidth numbers on Solaris

May 20, 2008

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.)


Comments on this page:

From 24.99.170.21 at 2008-06-30 12:29:43:

You can also use nicstat, which was designed for this purpose:

http://prefetch.net/blog/index.php/2005/08/21/viewing-nic-throughput-with-nicstat/

- Ryan

Written on 20 May 2008.
« Segregating your outgoing email to get blocked as little as possible
Combining dual identity routing and isolated interfaces »

Page tools: View Source, View Normal, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Tue May 20 22:43:43 2008
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.