loading ...

2006-11-27 | 实用的Linux命令技巧(日志分析)

标签: 技巧  文件  分析  下载  日志 

实用的Linux命令技巧(日志分析)

jimmy | 27 十一月, 2006 13:50

日志分析,大致两种:

1,根据apache的日志进行分析。有很多成熟的工具。

2,定制日志。通过改写Apache配置,或者在页面中插入定制的跟中代码,对访问进行尽录。通常分析需要自己编写程序进行处理。

这里主要讨论前者。对于后者,也有可以简单应用的方法,比如google提供的anaylistics。

Analog(http://www.analog.cx)是一个很简易的分析工具。这里介绍用analog分析日志的相关处理。

1,首先要对Apache的httpd.conf进行处理。

LogFormat "%h %l %u %t "%m %V%U %H" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%h %l %u %t "%r" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
CustomLog "|/usr/local/sbin/cronolog /cache/apachelogs/all_access%w.log" common env=!gif-image

注意语法: CustomLog 日志文件地址 日志格式

CustomLog logs/access.log combined

这个是比较常见的。为了防止日志文件过大,可以选择 common 格式,并且对日志进行按日的分隔。

推荐使用: cronolog 。下载以后安装一下就可以。

另一种常见的是用 TransferLog :

TransferLog "|/usr/local/apache/bin/rotatelogs /usr/local/apache/logs/log 86400"


日志文件中其实有很多是我们不需要的,比如:*.jpg, *.gif

可以通过设定环境变量来屏蔽。

找到 setenvif 模块,缺省的一般是这样:


<IfModule mod_setenvif.c>

BrowserMatch "Mozilla/2" nokeepalive
BrowserMatch "MSIE 4.0b2;" nokeepalive downgrade-1.0 force-response-1.0

BrowserMatch "RealPlayer 4.0" force-response-1.0
BrowserMatch "Java/1.0" force-response-1.0
BrowserMatch "JDK/1.0" force-response-1.0

</IfModule>

在其中添加:

SetEnvIf Request_URI .gif$ gif-image
SetEnvIf Request_URI .jpg$ gif-image
SetEnvIf Request_URI .css$ gif-image
SetEnvIf Request_URI .js$ gif-image

进一步的,可以把其它不想加入日志的放进去:

SetEnvIf Request_URI /left/ gif-image
SetEnvIf Request_URI main gif-image

在CustomLog定义的时候,声明不记录此类文件:

CustomLog "|/usr/local/sbin/cronolog /cache/apachelogs/all_access%w.log" common env=!gif-image

重启一下Apache,看看日志文件是不是有了。(注意:Log的日志目录必须先建立,比如我的是:mkdir /cache/apachelogs)

如果日志正常,接着安装和配置Analog。下载Analog并且安装(注意,RPM 与 源代码 方式安装产生的目录可能不同,最好查看一下)

analog默认配置文件在:/etc/analog.cfg 其实,默认的配置已经足够好了,我们只要调整一下输入和输出文件就可以:

LOGFILE /cache/apachelog/all_access3.log

OUTFILE /cache/apachelog/Report3.html

使用PHP的话,还要把 #PAGEINCLUDE *.php 前面的#去掉。

然后运行analog:

# analog

生成了report3.html,说明工作正常。

因为我们的日志是按天分开的,所以每次修改cfg文件会不方便,用命令行可以指定输入、输出文件:

先编辑/etc/analog.cfg, 把输入输出配置去掉。然后运行:

#analog +O/cache/apachelog/report3.html /cache/apachelog/all_access3.log

或者编写一个脚本: myanalog.sh

#!/bin/sh
cd /cache/apachelogs/analog
if [ $# -gt 0 ] ; then
day=$1;
else
day="0";
fioutputfile="/cache/apachelogs/analog/report$day.html"
logfile="/cache/apachelogs/all_access$day.log"date >> analog.log
echo $logfile >> analog.log
/usr/bin/analog +O$outputfile $logfile

#chmod +x myanalog.sh

#./myananlog.sh 3

这样就可以分析周三的日志文件了。

通过观察生成的结果文件,常常发现这样一个问题: request report部分,列的文件统计不是最想要的,或者是列得太多。

通过修改analog的配置文件来调节:

1,设置 Floor 值。例子:

DOMFLOOR 1000r #低于1000的虚拟主机不生成报告

REQFLOOR 1000p #低于1000次请求的页面不生成报告

可以设置的值和代码会在后面给出。

2,设置 Alias,将相似结果归并。

下面是我实用中添加的配置:

REQFLOOR -200r #只列出访问最高的200个页面URL

FILEALIAS /list_art_sub.new.php* /list_art_sub.new.php #把所有list_art_sub.new.php?b=xxx&a=xxx&t=xxx 等,都当作一个文件。下面类似
FILEALIAS /read_art_sub.new.php* /read_art_sub.new.php
FILEALIAS /small/* /small/
FILEALIAS /lianzai/* /lianzai/
FILEALIAS /read_art_sub.org.php* /read_art_sub.org.php
FILEALIAS /read_elite.php* /read_elite.php
FILEALIAS /list_elite.php* /list_elite.php
FILEALIAS /l-* /list_art
FILEALIAS /r-* /read_art
FILEALIAS /post_art.php* /post_art.php
FILEALIAS /elite_admin.php* /elite_admin.php
FILEALIAS /view_pic.php* /view_pic.php

这样生成的结果页就会很简炼。

附:

Analog的Floor属性:

HOSTFLOOR, REDIRHOSTFLOOR, FAILHOSTFLOOR, DOMFLOOR, ORGFLOOR, REQFLOOR, DIRFLOOR, TYPEFLOOR, REDIRFLOOR, FAILFLOOR, REFFLOOR, REFSITEFLOOR, SEARCHQUERYFLOOR, SEARCHWORDFLOOR, INTSEARCHQUERYFLOOR, INTSEARCHWORDFLOOR, REDIRREFFLOOR, FAILREFFLOOR, BROWREPFLOOR, BROWSUMFLOOR, OSFLOOR, VHOSTFLOOR, REDIRVHOSTFLOOR, FAILVHOSTFLOOR, USERFLOOR, REDIRUSERFLOOR, FAILUSERFLOOR and STATUSFLOOR

字符含义:

R
Number of requests
r
Percentage of the requests
S
Number of requests in the last 7 days
s
Percentage of the requests in the last 7 days
P
Number of page requests
p
Percentage of the page requests
Q
Number of page requests in the last 7 days
q
Percentage of the page requests in the last 7 days
B
Number of bytes transferred
b
Percentage of the bytes
C
Number of bytes transferred in the last 7 days
c
Percentage of the bytes in the last 7 days
d
Date of last access
D
Date and time of last access
e
Date of first access
E
Date and time of first access
N
The number of the item in the list

其它开关可以参考: #man analog

~~呵呵~~



欢迎转贴,请注明来处。【本帖地址】: http://www.jimmydong.com/blog/post/1/108
评论 (1) |  阅读 (?)  |  固定链接 |  发表于 17:44  | 最后修改于 2006-12-28 16:18
正在读取评论信息...
您还未登录,只能匿名发表评论。或者您可以 登录 后发表。
*
 
  *中国人爱国心,搜狗输入法爱国主题皮肤下载>>
表  情:
加载中...
回复通知: 同时用小纸条通知对方该回复