Linux: How to measure daily/montly network traffic?

Solution 1:

I also suggest using VnStat

vnStat 1.6 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          select interface (default: eth0)
     -?,  --help           short help
     -v,  --version        show version
     -tr, --traffic        calculate traffic
     -l,  --live           show transfer rate in real time


 eth0  /  monthly

   month         rx      |      tx      |   total
-------------------------+--------------+--------------------------------------
  Oct '09     225.70 GB  |   798.52 GB  |     1.00 TB   %%%%:::::::::::::
  Nov '09     138.46 GB  |   616.54 GB  |   755.01 GB   %%::::::::::

 eth0  /  daily

day         rx      |     tx      |  total
------------------------+-------------+----------------------------------------
   31.12.      6.56 GB  |   34.39 GB  |   40.95 GB   %%%::::::::::::::::
   01.01.      1.13 GB  |  746.92 MB  |    1.86 GB

Solution 2:

What Zypher was saying about rrdtool (and anything else that uses it as a backend - MRTG, Cacti etc) is probably correct. RRDTool is designed to be an 'averaged' historical trending analysis tool. It averages and stores counters in increasingly non-resolute increments increments the further back in time it goes.

This is, however, configurable by setting up the RRAs approriately. I confess to knowing absolutely nothing about configuring these, however, and have never personally had luck getting them right beyond my standard set (15 minutes, 8 hours, 1 week, 1 month, 1 year). I would recommend looking into configuring the RRAs to expect daily input and feed it your bandwidth counter from netstat. You'll get some very nice historical data and insight into your usage patterns.

To answer your current problem, about making sure you don't exceed a daily bandwidth limit; I would recommend grabbing the netstat counters daily for the interface in question. You can compare yesterday's traffic at time t with today's counters at time t and find out how much was transferred. A simple script with a flat text-file storage of the previous value would probably suffice. You could then disable the interface if you detect exceeded bandwidth or monitor it throughout the day and notify an admin if you are approaching your limit.

To get the input bytes on an OSX system you can use the following set of commands:

netstat -ib | grep -e "$INTERFACE" -m 1 | awk '{print $7}'

Conversely, output can be obtained with:

netstat -ib | grep -e "$INTERFACE" -m 1 | awk '{print $10}'

You could pop the relevant counters into a flat file stored somewhere and compare it with:

#!/bin/bash
set -e # exit if any error occurs

previous_days_bytes_xferred=`cat $flatfile_storage`
todays_bytes_xferred=`netstat -ib | grep -e "$INTERFACE" -m 1 | awk '{print $10}'`

if [ $((todays_bytes_xferred - previous_days_bytes_xferred)) -gt $threshold ]; then
   DO SOME STUFF
fi
echo $todays_bytes_xferred > $flatfile_storage

Just adjust the netstat processing to match your system (since I know you're not running OSX).


Solution 3:

The sysstat package contains the sar utility. Sar is a extremely configurable system "profiler", for lack of a better term, that collects various statistics at predefined intervals (cron). These are stored in binary files, one for each day, that are typically rotated weekly, but that is configurable. The sar program can be used to pull the statistics out of these files, and those results can easily be graphed with any office program or gnuplot (my personal preference).

http://pagesperso-orange.fr/sebastien.godard/ <= Sar information
http://www.gnuplot.info/ <= gnuplot info


Solution 4:

Vnstat is a great console based daily / weekly / monthly traffic monitor. It even allows you to monitor the traffic in realtime which is handy.

It uses very few system resources too ;)


Solution 5:

You want MRTG which is a network monitoring tool, its the thing that produces all the network graphs you see around the web (with rdtool to turn the numbers into pictures)

If you don't want to set it up, or just need something simpler, you can use iptables to record data transfer.