十五年测试老手,长期负责WEB\APP 项目测试,目前主要负责团队管理工作。

使用Awstats+JAWStats自动分析Nginx日志

上一篇 / 下一篇  2011-06-12 15:09:44 / 个人分类:nginx

http://www.mike.org.cn/articles/use-awstats-jawstats-analyse-nginx-log/

e(n v8Qe!Z3Q0

  Awstats是在SourceForge上发展很快的一个基于 Perl 的 WEB 日志分析工具,一个充分的日志分析让Awstats显示您下列资料:

3{1yC8rkPP5X0

  访问次数、独特访客人数
Gh`2w3R%K0  访问时间和上次访问51Testing软件测试网1H+v'Oqh/d u1iTO;G&Q
  使用者认证、最近认证的访问51Testing软件测试网+HB!a3WT#@
  每周的高峰时间(页数,点击率,每小时和一周的千字节)51Testing软件测试网2h,S5J ig]@/bV/m
  域名/国家的主机访客(页数,点击率,字节,269域名/国家检测, geoip 检测)
W#k#g Fn$R)Gg0  主机名单,最近访问和未解析的 IP 地址名单51Testing软件测试网(~+M2gt iG
  大多数看过的进出页面
!bhS4~*zq7s:]ZO0  档案类型
yY!Ze Rg0  网站压缩统计表(mod_gzip 或者 mod_deflate)
7EMrd8\"@0^0  使用的操作系统 (每个操作系统的页数,点击率 ,字节, 35 OS detected)
tkE!e"a~"jp0  使用的浏览器
o}.ow?9|;O.s I!P0  机器人访问(检测 319 个机器人)
4Onk wFKM0  蠕虫攻击 (5 个蠕虫家族)
IS8P,{Zqx0  搜索引擎,利用关键词检索找到你的地址
)x;_&jqZr8S0  HTTP 协议错误(最近查阅没有找到的页面)51Testing软件测试网~"w+HY-MC8y
  其他基于 URL 的个性报导,链接参数, 涉及综合行销领域目的51Testing软件测试网'Ax%{.[^~9{"J
  贵网站被加入”最喜爱的书签”.次数51Testing软件测试网6O*hwD'[ ] Uz
  屏幕大小(需要在索引页补充一些 HTML 标签)51Testing软件测试网{?6C_X
  浏览器的支持比例: Java, Flash, RealG2 reader, Quicktime reader, WMA reader, PDF reader
xT#\@&Or2_s$O0  负载平衡服务器比率集群报告.

N"Q+O e?j+mAjK0

  Awstats的运行是需要PERL环境的支持,从Awstats的文档来看,它对Apache HTTP Server的支持是非常完美的,而当我们把Web服务器换成Nginx后,要运行Awstats变得很麻烦。首先Nginx本身对Perl的支持是比较弱的,另外在日志格式上有需要修改后才能运行。

.c,II"Rr*g0

  注:如果你安装Nginx时启用了Fastcgi支持,可以通过Fastcgi来执行Perl。稍后我会再写一篇如何通过Perl方式来使用Awstats。

d$rw$?%h9p \oq0

  本文主要介绍通过让Awstats对日志统计的结果生成静态页面,然后通过Nginx输出以达到统计Nginx访问日志的效果。

2{%f_$C8A t9G0

  一、安装和配置Awstats51Testing软件测试网v)Bcakz3Q1uD

  安装前提51Testing软件测试网`4E$n:G:i p J

  1、你已经有一个可正常运行并能自动切割日志的Nginx环境。如果你还没有,可先参考我的另一篇文章:CentOS 5.6(X64)下编译安装LNMP平台(Nginx1.0+PHP5.3.6+Mysql5.5.11)51Testing软件测试网]3u#e9q O#@
  2、必须确认你的服务器上Perl的环境已经就绪。查看当前环境Perl版本的命令是perl –version51Testing软件测试网+T$_a"U:UY,w

1
2
3
4
5
perl--version 
This isperl, v5.8.8 builtforx86_64-linux-thread-multi
 
Copyright1987-2006, Larry Wall

  下载Awstats并安装51Testing软件测试网[\8G.aMdiK(V

1
2
3
wgethttp://prdownloads.sourceforge.net/awstats/awstats-7.0.tar.gztarxvf awstats-7.0.tar.gzmvawstats-7.0/usr/local/awstats

  修改权限51Testing软件测试网b)wT{ Z9i(c gl&}T/Om

  wget下载的包中权限可能是一个不存在的UID用户,赋予权限让.pl的文件可以运行。51Testing软件测试网P}yT"s-uL3X1b ?M

1
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进行配置51Testing软件测试网vDm/AnT:@

1
2
cd/usr/local/awstats/tools
./awstats_configure.pl

  如果没有赋予执行权限,可以这样执行:51Testing软件测试网#e E&_^ Q g+R IP

1
2
cd/usr/local/awstats/toolsperlawstats_configure.pl

  注意:这里要在Awstats安装目录运行。否则会有一些关于标准目录的提示。Awstats缺省标准安装目录是/usr/local/awstats,如果要安装到其它目录需要对awstats_configure.pl进行修改。以后有时间我会写一个关于安装到其它目录的文档。51Testing软件测试网jg&XY%J(j+h

  将会有如下一些提示:51Testing软件测试网 ^1u9v*KZL

1
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配置文件51Testing软件测试网J*XkLao`4_

1
vi/etc/awstats/awstats.www.mike.me.conf

  找到统计的日志文件的路径

9|%k1oVp/q%pN(l+o0
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小时,也就是昨天的日志目录。

-O`m@MLP0

  这里Awstats执行统计的顺序是:51Testing软件测试网3e$UcWY3V)M

  Nginx产生日志–>日志切割–>Nginx继续产生日志–>另存切割日志–>交由Awstats统计–>生成结果

t la.E`Z!O0

  修改Nginx日志格式

Hsb2U C5f6L0

  nginx的日志应该遵循以下格式,才可以被awstats识别。这里的main是指格式代称,如果有多个虚拟主机,代称不能一样。

:O&[Mp#s0
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软件测试网 g"Y3z{ a M k6~


相关阅读:

TAG: AWStats Awstats JAWStats Nginx日志

 

评分:0

我来说两句

Open Toolbar