http://tech.ddvip.com/2013-03/1364212237192554.html

http://www.linuxde.net/2013/06/14027.html

http://myhat.blog.51cto.com/391263/982733

http://www.linuxidc.com/Linux/2013-06/85984.htm

http://blog.csdn.net/firefoxbug/article/details/8090193   AWStats扩展GeoIP

http://os.51cto.com/art/201311/415087_all.htm    日志分析工具Awstats实战之Apache篇:多站点日志分析

http://os.51cto.com/art/201310/414882_all.htm    日志分析工具Awstats实战之Nginx篇:分析结果动态化

 

Awstats简介

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

  • 访问次数、独特访客人数,

  • 访问时间和上次访问,

  • 使用者认证、最近认证的访问,

  • 每周的高峰时间(页数,点击率,每小时和一周的千字节),

  • 域名/国家的主机访客(页数,点击率,字节,269域名/国家检测, geoip 检测),

  • 主机名单,最近访问和未解析的 IP 地址名单

  • 大多数看过的进出页面,

  • 档案类型,

  • 网站压缩统计表(mod_gzip 或者 mod_deflate),

  • 使用的操作系统 (每个操作系统的页数,点击率 ,字节, 35 OS detected),

  • 使用的浏览器,

  • 机器人访问(检测 319 个机器人),

  • 蠕虫*** (5 个蠕虫家族),

  • 搜索引擎,利用关键词检索找到你的地址,

  • HTTP 协议错误(最近查阅没有找到的页面),

  • 其他基于 URL 的个性报导,链接参数, 涉及综合行销领域目的.

  • 贵网站被加入"最喜爱的书签".次数.

  • 屏幕大小(需要在索引页补充一些 HTML 标签).

  • 浏览器的支持比例: JAVA, Flash, RealG2 reader, Quicktime reader, WMA reader, PDF reader.

  • 负载平衡服务器比率集群报告.

Awstats 是一款基于 Perl 的 WEB 日志分析工具,从Awstats的文档来看,他对 Apache HTTP Server 的支持是非常完美的。同时 JAWStats 是一款基于 PHP 的统计套件,旨在为 Awstats 提供更为精美图形界面。

环境信息约定:

  • 日志文件位置:/htdoc/logs

  • 系统:CentOS7 X64

  • 软件存储位置:/usr/local/src

  • awstats安装位置:/usr/local/awstats

  • JAWStats安装位置:/htdoc/stats/

  • 这里我们配置的站点域名:slog.fity.cn

  • WEB服务器:Nginx

  • 静态页面文件输出路径:/usr/local/apache/data(可选)

软件下载(下载以下软件有可能下载到一半下载不到,可以×××后再下载就很顺畅)

awstats

awstats项目地址:http://sourceforge.net/projects/awstats/

wget http://softlayer-sng.dl.sourceforge.net/project/awstats/AWStats/7.3/awstats-7.3.zip              

wget http://static.jawstats.com/src/jawstats-0.7beta.tar.gz                      #jawstats

wget http://www.jawstats.com/src/languagepack.zip                               #jawstats languagepack

wget http://www.maxmind.com/download/geoip/api/c/GeoIP-1.4.8.tar.gz

wget http://geolite.maxmind.com/download/geoip/api/perl/Geo-IP-1.40.tar.gz 

wget http://search.cpan.org/CPAN/authors/id/G/GM/GMPASSOS/Geo-IPfree-0.2.tar.gz

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz



一、安装和配置 Awstats

Awstats 安装非常简单,但必须先确认你服务器上 Perl 的环境已经就绪。

1、下载和安装 Awstats


目前最新版本 7.3,这里我们下载 7.3

[root@iredmail ~]# cd /usr/local/src[root@iredmail src]# wget http://softlayer-sng.dl.sourceforge.net/project/awstats/AWStats/7.3/awstats-7.3.zip[root@iredmail src]# unzikp awstats-7.3.zip 
[root@iredmail src]# mv awstats-7.1.1 /usr/local/awstats

wget下载的包中权限可能是一个不存在的UID用户,赋予权限让.pl的文件可以运行:

[root@iredmail opt]# chown -R root:root /usr/local/awstats
[root@iredmail opt]# chmod +x /usr/local/awstats/tools/*.pl
[root@iredmail opt]# chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl

2、生成配置文件

