awstat网页数据分析

前  言


  通过我们构建的HTTP服务器将Web发布后,随着不断被访问,Apache会记下每个访客的基本信息。CentOS的情况下,这些信息通常保存在位于/var/log/httpd/下的相应日志文件中。但Apache单调的日志,除了提供给我们一些最基本的服务器被访信息、以及出错信息之外,我们很难单纯的通过日志文件得到更多、更系统的关于时间、规律性、地点方面的信息。这也使得很难通过单纯的日志去系统的分析流量。

  但通过Awstats,我们可以把Apache的日志作为一种有效资源,获得更加系统化的信息。

  实质上,Awstats就是一套分析系统,它将日志文件统计学式的进行分析与统计,然后提供一个能够反映规律性的界面给我们。Awstats并不是仅能应用于分析Apache的日志,在邮件服务器等等方面,它也被广泛的应用。但在这一节中,只介绍在Web服务器中运用Awstats建立统计系统的方法。

下载并安装 Awstats


   首先下载并安装Awstats

[root@sample ~]# wget http://nchc.dl.sourceforge.net/sourceforge/awstats/awstats-6.5-1.noarch.rpm  下载Awstatsrpm

--15:34:59-- http://nchc.dl.sourceforge.net/sourceforge/awstats/awstats-6.5-1.noarch.rpm
=> `awstats-6.5-1.noarch.rpm'
Resolving nchc.dl.sourceforge.net... 211.79.61.10
Connecting to nchc.dl.sourceforge.net|211.79.61.10|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1,095,131 (1.0M) [text/plain]

100%[====================================>] 1,095,131 155.28K/s ETA 00:00

15:35:06 (158.94 KB/s) - `awstats-6.5-1.noarch.rpm' saved [1095131/1095131]

[root@sample ~]# rpm -ivh awstats-6.5-1.noarch.rpm 安装Awstats

Preparing... ########################################### [100%]
1:awstats ########################################### [100%]

----- AWStats 6.5 - Laurent Destailleur -----
AWStats files have been installed in /usr/local/awstats

If first install, follow instructions in documentation
(/usr/local/awstats/docs/index.html) to setup AWStats in 3 steps:
Step 1 : Install and Setup with awstats_configure.pl (or manually)
Step 2 : Build/Update Statistics with awstats.pl
Step 3 : Read Statistics


  然后删除安装后的遗留源文件。

[root@sample ~]# rm -f awstats-6.5-1.noarch.rpm 删除安装后的遗留源文件

 

配置 Awstats


   接下来对Awstats进行初始配置。

[root@sample ~]# /usr/local/awstats/tools/awstats_configure.pl 运行初始配置脚本

----- AWStats awstats_configure 1.0 (build 1.6) (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
Warning: AWStats standard directory on Linux OS is '/usr/local/awstats'.
If you want to use standard directory, you should first move all content
of AWStats distribution from current directory:
/root
to standard directory:
/usr/local/awstats
And then, run configure.pl from this location.
Do you want to continue setup from this NON standard directory [yN] ?
y  选择y,同意安装到默认目录下

-----> 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):
>
/etc/httpd/conf/httpd.conf  指定Apache配置文件所在位置

-----> Check and complete web server config file '/etc/httpd/conf/httpd.conf'
Add 'Alias /awstatsclasses "/root/wwwroot/classes/"'
Add 'Alias /awstatscss "/root/wwwroot/css/"'
Add 'Alias /awstatsicons "/root/wwwroot/icon/"'
Add 'ScriptAlias /awstats/ "/root/wwwroot/cgi-bin/"'
Add '<Directory>' directive
AWStats directives added to Apache config file.

-----> Update model config file '/etc/awstats/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:
>
www.centospub.com  为统计对象创建代号(推荐以网站URL作为代号)

-----> 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):
>
  直接按回车,接受将Awstats的配置文件置于/etc/awstats

-----> Create config file '/etc/awstats/awstats.www.centospub.com.conf'
Config file /etc/awstats/awstats.www.centospub.com.conf created.

-----> Restart Web server with '/sbin/service httpd restart'  ← HTTP服务重新启动

Stopping httpd:               [ OK ]
Starting httpd:
               [ OK ]

