Linux: How to measure daily/montly network traffic

centoslinuxmeasurementnetwork-traffictraffic

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.

Best Answer

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