Skip to content

Instantly share code, notes, and snippets.

@levidurfee
Created February 17, 2017 23:10
Show Gist options
  • Save levidurfee/3c76da3821f4ddc73df309f40817a8d6 to your computer and use it in GitHub Desktop.
Save levidurfee/3c76da3821f4ddc73df309f40817a8d6 to your computer and use it in GitHub Desktop.
fastcgi_cache analyzer
<?php
error_reporting(0);
include('Cache.php');
$c = new fcgiCacheAnalyze('/var/log/nginx/levi.cache.log');
<?php
class fcgiCacheAnalyze {
public function __construct($file) {
$log = file_get_contents($file);
$lines = explode("\n", $log);
$pages = [];
for($i=0;$i<count($lines) - 1;$i++) {
$line = explode(" ", $lines[$i]);
if($line[2] == "-") {
$line[2] = 'MISS';
}
if(isset($pages[$line[1]][$line[2]])) {
$pages[ $line[1] ] [ $line[2] ]++;
} else {
$pages[ $line[1] ] [ $line[2] ] = 1;
}
$pages[ $line[1] ] ['size'] = $line[3];
if($line[2] == "MISS") {
$pages[ $line[1] ] ['time'] = $line[4];
}
}
$mask = "| %-20.20s | %-5.5s | %-5.5s | %-5.5s | %-5.5s | %-5.5s | %-5.5s\n";
printf($mask, 'Page', 'TOTAL', 'HITS', 'MISS', 'EXPIRED', 'TIME', 'RATIO');
foreach($pages as $page => $value) {
$total = $pages[$page]['HIT'] + $pages[$page]['MISS'] + $pages[$page]['EXPIRED'];
$ratio = ($pages[$page]['HIT'] / $total) * 100;
printf($mask, $page, $total, $pages[$page]['HIT'], $pages[$page]['MISS'],
$pages[$page]['EXPIRED'], $pages[$page]['time'], $ratio);
}
}
}
http {
// . . .
log_format cache '$request_method $uri $sent_http_x_cache $bytes_sent $request_time';
// . . .
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment