天天看点

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
           

继续阅读