OpenWrt Forum Archive

Topic: Apps for monitoring traffic?

The content of this topic has been archived on 30 Mar 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi,

i'm searching for a way to monitor my traffic.
Finally, i want to have some kind of table where the traffic usage of every day is displayed.
Do you know any programs which may manage this for the wrt54g?

I already thought about a small script, which parses the 'ifconfig' output and then writes it into /tmp. But the problem is, that it's lost after a reboot.
Writing into another directory than /tmp is unacceptable, i think.

So, the best way will be to manage this the snmp-like way:
Doing a cronscript which sends traffic data to a remote host every 5 minutes. Do you know any scripts which may be nice for this kind of traffic accounting?

Cheers,
phil

P.S.: Because lots of people told me on IRC .. I do not want to monitor my traffic via mrtg or rrdtool. This only shows me the bandwith utilization. What I want to know is, how much traffic i made on what day.

I'm not quite sure what you mean. If I read your message correctly then it *would* be snmpd/rrdtool type of thing that you are after. If you are absolutely sure this is not what you want then I think you have to get a little more specific. What you proprose gives you the same thing you can get from snmp and rrdtool. I have built a net-snmp ipkg and am currently graphing one of my wrt54g's interfaces:

http://voidmain.is-a-geek.net/cacti/gra … ;leaf_id=9

Actually I just started my graphs going again a few hours ago as my WRT was down for the last few days and I didn't realize it.

Here's the ipkgs (I suggest installing the static server):
http://voidmain.is-a-geek.net/files/ipkg/

Of course others have come up with ways to do similar things via the web server. If you can get a little more specific I could probably come up with a better answer. Of course you probably already know all of this because you were involved in the building of the ipkg. smile  The only other thing I can think you are asking for is listing the total number of bytes in each 24 hour period for each interface in a text page. If this is what you are after it would be very trivial to do, in fact I would still use SNMP to do this. The OIDs that are used to graph the interface usage on my page are just counters. You can have a program on another machine poll these counters and keep track of the total traffic.

List of interfaces (same as reported by ifconfig):
$ snmpwalk -v 1 -c public xxx.xxx.xxx.xxx ifDescr

RX Bytes (same as reported by ifconfig):
$ snmpwalk -v 1 -c public xxx.xxx.xxx.xxx ifInOctets

TX Bytes (same as reported by ifconfig):
$ snmpwalk -v 1 -c public xxx.xxx.xxx.xxx ifOutOctets

You'll want to check these counters fairly often and update your totals because if you have a high traffic load these counters could roll over relatively often. The basic logic in keeping the total would be to query each of the above OIDs and if the value is greater than the value of the previous query then subtract the previous query from the current one and add the value to the total. If the current value is less than the previous value then just add the current value to the total because it would indicate a rollover.  After 24 hours of this, record your totals and zero them out.

On my 1.1 router eth0 is index 2 in the snmp output (loopback is index 1) so to get the current RX bytes for eth0 I would:

$ snmpget -O qv -v 1 -c public xxx.xxx.xxx.xxx ifInOctets.2

well, the last stuff, you described is exactly that what i want.
But now, I want to have this saved at some place. Sure, the best way would be a html-page on a remote host, where the traffic of every day is logged.
Do you know any programs which i may use for this?

Yes, PHP has SNMP support built right in (if you have compiled it in or installed the appropriate package for your distro. php-snmp in Red Hat/Fedora). Since I also run MySQL and/or PostgreSQL with Apache I would probably write a simple PHP script to query the OIDs every 5 minutes from a cron job to keep a simple table updated with the current totals. You could add a new row for each day that kept the totals in separate columns. You could then create a nicely formated web page with PHP and have it query the table to display the results. Of course you could use Perl or any other language to do this and you wouldn't have to use a database but could use a flat file. A table in a database would be the easiest in my opinion. If you would like me to create an example for you I could (except for making it "pretty", you'll know what I mean if you have ever seen any of my pages smile).

Well, since there don't seem to be any other programs for this: Please, do it! wink

Here's another alternative using rrdtool:

