RobertP Posted January 6, 2013 Share Posted January 6, 2013 Let me know what you think... <?php $hitDebug = false; $startMemory = memory_get_usage(); $startTime = microtime(true); print '<pre>' . PHP_EOL; $totalFiles = $bandwidth = $hits = 0; $uniqueHits = array(); $uniqueHits24h = array(); foreach (glob('/var/log/apache2/access.*') as $logFile) { if (stristr($logFile, '.gz'))//I manually extracted them.. continue; /* Array ( [0] => 127.0.0.1 [1] => - [2] => - [3] => [12/Dec/2012:00:57:29 [4] => -0500] [5] => GET /test.php HTTP/1.1 [6] => 200 [7] => 313 [8] => - [9] => Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11 ) */ $handle = fopen($logFile, 'r'); if ($handle) { $lineStartMemory = memory_get_usage(); if ($hitDebug) print "Hit #\t\tMemory\t\tMicrosecond" . PHP_EOL; while (($line = stream_get_line($handle, 1024, PHP_EOL)) !== false) { $lineStartTime = microtime(true); $results = str_getcsv($line, " ", '"', '\\'); $bandwidth += $results[7]; if (!isset($uniqueHits[$results[0]])) { $uniqueHits[$results[0]] = null; list($d, $M, $y, $h, $m, $s, $z) = sscanf($results[3] . ' ' . $results[4], '[%2d/%3s/%4d:%2d:%2d:%2d %5s]'); $time = strtotime($d . ' ' . $M . ' ' . $y . ' ' . $h . ':' . $m . ':' . $s . ' ' . $z); if ((microtime(true) - $time) < 86400) $uniqueHits24h[$results[0]] = null; } $hits++; if ($hits % 100 == 0 && $hitDebug) print 'Hit #' . $hits . "\t" . number_format(memory_get_usage() - $lineStartMemory) . "\t\t" . substr(((microtime(true) - $lineStartTime) * 1000000), 1) . PHP_EOL; } if ($hitDebug) print "\n"; if (!feof($handle)) print 'Error: unexpected fgets() fail' . PHP_EOL; fclose($handle); } print 'Log File: ' . $logFile . ' (' . number_format(filesize($logFile) / 1024, 2) . ' KB)' . PHP_EOL; $totalFiles++; } $parserTime = number_format(microtime(true) - $startTime, 4); print PHP_EOL; print 'Total Files: ' . number_format($totalFiles) . PHP_EOL; print 'Total Hits: ' . number_format($hits) . PHP_EOL; print 'Total Unique Hits: ' . number_format(count($uniqueHits)) . ' (' . number_format(count($uniqueHits24h)) . ' within the last 24 hours)' . PHP_EOL; print 'Total Bandwidth: ' . number_format($bandwidth / 1024 / 1024, 2) . ' MB (' . number_format($bandwidth) . ' B)' . PHP_EOL; print PHP_EOL; print '[PARSER]' . PHP_EOL; print 'Total Time: ' . $parserTime . PHP_EOL; print 'Total Memory: ' . (memory_get_usage() - $startMemory) . ' B' . PHP_EOL; print 'Memory Peak: ' . memory_get_peak_usage() . ' B' . PHP_EOL; print '</pre>'; ?> demo: http://auth.gludoe.com/bandwidth.php Quote Link to comment Share on other sites More sharing options...
Christian F. Posted January 6, 2013 Share Posted January 6, 2013 It's missing comments... Quote Link to comment Share on other sites More sharing options...
cpd Posted January 7, 2013 Share Posted January 7, 2013 Cache the result maybe? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.