apache日志分析简介


 

对apache的日志分析做下简单的介绍,主要参考apache官网的Log Files,手册参照http://httpd.apache.org/docs/2.2/logs.html

 

一.日志分析

如果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地址,如果客户端使用了代理服务器,那么这里的ip就是代理服务器的地址,而不是原机.

 

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) - 暂不知

 

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

 

 

 

三.使用awstats自动分析日志

当然啦,如果想最简单和最直观的分析日志还是用工具,现在网上较流行的工具是awstats,一个基于perl的web日志分析工具,功能很强大也支持IIS等服务器

下载地址http://awstats.sourceforge.net

安装配置见http://blog.s135.com/post/199/

 

 


简单的界面
\
 
作者:21aspnet

相关内容

    暂无相关文章