使用Awstats定时分析nginx日志

使用51.la,CNZZ之类的统计服务往往只能统计到网页被展示出来,该段JS被解析的时候,浏览者的记录,而对于很多浏览行为,例如蜘蛛行为,迅雷吸血行为,访问出现404等其他错误,这些第三方统计网站往往就心有余而力不足了,这个时候,就轮到网站原始的访问日志大显身手的时候了。 但是很多人都很讨厌网站的访问日志,有些人甚至干脆不记录网站的访问日志,原因就在于网站的原始访问日志往往羞涩难懂,机械的记录了每一个连接的请求,靠人肉是很难从访问日志中看出个究竟的,这时候就需要日志分析工具的出场了,这里我推荐Awstats,之前也试用了Weblizer,感觉太简陋,还是Awstats看着比较清爽,一目了然。

首先,你需要对Nginx的日志进行分割,当然不分割也可以,但是分析的时间会越来越长,对性能而言往往是不利的。如果你不知道如何分割Nginx日志,可以先看我之前的文章《Nginx日志分割脚本》

日志分割完成以后,可以去Awstats网站http://awstats.sourceforge.net/下载最新版的Awstats(目前的版本是7.0),下载之后解压并放到/wp-content/local/awstats目录下(如果不放到这个目录下面,执行命令的时候会提示你不是放在最合适的目录,可能会产生一些问题),执行如下命令:(备注:假设你的nginx日志分割以后是放在/home/wwwlogs/年/月/access_20XXXXXX.log的形式)

perl /wp-content/local/awstats/tools/awstats_configure.pl

这个命令用于初始化awstats的设置并配置一个conf文件,接下来会有如下步骤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-----> Check for web server install Enter full config file path of your Web server. 
Example: /etc/httpd/httpd.conf
Example: /wp-content/local/apache2/conf/httpd.conf
Example: c:\Program files\apache group\apache\conf\httpd.conf
Config file path ('none' to skip web server setup): #> none
输入none,因为我们要为nginx创建一个新的配置文件
Your web server config file(s) could not be found. You will need to setup your web server manually to declare AWStats script as a CGI, if you want to build reports dynamically. See AWStats setup documentation (file docs/index.html)
-----> Update model config file '/wp-content/local/awstats/wwwroot/cgi-bin/awstats.model.conf' File awstats.model.conf updated.
-----> Need to create a new config file ? Do you want me to build a new AWStats config/profile file (required if first install) [y/N] ? #> y
输入y,创建一个新的配置文件
-----> Define config file name 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:
#> jayshao.com
然后一路回车
-----> Define config file path In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directory path to store config file(s) (Enter for default):
#>
----> Add update process inside a scheduler
Sorry, configure.pl does not support automatic add to cron yet. You can do it manually by adding the following command to your cron: /wp-content/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=jayshao.com Or if you have several config files and prefer having only one command: /wp-content/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue...
A SIMPLE config file has been created: /etc/awstats/awstats.jayshao.com.conf You should have a look inside to check and change manually main parameters. You can then manually update your statistics for 'jayshao.com' with command: > perl awstats.pl -update -config=jayshao.com You can also build static report pages for 'jayshao.com' with command: > perl awstats.pl -output=pagetype -config=jayshao.com Press ENTER to finish...

到这里,你的awstats的配置文件就创建了,然后我们需要修改一个设置,

1
2
vi /etc/awstats/awstats.jayshao.com.conf
LogFile="/home/wwwlogs/%YYYY-24/%MM-24/access_%YYYY-24%MM-24%DD-24.log"

这里设置了以后,Awstat就能够自动找到你每天分割好的日志了。然后运行如下命令:

1
2
/wp-content/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=jayshao.com 
/wp-content/local/awstats/tools/awstats_buildstaticpages.pl -update -config=jayshao.com -lang=cn -dir=/home/bops.jayshao.com/analytic/ -awstatsprog=/wp-content/local/awstats/wwwroot/cgi-bin/awstats.pl

第一行分析了当前的日志,并保存至Awstats的数据库中,第二行命令生成了静态的html页面到指定的目录里面,这样就能直接从浏览器中打开访问了。需要做到自动化的话,只要把上述命令加到corntab里面去,每天0点运行分割日志,0点01分运行日志分析,相当给力!