Apacheのアクセスログを集計する方法をまとめました。
Linux コマンドで集計
時間単位
# grep "日付/月/年" アクセスログファイル名 | awk '{print $4}' | cut -b 1-15 | sort | uniq -c
分単位
# grep "日付/月/年:時" アクセスログファイル名 | awk '{print $4}' | cut -b 1-15 | sort | uniq -c
秒単位
# grep "日付/月/年:時:分" アクセスログファイル名 | awk '{print $4}' | cut -b 1-15 | sort | uniq -c
phpプログラムで集計
phpで集計するプログラムも作成してみましたので、ご参考までに。
<?php define("TESTFILE","./test.txt"); define("HOURS_FILE","./hours.txt"); define("MINUTES_FILE","./minutes.txt"); define("SECONDS_FILE","./seconds.txt"); $fp = fopen(TESTFILE, "r"); $fp_h = fopen(HOURS_FILE, "w"); $fp_m = fopen(MINUTES_FILE, "w"); $fp_s = fopen(SECONDS_FILE, "w"); $array_hours = array(); $array_minutes = array(); $array_seconds = array(); $tmp = array(); while ($line = fgets($fp)) { preg_match('/([0-9]{1,2}\/.+\/2019:[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,3})/', $line, $match_hours); preg_match('/([0-9]{1,2}\/.+\/2019:[0-9]{1,2}:[0-9]{1,2})/', $line, $match_minutes); preg_match('/([0-9]{1,2}\/.+\/2019:[0-9]{1,2})/', $line, $match_seconds); $array_hours[] = $match_hours[0]; $array_minutes[] = $match_minutes[0]; $array_seconds[] = $match_seconds[0]; } /* 時間別集計 */ foreach ($array_hours as $key => $value){ $index = $value; if (array_key_exists($index, $tmp)){ $tmp[$index]++; } else { $tmp[$index] = 1; } } foreach ($tmp as $key => $value){ echo $key; fwrite($fp_h,$key.",".$value); } unset($tmp); /* 分別集計 */ foreach ($array_minutes as $key => $value){ $index = $value; if (array_key_exists($index, $tmp)){ $tmp[$index]++; } else { $tmp[$index] = 1; } } foreach ($tmp as $key => $value){ echo $key; fwrite($fp_m,$key.",".$value); } unset($tmp); /* 秒別集計 */ foreach ($array_seconds as $key => $value){ $index = $value; if (array_key_exists($index, $tmp)){ $tmp[$index]++; } else { $tmp[$index] = 1; } } foreach ($tmp as $key => $value){ echo $key; fwrite($fp_s,$key.",".$value); } fclose($fp); fclose($fp_h); fclose($fp_m); fclose($fp_s); ?<