awstats分析nginx日志

张映 发表于 2012-05-18

分类目录: linux

标签:, ,

看了awstats介绍后,感觉是个好东西,等装好来用的时候,不像那么一回事。awstats说白了就是对nginx,apache产生的日志进行分析。awstats分析出来的数据不准,日志是按照一定的规则来生成的,把访问数据存入到文件中,但是数据存入的时候可能是不全的,awstats分析的时候就有误差。

一,安装awstats

yum install awstats

二,配置awstats

1,nginx的日志格式

  1. log_format  access_www  '$remote_addr - $remote_user [$time_local] "$request" '  
  2.  '$status $body_bytes_sent "$http_referer" '  
  3.  '"$http_user_agent" $http_x_forwarded_for';  
  4.  access_log  /var/log/nginx/access.log  access_www;  

awstats分析现有的日志文件,产生的日志文件名,从头到尾要对得上,要不然会报错的。

2,切割日志脚本cut.sh

  1. [root@localhost access] cat /home/zhangy/cut.sh  
  2. #!/bin/bash  
  3. logs_path="/var/log/nginx/" #nginx日志位置  
  4. # 你的日志名, access_www是web服务器对应log名,accesss_img是图片服务器对应的log名  
  5. logs_names=(access_www access_img)  
  6. num=${#logs_names[@]}        #数组总数  
  7. for((i=0;i<num ;i++));do     #循环  
  8.     if [ ! -d ${logs_path}/${logs_names[i]} ]    #判断目录是不是存在  
  9.     then  
  10.         mkdir -p ${logs_path}${logs_names[i]}   #不存在,建之,日志重新建文件夹,切割后文件很多,所以分目录  
  11.     fi  
  12.     log_name="${logs_path}${logs_names[i]}.log"  #原始目录位置  
  13.     if [ -f "$log_name" ]; then  
  14.         mv ${logs_path}${logs_names[i]}.log ${logs_path}${logs_names[i]}/${logs_names[i]}.log-$(date -d "yesterday" +"%Y%m%d")  
  15.     fi  
  16. done  
  17. nginx -s reopen   #重新生成原始日志文件  

3,awstats_configure.pl生成配置文件

  1. [root@localhost awstats]# find / -name "awstats_configure.pl" -print  
  2. /usr/share/awstats/tools/awstats_configure.pl  

yum装的,不知道什么位置查找一下。下面重点说一下,生成过程

  1. [root@localhost access]# /usr/share/awstats/tools/awstats_configure.pl  
  2. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。  
  3. Do you want to continue setup from this NON standard directory [yN] ? y  
  4. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。  
  5. Config file path ('none' to skip web server setup):  
  6. > none   #用的是nginx上面提示都不符合,在这里选择none  
  7. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。  
  8. Do you want me to build a new AWStats config/profile  
  9. file (required if first install) [y/N] ? y   #是否要求新建配置文件  
  10. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。  
  11. Your web site, virtual server or profile name:  
  12. > access_www      #这里填写的东西,根日志的名称一样就行了,域名,非域名都行  
  13. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。  
  14. #下面一路回车就行了。  

这个操作完了以后,会在/etc/awstats下面生成一个配置文件。

  1. [root@localhost awstats]# ls /etc/awstats/ |grep access  
  2. awstats.access_www.conf  

如果你分析多个域名的日志,就把这一步多操作几次

4,修改生成的配置文件

修改二个部LogFile和LogFormat

  1. LogFile="/var/log/nginx/access/access.log-%YYYY-24%MM-24%DD-24"  
  2. LogFormat="%host %logname %time1 %methodurl %code %bytesd %refererquot %uaquot %other"  

-24表示一天前的,-0表示当前,配置文件里面有说明

5,生成awstats的静态html

  1. [root@localhost awstats]# mkdir -p /var/www/awstats  
  2. [root@localhost awstats]# chown -R nginx:nginx /var/www/awstats #nginx的启动用户  

上面建了一个web目录awstats来存生成的html

  1. [root@localhost ~]# cat /home/zhangy/static.sh  
  2. #!/bin/bash  
  3. # The Nginx logs path  
  4. logs_names=(access_www access_img)     #log的名称  
  5. web_path="/var/www/awstats"            #静态html的路径  
  6. cgi_path="/usr/share/awstats/wwwroot/cgi-bin"  #awstats可执行文件的存放路径  
  7. static_path="/usr/share/awstats/tools"         #awstats可执行文件的存放路径  
  8.   
  9. num=${#logs_names[@]}  
  10. for((i=0;i<num ;i++));do  
  11.     if [ ! -d ${web_path}/${logs_names[i]} ]  
  12.     then  
  13.         mkdir -p ${web_path}/${logs_names[i]}  
  14.     fi  
  15.     ${static_path}/awstats_buildstaticpages.pl -update \  
  16.     -config=${logs_names[i]} -lang=cn -dir=${web_path}/${logs_names[i]} \  
  17.     -awstatsprog=${cgi_path}/awstats.pl  
  18. done  

当执行完static.sh后,会在/var/lib/awstats/下面生成一个文件,里面存放了分析后的数据

  1. [root@localhost awstats]# ls /var/lib/awstats/ |grep access  
  2. awstats052012.access_www.txt  

http://www.XXX.com/awstats/access_www/awstats.access_www.html

可以通过这个页面来访问,前提是nginx的root设置的目录是/var/www才行。

看一下效果图

awstats

awstats

总结:

awstats每次只能分析一个log文件,也就是说我有10日志文件,当我们运行static.sh的时候,只会读取一个。所以每天都要用awstats来分析日志,/var/lib/awstats下面的数据文件会不断的增加,用crontab定时跑一下。

  1. 59 23 * * * /home/zhangy/cut.sh  #夜里12点切日志  
  2. 30 04 * * * /home/zhangy/static.sh #凌晨4点生成静态html  


转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/linux/1411.html

1 条评论

  1. wei 留言

    请教, [root@localhost awstats]# ls /var/lib/awstats/ |grep access
    awstats052012.access_www.txt
    生成的这个文件,怎样用JAWStats显示呢?我的问题在这里http://www.oschina.net/question/87787_116794