用途:
程序执行的太快,可能相邻2s的日志会交错打印,如果看每秒处理的请求数就不太方便,可以通过如下脚本来解决
脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php /* * command: tail -f access_log|awk '{print $5}' |php a.php * time format: [03/May/2013:16:51:30 */ $old = 0; $mid = 0; while(!feof(STDIN)) { $line = str_replace("[", "", trim(fgets(STDIN))); list($day, $month, $year) = explode("/", $line); list($year, $h, $i, $s) = explode(":", $year); $time = strtotime("$year-$month-$day $h:$i:$s"); if ($old == 0) { $old = $time; } if ($time - $old > 1) { echo date("Y-m-d H:i:s", $old),"\t", $arr[$old], "\n"; unset($arr[$old]); $old = $mid; } if ($time - $old == 1) { $mid = $time; } $arr[$time]++; } |