用shell做nginx日誌分析
if [[ $# == 0 ]];then
echo "Error:請輸入要查詢的天數???"
else
echo "暫時不支持centos的7的版本!!!"
NOW_TIME_CUO=date +%s
((END_TIME_CUO=${NOW_TIME_CUO}-echo $1*3600*24 | bc
))
NOW_TIME=date | awk ‘{print $3"\\\/"$2"\\\/"$NF}‘
END_TIME=date -d @${END_TIME_CUO}| awk ‘{print $3"\\\/"$2"\\\/"$NF}‘
##根據ip統計一段時間的UV
cat access.log|grep -v curl|sed -n "/${END_TIME}/,/${NOW_TIME}/p"| awk ‘{print $1}‘| sort| uniq -c| wc -l
##根據訪問URL訪問PV
echo -e "\033[31mOutput $1 day‘s PV: \033[0m"
cat access.log|grep -v curl|sed -n "/${END_TIME}/,/${NOW_TIME}/p"| awk ‘{print $7}‘| wc -l
##查詢訪問最頻繁的URL
echo -e "\033[31mOutput $1 day‘s 10 HOT URL: \033[0m"
cat access.log|grep -v curl|sed -n "/${END_TIME}/,/${NOW_TIME}/p"| awk ‘{print $7}‘| sort|uniq -c|sort -n -k 1 -r|head
##查詢訪問最頻繁的IP
echo -e "\033[31mOutput $1 day‘s 10 HOT IP: \033[0m"
cat access.log| grep -v curl|sed -n "/${END_TIME}/,/${NOW_TIME}/p"|awk ‘{print $1}‘|sort|uniq -c|sort -n -k 1 -r|head
fi
:<<DOWN
1、根據訪問IP統計UV
awk ‘{print $1}‘ access.log | sort | uniq -c | wc -l
2、統計訪問URL統計PV
awk ‘{print $7}‘ access.log | wc -l
3、查詢訪問最頻繁的URL
awk ‘{print $7}‘ access.log | sort | uniq -c |sort -n -k 1 -r | more
4、查詢訪問最頻繁的IP
awk ‘{print $1}‘ access.log | sort | uniq -c |sort -n -k 1 -r | more
5、根據時間段統計查看日誌
cat access.log |sed -n ‘/14\/Mar\/2015:21/,/14\/Mar\/2015:22/p‘| more
DOWN
用shell做nginx日誌分析