Getting live network bandwidth numbers on Linux
Today I got curious about a simple question: was my iSCSI target machine actually running at its full potential read speed?
The machine exports individual disks to its clients, so measuring single disk performance wouldn't give me the answer. Summing up IO across all the disks would have given me a number, but so would just getting the network bandwidth utilization; if the machine was saturating its gigabit link, it was clearly running as fast as it could.
There doesn't seem to be a program that will directly show this
information (at least not on Red Hat Enterprise 5), but you can get the
total byte counts for an interface from ifconfig
, which means that
with a small script I had what I wanted. (Then I rewrote it to read the
stats directly from /proc/net/dev
instead of running ifconfig
and
groping through the output.)
Since it may be useful for other people, here's what I'm calling
netvolmon
:
#!/bin/sh # usage: netvolmon DEV [INTERVAL] DEV=$1 IVAL=5 if [ "$#" -eq 2 ]; then IVAL=$2 fi getrxtx() { grep "$1:" /proc/net/dev | sed 's/^.*://' | awk '{print $1, $9}' } rxtx=$(getrxtx $DEV) while sleep $IVAL; do nrxtx=$(getrxtx $DEV) (echo $IVAL $rxtx $nrxtx) | awk '{rxd = ($4 - $2) / (1024*1024*$1); txd = ($5 - $3) / (1024*1024*$1); printf "%6.2f MB/s RX %6.2f MB/s TX\n", rxd, txd}' rxtx="$nrxtx" done
Unfortunately this illustrates one reason why shell scripting is so
pervasive: it is such a convenient way of banging rocks together in a
hurry. Once I hit on the trick of using awk
for all the arithmetic,
it probably took me longer to fiddle with the output formatting than
to write the rest of the script.
(And I have to give bash
a big raspberry for making array variables
useless for precisely the situation where they would be most useful,
namely picking individual elements out of the output of a command that
prints multiple pieces of information.)
|
|