使用shell脚本分析网站日志统计PV、404、500等数据
#!/bin/bash
#purpose:countnginxorapacheorotherwebserverstatuscodeusingjiankongbao
#howto:runthescriptevery5minuteswithcrontab
log_path="/var/log/nginx/www.nhooo.com/access.log"
becur=`date-d"5minuteago"+%H%M%S`
code=(`tac$log_path |awk -va="$becur"-vtotal=0-F['':]'{
t=$5$6$7
if(t>=a){
code[$12]++
total++
}
else{
exit;
}
}END{
printcode[404]?code[404]:0,code[500]?code[500]:0,total
}'
`)
c404=${code[0]}
c500=${code[1]}
total=${code[2]}
echo-e"<pre>\nc404:${c404}\nc500:${c500}\ntotal:${total}\n</pre>">/data/www/status/www.nhooo.com.html脚本最后一行是以:
<pre> c404:1102 c500:545 total:55463 </pre>
的格式写入到一个www.nhooo.comhtml文件,再结合监控宝的自定义监控来收集这些信息。非常的方便,监控宝会自动出图表。