[root@iredmail opt]# cd /usr/local/awstats/tools
[root@iredmail tools]# ./awstats_configure.pl
  1. 输入 none 然后回车

  2. 输入 y 确认创建配置文件

  3. 输入配置文件名称,一般输入域名,这里是slog.fity.cn

  4. 配置文件使用默认路径 /etc/awstats

  5. 按回车继续

  6. 按回车完成配置文件的创建

以下为安装时抓取的过程内容,请注意查看注释:

[root@iredmail tools]# ./awstats_configure.pl

----- AWStats awstats_configure 1.0 (build 1.9) (c) Laurent Destailleur -----
This tool will help you to configure AWStats to analyze statistics for
one web server. You can try to use it to let it do all that is possible
in AWStats setup, however following the step by step manual setup
documentation (docs/index.html) is often a better idea. Above all if:
- You are not an administrator user,
- You want to analyze downloaded log files without web server,
- You want to analyze mail or ftp log files instead of web log files,
- You need to analyze load balanced servers log files,
- You want to 'understand' all possible ways to use AWStats...
Read the AWStats documentation (docs/index.html).

-----> Running OS detected: Linux, BSD or Unix

-----> Check for web server install

Enter full config file path 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
Config file path ('none' to skip web server setup):
> none  #这里填none并回车,因为我们没有使用apache,我们这里用的是 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 '/usr/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:
> slog.fity.cn   #统计网站的域名,例如slog.fity.cn

-----> 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):
>     #直接回车,使用默认路径/etc/awstats

-----> Create config file '/etc/awstats/awstats.slog.fity.cn.conf'
Config file /etc/awstats/awstats.slog.fity.cn.conf created.

-----> 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:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=slog.fity.cn  #回头把该命令填入crontab 按指定时间执行
Or if you have several config files and prefer having only one command:
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue...    #按回车继续

A SIMPLE config file has been created: /etc/awstats/awstats.slog.fity.cn.conf  #新配置文件所在的路径
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'slog.fity.cn' with command:
> perl awstats.pl -update -config=slog.fity.cn
You can also build static report pages for 'slog.fity.cn' with command:
> perl awstats.pl -output=pagetype -config=slog.fity.cn

Press ENTER to finish...   #回车完成配置文件的创建

注意:这里要在Awstats安装目录运行。否则会有一些关于标准目录的提示。Awstats缺省标准安装目录是/usr/local/awstats,如果要安装到其它目录需要对awstats_configure.pl进行修改。

3、修改awstats配置文件

Awstats 支持基于日期变化的动态日志文件,具体可以看注释。

[root@iredmail tools]# vi /etc/awstats/awstats.slog.fity.cn.conf

(1)指定日志文件路径:

LogFile="/usr/local/awstats/logs/slog.xkhouse.com/access.log"(51行左右) //指定log文件的存储位置

如果你希望每天能定时生成昨天的报表,可以:

LogFile="/usr/local/awstats/logs/slog.xkhouse.com/access_%YYYY-24%MM-24%DD-24.log"

注: 根据你的日志路径和对应的日志文件名进行修改。对应Nginx日志切割程序所生成的目录存放结构保存的nginx日志文件。要注意Awstats的年月日 格式的跟Nginx的写法有所不同,其中%YYYY-24/%MM-24/%DD-24表示年月日都减去24小时,也就是昨天的日志目录。

(2)修改Nginx日志格式

这里Awstats执行统计的顺序是:

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

修改日志格式,由于这里nginx生成日志格式要与awstats所需要的匹配,所以要修改日志格式,以便awstats读取。注释掉LogFormat=1,添加如下,每一个变量的参数,配置文件上方有解释,这里不多说

#LogFormat=1
LogFormat="%host %other %other %time1 %other %methodurl %code %bytesd %refererquot %uaquot"

修改Nginx日志格式:

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

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

使用日志格式

access_log  /home/www/logs/access.log  main;

补充说明:把log_format这段代码放在你nginx的http的定义段中,可以在下面的每一个server中引用此格式。不必在每个server里面都去定义格式。

(3)修改SiteDomain=slog.fity.cn网站域名

(4)修改LoadPlugin="decodeutfkeys",去掉注视,否则会出现中文乱码

(5)修改Lang="cn"语言为中文

(6) 添加LIMITFLUSH=5000000尽量大,可根据情况而定。增大AWStats的$LIMITFLUSH,减少磁盘临时文件读写 Flush history file on disk (unique url reach flush limit of 5000)

4、生成 Awstats 统计数据

创建存放数据的目录

[root@iredmail tools]# cd ..
[root@iredmail awstats]# mkdir -p /var/lib/awstats

该 路径为配置文件中定义的默认路径,这个目录是可以修改的,位于配置文件的203行:DirData="/var/lib/awstats",这里为了集中 管理,我把统计数据的目录放在了安装目录下:DirData="/usr/local/awstats/var/lib/awstats"

然后便可以执行 Awstats 日志更新程序开始统计分析,有两种更新方式:

方法一:为指定的站点生成数据。

[root@iredmail awstats]# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=slog.fity.cn

#方法二:为所有网站生成数据

[root@iredmail awstats]# /usr/local/awstats/tools/awstats_updateall.pl now

看到类似如下信息说明Awstats统计数据被成功生成了:

[root@iredmail awstats]# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=slog.fity.cn
Create/Update database for config "/etc/awstats/awstats.slog.fity.cn.conf" by AWStats version 7.1.1 (build 1.989)
From data in log file "/usr/local/awstats/tools/logresolvemerge.pl /htdoc/logs/blog*.log|"...
Phase 1 : First bypass old records, searching new record...
Direct access to last remembered record has fallen on another record.
So searching new records from beginning of log file...
Phase 2 : Now process new records (Flush history on disk after 200000 hosts)...
Jumped lines in file: 0
Parsed lines in file: 27924147
Found 150 dropped records,
Found 0 comments,
Found 0 blank records,
Found 13545065 corrupted records,
Found 14289184 old records,
Found 89750 new qualified records.

5. 输出统计结果静态页面 (可选)

若在 Apache 上,那就可以直接打开网页查看统计了,当然 Nginx 也可以省略此步,因为 JAWStats 还尚未出场呢。。。

[root@iredmail awstats]# mkdir -p /var/www/awstats/slog.fity.cn # 创建存放结果的目录
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update  -config=slog.fity.cn -lang=cn -dir=/var/www/awstats/slog.fity.cn -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

说明:    

/usr/local/awstats/tools/awstats_buildstaticpages.pl  Awstats静态页面生成工具
-update -config=slog.fity.cn  更新配置项
-lang=cn  语言为中文
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl  Awstats日志更新程序路径

6. 配置 Awstats 自动运行

[root@iredmail awstats]# 在 /etc/cronta 末行添加以下这行
0 * * * *    root    /usr/local/awstats/tools/awstats_updateall.pl now

你也建立一个 script.sh,内容为:

/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=域名1 > /dev/null
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=域名2 > /dev/null

二、安装和配置 JAWStatsp

JAWStats 是基于 Awstats 统计数据的,但其界面更为强悍,有了JAWStats 你甚至可以抛弃第三方统计了。

1、下载与安装

[root@iredmail awstats]# cd /opt
[root@iredmail opt]# wget http://static.jawstats.com/src/jawstats-0.7beta.tar.gz
[root@iredmail opt]# mkdir /htdoc/stats/
[root@iredmail opt]# tar -zxvf jawstats-0.7beta.tar.gz -C /htdoc/stats/
[root@iredmail opt]# cd /htdoc/stats/
[root@iredmail stats]# cp config.dist.php config.php

2、修改配置文件

官方配置说明见:http://www.jawstats.com/documentation

jawstats 是支持多站点的,每个 $aConfig 对应一个站点,并可以自由切换

<?php 
    // core config parameters 
    $sDefaultLanguage      = "zh-cn"; // 设置默认语言 
    $sConfigDefaultView    = "thismonth.all"; 
    $bConfigChangeSites    = false; //是否可以更换站点 
    $bConfigUpdateSites    = false; //是否可以更新统计数据 
    $sUpdateSiteFilename   = "xml_update.php"; 
      
   // individual site configuration 
   $aConfig["site1"] = array( // site1 网站域名(注意不能乱填的) 
    "statspath"   => "/var/lib/awstats/", //awstats 统计数据目录,根据自己的实际路径填写 
    "updatepath"  => "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl/",  //用于更新统计数据 
    "siteurl"     => "http://slog.fity.cn",  //点击首页标题后会前往这个地址 
    "sitename"    => "slog.fity.cn",    //首页标题 
    "theme"       => "default", 
    "fadespeed"   => 250, 
    "password"    => "my-1st-password",  //更新统计数据需要输入这个密码 
    "includes"    => "", 
    "language"    => "zh-cn" 
  ); 
