分析日志统计网站pv 404 500状态码的shell脚本
时间:2014-11-05 12:37 来源:linux.it.net.cn 作者:IT
本节分享的这段shell脚本,可以统计出网站的总访问量,以及404,500出现的次数。
统计出来后,可以结合监控宝来进行记录,进而可以看出网站访问量是否异常,是否存在攻击。
另外,还可以根据查看500出现的次数,以判断网站程序是否出现异常。
shell脚本代码:
复制代码代码示例:
#!/bin/bash
#purpose:count nginx or apache or other webserver status code using jiankongbao
#how to:run the script every 5 minutes with crontab
#edit: www.jbxue.com
#
log_path="/var/log/nginx/www.it.net.cn/access.log"
becur=`date -d "5 minute ago" +%H%M%S`
code=(`tac $log_path | awk -v a="$becur" -v total=0 -F [' ':] '{
t=$5$6$7
if (t>=a){
code[$12]++
total++
}
else {
exit;
}
}END{
print code[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/itnetcn.html
注意:
本脚本最后一行是以:
<pre>
c404:1102
c500:545
total:55463
</pre>
的格式写入到一个itnetcn.html文件。
有兴趣的朋友,可以研究下结合监控宝的自定义监控来收集这些信息的方法。
(责任编辑:IT)
本节分享的这段shell脚本,可以统计出网站的总访问量,以及404,500出现的次数。
shell脚本代码:
复制代码代码示例:
#!/bin/bash
#purpose:count nginx or apache or other webserver status code using jiankongbao #how to:run the script every 5 minutes with crontab #edit: www.jbxue.com # log_path="/var/log/nginx/www.it.net.cn/access.log" becur=`date -d "5 minute ago" +%H%M%S` code=(`tac $log_path | awk -v a="$becur" -v total=0 -F [' ':] '{ t=$5$6$7 if (t>=a){ code[$12]++ total++ } else { exit; } }END{ print code[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/itnetcn.html
注意:
<pre>
c404:1102 c500:545 total:55463 </pre> 的格式写入到一个itnetcn.html文件。 有兴趣的朋友,可以研究下结合监控宝的自定义监控来收集这些信息的方法。 (责任编辑:IT) |