免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1068 | 回复: 0
打印 上一主题 下一主题

实用的Linux命令技巧(日志分析) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-20 09:43 |只看该作者 |倒序浏览
实用的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 模块,缺省的一般是这样:
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
在其中添加:
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
~~呵呵~~
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/70610/showart_1086847.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP