TheLinker -
Joined: 05 Apr 2002 Posts: 165 Location: Oslo, Norway
|
Posted: Tue Feb 18, 2003 11:24 am Post subject: A simple log analyze script |
|
|
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) |
|