?>

3、安装中文语言包

首先下载 jawstats 多国语言包,解压缩后上传替换 lang 目录。

http://www.jawstats.com/src/languagepack.zip

需要注意的是,你要修改 zh-cn.js 文件的编码为UTF-8,不然全乱码了。 另外修改汉化文件,也是translations.php,不能只修改zh-cn.js文件。

4、设置JAWStats访问密码 

Nginx 支持为目录添加密码认证,若不想所有人都能访问到你的统计结果,那继续下面步骤吧。。。

(1)使用apache 的 htpasswd 来创建密码文件

语法:htpasswd -b -c /home/test/.htpasswds 用户名 密码

     server {
  listen       80;
  server_name  slog.fity.cn;
  root html;
  index index.html;
  location ~ /stats {
           root /htdoc;
           location ~ \.php$ {
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
                include  fastcgi_params;
            }
            auth_basic "JAWStats";
            auth_basic_user_file /home/test/.htpasswds;
   }
}

最后大功告成,输入 https://slog.fity.cn/stats/ 便可以访问了。

  1. 解决报表中没有国家代码的问题(jawstats里没有区域名称) 

  2.  

  3. # wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz 

  4. # wget  http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz 

  5. # wget  http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz 

  6.  

  7. # tar xzf GeoIP.tar.gz  

  8. # cd GeoIP-1.4.8/ 

  9. # ./configure && make && make install  

  10.  

  11. # gunzip GeoLiteCity.dat.gz  

  12. # mv GeoLiteCity.dat /usr/local/awstats/wwwroot/cgi-bin/ 

  13.  

  14. # gunzip GeoIP.dat.gz  

  15. # mv GeoIP.dat /usr/local/awstats/wwwroot/cgi-bin/ 

  16.  

  17. # vi /usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf  

  18. LoadPlugin="decodeutfkeys" 

  19. LoadPlugin="geoip_isp_maxmind GEOIP_STANDARD /usr/local/awstats/wwwroot/cgi-bin/GeoIPISP.dat" 

  20. LoadPlugin="geoip GEOIP_STANDARD /usr/local/awstats/wwwroot/cgi-bin/GeoIP.dat" 

  21. LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/local/awstats/wwwroot/cgi-bin/GeoLiteCity.dat" 