-----> 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:
/root/wwwroot/cgi-bin/awstats.pl -update -config=www.centospub.com
Or if you have several config files and prefer having only one command:
/root/tools/awstats_updateall.pl now
Press ENTER to continue...
 按回车继续进行配置

A SIMPLE config file has been created: /etc/awstats/awstats.www.centospub.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.centospub.com' with command:
> perl awstats.pl -update -config=www.centospub.com
You can also read your statistics for 'www.centospub.com' with URL:
> http://localhost/awstats/awstats.pl?config=www.centospub.com

Press ENTER to finish...  按回车结束初始配置


  然后对刚刚创建的对象配置文件进行配置。

[root@sample ~]# vi /etc/awstats/awstats.www.centospub.com.conf  修改对象配置文件(文件名与上一步初始配置中设置的域名对应)

LogFile="/var/log/httpd/mylog.log" 找到这一行,修改日至文件所在位置(指定Apache的日志文件)
 
LogFile="/var/log/httpd/access_log"   变为此状态

DirData="/var/lib/awstats"  找到这一行,更改Awstats数据的保存位置
 
DirData="."  变为此状态,让数据与awstats.pl脚本在同目录下

Lang="auto"  找到这一行,将auto改为cn
 
Lang="cn"   变为此状态,让语言默认为中文

SkipHosts=""  找到这一行,在""之间添加无效访问规则
 
SkipHosts="127.0.0.1 REGEX[^192/.168/.]"   变为此状态,本地及内部的访问不做分析统计

LevelForWormsDetection=0  找到这一行,将0改为2
 
LevelForWormsDetection=2  变为此状态,将来自Worm的访问也不做分析统计

 


  接下来,再对httpd.conf中面向Awstats的部分进行必要设置。

[root@sample ~]# vi /etc/httpd/conf/httpd.conf  编辑Apache的配置文件

#
# Directives to allow use of AWStats as a CGI
#

----------------------------------------------------
Alias /awstatsclasses "/root/wwwroot/classes/"
Alias /awstatscss "/root/wwwroot/css/"
Alias /awstatsicons "/root/wwwroot/icon/"
ScriptAlias /awstats/ "/root/wwwroot/cgi-bin/"
----------------------------------------------------


找到以上水平线间的语句群,对相应路径进行正确修改。变为下面水平线间状态:

----------------------------------------------------
Alias /awstatsclasses "
/usr/local/awstats/wwwroot/classes/"
Alias /awstatscss "
/usr/local/awstats/wwwroot/css/"
Alias /awstatsicons "
/usr/local/awstats/wwwroot/icon/"
ScriptAlias /awstats/ "
/usr/local/awstats/wwwroot/cgi-bin/"
----------------------------------------------------


#
# This is to permit URL access to scripts/files in AWStats directory.
#

<Directory "/root/wwwroot">  找到这一行,对相应路径进行正确修改
 
<Directory "/usr/local/awstats/wwwroot"> 变为此状态


  然后,重新启动HTTP服务,使新的设置生效。

[root@sample ~]# /etc/rc.d/init.d/httpd restart  重新启动HTTP服务,使设置生效

Stopping httpd:               [ OK ]
Starting httpd:
              [ OK ]

 

对日志进行分析统计


  配置完毕后,我们可以通过各种适用于自己的方式,定义统计的方式以及输出的结果。在这里,介绍最简单,也是最广泛的统计方法,如下:

[root@sample ~]# vi /root/awstats.sh  建立统计用脚本如下:

#!/bin/bash
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=
www.centospub.com
(以上紫色字体部分的域名请根据实际情况设置)

[root@sample ~]# chmod 700 /root/awstats.sh   赋予脚本可被执行的属性

[root@sample ~]# /root/awstats.sh   运行脚本,开始进行统计(如日志量比较大的话,要花一段时间)

Update for config "/etc/awstats/awstats.www.centospub.com.conf"
With data in log file "/var/log/httpd/access_log"...
Phase 1 : First bypass old records, searching new record...
Searching new records from beginning of log file...
Phase 2 : Now process new records (Flush history on disk after 20000 hosts)...
Jumped lines in file: 0
Parsed lines in file: 55
Found 52 dropped records,
Found 0 corrupted records,
Found 0 old records,
Found 3 new qualified records.


  然后在客户端的浏览器上访问 http://服务器IP地址(或你的域名)/awstats/awstats.pl?config=www.centospub.com 即可看到详细的分析统计资料,如下图:

