A simple log analyze script

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
TheLinker
-


Joined: 05 Apr 2002
Posts: 165
Location: Oslo, Norway

PostPosted: Tue Feb 18, 2003 11:24 am    Post subject: A simple log analyze script Reply with quote

I have seen some topics here regarding the Server Statistics available from the control panel of Abyss. I've put together a script that might give you some ideas on how to retrieve the information from the access log. Feel free to do what you want with the script. :)
Code:
<html>
<body>
<?php

// Full path to the access log
$accesslog = "D:\\Program Files\\Abyss Web Server\\log\\access.log";

// Reset the variables
$totalbytes = 0;
$totalhits = 0;
$errorhits = 0;

// Function returns 1 if parameter status is 404 or 500
function is_errorstatus( $status )
{
   switch( $status )
   {
      case "404":
      case "500": return 1;
      default: return 0;   
   }
}

// Open the access log for read
$hlog = fopen($accesslog, "r");

// Evaluate each line in the log, and filter out needed data with regular expression
while($found = fscanf($hlog, "%s - - [%[A-Za-z0-9/]:%[0-9:] %[+-0-9]] \"%[A-Z-] %s %[A-Z0-9/.]\" %[0-9-] %[0-9-]\n", &$remotehost, &$date, &$time, &$ofset, &$request, &$file, &$protocol, &$status, &$bytes))
{

   // Sum total bytes
   $totalbytes += $bytes;
   
   // Sum total hits
   $totalhits ++;
   
   // Sum error hits
   $errorhits += is_errorstatus($status);

   // Reset all variables
   unset($remotehost, $date, $time, $ofset, $request, $file, $protocol, $status, $bytes);
}

// Close the access log
fclose($hlog);

// Print out the information
echo "<b>Hits</b>: ".$totalhits."<br>\n";
echo "<b>Error Hits</b>: ".$errorhits."<br>\n";
echo "<b>Output (Kb)</b>: ".ceil($totalbytes / 1000)."<br>\n";

?>
</body>
</html>

The script is not a complete solution to every scenario... e.g. if your log file gets very big, you don't want to read through the log file every time.
One solution here is to write out the variables to some "cache" file, which holds the aggregated information... and timestamp the information. You could also split the access log file into your own controlled access log files, which may be sorted on date.

The script was put together rather quickly, so there might be errors lurking around... 8O

Best of regards... 8)
Back to top View user's profile Send private message Visit poster's website
DLashley
-


Joined: 18 Dec 2002
Posts: 207
Location: New York, NY

PostPosted: Sat Feb 22, 2003 7:29 am    Post subject: Reply with quote

Hey, thanks! :)

Lemme go check this out!
_________________
DLashley
Back to top View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group