Ngnix日誌分析

Ngnix日誌分析

  • cat用來讀取日誌內容
  • grep進行匹配的文本搜索
  • wc則進行最終的統計
  • grep與命令格式: grep -E 「a.*b」 file,ab條件同時成立
  • grep或命令的格式爲:grep -E 「a|b」 file ,ab兩個條件有一個成當即可

Nginx日誌格式:

$remote_addr$remote_user [$time_local] $request $status $apache_bytes_sent $http_referer $http_user_agent
127.0.0.1 - - [24/Mar/2011:12:45:07 +0800] "GET /fcgi_bin/xxx.fcgi?id=xxx HTTP/1.0" 200 160 "-" "Mozilla/4.0"

grep打印匹配的先後幾行

$grep -C 5 ‘parttern’ inputfile //打印匹配行的先後5行
$grep -A 5 ‘parttern’ inputfile //打印匹配行的後5行
$grep -B 5 ‘parttern’ inputfile //打印匹配行的前5行

統計某接口調用的次數

$cat access.log | grep 'GET /task/showContent' | wc -l
$grep 'GET /task/showContent' access.log -c

統計全部接口的調用次數並顯示出現次數最多的前二十的URL

$cat access.log|awk '{split($7,b,"?");COUNT[b[1]]++;}END{for(a in COUNT) print COUNT[a], a}'|sort -k1 -nr|head -n20

統計nginx日誌中報錯較多的接口

$cat nginx-ad-access.log|awk'{if($9==500) print $0}'|awk '{split($7,b,"?");COUNT[b[1]]++;}END{for(a in COUNT) print COUNT[a], a}'|sort -k 1 -nr|head -n10

經過日誌查看當天訪問頁面排前10的url

$cat access.log | grep "24/Mar/2011" | awk '{print $7}' | sort | uniq -c | sort -nr | head -n 10

經過日誌查看當天訪問次數最多的10個IP ,只須要在上一個命令後加上head命令

$cat access.log | grep "24/Mar/2011" |awk '{print $3}'|sort |uniq -c|sort -nr|head –n 10

經過日誌查看當天訪問次數最多的10個IP

$awk '{print $1}' access.log |sort |uniq -c|sort -nr|head

經過日誌查看當天指定ip訪問次數過的url和訪問次數

$cat access.log | grep "10.0.21.17" | awk '{print $7}' | sort | uniq -c | sort –nr

經過日誌查看當天訪問次數最多的時間段

$awk '{print $4}' access.log | grep "24/Mar/2011" |cut -c 14-18|sort|uniq -c|sort -nr|head
相關文章
相關標籤/搜索