AWStats扩展GeoIP

  1. wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz  

  2. wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz  ==>> 没能生效  

  3. wget http://geolite.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz  

  4.   

  5. mkdir /var/GeoIP  

  6. mv GeoIP.dat.gz /var/GeoIP/  

  7. gunzip /var/GeoIP/GeoIP.dat.gz  

  8. mv GeoLiteCity.dat.gz /var/GeoIP/  

  9. gunzip /var/GeoIP/GeoLiteCity.dat.gz  

  10. mv GeoIPASNum.dat.gz /var/GeoIP/  

  11. gunzip /var/GeoIP/GeoIPASNum.dat.gz  

  12.   

  13. yum install libtool  

  14. #先下载安装C库  

  15. wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.8.tar.gz  

  16. tar -zxf GeoIP-1.4.8.tar.gz  

  17. cd GeoIP-1.4.8/  

  18. ./configure  

  19. make && make install  

  20.   

  21. #下载Perl库  

  22. wget http://geolite.maxmind.com/download/geoip/api/perl/Geo-IP-1.40.tar.gz  

  23. tar -zxf Geo-IP-1.40.tar.gz  

  24. cd Geo-IP-1.40/  

  25. perl Makefile.PL  

  26. make && make install  

  27.   

  28. perl -MCPAN -e 'install "Geo::IP"'   

  29. perl -MCPAN -e 'install "Geo::IPfree"'   

  30.   

  31. vim /etc/awstats/awstats.common.conf  

  32.   

  33. #1422 行左右添加:  

  34. LoadPlugin="geoip GEOIP_STANDARD /var/GeoIP/GeoIP.dat"  

  35.    

  36. #1432 行左右添加:  

  37. LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /var/GeoIP/GeoLiteCity.dat"  

  38.    

  39. #1469 行左右添加:  

  40. LoadPlugin="geoip_org_maxmind GEOIP_STANDARD /var/GeoIP/GeoIPASNum.dat"  

  41.   

  42. rm -rf /var/lib/awstats/*  #删除旧的统计数据  

  43. /usr/local/awstats/tools/awstats_updateall.pl now  

  44. /usr/local/awstats/tools/awstats_buildstaticpages.pl -update  -config=website -lang=cn -dir=/path_of_your_website/awstats/website  


不过对于 city 没有生效!

问题0:Forum: 0.7Beta did not show the Browser an OS page

解决:You must be logged in to post in this thread.

问题1:Error: Failed to open '/root/wwwroot/cgi-bin/awstats.model.conf' for read. 

解决:进入awstats程序目录,本例为:/usr/local/awstats ,然后在运行

perl /usr/local/awstats/tools/awstats_configure.pl

 

问题2:Error: AWStats database directory defined in config file by 'DirData' 

parameter (/var/lib/awstats) does not exist or is not writable.

Setup ('/etc/awstats/awstats.www.sonkwo.com.conf' file, web server or permissions) may be wrong.

Check config file, permissions and AWStats documentation (in 'docs' directory).

解决:确保存在/var/lib/awstats 目录,如果存在改变权限 chmod 777 /var/lib/awstats -R

 

问题3:如何修改首页LOGO 和 链接地址

解决:在 /etc/awstas/awstats.yourdomain.conf 中找到如下两个选项

Logo="awstats_logo6.png"  #logo图片要放在awstats/wwwroot/icon/other/下 

LogoLink="http://www.sonkwo.com"

 

问题4:如何修改统计页面的中文字体

解决:要根据你在/etc/awstas/awstats.yourdomain.conf中设置的语言,如果你选择的是

Lang="cn"  则修改/usr/local/awstats/wwwroot/cgi-bin/lang/awstats-cn.txt文件即可

 

问题5:统计中,“用以搜索的关键字” “用以搜索的词语”字体乱码,如何解决?

分析:因为现在的搜索引擎都是使用UTF8,而Awstats是使用decodeUTFkeys这个plugin来处理搜索引擎的UTF8关键字,

默认是没有打开的,所以在显示上会出现乱码

解决:启用 DecodeUTFKeys,在 /etc/awstas/awstats.yourdomain.conf 把 LoadPlugin="decodeutfkeys"

的注释去掉,根据插件的描述(如下)还要安装Encode and URI::Escape 的perl模块,使用cpan安装

cpan> install Encode ; cpan> install  URI::Escape

# PLUGIN: DecodeUTFKeys

# REQUIRED MODULES: Encode and URI::Escape

# PARAMETERS: None

# DESCRIPTION: Allow AWStats to show correctly (in language charset) 

# keywords/keyphrases strings even if they were UTF8 coded by the 

# referer search engine.

#


问题6:jawstats全部空白页问题

解决:

近 期经朋友介绍使用jawstats来进行分析网站数据,但是每当分析第三天的数据时就会出现空白页的状况,开始以为是数据出现问题,重新安装分析后发现依 然存在,遂打开了php.ini 和php-fpm.conf的错误日志提示,但页面没有任何显示,后来在网上查阅资料和朋友提示后,发现jawstats根目录下面的index.php 里面还有个设置:

error_reporting(0) 改成 error_reporting(E_ALL)

进行上述修改后,再次打开jawstats显示空白页的站,发现提示如下:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 96779834 bytes) in /data/web/stats/clsAWStats.php on line 54

可以看出是为php分配的内存不够产生的问题,更改php.init后如下配置:

memory_limit = 128M    改成 memory_limit = 256M        或更大

更改后重新加载php-cgi,再次访问 发现问题已经解决


问题7:一直加载,无数据,加载不到awstats数据

43a7d933c895d143c6bdac2a70f082025baf07eb

解决:造成这个问题的原因是在 config.php配置文件后面的  ?>  还有空行,如图

960a304e251f95cae616d9bfca177f3e660952d3

即,在 config.php 文件末尾 只能以 ?> 结尾,不能有其他空行

d6ca7bcb0a46f21f7c1e145af5246b600c33ae95


问题7:All Months Blank全部月空白无数据

修改 xml_history.php 文件的第 47 行 为

$g_dtStatsMonth = ValidateDate((isset($_GET["year"])) ? $_GET["year"] : NULL, (isset($_GET["month"])) ? $_GET["month"] : NULL);