nginx日誌分析shell指令碼

來源:互聯網
上載者:User

先基本瞭解一幾條命令

一下指令碼都是基於上面日誌格式的,如果你的日誌格式不同需要調整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

下面我們進入正題了,利用grep的強大體現在N多方面,這裡用grepRegex來分析nginx日誌,為了方便多次使用,寫指令碼留念。暫時命名為 nla.sh

可以根據自己的需要修改,被grep的東西,來得到自己想要的結果。

 代碼如下 複製代碼

#!/bin/bash

#################################################
#
#  這是一個 nginx 預設日誌分析指令碼
#  主要使用grep來工作

#  考慮到很多人喜歡把日誌按日期分割並gz
#  加入了簡單的gz格式判斷
#  日誌分析完成後會把檔案還原
#  ccshaowei#gmail.com
#  2012/05/13
#  http://shaowei.info/
################################################

#  修改下面一行為日誌位置
log_dir='access.log-*'
##########
#  $key為grep的關鍵字,$word為提示,要求一一對應,數量相同
##########
#http 代碼
key[0]='" 200 [0-9]{3}';word[0]='http 200'
key[1]='" 206 [0-9]{3}';word[1]='http 206'
key[2]='" 404 [0-9]{3}';word[2]='http 404'
key[3]='" 503 [0-9]{3}';word[3]='http 503'
##########
#  seo/seo.html" target="_blank">搜尋引擎爬蟲
key[4]='Googlebot.*google.com/bot.html';word[4]='來自Google爬蟲'
key[5]='Baiduspider.*baidu.com/search/spider.html';word[5]='來自百度蜘蛛'
key[6]='bingbot.*bing.com/bingbot.htm';word[6]='來自Bing爬蟲'
#Soso 'Sosospider.*soso.com/webspider.htm'
#有道 'YoudaoBot.*youdao.com/help/webmaster/spider/'
#Yahoo中國 'Yahoo! Slurp China'
##########
#  瀏覽器
key[7]='MSIE';word[7]='MSIE'
key[8]='Gecko/.*Firefox';word[8]='Firefox'
key[9]='AppleWebKit.*like Gecko';word[9]='Webkit'
key[10]='Opera.*Presto';word[10]='Opera'
#360安全 'MSIE.*360SE' 或加ie核心版本 'MSIE 6.0 .*360SE' 'MSIE 7.0 .*360SE' 'MSIE 8.0 .*360SE' 'MSIE 9.0 .*360SE'
#360急速 'AppleWebKit.*QIHU 360EE'
##########
#  作業系統
key[11]='Windows NT 6.1';word[11]='Windows 7'
key[12]='Macintosh; Intel Mac OS X';word[12]='Mac OS X'
key[13]='X11.*Linux';word[13]='Linux with X11'
key[14]='Android;';word[14]='Android'
#Windows系列 win2000'Windows NT 5.0' winxp'Windows NT 5.1' winvasta'Windows NT 6.0' win7'Windows NT 6.1'
#SymbianOS 'SymbianOS'
##########
#  裝置
key[15]='iPad.*like Mac OS X';word[15]='iPad'
key[16]='Nokia';word[16]='諾基亞系列'
key[17]='Nokia5800';word[17]='Nokia5800 XpressMusic'
#iPhone 'iPhone.*like Mac OS X'
##########
#  其他
key[18]='GET /.*.mp3 HTTP';word[18]="訪問mp3檔案"
key[19]='GET /.*.jpg HTTP';word[19]="訪問jpg檔案"

#  配置結束
##############################################################################

log_num=$(ls ${log_dir} | wc -l)
fileid=0
isgz=0
#gz檢查
for file in $(ls ${log_dir})
do
if [ "${file##*.}" = "gz" ]; then
        isgz[$fileid]=1
        gzip -dvf $file
        logfile[$fileid]=$(echo $file | sed 's/.gz$//')
        (( fileid++ ))
else
        isgz[$fileid]=0
        logfile[$fileid]=$file
        (( fileid++ ))
 fi