http://ei.kefro.st/projects/rrdtool/

Notice they are getting the totals and displaying them on the graph. You could query the RRD every 24 hours from cron and save the results or even better you could also run "rrdtool graph ..." and generate a graph every 24 hours and save it for each interface. Might as well slap a date in the title of the graph. This is probably  a better way than my previous suggestion. Let the tools do the work for you.

hey Voidman,

i just wanted to let you know, that i'm working on a php script which records the traffic, writes it into a database and creates graph how much traffic (in mb or gb) is generated at a specified day.
Although, it's still a early beta, i can already read out the generated traffic via a cronscript every 24 hours. Now, i'm working on the mysql part: Writing the stuff into a database. The next thing, after that will be to create graphs. But i guess, it'll take some time till i've reached that point. wink
Anyway, i just wanted to tell you, not to start coding a script for the same thing. I don't won't us both writing the same script. wink

Maybe, i'll can release it here, within the next days.

Cheers,
phil

I've got something to add:
As you already said, snmp grabs it's data from the ifconfig output.
I don't think, that you can trust in the output of ifconfig. It's kinda weird: Yesterday (when my unit was up for 5 days or so), i had a input traffic of 3,7gigs on eth0. Now, I'm just having 1.9. Somethings wrong there .. I also saw the same happening on different leenox boxes, all using ifconfig.
Also, various people already told me, that ifconfig is displaying traffic the wrong way, and that I shouldn't use it.
Do you know anything about this? Does ifconfig reset it's traffic count every one or two days, or so?
If nobody has a solution for this, i'll have to use another way, i guess. I've heard that iptables are good for traffic logging .. But the problem then is, that i can't use snmp for this anymore.

ifconfig numbers would be the most accurate you can get and snmp will give you the same numbers. They are of type "counter32" which means that the counter is a 32 bit integer. The largest value a 32 bit integer can hold is 4GB (4,294,967,295). That is not a problem for calculating total data, in fact I mentioned in my first post that it does this. I even explained how you keep track of the total amount of data that has passed through the interface by checking the value often (at least as often as the minimum time it could possibly roll over). I do believe I made a mistake in the logic though.

Every 5 minutes should be plenty for counting the totals on a 100Mbps interface. You compare the difference in the counter between the current check and the last check. If the current check is greater than the last check then you know the counter has not rolled over and you just add the difference between the two to your total (total = total + (curr - last)). If the current check of the counter reads lower than the last check then you know the counter has rolled over so you have to do a little more on this one (total = total + cur + (4GB - last)). There may be some bitwise operation that would make more sense than that last calculation but I don't know what it would be off the top of my head. I could probably look through the rrdtool source and see how it does it.

At any rate, using the ifconfig and/or the snmp equivelant values are the most accurate way you can do it (in fact it would be exact). The only exception would be is if your system was rebooted. You could lose up to 5 minutes worth of counted data if you check every 5 minutes and your system rebooted.

On a side note, I also run Linux on Sparc and Alpha machines and the RX/TX counters are 64 bit so they don't roll over near as often (I doubt you would ever see them to roll over without doing the math on how long it would take at any given data rate).

okay - thanks for the info. i didn't realize, that it's that easy to manage.

I'll try to include this in my script.

Phew.
Got a big part of my script working now, after coding nearly the whole time of the last three days.
At the moment, it grabs the trafficdata correctly and checks wether it's "correct" or not (and updates it when it's not correct, of course).
Tomorrow, i'll start working on the output of the script.
I guess, that it'll be ready for release at the weekend.

Void Main wrote:

Here's another alternative using rrdtool:

http://ei.kefro.st/projects/rrdtool/

Notice they are getting the totals and displaying them on the graph. You could query the RRD every 24 hours from cron and save the results or even better you could also run "rrdtool graph ..." and generate a graph every 24 hours and save it for each interface. Might as well slap a date in the title of the graph. This is probably  a better way than my previous suggestion. Let the tools do the work for you.

indeed rrdtool owns, u should try it! smile

I am using vnstat.

The discussion might have continued from here.