以下檔案下載
#!/bin/bash # # Filename: count_req.sh # Revision: 1.0 # Author: Qicheng # Website: http://qicheng0211.blog.51cto.com # Description: 統計Nginx日誌裡IP訪問頻率 NGINXLOG="./access.log" start_time=$(head -n1 "$NGINXLOG" | grep -o " \[.*\] ") stop_time=$(tail -n1 "$NGINXLOG" | grep -o " \[.*\] ") echo -e "start:\t\e[92m$start_time\033[0m" echo -e "stop:\t\e[92m$stop_time\033[0m" echo '所有的請求TOP50-->>' # 所有的請求 cat "$NGINXLOG" | awk '{++S[$1]} END {for(a in S) print S[a],"\t", a}' | sort -rn -k1 | head -n 50 echo '--------------------------------------------------' echo '成功的請求TOP50-->>' # 成功的請求 grep ' 200 ' "$NGINXLOG" | awk '{++S[$1]} END {for(a in S) print S[a],"\t", a}' | sort -rn -k1 | head -n 50
把腳本放到和access.log同一目錄下執行即可。
這兩句後面要稍微改一下,顯示才正常
echo -e "start:\t\e[92m$start_time33[0m"
echo -e "stop:\t\e[92m$stop_time33[0m"
已修正