定制统计分析规则及相应外观设置


[1]
完成以上配置,我们已经可以通过客户端的浏览器,掌握Web服务器的访问量、来路等等详细的信息,但由于Awstats的统计是基于Apache日志的, 我们必须通过awstats.pl对当前最新的日志文件进行统计才能得到最新的统计结果。也就是说,在我们运行自行创建的awstats.sh脚本之前, 当前在客户端能够看到的统计分析结果,只是上一次运行awstats.sh脚本时统计的结果。想得到最新的统计结果,必须再次运行awstats.sh 在这里,我们将其设置为每天定时自动运行,以便于每天都能查看到最新的信息。如下:

[root@sample ~]# crontab -e  对定期任务执行时间表进行编辑,添加如下一行:

00 00 * * * /root/awstats.sh  添加此行到其中,让统计分析在每天0点更新

(以上操作方法与vi编辑器相同)

 

[2] 在一些情况,可能并不希望外界看到日志统计分析的结果。对此,我们可以为Apache添加面向于Awstats的访问限制,如下:

[root@sample ~]# vi /etc/httpd/conf.d/awstats.conf  创建面向于Awstats的配置文件

<Files "awstats.pl">
AllowOverride None
Order deny,allow
Deny from all
Allow from 192.168.0
  只允许局域网内访问
</Files>



[root@sample ~]# /etc/rc.d/init.d/httpd restart  重新启动HTTP服务,使设置生效

Stopping httpd:
              [ OK ]
Starting httpd:
              [ OK ]


[3]
Firefox浏览分析结果时,字体会显得很难看(因为FirefoxIE识别11px字体不一样),所以用Firefox浏览器的情况,可以通过修改awstats.pl输出css的相关字体尺寸进行修改,而达到用Firefox浏览时也和IE一样能够正确显示出适当尺寸的字体。方法就是直接编辑"/usr/local/awstats/wwwroot/cgi-bin/awstats.pl",将文件中所有“11px”的地方都替换为“12px

 

 

注意:apacheLOG日志需要设置为combined模式,来输出更为详细的信息例如:

CustomLog logs/sdg.com-access_log combined

 

扩展

安裝 GeoIP C API 

请到 http://geolite.maxmind.com/download/geoip/api/c/ 下载最新的版本。

解压缩后进入该目录(tar zxvf)。

执行 ./configure

执行 make

执行 make install

以上就完成安裝,而且会在 /usr/local/share/GeoIP 产生一個 GeoIP.dat 的文件。

再安裝 GeoIP Perl API

请到 http://geolite.maxmind.com/download/geoip/api/perl/ 下载最新的版本。

解压缩后进入该目录。(tar zxvf

执行 perl Makefile.PL

执行 make

执行 make install

完成安裝。

接下来就是编辑awstats.www.zwnking.com.conf 文件,因为我們已经安装了GeoIP,而 awstats在解析IP需要的就是GeoIP.dat,所以请按下面步骤编辑 awstats.www.zwnking.com.conf

LoadPlugin="tooltips"

LoadPlugin="decodeutfkeys"

LoadPlugin="geoip GEOIP_STANDARD /usr/local/share/GeoIP/GeoIP.dat"

LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/local/share/GeoIP/GeoLiteCity.dat"

2GeoLiteCity.dat 用來解析城市、地区的信息,请到 http://geolite.maxmind.com/download/geoip/database/下载GeoLiteCity.dat.gz,这样 awstats 就可以做IP反解析,报表中就可以显示访客的国家与城市来源。

把这个文件也要放在/usr/local/share/GeoIP下。

awstats.www.zwnking.com.conf中的配置:

LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/local/share/GeoIP /GeoLiteCity.dat"

 

 

按照我的方法 (mt) 中安装 AWStats 完成后,可以再做以下几方面的工作:

1、开启以下几个插件,可显示一些额外提示、把 IP 转换为国家和城市信息。
(
方法:打开 awstats.osxcn.com.conf,找到如下插件,去掉前面的注释,把 GeoIP.dat GeoLiteCity.dat 的路径改为自己实际的)

LoadPlugin=”tooltips”
LoadPlugin=”decodeutfkeys”
LoadPlugin=”geoip GEOIP_STANDARD /var/www/vhosts/osxcn.com/httpdocs/awstats/GeoIP.dat”
LoadPlugin=”geoip_city_maxmind GEOIP_STANDARD /var/www/vhosts/osxcn.com/httpdocs/awstats/GeoLiteCity.dat”

提示:需要先在 (mt) 上安装 Geo::IP::PurePerl 模块才能使用 GeoIP,到这里下载。

2、安装纯真版 IP 数据库来分析 IP

GeoLiteCity.dat 对中国城市的统计不准确,在国内还是珊蝴虫和纯真版 IP 数据库比较好,Albert 做了一个显示来访者位置的插件 qqhostinfo,使用纯真版 IP 数据库。

1) 下载 纯真版 IP 数据库,解压后得到 QQWry.Dat
2)
下载 qqhostinfo.pm
3)
下载 分析 QQWry.dat perl 脚本,得到 qqwry.pl,打开修改一下,把 ./QQWry.Dat 修改为 ${DIR}/plugins/QQWry.Dat
4)
QQWry.Dat, qqhostinfo.pm, qqwry.pl 放在 cgi-bin/plugins下面。
5)
打开 awstats.osxcn.com.conf,在 # PLUGINS 部分加上一句 LoadPlugin=”qqhostinfo”

