home January 01, 2017

Netstat Bandwidth Summary


a netstat reporting script

Here is a simple shell script to calculate the bandwidth usage on the external interface of a bsd or linux box. Netstat bandwidth summary works well on OpenBSD v4.4, but colleagues have mentioned 3.9 may not work. Linux should work without issue. Also, remember that the netstat stats will reset on reboot of the box.

One could use this script to keep track of Internet bandwidth in case their ISP accused them of using to much bandwidth. Comcast for example will call foul if you use more than 250 gigabytes of combined upload and download bandwidth per month. Comcast's policy is to work with the abusive customer, but more than likely they are already on the list to be terminated. Verizon says they do not have a limit, but they will contact bandwidth abusers if the need arises. Your ISP and in fact the same ISP in different parts of the country might have different rules so check with them. Then use this simple tool to make sure you know what you are using.

What does the report look like?

This is what the summary of your system might look like...

 External interface bandwidth usage:   
  uptime             16 days
  ExtIf in total     13 GBytes
  ExtIf out total    16 GBytes
  ExtIf in/day       831 MBytes/day
  ExtIf out/day      986 MBytes/day
  ExtIf in/30day     24 GBytes/month
  ExtIf out/30day    29 GBytes/month
  ExtIf in+out/30day 53 GBytes/month

The bandwidth summary script

Here is the script called "calomel_interface_stats.sh". You will need to edit the name of your external interface card. In this example we are using "em0" in the netstat lines starting with "netstat -I em0".

NOTE: The variable "$SECS" expects to see the uptime in days, hours and minutes. For this reason the box must be up for at least one day (24 hours) before this script will work properly.

#!/usr/local/bin/bash
#
## moneyslow.com  calomel_interface_stats.sh
#
SECS=`uptime | awk '{ if ($3 ~ /:/) { split($3,a,":"); print (a[1]*60+a[2])*60} else { split($3,b,":"); split($5,a,":"); print b[1]*86400+(a[1]*60+a[2])*60} }'`
EXT_IN=`netstat -b -n -I em0 | grep em0 | tail -1 | awk '{print $5}'`
EXT_OUT=`netstat -b -n -I em0 | grep em0 | tail -1 | awk '{print $6}'`

echo " "
echo "External interface bandwidth usage:"
echo " uptime             " $(($SECS/86400)) "days"
echo " ExtIf in total     " $(($EXT_IN/1000000000)) "GBytes"
echo " ExtIf out total    " $(($EXT_OUT/1000000000)) "GBytes"
echo " ExtIf in/day       " $(($EXT_IN*86400/$SECS/1000000)) "MBytes/day"
echo " ExtIf out/day      " $(($EXT_OUT*86400/$SECS/1000000)) "MBytes/day"
echo " ExtIf in/30day     " $(($EXT_IN*86400*30/$SECS/1000000000)) "GBytes/month"
echo " ExtIf out/30day    " $(($EXT_OUT*86400*30/$SECS/1000000000)) "GBytes/month"
echo " ExtIf in+out/30day " $((($EXT_OUT+$EXT_IN)*86400*30/$SECS/1000000000)) "GBytes/month"

Use the script in the daily email report

You could put the script's executable line into /etc/daily on the 13th line after the commands "sysctl -n kern.version" and "uptime". This way you will receive the summary in the "daily output" email the bsd box sends and includes the above stats.