A shell script to measure network throughput on Linux machines.

Here is a shell script to see how many (kilo-, mega-, giga-, terra-) bytes pass a network interface. The output looks like this:

$ ./network-traffic.sh --help
Usage: ./network-traffic.sh [-i INTERFACE] [-s INTERVAL] [-c COUNT]

-i INTERFACE
    The interface to monitor, default is eth0.
-s INTERVAL
    The time to wait in seconds between measurements, default is 3 seconds.
-c COUNT
    The number of times to measure, default is 10 times.
$ ./network-traffic.sh        
Monitoring eth0 every 3 seconds. (RXbyte total = 706 Mb TXbytes total = 1 Gb)
RXbytes = 104 b TXbytes = 194 b
RXbytes = 80 b TXbytes = 188 b
RXbytes = 52 b TXbytes = 146 b
RXbytes = 689 b TXbytes = 8 Kb
RXbytes = 52 b TXbytes = 146 b
RXbytes = 52 b TXbytes = 146 b
RXbytes = 52 b TXbytes = 146 b
RXbytes = 52 b TXbytes = 146 b
RXbytes = 4 Kb TXbytes = 4 Kb
RXbytes = 716 b TXbytes = 5 Kb

Here is the script:

#!/bin/sh

usage(){
echo "Usage: $0 [-i INTERFACE] [-s INTERVAL] [-c COUNT]"
echo
echo "-i INTERFACE"
echo "    The interface to monitor, default is eth0."
echo "-s INTERVAL"
echo "    The time to wait in seconds between measurements, default is 3 seconds."
echo "-c COUNT"
echo "    The number of times to measure, default is 10 times."
exit 3
}

readargs(){
while [ "$#" -gt 0 ] ; do
  case "$1" in
   -i)
    if [ "$2" ] ; then
     interface="$2"
     shift ; shift
    else
     echo "Missing a value for $1."
     echo
     shift
     usage
    fi
   ;;
   -s)
    if [ "$2" ] ; then
     sleep="$2"
     shift ; shift
    else
     echo "Missing a value for $1."
     echo
     shift
     usage
    fi
   ;;
   -c)
    if [ "$2" ] ; then
     counter="$2"
     shift ; shift
    else
     echo "Missing a value for $1."
     echo
     shift
     usage
    fi
   ;;
   *)
    echo "Unknown option $1."
    echo
    shift
    usage
   ;;
  esac
done
}

checkargs(){
if [ ! "$interface" ] ; then
  interface="eth0"
fi
if [ ! "$sleep" ] ; then
  sleep="3"
fi
if [ ! "$counter" ] ; then
  counter="10"
fi
}

printrxbytes(){
/sbin/ifconfig "$interface" | grep "RX bytes" | cut -d: -f2 | awk '{ print $1 }'
}

printtxbytes(){
/sbin/ifconfig "$interface" | grep "TX bytes" | cut -d: -f3 | awk '{ print $1 }'
}

bytestohumanreadable(){
multiplier="0"
number="$1"
while [ "$number" -ge 1024 ] ; do
  multiplier=$(($multiplier+1))
  number=$(($number/1024))
done
case "$multiplier" in
  1)
   echo "$number Kb"
  ;;
  2)
   echo "$number Mb"
  ;;
  3)
   echo "$number Gb"
  ;;
  4)
   echo "$number Tb"
  ;;
  *)
   echo "$1 b"
  ;;
esac
}
 
printresults(){
while [ "$counter" -ge 0 ] ; do
  counter=$(($counter - 1))
  if [ "$rxbytes" ] ; then
   oldrxbytes="$rxbytes"
   oldtxbytes="$txbytes"
  fi
  rxbytes=$(printrxbytes)
  txbytes=$(printtxbytes)
  if [ "$oldrxbytes" -a "$rxbytes" -a "$oldtxbytes" -a "$txbytes" ] ; then
   echo "RXbytes = $(bytestohumanreadable $(($rxbytes - $oldrxbytes))) TXbytes = $(bytestohumanreadable $(($txbytes - $oldtxbytes)))"
  else
   echo "Monitoring $interface every $sleep seconds. (RXbyte total = $(bytestohumanreadable $rxbytes) TXbytes total = $(bytestohumanreadable $txbytes))"
  fi
  sleep "$sleep"
done
}

