awstats+jawstats构建日志自动分析系统,同时 JAWStat


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系统:CentOS6.3 X64软件存储位置:/optawstats安装位置:/usr/local/awstatsJAWStats安装位置:/htdoc/stats/这里我们配置的站点域名:slog.fity.cnWEB服务器:Nginx静态页面文件输出路径:/usr/local/apache/data(可选)

一、安装和配置 Awstats

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

1、下载和安装 Awstats

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

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

[root@iredmail ~]# cd /opt
[root@iredmail opt]# wget http://sourceforge.net/projects/awstats/files/AWStats/7.1.1/awstats-7.1.1.tar.gz
[root@iredmail opt]# tar -zxf awstats-7.1.1.tar.gz
[root@iredmail opt]# 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
输入 none 然后回车输入 y 确认创建配置文件输入配置文件名称,一般输入域名,这里是slog.fity.cn配置文件使用默认路径 /etc/awstats按回车继续按回车完成配置文件的创建

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

[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=/usr/local/awstats/html -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]# crontab -e
0 * * * * /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 /htdoc;
  index index.html;
  location ~ /stats {
           root /htdoc/stats/;
           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/ 便可以访问了。

相关内容