zl程序教程

您现在的位置是:首页 >  其他

当前栏目

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

2023-06-13 09:20:06 时间

以下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.jquerycn.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 "
/nc404:${c404}/nc500:${c500}/ntotal:${total}/n

" /data/www/status/www.jquerycn.cn.html

脚本最后一行是以:

 

c404:1102 

c500:545 

total:55463 

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

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/3076.html

服务器部署程序员系统优化网站设置运维