提示:使用 qqhostinfo 插件需要安装 Net::XWhois 模块,到这里下载。

3、打上几个中文搜索引擎定义的补丁 (可选)

AWStats 6.6 在这方面已经做得很好了,如果有这个需要的可以打上车东提供的补丁。

TOP 8 Chinese local search engines

需要在本地打好补丁后再上传,如在 Ubuntu 上把他们放在同一个目录,然后:
patch search_engines.pm search_engines.pm.patch

4、让图标显示出来

按照上一篇文章安装 AWStats 后,没有把图标文件上传,查看 awstats.osxcn.com.conf 配置文件发现默认图标 DirIcons=”icon” 是这样的。那么只需要把 icon 放在 /httpdocs/awstats/ 目录下就可以了。

icon 文件夹在 AWStats 包里 awstats-6.6/wwwroot/icon 这个位置。

5、如果你的网站访问量达到千万级,可以使用AWStats 的千万级日志解决方案

命令行统计增加: -databasebreak=day
报表输出时增加: &databasebreak=day&day=DD

 

日志分割

 

1、下载(最新版本

#  wget http://cronolog.org/download/cronolog-1.6.2.tar.gz

2、解压缩

# tar zxvf cronolog-1.6.2.tar.gz

3、进入cronolog安装文件所在目录

# cd cronolog-1.6.2

4、运行安装

# ./configure
# make
# make install

5、查看cronolog安装后所在目录(验证安装是否成功)

# which cronolog

一般情况下显示为:/usr/local/sbin/cronolog

整个安装过程结束,下面需要把日志的格式配置一下:

1、虚拟主机配置文件 httpd-vhosts.conf

Web日志设置 CustomLog 修改为以下格式

CustomLog "|/usr/local/sbin/cronolog /www/logs/example_%Y%m%d.log" combined

 

CustomLog "|/usr/local/sbin/cronolog /usr/local/apache/logs/access _%Y%m%d.log" combined

 

当然,错误日志设置 ErrorLog 也可以利用 cronolog 分割,设置为

ErrorLog "|/usr/local/sbin/cronolog /www/logs/error_%Y%m%d.log"

2、如果服务器上只有一个站点(当然这种情况比较少),直接按上面所说格式修改 httpd.conf 文件日志设置部分。

说明:

  • 绿色部分 cronolog 安装后所在位置,系统版本不同可能位置不完全一样,以 which 命令查看到的位置为准;
  • 蓝色部分 为设置的日志文件所在位置,根据需要修改;
  • 红色部分 为设置的日志文件标识性字符,根据需要修改;
  • %Y%m%d 为日志文件分割方式,例子中为“年月日”,也可以修改成自己需要的。

配置apache日志

 

GNU/Linux LogFile="/path/to/apache/logs/access_%YYYY-24%MM-24%DD-24.log"

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值