Server data transmit log

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Server data transmit log

Post by toms100 »

whilst looking for some cool scripts i came across one which displays a graph of server data transmissions.
so i 'stole' the part of the script which gets the data from the server. this is a very cut down version which i modified to show what it gets:

Code: Select all

<?php
$procfile="/proc/net/dev";
$proc=fopen($procfile,'r');
while(!feof($proc)) {
		$line=fgets($proc,1024);
		$rinterface=substr($line,2,5);
		$values=substr($line,7,256);
		echo "LINE:".$line."<BR>";
		echo "RINTERFACE:".$rinterface."<BR>";
		echo "INTERFACE:".$interface."<BR>";
		// we parse the line
		if($rinterface == $interface) {
			sscanf($values,"%s %s %s %s %s %s %s %s %s",$rchars,$dummy,$dummy,$dummy,$dummy,$dummy,$dummy,$dummy,$tchars);
			echo "<br>".sscanf($values,"%s %s %s %s %s %s %s %s %s",$rchars,$dummy,$dummy,$dummy,$dummy,$dummy,$dummy,$dummy,$tchars);
			break;			
		}
	}

?>
When ran it outputs:
LINE:Inter-| Receive | Transmit
RINTERFACE:ter-|
INTERFACE:
LINE: face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
RINTERFACE:ace |
INTERFACE:
LINE: lo:65428759 208292 0 0 0 0 0 0 65428759 208292 0 0 0 0 0 0
RINTERFACE: lo:
INTERFACE:
LINE: eth0:318515552 2681176 0 0 0 0 0 0 1833500838 2067389 0 0 12 0 0 0
RINTERFACE:eth0:
INTERFACE:
LINE:
RINTERFACE:
INTERFACE:

-1
the thing which interests me most is
LINE: eth0:318515552 2681176 0 0 0 0 0 0 1833500838 2067389 0 0 12 0 0 0
i presume some part of that shows the ammount of data that has gone thru eth0, so what im thinking is:
put that in a database along with a timestamp.
run the script on each page.
do a comparison between the time between each record and data transmitted, and work out KB/s.

but any ideas about the fathom of info that eth0 spits out?

hope someone can help ;)

Tom
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Interesting reading?: http://www.geek.net.au/mrtg.html
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

thanks, ill have a look thru that;)
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

well i couldnt really work out what it was about ;)
but i searched on google and found this :

Code: Select all

&#1111;hartr@archenland hartr]$ cat /proc/net/dev
Inter-|   Receive                  |  Transmit
 face |packets errs drop fifo frame|packets errs drop fifo colls carrier
    lo:  92792    0    0    0    0    92792    0    0    0     0    0
  eth0: 621737   13   13    0   23   501621    0    0    0  1309    0
so i should be able to parse it now anyway
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You couldnt?
Gah, well I know camels arse about those things, but thought It what it (whatever that would be)...

Oh well... But please do post something on the issue later. Would be fun to see if you could get a kb/s, kbit/s, MB/day transfer stats from it...
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

well i did, but as it was in perl, i couldnt work out what was going on plus it seemed not to work on my server;)
and i have worked out how to get the KB transmitted so far, here is my code:

Code: Select all

<?php
$procfile="/proc/net/dev";
$interface='eth0:';
$proc=fopen($procfile,'r');
while(!feof($proc)) {
		$line=fgets($proc,1024);
		$rinterface=substr($line,2,5);
		$values=substr($line,7,256);
		if (strlen($values) > 20) 
			$savevalue = $values;
		// we parse the line
		if($rinterface == $interface) {			
			sscanf($values,"%s %s %s %s %s %s %s %s %s",$rchars,$dummy,$dummy,$dummy,$dummy,$dummy,$dummy,$dummy,$tchars);
			break;			
		}
	}
$dateth0 = explode(' ',$savevalue);
$c = count($dateth0);
$i = 0;
/// 0 and 38 are the business parts 
$DataOut = (($dateth0[38]/1024)/8);
$DataIn = (($dateth0[0]/1024)/8);
settype($DataOut,int);
settype($DataIn,int);
echo "Data Transmitted:".$DataOut."KB<br>";
echo "Data Recieved:".$DataIn."KB<br>";
?>
just need to do something fancy with it now :)
Post Reply