Home > Support > HOWTO List > Web > Virtual Hosts

Monitoring bandwidth usage with vnStat

vnStat is a very lightweight bandwidth monitoring tool. It's not a packet filter. It works by regularly monitoring /proc entries and updating its simple database of network activity for each interface.

Sample output:


vnstat -m

        inet (eth0)

           month        rx      |       tx      |    total
        ------------------------+---------------+---------------
          Mar '03    15,959 MB  |    18,823 MB  |    34,782 MB
          Apr '03    24,645 MB  |    20,440 MB  |    45,085 MB
          May '03    31,314 MB  |    39,103 MB  |    70,417 MB
          Jun '03    28,116 MB  |    42,988 MB  |    71,104 MB
          Jul '03    27,552 MB  |    33,924 MB  |    61,476 MB
          Aug '03    37,498 MB  |    38,189 MB  |    75,687 MB
          Sep '03    19,750 MB  |    17,488 MB  |    37,238 MB
          Oct '03     3,999 MB  |     4,492 MB  |     8,491 MB
        ------------------------+---------------+---------------
        estimated    18,479 MB  |    20,758 MB  |    39,237 MB

For more 'screenshots' and documentation, refer to the project home page at http://humdi.net/vnstat/ .

Installation

This simple script will help you to install vnstat in your server, no matter which distro you're using



wget  http://proj.ri.mu/installvnstat.sh

bash -x installvnstat.sh

Usage

vnStat will record historical bandwidth usage. You can see all options available using --help:


yves@pressa:~$ vnstat --help
 vnStat 1.4 by Teemu Toivola <tst at iki dot fi>

         -q, --query             query database
         -h, --hours             show hours
         -d, --days              show days
         -m, --months            show months
         -w, --weeks             show weeks
         -t, --top10             show top10
         -s, --short             use short output
         -u, --update            update database
         -i, --iface             change interface (default: eth0)
         -?, --help              short help
         -v, --version           show version
         -tr, --traffic          calculate traffic

Sample outputs for a low-traffic ppp0 interface:


vnstat -h
 eth0                                                                     15:45 
  ^                       r                                                     
  |                       rt                                                    
  |                       rt                                                    
  |                       rt    r                                               
  |                       rt    r                                               
  |                       rt    r                             r  r              
  |                       rt r  r  r  r     r  r  r  r  r  r  r  r  rt          
  |      t           t    rt r  r  r  r  r  r  r  r  r  r  r  r  rt rt          
  |     rt       rt rt    rt r  r  r  r  r  r  r  r  r  r  r  r  rt rt rt       
  |  rt rt rt rt rt rt rt rt rt rt rt r  r  r  r  r  r  r  r  rt rt rt rt rt    
 -+---------------------------------------------------------------------------> 
  |  16 17 18 19 20 21 22 23 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15    
                                                                                
 h   rx (kB)    tx (kB)      h   rx (kB)    tx (kB)      h   rx (kB)    tx (kB) 
16      2,684      3,135    00      8,960      3,033    08      8,071      1,074
17      4,401      5,433    01     12,740      1,981    09      8,395      1,354
18      2,443      3,056    02      8,955      2,658    10      8,702      1,671
19      2,059      2,563    03      7,748      1,440    11      9,211      3,463
20      3,920      5,227    04      6,386      1,615    12      9,530      5,985
21      3,884      5,491    05      8,187      1,055    13      7,633      8,276
22      2,139      2,289    06      8,100      1,130    14      3,771      4,914
23     18,036     17,629    07      8,101      1,111    15      2,657      2,998

example cron script


# (Use to post in the top of your crontab)
# ----------------- minute (0 - 59)
# |  -------------- hour (0 - 23)
# |  |  ----------- day of month (1 - 31)
# |  |  |  -------- month (1 - 12)
# |  |  |  |  ----- day of week (0 - 7) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *  command to be executed

# check transfer usage for this month every day. if it's greater than 10GB, email me.
MAILTO="your-email@email.com"
0 0 * * * vnstat --dumpdb | awk --field-separator ";" '{if($1=="m"&&$8=="1"){if($5>10000){print "you have transferred " $5 " MB this month"}}}'