Linux下apache日誌分析與狀態查看方法

來源:互聯網
上載者:User

假設apache日誌格式為:
118.78.199.98 – - [09/Jan/2010:00:59:59 +0800] “GET /Public/Css/index.css HTTP/1.1″ 304 – “http://www.a.cn/common/index.php” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6.3)”

問題1:在apachelog中找出訪問次數最多的10個IP。
awk '{print $1}' apache_log |sort |uniq -c|sort -nr|head -n 10

awk 首先將每條日誌中的IP抓出來,如日誌格式被自訂過,可以 -F 定義分隔字元和 print指定列;
sort進行初次排序,為的使相同的記錄排列到一起;
upiq -c 合并重複的行,並記錄重複次數。
head進行前十名篩選;
sort -nr按照數字進行倒敘排序。

我參考的命令是:
顯示10條最常用的命令
sed -e "s/| //n/g" ~/.bash_history | cut -d ' ' -f 1 | sort | uniq -c | sort -nr | head

問題2:在apache日誌中找出訪問次數最多的幾個分鐘。
awk '{print $4}' access_log |cut -c 14-18|sort|uniq -c|sort -nr|head
awk 用空格分出來的第四列是[09/Jan/2010:00:59:59;
cut -c 提取14到18個字元
剩下的內容和問題1類似。

問題3:在apache日誌中找到訪問最多的頁面:
awk '{print $11}' apache_log |sed 's/^.*cn/(.*/)/"//1/g'|sort |uniq -c|sort -rn|head

類似問題1和2,唯一特殊是用sed的替換功能將”http://www.a.cn/common/index.php”替換成括弧內的內容:”http://www.a.cn(/common/index.php)”

問題4:在apache日誌中找出訪問次數最多(負載最重)的幾個時間段(以分鐘為單位),然後在看看這些時間哪幾個IP訪問的最多?
1,查看apache進程:
ps aux | grep httpd | grep -v grep | wc -l

2,查看80連接埠的tcp串連:
netstat -tan | grep "ESTABLISHED" | grep ":80" | wc -l

3,通過日誌查看當天ip串連數,過濾重複:
cat access_log | grep "19/May/2011" | awk '{print $2}' | sort | uniq -c | sort -nr

4,當天ip串連數最高的ip都在幹些什麼(原來是蜘蛛):
cat access_log | grep "19/May/2011:00" | grep "61.135.166.230" | awk '{print $8}' | sort | uniq -c | sort -nr | head -n 10

5,當天訪問頁面排前10的url:
cat access_log | grep "19/May/2010:00" | awk '{print $8}' | sort | uniq -c | sort -nr | head -n 10

6,用tcpdump嗅探80連接埠的訪問看看誰最高
tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr

接著從日誌裡查看該ip在幹嘛:
cat access_log | grep 220.181.38.183| awk '{print $1"/t"$8}' | sort | uniq -c | sort -nr | less

7,查看某一時間段的ip串連數:
grep "2006:0[7-8]" www20110519.log | awk '{print $2}' | sort | uniq -c| sort -nr | wc -l

8,當前WEB伺服器中聯結次數最多的20條ip地址:
netstat -ntu |awk '{print $5}' |sort | uniq -c| sort -n -r | head -n 20

9,查看日誌中訪問次數最多的前10個IP
cat access_log |cut -d ' ' -f 1 |sort |uniq -c | sort -nr | awk '{print $0 }' | head -n 10 |less

10,查看日誌中出現100次以上的IP
cat access_log |cut -d ' ' -f 1 |sort |uniq -c | awk '{if ($1 > 100) print $0}'|sort -nr |less

11,查看最近訪問量最高的檔案
cat access_log |tail -10000|awk '{print $7}'|sort|uniq -c|sort -nr|less

12,查看日誌中訪問超過100次的頁面
cat access_log | cut -d ' ' -f 7 | sort |uniq -c | awk '{if ($1 > 100) print $0}' | less

13,列出傳輸時間超過 30 秒的檔案
cat access_log|awk '($NF > 30){print $7}'|sort -n|uniq -c|sort -nr|head -20

14,列出最最耗時的頁面(超過60秒的)的以及對應頁面發生次數
cat access_log |awk '($NF > 60 && $7~//.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.