apache記錄檔詳解和實用分析命令_Linux

來源:互聯網
上載者:User

一.日誌分析
如果apache的安裝時採用預設的配置,那麼在/logs目錄下就會產生兩個檔案,分別是access_log和error_log
1).access_log
access_log為訪問日誌,記錄所有對apache伺服器進行請求的訪問,它的位置和內容由CustomLog指令控制,LogFormat指令可以用來簡化該日誌的內容和格式
例如,我的其中一台伺服器配置如下:

複製代碼 代碼如下:

CustomLog "| /usr/sbin/rotatelogs /var/log/apache2/%Y_%m_%d_other_vhosts_access.log 86400 480" vhost_combined


-rw-r--r-- 1 root root 22310750 12-05 23:59 2010_12_05_other_vhosts_access.log
-rw-r--r-- 1 root root 26873180 12-06 23:59 2010_12_06_other_vhosts_access.log
-rw-r--r-- 1 root root 26810003 12-07 23:59 2010_12_07_other_vhosts_access.log
-rw-r--r-- 1 root root 24530219 12-08 23:59 2010_12_08_other_vhosts_access.log
-rw-r--r-- 1 root root 24536681 12-09 23:59 2010_12_09_other_vhosts_access.log
-rw-r--r-- 1 root root 14003409 12-10 14:57 2010_12_10_other_vhosts_access.log

#通過CustomLog指令,每天一天產生一個獨立的記錄檔,同時也寫了定時器將一周前的記錄檔全部清除,這樣可以顯得更清晰,既可以分離每一天的日誌又可以清除一定時間以前的日誌通過制,LogFormat定義日誌的記錄格式

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedproxy
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

隨意的tail一個access_log檔案,下面是一條經典的訪問記錄

複製代碼 代碼如下:

218.19.140.242 - - [10/Dec/2010:09:31:17 +0800] "GET /query/trendxml/district/todayreturn/month/2009-12-14/2010-12-09/haizhu_tianhe.xml HTTP/1.1" 200 1933 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 (.NET CLR 3.5.30729)"

一共是有9項,將他們一一拆開來說明:

複製代碼 代碼如下:

218.19.140.242
-
-
[10/Dec/2010:09:31:17 +0800]
"GET /query/trendxml/district/todayreturn/month/2009-12-14/2010-12-09/haizhu_tianhe.xml HTTP/1.1"
200
1933
"-"
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 (.NET CLR 3.5.30729)"

1)  218.19.140.242 這是一個請求到apache伺服器的用戶端ip,預設的情況下,第一項資訊只是遠程主機的ip地址,但我們如果需要apache查出主機的名字,可以將 HostnameLookups設定為on,但這種做法是不推薦使用,因為它大大的減緩了伺服器.另外這裡的ip地址不一定就是客戶主機的ip地址,如果 用戶端使用了Proxy 伺服器,那麼這裡的ip就是Proxy 伺服器的地址,而不是原機.
2) - 這一項是空白,使用"-"來代替,這個位置是用於標註訪問者的標示,這個資訊是由identd的用戶端存在,除非IdentityCheck為on,非則apache是不會去擷取該部分的資訊(ps:不太理解,基本上這一項都是為空白,奉上原文)
The "hyphen" in the output indicates that the requested piece of information is not available. In this case, the information that is not available is the RFC 1413 identity of the client determined by identd on the clients machine. This information is highly unreliable and should almost never be used except on tightly controlled internal networks. Apache httpd will not even attempt to determine this information unless IdentityCheck is set to On.
3) - 這一項又是為空白,不過這項是使用者記錄使用者HTTP的身分識別驗證,如果某些網站要求使用者進行身份雁陣,那麼這一項就是記錄使用者的身份資訊
4) [10/Dec/2010:09:31:17 +0800] 第四項是記錄請求的時間,格式為[day/month/year:hour:minute:second zone],最後的+0800表示伺服器所處的時區為東八區
5) "GET /..haizhu_tianhe.xml HTTP/1.1" 這一項整個記錄中最有用的資訊,首先,它告訴我們的伺服器收到的是一個GET請求,其次,是用戶端請求的資源路徑,第三,用戶端使用的協議時HTTP/1.1,整個格式為"%m %U%q %H",即"要求方法/訪問路徑/協議"
6) 200 這是一個狀態代碼,由伺服器端發送回用戶端,它告訴我們用戶端的請求是否成功,或者是重新導向,或者是碰到了什麼樣的錯誤,這項值為200,表示伺服器已經成 功的響應了用戶端的請求,一般來說,這項值以2開頭的表示請求成功,以3開頭的表示重新導向,以4開頭的標示用戶端存在某些的錯誤,以5開頭的標示伺服器端 存在某些錯誤,詳細的可以參見 HTTP specification (RFC2616 section 10).[http://www.w3.org/Protocols/rfc2616/rfc2616.txt]
7) 1933 這項表示伺服器向用戶端發送了多少的位元組,在日誌分析統計的時侯,把這些位元組加起來就可以得知伺服器在某點時間內總的發送資料量是多少
8) - HTTP Referer:告訴伺服器我是從哪個頁面連結過來的,沒有值時可能是直接開啟網頁的原因。
9) "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 (.NET CLR 3.5.30729)" 這項主要記錄用戶端的瀏覽器資訊
2).error_log
error_log為錯誤記錄檔,記錄下任何錯誤的處理請求,它的位置和內容由ErrorLog指令控制,通常伺服器出現什麼錯誤,首先對它進行查閱,是一個最重要的記錄檔
tail error_log,隨意摘取一個記錄

複製代碼 代碼如下:

[Fri Dec 10 15:03:59 2010] [error] [client 218.19.140.242] File does not exist: /home/htmlfile/tradedata/favicon.ico

同樣也是分為幾個項:

複製代碼 代碼如下:

[Fri Dec 10 15:03:59 2010]
[error]
[client 218.19.140.242]
File does not exist: /home/htmlfile/tradedata/favicon.ico

1) [Fri Dec 10 15:03:59 2010] 記錄錯誤發生的時間,注意,它跟我們上面access_log記錄的時間格式是不同的
2) [error] 這一項為錯誤的層級,根據LogLevel指令來控制錯誤的類別,上面的404是屬於error層級
3) [client 218.19.140.242] 記錄用戶端的ip地址
4) File does not exist: /home/htmlfile/tradedata/favicon.ico 這一項首先對錯誤進行了描述,例如用戶端訪問一個不存在或路徑錯誤的檔案,就會給出404的提示錯誤

二.實用的日誌分析命令和指令碼

瞭解日誌的各種定義後,這裡分享一下從網上淘來的一些對日誌分析的指令碼。

1.查看apache的進程數
ps -aux | grep httpd | wc -l
2.分析日誌查看當天的ip串連數
cat default-access_log | grep "10/Dec/2010" | awk '{print $2}' | sort | uniq -c | sort -nr
3.查看指定的ip在當天究竟訪問了什麼url
cat default-access_log | grep "10/Dec/2010" | grep "218.19.140.242" | awk '{print $7}' | sort | uniq -c | sort -nr
4.查看當天訪問排行前10的url
cat default-access_log | grep "10/Dec/2010" | awk '{print $7}' | sort | uniq -c | sort -nr | head -n 10
5.看到指定的ip究竟幹了什麼
cat default-access_log | grep 218.19.140.242 | awk '{print $1"\t"$8}' | sort | uniq -c | sort -nr | less
6.查看訪問次數最多的幾個分鐘(找到熱點)
awk '{print $4}' default-access_log |cut -c 14-18|sort|uniq -c|sort -nr|head


 

聯繫我們

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