使用Awstats+JAWStats自动分析Nginx日志
上一篇 / 下一篇 2011-06-12 15:09:44 / 个人分类:nginx
http://www.mike.org.cn/articles/use-awstats-jawstats-analyse-nginx-log/51Testing软件测试网:}db$@3K
Awstats是在SourceForge上发展很快的一个基于 Perl 的 WEB 日志分析工具,一个充分的日志分析让Awstats显示您下列资料:51Testing软件测试网`oC4{r
访问次数、独特访客人数
n:O ]*b
L^0 访问时间和上次访问51Testing软件测试网ys8XUU~ _
使用者认证、最近认证的访问
^M7Zjsy)RE0 每周的高峰时间(页数,点击率,每小时和一周的千字节)
z-c*Z@cun)Ee0 域名/国家的主机访客(页数,点击率,字节,269域名/国家检测, geoip 检测)
ar t-C uK&h(_0 主机名单,最近访问和未解析的 IP 地址名单
np4O
g*ly2^0 大多数看过的进出页面51Testing软件测试网 A:_E$z2z G2o
档案类型51Testing软件测试网0sB"M3v5cX~)M
网站压缩统计表(mod_gzip 或者 mod_deflate)
r*W0m}s*U8Uq0 使用的操作系统 (每个操作系统的页数,点击率 ,字节, 35 OS detected)51Testing软件测试网y%~$~:?&a4JN
使用的浏览器51Testing软件测试网*r(G9_v1DcI.D+}
机器人访问(检测 319 个机器人)
b
S:p B8o0|1P/E5k0 蠕虫攻击 (5 个蠕虫家族)
-sc:moD?0 搜索引擎,利用关键词检索找到你的地址
z-Q(j$[Pz L0OP0 HTTP 协议错误(最近查阅没有找到的页面)
"M:qD;LMCD0 其他基于 URL 的个性报导,链接参数, 涉及综合行销领域目的
,y,\C6e8?0ZIM|}0 贵网站被加入”最喜爱的书签”.次数51Testing软件测试网P
K_:_a
屏幕大小(需要在索引页补充一些 HTML 标签)51Testing软件测试网'e)wcYp'Z
浏览器的支持比例: Java, Flash, RealG2 reader, Quicktime reader, WMA reader, PDF reader51Testing软件测试网,?V,a5~X_#M.{|
负载平衡服务器比率集群报告.51Testing软件测试网7Oq.l#M.]E
Awstats的运行是需要PERL环境的支持,从Awstats的文档来看,它对Apache HTTP Server的支持是非常完美的,而当我们把Web服务器换成Nginx后,要运行Awstats变得很麻烦。首先Nginx本身对Perl的支持是比较弱的,另外在日志格式上有需要修改后才能运行。
(C5J+HU djAa?0注:如果你安装Nginx时启用了Fastcgi支持,可以通过Fastcgi来执行Perl。稍后我会再写一篇如何通过Perl方式来使用Awstats。
|/O-\7d]W`$vwr0本文主要介绍通过让Awstats对日志统计的结果生成静态页面,然后通过Nginx输出以达到统计Nginx访问日志的效果。51Testing软件测试网 `|%@A8n7elZK
一、安装和配置Awstats
s\ jf-N8?8G0安装前提51Testing软件测试网m&[7r]H[
1、你已经有一个可正常运行并能自动切割日志的Nginx环境。如果你还没有,可先参考我的另一篇文章:CentOS 5.6(X64)下编译安装LNMP平台(Nginx1.0+PHP5.3.6+Mysql5.5.11)51Testing软件测试网]"i9\)OA'T
2、必须确认你的服务器上Perl的环境已经就绪。查看当前环境Perl版本的命令是perl –version51Testing软件测试网4~#Z
KU:D+u1j.ns_/C
1 2 3 4 5 | perl--version This isperl, v5.8.8 builtforx86_64-linux-thread-multi Copyright1987-2006, Larry Wall |
下载Awstats并安装51Testing软件测试网)K2X__[ f
1 2 3 | wgethttp://prdownloads.sourceforge.net/awstats/awstats-7.0.tar.gztarxvf awstats-7.0.tar.gzmvawstats-7.0/usr/local/awstats |
修改权限51Testing软件测试网w6~9s6P5mii
wget下载的包中权限可能是一个不存在的UID用户,赋予权限让.pl的文件可以运行。
|(K(ZnQ01 2 3 | chown-Rwww:www/usr/local/awstatschmod+x/usr/local/awstats/tools/*.plchmod+x/usr/local/awstats/wwwroot/cgi-bin/*.pl |
执行awstats_configure.pl进行配置
uV-?U#_;?a01 2 | cd/usr/local/awstats/tools ./awstats_configure.pl |
如果没有赋予执行权限,可以这样执行:
Z H7[G!a:zbt01 2 | cd/usr/local/awstats/toolsperlawstats_configure.pl |
注意:这里要在Awstats安装目录运行。否则会有一些关于标准目录的提示。Awstats缺省标准安装目录是/usr/local/awstats,如果要安装到其它目录需要对awstats_configure.pl进行修改。以后有时间我会写一个关于安装到其它目录的文档。51Testing软件测试网?+K"ZQ1w%Y\(K
将会有如下一些提示:
~?c+QT?x{4|01 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | ----->Running OS detected: Linux, BSD or Unix ----->Checkforweb serverinstall Enter full configfilepath of your Web server. Example:/etc/httpd/httpd.conf Example:/usr/local/apache2/conf/httpd.conf Example: c:\Program files\apache group\apache\conf\httpd.conf Configfilepath('none'to skip web server setup):>none#这里添none并回车,因为我们没有使用apache Your web server configfile(s)could not be found. You will need to setup your web server manually todeclareAWStats script.asa CGI,ifyou want to build reports dynamically. See AWStats setup documentation(filedocs/index.html) ----->Update model configfile'/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf'File awstats.model.conf updated. ----->Need to create a new configfile? Do you want me to build a new AWStats config/profilefile(requirediffirstinstall)[y/N]? y ----->Define configfilename to create What is the name of your web site or profile analysis ? Example: www.mysite.com Example: demo Your web site, virtual server or profile name:>www.mike.me#这里输入你要分析的域名,或是随便一个你易记的配置名并回车 ----->Define configfilepath Inwhichdirectorydoyou plan to store your configfile(s)? Default:/etc/awstats Directory path to store configfile(s)(Enterfordefault):>#直接回车,使用默认路径/etc/awstats ----->Create configfile'/etc/awstats/awstats.www.mike.me.conf'Configfile/etc/awstats/awstats.www.mike.me.conf created. ----->Add update process inside a scheduler Sorry, configure.pl does not support automatic add to cron yet. You candoit manually by adding the followingcommandto your cron:/usr/local/awstats/wwwroot/cgi-bin/awstats.pl-update-config=www.mike.me Orifyou have several config files and prefer having only onecommand:/usr/local/awstats/tools/awstats_updateall.pl now Press ENTER to continue...#按回车继续 A SIMPLE configfilehas been created:/etc/awstats/awstats.www.mike.me.conf You should have a look inside to check and change manually main parameters. You canthenmanually update your statisticsfor'www.mike.me'withcommand:>perlawstats.pl-update-config=www.mike.me You can also build static report pagesfor'www.mike.me'withcommand:>perlawstats.pl-output=pagetype-config=www.mike.me Press ENTER to finish...#回车完成配置文件的创建 |
修改Awstats配置文件
%Y2_ `yHm01 | vi/etc/awstats/awstats.www.mike.me.conf |
找到统计的日志文件的路径51Testing软件测试网6Lx7j/BkUL
1 2 3 4 5 | LogFile="/var/log/httpd/mylog.log" 改为 LogFile="/data1/logs/%YYYY/%MM/access_www_%YYYY-24%MM-24%DD-24.log" |
注:根据你的日志路径和对应的日志文件名进行修改。对应Nginx日志切割程序所生成的目录存放结构保存的nginx日志文件。要注意Awstats的年月日格式的跟Nginx的写法有所不同,其中%YYYY-24/%MM-24/%DD-24表示年月日都减去24小时,也就是昨天的日志目录。
,}%H H'b7r0这里Awstats执行统计的顺序是:
1ZLn4Gr_8} @ F0Nginx产生日志–>日志切割–>Nginx继续产生日志–>另存切割日志–>交由Awstats统计–>生成结果
GAd _po7y E`0修改Nginx日志格式51Testing软件测试网/AO8\F\t9Y'N3|s-V`
nginx的日志应该遵循以下格式,才可以被awstats识别。这里的main是指格式代称,如果有多个虚拟主机,代称不能一样。51Testing软件测试网4o4k1T2w{H
1 2 3 | log_format main'$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"'; |
使用日志格式51Testing软件测试网V6r1]$E*|&h