Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a CRON/PHP script that will check the server stats every 5 minutes (CPU/RAM/SSD) and log the info to the database. This way I will be able to see server usage over different periods of time.

I am curious also to see how much bandwidth is used at what time in a day. I'd like to have a counter which would always increase. Then I could read the file every 5 minutes and calculate the difference.

The alternative solution I am using now is:

bwm-ng -o plain -u bytes -T sum -c 1 -t $(( 1000 * 5 * 60 ))

However, the problem with the latter is that it takes 5 minutes to return the result. Therefore, can anyone suggest how to approach the issue with the file that stores data usage all the time?

share|improve this question
You are reinventing the wheel. There are tons of utilities available that do what you want to do. Have a look at Munin, Cacti, Nagios, mrtg, just to name a few. – Sven Nov 8 '11 at 23:52
I don't want anything heavy, just simple script. Munin and others therefore don't seem like a solution. – Gajus Nov 9 '11 at 0:00
Munin is not heavy at all, the time you take to develop, debug and tweak your script would definitely be better used learning how to set up Munin. – ThatGraemeGuy Nov 9 '11 at 8:57
Have you looked at rrdtool? you can configure it to do what you want and build nice pretty graphics tto display in a web page. No need to install a database server for this either. It's all built in. – Matt Nov 9 '11 at 9:28
Munin uses rrdtool in the background, IMHO trying to learn how to use rrdtool "raw" is just as much reinventing the wheel. – ThatGraemeGuy Nov 9 '11 at 9:41

Easiest way imho is to use iptables.

Add a line to the top of your iptables that matches everything, and then just cut the output of iptables -L -v -n and then iptables -Z to zero the counters.

Its quick and dirty, but it'll likely work for your needs.

share|improve this answer

On the command line, you have quite a few tools that can do the job. If you're not interested in the established Orca, Cacti, Munin, and mrtg tools, look at nmon from IBM. This can be run interactively, but also outputs data to: /var/log/nmon. That's an option.

share|improve this answer
It looks like the last time nmon was updated was back in 2006? – Gajus Nov 9 '11 at 0:06

Munin isn't particular heavyweight, but anyway: The most basic command to get your info is just plain ifconfig.

eth0      Link encap:Ethernet  HWaddr xx:xx:xx:xx:xx:xx  
          inet addr:x.x.x.x  Bcast:x.x.x.x  Mask:
          inet6 addr: xxx Scope:Link
          RX packets:108930219 errors:0 dropped:10 overruns:0 frame:0
          TX packets:12486564 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2734738082 (2.7 GB)  TX bytes:2665032097 (2.6 GB)
          Interrupt:11 Base address:0x8000 

See the line with RX bytes? Use a

With netstat -s or /proc/net/netstat, you can drill down a lot more down to specific protocols.

share|improve this answer
bear in mind that loops at 4gb. – Sirex Nov 9 '11 at 9:02
Yep, but avoiding that pitfall should be straightforward (and Munin etc. already do that ...). – Sven Nov 9 '11 at 9:38

A quick and dirty demo using iptables. You should be ablr to figure out for yourself how to modify this for your needs.

iptables -I INPUT
iptables -I OUTPUT
iptables -Z
while true
    iptables -L -Z  INPUT  -v -n | awk 'NR==3 { print "INPUT bytes " $2 }'
    iptables -L -Z  OUTPUT  -v -n | awk 'NR==3 { print "OUTPUT Bytes " $2 }'
    sleep 20

which gives

INPUT bytes 0
OUTPUT Bytes 108
INPUT bytes 270K
OUTPUT Bytes 1777K
INPUT bytes 1760K
OUTPUT Bytes 12M
INPUT bytes 40
OUTPUT Bytes 216

Note that each time this is run a new blank rule is added to the INPUT and OUTPUT chain which is not a good thing.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.