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

 

 


Posted by Anonymous (213.164.xx.xx) on Thu 12 Jan 2006 at 13:39
vnstat's estimated statistics are nice, but simple (the algorithm doesn't look at trends).

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 ]

Posted by samb (213.106.xx.xx) on Thu 12 Jan 2006 at 15:39
Yeah, I'm a massive fan of bwm too.
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 ]

Posted by DaveV (24.8.xx.xx) on Thu 12 Jan 2006 at 16:49
Check out iftop too. Great for monitoring current performance.

[ Parent | Reply to this comment ]

Posted by sno (62.254.xx.xx) on Thu 12 Jan 2006 at 18:06
[ View Weblogs ]
have tried out iftop and tcptrack which are nice little apps, will give this vnstat a go. thx

[ Parent | Reply to this comment ]

Posted by rxfh (64.85.xx.xx) on Thu 12 Jan 2006 at 18:36
If you're going to get into the more generic network usage tools, don't forget the quintessential `iptraf` package. It is most excellent.

[ Parent | Reply to this comment ]

Posted by naldy (222.124.xx.xx) on Mon 18 Sep 2006 at 05:55
[ View naldy's Scratchpad | View Weblogs ]
iptraf is the best tool i ever seen ...
Thank you

[ Parent | Reply to this comment ]

Posted by simms (216.46.xx.xx) on Thu 12 Jan 2006 at 20:14
i just installed vnstat on my home server, and it seems to be doing its job, but i'm confused about the way it works: i don't see any running vnstat daemons, and i wonder whether vnstat will continue to work in the same way after a reboot (i.e., without explicit initialization).

any ideas?

[ Parent | Reply to this comment ]

Posted by Steve (82.41.xx.xx) on Thu 12 Jan 2006 at 20:20
[ 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 :)

Steve

[ Parent | Reply to this comment ]

Posted by simonw (84.45.xx.xx) on Thu 12 Jan 2006 at 20:42
[ View Weblogs ]
We have ntop on one of our choke point boxes (traffic shaper). Biggest problem is that it collects too many statistics for a busy ISP, but probably fine for less busy networks. Still I run it with minimal stats whenever I need to see what is happening.

[ Parent | Reply to this comment ]

Posted by debianex (195.222.xx.xx) on Fri 13 Jan 2006 at 06:34
I do not know what to say

I am speechles because of iptraf....
it is fantastic

Thank you

[ Parent | Reply to this comment ]

Posted by Anonymous (59.176.xx.xx) on Sat 14 Jan 2006 at 06:23
  • 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 ]

Posted by Anonymous (59.176.xx.xx) on Sat 14 Jan 2006 at 06:31
Aw, crap. I got ipfm and iptraf mixed up - I thought one was the other. So the previous post will make little sense. Sorry about the confusion.

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 ]

Posted by undefined (192.91.xx.xx) on Fri 13 Jan 2006 at 18:44
i run ntop on an old 450 mhz pentium 2 that serves as a firewall/router and it sometimes maxes out the cpu during heavy network traffic.

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 ]

Posted by Anonymous (213.216.xx.xx) on Fri 13 Jan 2006 at 20:31
Why on earth wouldn't a text file under /var be a real database? They are the best kind of database for most uses!

[ Parent | Reply to this comment ]

Posted by Steve (82.41.xx.xx) on Sat 14 Jan 2006 at 15:20
[ 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!)

Steve

[ Parent | Reply to this comment ]

Posted by Anonymous (61.246.xx.xx) on Tue 7 Feb 2006 at 19:41
There seems to be some serious problem with vnstat. If a network interface is shutdown and then brought up after sometime, and vnstat update is run on that interface, vnstat seems to add high traffic usage for the said interface.

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 ]

Posted by Anonymous (58.6.xx.xx) on Wed 15 Mar 2006 at 08:47
try ntop, the current debian package does muddle up few permissions needed to execute, ntop has it's own mysql and apache server running standalone, and gives ALOT of interesting information.

Mark.

[ Parent | Reply to this comment ]

Posted by thom (196.2.xx.xx) on Fri 7 Apr 2006 at 10:34
Hi - I am a linux newbie. I have 4 windows boxes (2x2000 and 2xXP) and 1 ubuntu box on a hub, connected to a WRT54GL router and then on to broadband. That router is nice beca

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 ]

Posted by Anonymous (83.98.xx.xx) on Sun 3 Sep 2006 at 01:16
Im looking into bandwidth monitoring for misuse monitoring and it is probable that the <b>openwrt</b> linux firmware for ur router has a package that monitors bandwidth usage on the WAN port and possibly seperate LAN ports. When my software options run out ill be looking into this.<rant> A bit of a linux noob myself and scared of "linux type troubleshooting" cus i hate reading a zillion docs for it.</rant>:p

[ Parent | Reply to this comment ]

Posted by Anonymous (70.243.xx.xx) on Fri 23 Mar 2007 at 22:38
Good article.

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 ]

Posted by Anonymous (142.90.xx.xx) on Tue 29 May 2007 at 23:21
I like vnstat, but I'm on a cable line, so I get falsely inflated counts because of all the ARP traffic coming from my ISP and the rest of thier customers. Does anyone know if there there an easy way to quantify how much of the vnstat total is ARP and subtract it off? I've used ipac-ng to do the same job, but I like vnstat's simplicity.



[ Parent | Reply to this comment ]

Sign In

Username:

Password:

[Register|Advanced]

 

Flattr

 

Current Poll

What do you use for configuration management?








( 458 votes ~ 5 comments )

 

 

Related Links