Skip to content

Instantly share code, notes, and snippets.

@waltertschwe
Created June 4, 2018 21:51
Show Gist options
  • Select an option

  • Save waltertschwe/f7dfb24bee254365da615c36c162e8e2 to your computer and use it in GitHub Desktop.

Select an option

Save waltertschwe/f7dfb24bee254365da615c36c162e8e2 to your computer and use it in GitHub Desktop.
<?php
try {
$logFile = fopen("/WWW/nginx/godaddy/log-file.log", 'rb');
} catch( Exception $e ) {
error_log("File could not be opened. Exiting.", $e);
exit();
}
$totalRequests = 0;
$codes = [];
$pageVists = [];
$logRegex = '/^(\S+) (\S+) (\S+) (\S+) (\S+) \"(\S+) (\S+) (\S+) (\S+) (\S+)/';
while (($line = fgets($logFile)) !== false) {
preg_match($logRegex ,$line, $matches);
// process status codes
array_key_exists(9,$matches) ? $statusCode = $matches[9] : $statusCode = "unknown";
(!isset($codes[$statusCode])) ? $codes[$statusCode] = 1 : $codes[$statusCode]++;
// process popular files
array_key_exists(7,$matches) ? $url = $matches[7] : $url = "unknown";
(!isset($pageVisits[$url])) ? $pageVisits[$url] = 1 : $pageVisits[$url]++;
$totalRequests++;
}
fclose($logFile);
foreach($codes as $code => $total) {
echo "Total " . $code . " codes = " . $total . " <br/> ";
}
echo "Total Requests = " . $totalRequests;
echo "<br/><br/> <b>Top 5 pages visited </b><br/>";
arsort($pageVisits);
$pageVisitCounter = 1;
foreach($pageVisits as $url => $total) {
echo $pageVisitCounter . ". " . $url . " = " . $total . "<br/>";
if($pageVisitCounter >= 5)
break;
$pageVisitCounter++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment