天天看點

shell腳本分析apache的通路日志1. shell腳本分析apache的通路日志

1. shell腳本分析apache的通路日志

前言:

ip值:

ip可以了解為獨立IP的通路使用者,指1天内使用不同IP位址的使用者通路網站的數量,同一IP無論通路了幾個頁面,獨立IP數均為1。但是假如說兩台機器通路而使用的是同一個IP,那麼隻能算是一個IP的通路。

pv值:

即頁面浏覽量,或點選量;網站的PV值,即網站的人均浏覽次數;pv通常是衡量一個網絡新聞頻道或網站甚至一條網絡新聞的主要名額,或者簡單的說,PV值就是一個通路者在24小時(0點到24點)内到底看了網站多少個頁面。這裡需要強調:同一個人浏覽你網站同一個頁面,不重複計算pv量,點100次也算1次。

PV值對于網站來說,就像電視的收視率一樣,從某種程度上已成為投資者衡量商業網站表現的最重要尺度。

PV值的計算:

當一個通路者通路網站的時候,記錄他所通路的頁面和對應的IP,然後确定這個IP今天通路了這個頁面沒有。如果你的網站到了23點,單純IP有60萬條的話,每個通路者平均通路了3個頁面,那麼PV表的記錄就要有180(60×3)萬條。

當然,有時還會同時考察另外一個名額,即uv(uniquevisitor),指通路某個站點或點選某條新聞的不同IP位址的人數。在同一天内,UV隻記錄第一次進入網站的具有獨立IP的通路者,在同一天内再次通路該網站則不計數。

Apache日志格式如下:

shell腳本分析apache的通路日志1. shell腳本分析apache的通路日志

1.1 擷取通路日志中每個ip的通路次數

[[email protected] ~]# cat /var/log/httpd/access_log |awk '{print $1}'|sort|uniq  -c
      2 ::1
     10 192.168.153.1
    110 192.168.153.130
      4 192.168.153.149
    119 192.168.153.177
      3 192.168.153.183
     29 192.168.153.185
           

1.2 列印出通路次數前5的ip位址和通路次數

[[email protected] ~]# cat /var/log/httpd/access_log |awk '{print $1}'|sort|uniq  -c|sort -nr|head -5
    119 192.168.153.177
    110 192.168.153.130
     29 192.168.153.185
     10 192.168.153.1
      4 192.168.153.149
           

1.3 提取通路次數超過20次的頁面

[[email protected] httpd]# cat access_log|awk '{print $7}'|sort |uniq -c|awk '{if ($1>20) print $2}'
/
/images/apache_pb.gif
/images/poweredby.png
/noindex/css/bootstrap.min.css
/noindex/css/fonts/Bold/OpenSans-Bold.ttf
/noindex/css/fonts/Bold/OpenSans-Bold.woff
/noindex/css/fonts/Light/OpenSans-Light.ttf
/noindex/css/fonts/Light/OpenSans-Light.woff
/noindex/css/open-sans.css
           

1.4 列印出通路量最大的頁面

[[email protected] httpd]# cat access_log|awk '{print $7}'|sort|uniq -c|sort -nr|head -1
     62 /
           

1.5 檢視傳輸位元組數大于200的資源

[[email protected] httpd]# cat access_log|awk '{print $7,$10}'|sort|uniq|awk '{if ($2>200) print $1}'
/
/favicon.ico
/images/apache_pb.gif
/images/poweredby.png
/noindex/css/bootstrap.min.css
/noindex/css/fonts/Bold/OpenSans-Bold.ttf
/noindex/css/fonts/Bold/OpenSans-Bold.woff
/noindex/css/fonts/Light/OpenSans-Light.ttf
/noindex/css/fonts/Light/OpenSans-Light.woff
/noindex/css/open-sans.css
           

1.6 列印出消耗帶寬最大的頁面

[[email protected] ~]# cat /var/log/httpd/access_log |awk '{print $7,$10}'|sort -nrk2|uniq|head -1|awk '{print $1}'
/noindex/css/bootstrap.min.css
           

1.7 檢視通路次數最多的ip

[[email protected] ~]# cat /var/log/httpd/access_log |awk '{print $1}'|sort|uniq  -c|sort -nr|head -1|cut -f 1
    119 192.168.153.177
           

1.8 檢視某一天的獨立ip通路數量

[[email protected] httpd]# cat access_log|awk '{print $1}'|sort|uniq|wc -l
7
           

1.9 提取出某一天的PV量

[[email protected] ~]# cat /var/log/httpd/access_log| wc -l
277
           

繼續閱讀