readargs "$@"
checkargs
printresults

Comments

Thanks for the code this is

Thanks for the code this is very useful while transferring data between servers. We don't have many options on the server. I am reposting the code with your link in my blog and thinking of developing the code. If it is ok with you I will host it in a repo under GPL v3 license. Thanks again.

The process really worked.

The process really worked. It's good to find this solution. - Mark Zokle

hi everyone, just to know how

hi everyone,
just to know how to plot the printtxbytes and printrxbytes with gnuplot .
thanks!

You could get rid of that

You could get rid of that awful command chain parsing ifconfig output and do

rxbytes=`cat /sys/class/net/$interface/statistics/rx_bytes`
txbytes=`cat /sys/class/net/$interface/statistics/tx_bytes`

instead.

Excellent suggestion, exactly

Excellent suggestion, exactly what I was looking for. Replaced the ifconfig lines with 'cat /sys/class/net/$interface/statistics/rx_bytes' (and tx_bytes), works well. I also suggest changing Mb, Gb etc with capital B (MB, GB), since it's measuring bytes, not bits.

Unfortunally, this doesn't

Unfortunally, this doesn't work for a server, since in some scenarios (bonding, bridges), ifconfig will show:

eth0 tx 100
eth1 tx 100
bond0 tx 200

eth2 rx 50
eth3 rx 40
bond1 rx 90

The traffic is tx 200 and rx 90 here, but your script will show tx 400 and rx 180.

To do it like a PRO, get rid of ifconfig and go check snmp.

That is a very useful script,

That is a very useful script, but it assumes that ifconfig uses IEC binary prefixes (1 kibibyte = 1024 Bytes) , while in fact ifconfig uses SI decimal prefixes (1 kilobyte = 1000 Bytes). Said assumption results in an error of approximately 5% when dealing with Megabytes, and approximately 10% when dealing with Terabytes.

It can be easily corrected by substituting 1024 with 1000 in the script.

Also, you might opt to echo 'KB' instead of 'Kb', 'MB' instead of 'Mb', etc, to prevent the misunderstanding that the script outputs bit values rather than byte one (an eightfold error).

Hi! I was looking at your

Hi!

I was looking at your script and looks very neat, almost readable for my limited programming skills. Unfortunately it's not exactly what I'm looking for, but very close... I just wanna keep track of the total amount to not surpass a monthly limit.

Is there a way to modify your script to store the network traffic daily/weekly/monthly? Or at the end of each session? Something like: ifconfig eth0 | grep bytes >>storeit.txt but automatically...

The counters spitted by ifconfig (RX bytes and TX bytes) get renewed at each session? (man ifconfig didn't help much). How would I execute this command every time before the counter gets renewed?

Hopefully this explains the idea...
Thanks!
fbv

Ifconfig keeps track of the

Ifconfig keeps track of the traffic but resets this information at reboot.

So; if you keep your machine on for 1 month, you're fine. Just type ifconfig and read the RX and TX information.

Or keep a record, just like you explained: ifconfig eth0 | grep bytes >>storeit.txt

But when reboots happened, you'd have to open that file, do quite some logic to figure out how many bytes in total you have used.

Googling for "linux total traffic usage" showed me http://humdi.net/vnstat/ . That might be a good solution...

Anyway, good luck!

I think there might be a typo

I think there might be a typo - should the printtxbytes () be grepping for "TX bytes" instead of "RX bytes"?

About Consultancy Articles Contact




References Red Hat Certified Architect By Robert de Bock Robert de Bock
Curriculum Vitae By Fred Clausen +31 6 14 39 58 72
By Nelson Manning [email protected]