| Forums
PHP code.
|
|
| Handover Phist | Sat Jun 10 2006, 09:39 | |
| BaBAM
 Joined: Sun Feb 19 2006, 12:55 Posts: 117
| So I decided to build a PHP script that parses the logfile on my server and returns useful information. This only works for Linux boxen running Apache, and assumes Apache is logging everything to a single file. I've managed to get PHP to read the file into an array and give me a hit count, but being a shell coder I dont know how to do some things with the array, such count keys that DONT contain certain IP addresses for example.
This is what I've got so far:
$hitcount=0; $logfile = fopen("/path/to/apache/access_log","r"); while (!feof($logfile)) { $data = array(fgets($logfile));
foreach($data as $line) { $hitcount = $hitcount+1; } }
echo $hitcount; echo " hits were counted. \n";
This works and works FAST. Way faster than shell scripting.
[ Edited Sun Jun 11 2006, 09:23 ] | | Back to top | |
| the-Vyper | Sat Jun 10 2006, 10:17 | |
| Registered Member #14 Joined: Tue Feb 28 2006, 02:38 Posts: 20
| please increase the text size of the PHP so it's easier to read please
In a world of extreme change to survive you must be an extremist trying to change the world | | Back to top | |
| the-Vyper | Sat Jun 10 2006, 10:18 | |
| Registered Member #14 Joined: Tue Feb 28 2006, 02:38 Posts: 20
| maybe also give a sample logfile with the count key and such
In a world of extreme change to survive you must be an extremist trying to change the world | | Back to top | |
| Handover Phist | Sun Jun 11 2006, 09:27 | |
|
 Joined: Sun Feb 19 2006, 12:55 Posts: 117
| Here's a single entry from the logfile:
192.168.1.21 - - [09/Jun/2006:07:59:02 -0700] "GET /index.php HTTP/1.1" 302 5 "http://websterscafe.com/news.php" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4"
I want to explode this and count the various elements. Any lines that start "192.168.1." should be excluded as they originate from my network and not the Internet. | | Back to top | |
| Handover Phist | Sun Jun 11 2006, 10:11 | |
|
 Joined: Sun Feb 19 2006, 12:55 Posts: 117
| This seems to do the trick. Now I'm going to start counting stats like browsers used and which subdomain they were going to.
$head="
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><HTML><HEAD><TITLE> Apache logfile parser. </TITLE></HEAD><BODY bgcolor=\"#000000\" text=\"#FFFFFF\" alink=\"#c1c6ff\" vlink=\"#c1c6ff\" link=\"#c1c6ff\"><center><h2>Jasons server stats</h2></center><table width=800 border=1 align=center><tr><td width=1 rowspan=30 bgcolor=\"#FFFFFF\"></td><td colspan=28 width=800 height=1 bgcolor=\"#FFFFFF\"></td><td width=1 rowspan=30 bgcolor=\"#FFFFFF\"></td></tr><td><br />";
echo $head;
$foot="
<br /></td> <tr><td colspan=28 width=750 height=1 bgcolor=\"#FFFFFF\"></td></tr> </table> </BODY> </HTML> ";
function parseit() {
$hitcount=0; $localcount=0;
$logfile = fopen("/var/log/apache/access_log","r");
while (!feof($logfile)) {
$data = array(fgets($logfile));
foreach($data as $line) {
$linearray = explode(" ", $line);
$seelocal=substr($linearray[0], 0, 7);
if ( $seelocal == "192.168" ) {
$localcount = $localcount+1;
} else {
$hitcount = $hitcount+1;
}
}
}
echo $localcount;
echo " hits from within the network were counted <br />\n";
echo $hitcount;
echo " hits were counted from outside the network. <br />\n";
asort($data);
}
parseit();
echo $foot;
[ Edited Sun Jun 11 2006, 10:14 ] | | Back to top | |
| Handover Phist | Sun Jun 11 2006, 01:02 | |
|
 Joined: Sun Feb 19 2006, 12:55 Posts: 117
| Ok, I'm having entirely too much fun. I'm stopping work on the script, but here it is for anyone who might find it handy:
<?php
/*
* Written by Jason Cutting
* Apache log parsing and report program, I haven't named it yet.
*
* CHANGELOG
*
* Sun Jun 11
* Added hit counter.
* Added percentage reporting of internal vs external hits
* Added configuration section for portability, no more guessing what the other users are doing.
*/
// Configuration section. Make changes to suit your system
$alogfile="/var/log/apache/access_log";
$localnet="192.168.1";
$head="
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><HTML><HEAD><TITLE> Apache logfile parser. </TITLE></HEAD><BODY bgcolor=\"#000000\" text=\"#FFFFFF\" alink=\"#c1c6ff\" vlink=\"#c1c6ff\" link=\"#c1c6ff\"><center><h2>Jasons server stats</h2></center><table width=800 border=1 align=center><tr><td width=1 rowspan=30 bgcolor=\"#FFFFFF\"></td><td colspan=28 width=800 height=1 bgcolor=\"#FFFFFF\"></td><td width=1 rowspan=30 bgcolor=\"#FFFFFF\"></td></tr><td><br /><center>";
$foot="
</center> <br /></td> <tr><td colspan=28 width=750 height=1 bgcolor=\"#FFFFFF\"></td></tr> </table> </BODY> </HTML> ";
function hitcount() {
global $alogfile, $localnet;
$hitcount=0; $localcount=0; $outsidecount=0;
$logfile = fopen($alogfile, "r");
while (!feof($logfile)) {
$data = array(fgets($logfile));
foreach($data as $line) {
$linearray = explode(" ", $line);
$seelocal=substr($linearray[0], 0, 9);
if ( $seelocal == $localnet ) {
$localcount = $localcount+1;
} else {
$outsidecount = $outsidecount+1;
}
$hitcount=$hitcount+1;
}
}
$localpercent=($localcount/$hitcount)*100;
$outpercent=($outsidecount/$hitcount)*100;
echo "Out of a total of ". $hitcount ." hits:<br /><br />";
echo $localcount ." hits from within the network were counted accounting for %". round($localpercent) ." of the total hits<br />\n";
echo $outsidecount ." hits were counted from outside the network accounting for %". round($outpercent) ." of the total hits <br />\n";
}
// Output
echo $head;
hitcount();
echo $foot;
?>
[ Edited Sun Jun 11 2006, 01:04 ] | | Back to top | |
| Handover Phist | Sun Jun 11 2006, 06:00 | |
|
 Joined: Sun Feb 19 2006, 12:55 Posts: 117
| OK, I've decided to move the code to a sticky thread and use this thread for discussion. There are dead groups in Sourceforge that advertised at one point they wanted to make a script like this, well, here one is. | | Back to top | |
Moderators: Handover Phist
|
|
Powered by e107 Forum System
| |