如何用shell脚本分析网站日志统计PV、404、500等数据

发布时间:2019-09-30编辑:脚本学堂
本文介绍了使用shell脚本分析网站日志统计PV(浏览量)、404、500等数据的方法,学习下shell脚本做数据分析的方法,有需要的朋友做个参考。

以下shell/ target=_blank class=infotextkey>shell脚本能统计出网站的总访问量,以及404,500出现的次数。
统计出来后,可以结合监控宝来进行记录,进而可以看出网站访问量是否异常,是否存在攻击。
还可以根据查看500出现的次数,进而判断网站程序是否出现异常。

完整脚本代码:
 

复制代码 代码示例:

#!/bin/bash
#purpose:count nginx or apache or other webserver status code using jiankongbao
#how to:run the script every 5 minutes with crontab

log_path="/var/log/nginx/www.jb200.com/access.log"
becur=`date -d "5 minute ago" +%H%M%S`
code=(`tac $log_path  | linuxjishu/13830.html target=_blank class=infotextkey>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/www.jb200.com.html

脚本最后一行是以:
 

复制代码 代码示例:
<pre>
c404:1102
c500:545
total:55463
</pre>

的格式写入到一个www.jb200.com.html文件,再结合监控宝的自定义监控来收集这些信息。
非常的方便,监控宝会自动出图表。