shell分析nginx日誌
日誌格式:
178.255.215.86 - - [04/Jul/2013:00:00:31 +0800] "GET /tag/316/PostgreSQL HTTP/1.1" 200 4779 "-" "Mozilla/5.0 (compatible; Exabot/3.0 (BiggerBetter); +http://www.exabot.com/go/robot)" "-"- 178.255.215.86 - - [04/Jul/2013:00:00:34 +0800] "GET /tag/317/edit HTTP/1.1" 303 5 "-" "Mozilla/5.0 (compatible; Exabot/3.0 (BiggerBetter); +http://www.exabot.com/go/robot)" "-"- 103.29.134.200 - - [04/Jul/2013:00:00:34 +0800] "GET /code-snippet/2022/edit HTTP/1.0" 303 0 "-" "Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/17.0 Firefox/17.0" "-"- 103.29.134.200 - - [04/Jul/2013:00:00:35 +0800] "GET /user/login?url=http%3A//outofmemory.cn/code-snippet/2022/edit HTTP/1.0" 200 4748 "-" "Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/17.0 Firefox/17.0" "-"-
提取日誌 分別是訪問URL和URL訪問來源 排序
awk '{print $7}' access.log | sort | uniq -c |sort -nr | head -n10 > test.txt
以下指令碼都是基於上面日誌格式的,如果你的日誌格式不同需要調整awk後面的引數。
分析日誌中的UserAgent
cat access_20130704.log | awk -F "\"" '{print $(NF-3)}' | sort | uniq -c | sort -nr | head -20
上面的指令碼將分析出日誌檔案中最多的20個UserAgent
分析日誌中那些IP訪問最多
cat access_20130704.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -20
分析日誌中那些Url請求訪問次數最多
cat access_20130704.log | awk -F "\"" '{print $(NF-5)}' | sort | uniq -c | sort -nr | head -20
分析網路tcp連線
netstat -an|awk '/^tcp/{++S[$NF]}END{for (a in S)print a,S[a]}' 檢視tcp連結數
netstat -pant |grep ":8443"|awk '{print $5}' | awk -F: '{print $1}'|sort|uniq -c|sort -nr 檢視連線數最多的ip
cat access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -n10
注意TIME_WAIT
檢視核心的tcp控制代碼限制:#ulimit -n #ulimit -a 若TIME_WAIT 且為自身程式,需要修改調整tcp操作型別,若是為核心限制過小,可以調整核心引數進行處理,一旦所用控制代碼已經達到限制,則程式對外任何tcp均無法建立