结合运用了很多技巧,值得收藏。
一,日志样例
172.40.62.167 - - [30/Sep/2018:22:38:57 +0800] "GET /styles/blue-theme.css HTTP/1.1" 200 130510 "-" "http://127.0.0.1/setup.php" "Mozilla/5.0 (X11;Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
172.40.58.146 - - [19/Nov/2018:09:01:46 +0800] "GET /course HTTP/1.1" 404 169 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
172.40.58.152 - - [19/Nov/2018:08:58:40 +0800] "GET /favicon.ico HTTP/1.1"200 32988 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
二,脚本
#!/bin/bash
# NGINX标准日志分析脚本
GREEN_COL='\033[32m'
NONE_COL='\033[0m'
line='echo ++++++++++++++++++++++++++++++++++'
read -p "请输入日志文件:" logfile
echo
# 统计页面访问量(PV)
PV=$(cat $logfile|wc -l)
# 统计用户数量(UV)
UV=$(cut -f1 -d' ' $logfile|sort |uniq |wc -l)
# 统计人均访问次量
Average_PV=$(echo "scale=2;$PV/$UV" |bc)
# 统计每个IP的访问次数
declare -A IP
while read ip other
do
let IP[$ip]+=1
done < $logfile
# 统计各种HTTP状态码个数
declare -A IP
while read ip dash user time zone method file protocol code size other
do
let STATUS[$code]++
done < $logfile
# 统计网页累计访问字节大小
while read ip dash user time zone method file protocol code size other
do
let Body_size+=$size
done < $logfile
# 统计热点数据
declare -A URI
while read ip dash user time zone method file protocol code size other
do
let URI[$file]++
done < $logfile
echo -e "\33[91m\t日志分析数据报表\033[0m"
# 显示PV和UV访问量,平均用户访问量
$line
echo -e "累计PV量:$GREEN_COL$PV$NONE_COL"
echo -e "累计UV量:$GREEN_COL$UV$NONE_COL"
echo -e "平均用户访问量:$GREEN_COL$Average_PV$NONE_COL"
# 显示网页累计访问字节数
$line
echo -e "累计访问字节数:$GREEN_COL$Body_size$NONE_COL Byte"
# 显示指定的HTTP状态码数量
$line
for i in 200 404 500
do
if [ ${STATUS[$i]} ]; then
echo -e "$i 状态码次数:$GREEN_COL ${STATUS[$i]} $NONE_COL"
else
echo -e "$i 状态码次数:$GREEN_COL 0 $NONE_COL"
fi
done
# 显示每个IP的访问次数
$line
for i in ${!IP[@]}
do
printf "%-15s的访问次数为:$GREEN_COL$s$NONE_COL\n" $i ${IP[$i]}
done
echo
# 显示访问量大于500的URI
echo -e "$GREEN_COL 访问量大于500的URI:$NONE_COL"
for i in "${!URI[@]}"
do
if [ ${URI["$i"]} -gt 500 ]; then
echo "----------------------------------"
echo "$i"
echo "$URI[$i]}次"
echo "-----------------------------------"
fi
done
网友评论