Monitoring your bandwidth usage with vnstat
Posted by Steve on Thu 12 Jan 2006 at 12:43
There are many occasions where it is useful to have an idea of your bandwidth usage, perhaps to know when you're going to be charged more by your ISP, or perhaps just as part of general monitoring. The vnstat tool is a simple means of doing just that.
vnstat is a console based tool which is simple to setup and capable of monitoring your incoming and outgoing bandwidth usage.
The output allows you to see the bandwidth you've used in a variety of ways, such as by hour, by day, etc. As a bonus it doesn't require the use of root privileges once it has been installed.
The Debian package is contained in both Sarge, Testing and Sid, and can be installed via the following command: root@lappy:~# apt-get install vnstat
Once installed you'll need to initialise the database the system uses for recording your traffic details. The tool uses one database for each network interface you wish it to monitor. (These are not real databases, just text files located in the directory /var/lib/vnstat.)
Since my system only has a single network interface I just need to run:
root@lappy:~# vnstat -u -i eth0
(You might wish to repeat this changing eth0 for each interface you wish to monitor.)
Once the text database(s) have been initialised you're ready to monitor your bandwidth usage. The package installs a cronjob which will update the database every five minutes.
Once you've left the system alone for a while to quietly collect the data you can view it using one of the reporting modes. The most simple way of doing so is to just invoke the command with no arguments:
skx@lappy:~$ vnstat
Database updated: Thu Jan 12 12:35:01 2006
eth0
received: 76.17 MB (87.4%)
transmitted: 11.72 MB (12.6%)
total: 87.90 MB
rx | tx | total
-----------------------+------------+-----------
yesterday 53.66 MB | 7.69 MB | 61.36 MB
today 22.50 MB | 4.03 MB | 26.54 MB
-----------------------+------------+-----------
estimated 41 MB | 7 MB | 48 MB
There are several different ways of viewing the bandwidth usage as we suggested earlier. These are invoked via command line arguments such as --hours, --days, etc.
This is an hourly view:
eth0 12:35 ^ t | t | t | t t t | t t t t t t | t t t t t t t t | t t t t t t t t t t t t t | t t t t t t t t t t t t t t t t t t | t t t t t t t t t t t t t t t t t t t t t t t t | rt rt rt t t t t rt t t t t t t t t t t t t t t t t -+---------------------------------------------------------------------------> | 13 14 15 16 17 18 19 20 21 22 23 00 01 02 03 04 05 06 07 08 09 10 11 12
vnstat is a useful tool for showing you the total transmitted and received network data. One drawback is that it won't show you what has been sending or receiving data - just the count of data sent. If you're looking to view more advanced network statistics you'll probably wish to use something like ntop instead.
For more details of the available output modes please see the vnstat manpage which you can read by running:
man vnstat
I find bwm is a nice way of getting a live snapshot of how much bandwidth you are using right now.
If you are just concerted about apache traffic, apachetop is good.
[ Parent | Reply to this comment ]
For long term trend graphing I usually just install net-snmpd and use Cacti.
Sam Bashton<br>
Bashton Ltd - Linux Consultancy
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Send Message | View naldy's Scratchpad | View Weblogs ]
Thank you
[ Parent | Reply to this comment ]
any ideas?
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
As mentioned, briefly, in the article the system runs via a cronjob.
The package installs the file /etc/cron.d/vnstat with the following contents:
0-55/5 * * * * ...
This causes the main binary to be called every five minutes to update the statistics; and as such it'll survive a reboot just fine :)
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
I am speechles because of iptraf....
it is fantastic
Thank you
[ Parent | Reply to this comment ]
- ipfm: Heh, if you liked iptraf, then I think you'll be in raptures over ipfm - ip flow meter. That's a live tracker that runs in curses mode that I use to zero in at a still fairly high level on what's going on.
- iptraf: Yup, iptraf is good - the only hard bit is setting it up - and even that is not that hard - anyone who can rtfm can figure it out.
iptraf and ipfm is what I used on my old p100 gateway box.
- bwm: bwm is also a good one for absolutely minimal stuff and minimal load - it even displays in black and white to show how stripped down it is :-).
- bwm-ng: bwm-ng is the next generation of bwm apparently around, but I have no idea on that one yet.
- vnstat: Steve's vnstat sounds exactly right for the gateway box on my current metered connection. Very timely too, since I just got it going yesterday :-)
PJ
[ Parent | Reply to this comment ]
That's the trouble with debian stability, the systems just keeps running for so long without any hitch that you end up never having to use such monitoring tools after you've set it up! Unlike our pitiful MS windows brethren. ;-)
PJ
[ Parent | Reply to this comment ]
i hardly look at the stats because i don't often need that kind of detail. probably need to stop it and only start it when i'm needing to investigate the network (like "who is using all my bandwidth?").
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
Well it is a database - I just wanted to make the point that it wasn't a full relational database and make sure people didn't think they needed Postgres/Mysql/sqlite/etc installed.
(I got told off in the past for not making the distinction!)
[ Parent | Reply to this comment ]
This behaviour has been noted by many users and posted on the vnstat forums as well, however there seems to be no resolution / reply on the forums.
Does anyone know of anything else like vnstat which does the job of keeping the figures and isn't intensive on cpu ?
Thanks.
[ Parent | Reply to this comment ]
Mark.
[ Parent | Reply to this comment ]
I am sharing the broadband contract with another educational project down the road. So for billing purposes we each need to know how much traffic we each have.
I am trying to set up vnstat so that it will monitor the traffic at the interface between the router and the modem. I don't need to see what each box on the LAN is doing quite yet. Is this possible, or can I only monitor what passes through the ethernet adapter on my linux box itself?
Cheers :)
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
Something I found out, not documented in the man page or in this article:
to remove an interface from vnstat after you've initialized it, simply manually remove the database file with the corresponding name as the interface you wish to delete from /var/lib/vnstat/
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]