This post was originally published on go2linux.org, a Linux blog I ran from 2007 to 2011. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.
It is always useful to know how much bandwidth you are using, also to check if your provider is honoring the contract you signed with them.
At my office I have installed Cacti and as I have a layer 3 SNMP capable switch, I can graph the bandwidth that all my office's PCs uses, and also the main pipe to the Internet (My ADSL connection), but at home, I do not have such an expensive switch (and I do not need it), and my ADSL modem is not SNMP capable, so Cacti is not a solution, but with Linux Operating System there is always a solution, and the one for this kind of problem is vnstat.
To install it run:
sudo aptitude install vnstat
Then you need to initialize the database, and add a cronjob task, but it is easy and automatic. First determine the name of your NIC or NICs for that run:
sudo ifconfig
My output is:
eth2 Link encap:Ethernet HWaddr 00:19:d1:ea:e6:3f
inet addr:10.87.58.2 Bcast:10.87.58.7 Mask:255.255.255.248
inet6 addr: fe80::219:d1ff:feea:e63f/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:55660 errors:0 dropped:0 overruns:0 frame:0
TX packets:47538 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:66480203 (63.4 MiB) TX bytes:5316732 (5.0 MiB)
Base address:0x20c0 Memory:92200000-92220000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:66740 errors:0 dropped:0 overruns:0 frame:0
TX packets:66740 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:64959780 (61.9 MiB) TX bytes:64959780 (61.9 MiB)
As you can see, my unique interface is called eth2 so I need to run:
vnstat -u -i eth2
This will initiate the database, and add one script called /etc/cron.d/vnstat, containing this line:
# /etc/cron.d/vnstat: crontab entries for the vnstat package 0-55/5 * * * * root if [ -x /usr/bin/vnstat ] && [ `ls /var/lib/vnstat/ | wc -l` -ge 1 ]; then /usr/bin/vnstat -u; fi
So, now you just need to wait until the database populate with data, after a while you can run some of these commands:
vnstat -h
And will show an hourly graph like this:
eth2 10:10
^ r
| r
| r
| r
| r
| r r
| r r
| r r
| r r
| r r
-+--------------------------------------------------------------------------->
| 11 12 13 14 15 16 17 18 19 20 21 22 23 00 01 02 03 04 05 06 07 08 09 10
h rx (kB) tx (kB) h rx (kB) tx (kB) h rx (kB) tx (kB)
11 0 0 19 0 0 03 0 0
12 0 0 20 0 0 04 0 0
13 0 0 21 0 0 05 0 0
14 0 0 22 0 0 06 0 0
15 0 0 23 0 0 07 0 0
16 0 0 00 0 0 08 0 0
17 0 0 01 0 0 09 31947 1798
18 0 0 02 0 0 10 16201 772
or:
vnstat -d
And you will get a daily graph like this:
eth2 / daily
day rx | tx | total
------------------------+-------------+----------------------------------------
13.05. 47.02 MB | 2.51 MB | 49.53 MB %%%%%%%%%%%%%%%%%%%%%%%%:
------------------------+-------------+----------------------------------------
estimated 110 MB | 4 MB | 114 MB
As I do not have enough data, my graphs are a little bit boring. You can read the man page to explore other options.