- 论坛徽章:
- 0
|
不做运维很久了, awk,sed 快忘光了,前两天要做access_log ip 统计,所以只能拿PHP当shell来用了
功能: 统计apache access_log 日志文件中的每ip 访问次数排行.
#!/usr/local/php/bin/php
<?
$arr = file("./access_log" ;
$ip_array = array();
foreach($arr as $v){
$array = explode(" ",$v);
$ip_array[] = $array[0];
}
$temp = array_count_values($ip_array);
asort($temp);
foreach($temp as $k=>$v) {
echo $k." ".$v."\n";
}
$ip_num = count($temp);
$maxcount = max($temp);
$totalcount = array_sum($temp);
foreach ($temp as $k=>$v) {
if ($v == $maxcount) {
$topip = $k;
break;
}
}
echo "\n";
echo "=== Total {$ip_num} ips ===\n"; // 总共不同的IP数
echo "=== Total {$totalcount} counts ===\n"; // 总访问数
echo "=== The Max counts ip is {$topip} the count is {$maxcount} ===\n"; // 访问数最高的IP及访问次数
?> |
|