How to use shell script to analyze website log statistics PV, 404, 500 and other data

The following shell script can count the total visits of the website and the number of occurrences of 404,500.
After the statistics are obtained, it can be recorded in combination with monitoring Baolai, and then it can be seen whether the website traffic is abnormal and whether there is an attack.
You can also judge whether the website program is abnormal by checking the number of occurrences of 500.

Complete script code:

 

#!/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 "<pre>\nc404:${c404}\nc500:${c500}\ntotal:${total}\n</pre>" > /data/www/status/www.jquerycn.cn.html

 

The last line of the script is:

<pre>
c404:1102
c500:545
total:55463
</pre>

The format is written to a www.jquerycn.cn.html file, and then combined with the custom monitoring of Monitoring Bao to collect this information.
Very convenient, the monitoring treasure will automatically display the chart.

Guess you like

Origin blog.csdn.net/qq_39436397/article/details/105389271