-2

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?

mdpc
  • 11,856
  • 28
  • 53
  • 67
Gajus
  • 851
  • 5
  • 16
  • 28
  • 7
    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 08 '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 09 '11 at 00: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 09 '11 at 08: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. – hookenz Nov 09 '11 at 09:28
  • 1
    Munin uses rrdtool in the background, IMHO trying to learn how to use rrdtool "raw" is just as much reinventing the wheel. – ThatGraemeGuy Nov 09 '11 at 09:41

4 Answers4

2

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.

Bart De Vos
  • 17,911
  • 6
  • 63
  • 82
Sirex
  • 5,499
  • 2
  • 33
  • 54
1

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.

ewwhite
  • 197,159
  • 92
  • 443
  • 809
1

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:255.255.255.0
          inet6 addr: xxx Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          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.

Sven
  • 98,649
  • 14
  • 180
  • 226
0

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

#!/bin/bash
iptables -I INPUT
iptables -I OUTPUT
iptables -Z
while true
do
    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
done

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.

user9517
  • 115,471
  • 20
  • 215
  • 297