done
#  檢查key和 word 的數量是否一致
if [ ${#word[@]} -ne ${#key[@]} ]
then
 echo "配置有錯誤,key和word的數量不一致"
else

checkid=0
while [ $checkid -lt $log_num  ]
do
filename=${logfile[$checkid]}
totle=$(cat $filename | wc -l)
echo "日誌 ${filename} 共 ${totle} 行,需要處理 ${#key[@]} 項"
echo "來源IP數:$(cat $filename | awk '{print $1}' |sort|uniq|wc -l)"
i=0
while [ $i -lt ${#key[@]} ]
do
 s1=${word[$i]}
 s2=$(cat $filename | grep ''"${key[$i]}"'' | wc -l)
 s3=$(awk 'BEGIN{printf "%.2f%n",('$s2'/'$totle')*100}')
 echo "${s3} ${s1}: ${s2}"
 ((i++))
done
(( checkid++ ))
echo "-----------------"
done
fi
#  還原壓縮檔
gzid=0
while [ $gzid -lt $log_num ]
do
if [ "${isgz[$gzid]}" = "1" ]
then
gzip -v ${logfile[$gzid]}
fi
(( gzid++ ))
done

運行結果範例如下:

[root@hostname temp]# ls -lh
總用量 299M
-rw-r----- 1 root root  11M  5月 14 13:25 access.log-20120508.gz
-rw-r----- 1 root root 158M  5月 14 13:25 access.log-20120509
-rw-r----- 1 root root 2.2M  5月 14 13:25 access.log-20120510.gz
-rw-r----- 1 root root 129M  5月 14 13:25 access.log-20120511
-rwxr-xr-x 1 root root 3.4K  5月 14 13:10 nla.sh
[root@hostname temp]# sh nla.sh
access.log-20120508.gz:     93.5% -- replaced with access.log-20120508
access.log-20120510.gz:     93.9% -- replaced with access.log-20120510
日誌 access.log-20120508 共 643281 行,需要處理 20 項
來源IP數:7483
44.52%    http 200: 286400
3.55%    http 206: 22824
20.23%    http 404: 130128
14.31%    http 503: 92029
1.94%    來自Google爬蟲: 12491
2.01%    來自百度蜘蛛: 12943
0.90%    來自Bing爬蟲: 5780
76.53%    MSIE: 492291
2.21%    Firefox: 14209
7.03%    Webkit: 45215
0.27%    Opera: 1736
25.17%    Windows 7: 161935
1.37%    Mac OS X: 8830
0.03%    Linux with X11: 202
0.03%    Android: 190
0.11%    iPad: 677
0.50%    諾基亞系列: 3207
0.02%    Nokia5800 XpressMusic: 102
36.06%    訪問mp3檔案: 231959
23.10%    訪問jpg檔案: 148600
-----------------
日誌 access.log-20120509 共 608316 行,需要處理 20 項
來源IP數:7429
45.15%    http 200: 274651
1.79%    http 206: 10884
15.59%    http 404: 94854
19.95%    http 503: 121376
2.83%    來自Google爬蟲: 17245
1.80%    來自百度蜘蛛: 10970
0.23%    來自Bing爬蟲: 1410
78.96%    MSIE: 480324
1.28%    Firefox: 7783
7.85%    Webkit: 47774
0.43%    Opera: 2597
22.85%    Windows 7: 139022
0.63%    Mac OS X: 3827
0.06%    Linux with X11: 389
0.06%    Android: 372
0.06%    iPad: 351
0.19%    諾基亞系列: 1158
0.00%    Nokia5800 XpressMusic: 4
34.94%    訪問mp3檔案: 212555
23.46%    訪問jpg檔案: 142702
-----------------
日誌 access.log-20120510 共 141224 行,需要處理 20 項
來源IP數:2040
50.15%    http 200: 70823
1.67%    http 206: 2354
14.15%    http 404: 19987
17.37%    http 503: 24534
4.53%    來自Google爬蟲: 6399
2.66%    來自百度蜘蛛: 3754
0.44%    來自Bing爬蟲: 622
69.34%    MSIE: 97921
1.19%    Firefox: 1682
9.54%    Webkit: 13470
0.53%    Opera: 742
19.37%    Windows 7: 27351
1.23%    Mac OS X: 1737
0.03%    Linux with X11: 45
0.00%    Android: 0
0.09%    iPad: 130
0.86%    諾基亞系列: 1220
0.00%    Nokia5800 XpressMusic: 0
30.29%    訪問mp3檔案: 42777
23.91%    訪問jpg檔案: 33768
-----------------
日誌 access.log-20120511 共 473259 行,需要處理 20 項
來源IP數:5093
44.91%    http 200: 212551
1.96%    http 206: 9286
15.14%    http 404: 71671
21.20%    http 503: 100322
2.44%    來自Google爬蟲: 11548
1.40%    來自百度蜘蛛: 6616
3.40%    來自Bing爬蟲: 16068
76.75%    MSIE: 363224
0.93%    Firefox: 4388
6.75%    Webkit: 31937
0.31%    Opera: 1444
28.62%    Windows 7: 135444
0.43%    Mac OS X: 2057
0.02%    Linux with X11: 116
0.00%    Android: 0
0.09%    iPad: 419
0.23%    諾基亞系列: 1094
0.00%    Nokia5800 XpressMusic: 0
35.77%    訪問mp3檔案: 169274
22.46%    訪問jpg檔案: 106299
-----------------
access.log-20120508:     93.5% -- replaced with access.log-20120508.gz
access.log-20120510:     93.9% -- replaced with access.log-20120510.gz
[root@hostname temp]# ls -lh
總用量 299M
-rw-r----- 1 root root  11M  5月 14 13:25 access.log-20120508.gz
-rw-r----- 1 root root 158M  5月 14 13:25 access.log-20120509
-rw-r----- 1 root root 2.2M  5月 14 13:25 access.log-20120510.gz
-rw-r----- 1 root root 129M  5月 14 13:25 access.log-20120511
-rwxr-xr-x 1 root root 3.4K  5月 14 13:10 nla.sh

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.