# Linux: How to measure daily/montly network traffic?

I need to keep statistics of daily network traffic for a linux machine (CentOS 5).

Is there a way to do it using standard/native tools or utilities?
Or do I have to dowload special software for that?

Thanks.

• How accurate do you need the statistics? In other words do you need something specific enough to re-bill for bandwidth or are averages good? – Zypher Jan 28 '10 at 23:11
• I need to know exactly how much traffic I've been used each day because I dont want to exceed certain limits. – GetFree Jan 28 '10 at 23:51
• Then you don't want any of the RRD based tools (Cacti, MRTG, Munin, etc) RRD averages the numbers so you'll never get an exact metric, and it will actually get worse over time. – Zypher Jan 29 '10 at 0:17

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).

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


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

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 ;)

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.

I used mrtg, it's a very nice solution, and there are other tool that seems to be interesting and that give a lot of information about traffic named ntop a network probe, here find a link about installing ntop on CentOS

Either MRTG or Cacti. Cacti is a pain to set up, but gives some very pretty graphs. MRTG is easy to set a basic plot up, but for anything pretty or complicated, it's pretty tough.

Munin is another nice RRD-based tool that's very easy to set up; the default network stats are quite basic, but it's easy to add additional plugins. I've attached a copy of the daily and weekly stats on one of my servers; you also get monthly and yearly stats on a different page.

Munin network graph