Forum Moderators: coopster

Message Too Old, No Replies

Extracting RAW Logs

         

norbiu

2:33 pm on Jul 20, 2009 (gmt 0)

10+ Year Member



Is there a way to automatically extract RAW logs using PHP or do I need to download them manually from cPanel?

mattclayb

3:21 pm on Jul 20, 2009 (gmt 0)

10+ Year Member



if you know the path to the log file and php has sufficient access privilages then you can access the log files by using fopen().

You can then write whatever script you want to parse the file like you would with any other.

something like this will parse the log file andd store each part of the log entry into named variables -

$log_file = '/usr/local/apache/logs/access.log';
$pattern = '/^([^ ]+) ([^ ]+) ([^ ]+) (\[[^\]]+\]) "(.*) (.*) (.*)" ([0-9\-]+)
([0-9\-]+) "(.*)" "(.*)"$/';

$fh = fopen($log_file,'r') or die($php_errormsg);
$i = 1;
$requests = array();
while (! feof($fh)) {
// read each line and trim off leading/trailing whitespace
if ($s = trim(fgets($fh,16384))) {
// match the line to the pattern
if (preg_match($pattern,$s,$matches)) {
/* put each part of the match in an appropriately-named
* variable */
list($whole_match,$remote_host,$logname,$user,$time,
$method,$request,$protocol,$status,$bytes,$referer,
$user_agent) = $matches;
// keep track of the count of each request
$requests[$request]++;
} else {
// complain if the line didn't match the pattern
error_log("Can't parse line $i: $s");
}
}
$i++;
}
fclose($fh) or die($php_errormsg);

// sort the array (in reverse) by number of requests
arsort($requests);

// print formatted results
foreach ($requests as $request => $accesses) {
printf("%6d %s\n",$accesses,$request);
}

norbiu

11:16 am on Jul 22, 2009 (gmt 0)

10+ Year Member



Wow, thanks!