|
原文地址:http://yahoon.blog.运维网.com/13184/40392
前言,公司要用awstats做web服务器的流量统计,到运维网找了下,发现文章很少,所以写了这篇文章,记录下自己配置的过程,也希望能帮到有需要的朋友.强大的日志分析工具AWStats主页http://awstats.sourceforge.net注:此文基本按照官方文档的操作一步步进行,在安装过程中参考了相关的文档作了必要的改动.环境 :redhat 9Apache/2.0.54 源码安装,路径为/usr/local/apache2主机IP 192.168.0.111域名 s1.domain1.com所用的版本awstats-6.5.tar.gz实现功能:通过awstats统计s1.domain1.com的访问信息,并提供web页面查看AWStats的功能很多,具体的可点击上面的官网,我在此主要用它来分析apache服务器的日志.使用之前还是说说大致的原理:awstats提供一系列的perl脚本实现:服务配置,日志读取,报表生成等功能.而功能实现的具体执行过程是:首先当然是apache将访问情况记录到日志中,AWStats每次执行更新时读取这些日志,分析日志数据,将结果存储到数据库中,(这个数据库是awstats自带的,并不需要第三方软件支持.),最后AWStats提供一个cgi程序通过web页面来显示数据库中所统计的数据.首先看当前apache的配置,vi httpd.conf
ServerAdmin yahoon@xxx.comDocumentRoot /var/www/html/s1ServerName s1.domain1.comErrorLog logs/s1_web-error_logTransferLog logs/s1_web-access_log访问s1.domain1.com的日志记录到/usr/local/apache2/logs/s1_web-access_log安装过程如下将软件包解压缩为/usr/local/awstats第一步是为网站生成配置文件,其实有很多方式,按照官方文档的说明推荐使用脚本,而且这样也是比较简单的一种方式.转到/usr/local/awstats/tools,此目录下有很多不同功能的perl脚本.执行[root@server1 tools]# perl 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 forone web server. You can try to use it to let it do all that is possiblein AWStats setup, however following the step by step manual setupdocumentation (docs/index.html) is often a better>/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=s1.domain1.comOr if you have several config files and prefer having only one command:/usr/local/awstats/tools/awstats_updateall.pl nowPress ENTER to continue...由于要得到新信息,就必须更新数据库,也就是说重新做读取日志.分析日志提取里面新增的部分进行分析,将更新的数据存入数据库的过程.最好就让它定时执行.这段话是提醒你,要实现这个功能就需要手动把上面粗体的两行中的任一行写入crontab,让它定时执行.其中第一句是更新s1.domain1.com,而第二句是在有多个站点情况下,将所有的站点都作更新.我在此不做这个操作.回车.A SIMPLE config file has been created: /etc/awstats/awstats.s1.domain1.com.confYou should have a look inside to check and change manually main parameters.You can then manually update your statistics for 's1.domain1.com' with command:> perl awstats.pl -update -config=s1.domain1.comYou can also read your statistics for 's1.domain1.com' with URL:http://localhost/awstats/awstats.pl?config=s1.domain1.comPress ENTER to finish...按回车来结束安装.这段话就写的很清楚,接下来的工作就是检查配置文件执行perl awstats.pl -update -config=s1.domain1.com来更新数据库通过http://localhost/awstats/awstats.pl?config=s1.domain1.com来查看统计的信息这段脚本执行完,我们的httpd.conf也更新了,下面是它主要改动的部分将CustomLog logs/access_log common改为CustomLog logs/access_log combined在文件的最后面增加了# Directives to allow use of AWStats as a CGIAlias /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.Options NoneAllowOverride NoneOrder allow,denyAllow from all在修改之前,还是先停掉脚本启动的apache,将正确的apache启动[root@server1 tools]# service httpd stop[root@server1 tools]# /usr/local/apache2/bin/apachectl start访问网站s1.domain1.com正常显示,这次访问就应该被记录到了日志里面.访问http://192.168.0.111t/awstats/awstats.pl?config=s1.domain1.com,提示Forbidden
You don't have permission to access /awstats/awstats.pl on this server很明白是权限问题,到/usr/local下执行chmod -R 777 awstats (应该755就行了,因为我只是测试而已)检查配置文件的关键项目vi /etc/awstats/awstats.s1.domain1.com.conf检查并做如下修改# LogFile="/var/log/httpd/mylog.log"LogFile="/usr/local/apache2/logs/s1_web-access_log"指到apche的日志LogType=W表示分析的是web日志LogFormat=1表示日志格式为combinedSiteDomain="s1.domain1.com"域名HostAliases="s1.domain1.com www.s1.domain1.com 127.0.0.1 localhost"这个变量的意思是这个域的别名.即多个域名对应同一网站的情况,这句是自动生成的.我这里没有用到所以就没有改.更新数据库cd /usr/local/awstats/wwwroot/cgi-binperl awstats.pl -config=s1.domain1.com -update得到如下错误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.s1.domain1.com.conf' file, web server or permissions) may be wrong.Check config file, permissions and AWStats documentation (in 'docs' directory).同时访问http://192.168.0.111t/awstats/awstats.pl?config=s1.domain1.com提示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.s1.domain1.com.conf' file, web server or permissions) may be wrong.Check config file, permissions and AWStats documentation (in 'docs' directory).很明显是/var/lib/awstats这个目录不存在这里需要说明,这个路径是由配置文件/etc/awstats/awstats.s1.domain1.com.conf里面的DirData参数决定的,表示存放数据库的地方.我没有修改所以依然是默认值DirData="/var/lib/awstats"既然提示不存在,现在就来新建它cd /var/libmkdir awstats进行数据库更新[root@server1 cgi-bin]# perl awstats.pl -config=s1.domain1.com -update信息如下Update for config "/etc/awstats/awstats.s1.domain1.com.conf"With data in log file "/usr/local/apache2/logs/s1_web-access_log"...Phase 1 : First bypass old records, searching new record...Searching new records from beginning of log file...Jumped lines in file: 0Parsed lines in file: 1处理的总行数(apache的日志是一行代表一个记录)Found 0 dropped records,Found 1 corrupted records, 发现了一个损坏的记录Found 0 old records,Found 0 new qualified records.查看日志的内容[root@server1 cgi-bin]# less /usr/local/apache2/logs/s1_web-access_log192.168.0.28 - - [30/Aug/2007:10:19:59 +0800] "GET / HTTP/1.1" 200 23访问http://192.168.0.111/awstats/awstats.pl?config=s1.domain1.com查看统计信息 很是奇怪,为什么会这样呢?打开httpd.conf发现虚拟主机的部分并没有改动,所以记录下来的日志类型并没有改变,需要单独设置使用的日志类型.ServerAdmin yahoon@xxx.comDocumentRoot /var/www/html/s1ServerName s1.domain1.comLogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined1CustomLog logs/s1_web-access_log combined1ErrorLog logs/s1_web-error_log可以看到我这里将这种日志格式命名为combine1,只是名称而已,可以随便起.修改后重启apache将日志清空,然后访问s1.domain1.com,产生日志如下[root@server1 cgi-bin]# less /usr/local/apache2/logs/s1_web-access_log192.168.0.28 - - [30/Aug/2007:13:04:05 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1; TencentTraveler )"192.168.0.28 - - [30/Aug/2007:13:05:30 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1; TencentTraveler )"192.168.0.28 - - [30/Aug/2007:13:05:30 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1; TencentTraveler )"192.168.0.28 - - [30/Aug/2007:13:05:30 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1; TencentTraveler )"192.168.0.28 - - [30/Aug/2007:13:05:31 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1; TencentTraveler )"192.168.0.28 - - [30/Aug/2007:13:05:31 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1; TencentTraveler )"……再次更新数据库[root@server1 cgi-bin]# perl awstats.pl -config=s1.domain1.com -updateUpdate for config "/etc/awstats/awstats.s1.domain1.com.conf"With data in log file "/usr/local/apache2/logs/s1_web-access_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 20000 hosts)...Jumped lines in file: 0Parsed lines in file: 6Found 0 dropped records,Found 0 corrupted records,Found 0 old records,Found 6 new qualified records.访问http://192.168.0.111/awstats/awstats.pl?config=s1.domain1.com最后需要说明的是,在文中已经多次提到过,要能反应最新信息就必须更新数据库.也就是说你可能已经对它进行了多次访问,但是查看统计页面发现没变化.这是因为这些日志还没有更新到数据库.简单的方法就是定时执行.当然现在写的也只是这个软件的基本安装和使用,它的使用远不止于这些.尤其其中涉及到了apache日志的相关知识,例如rotate日志循环,负载均衡的日志合并等等.我也刚接触,希望大家多提宝贵意见.下面是相关的网页http://www.chinaunix.net/jh/13/641101.htmlhttp://www.chedong.com/tech/awstats.htmlhttp://www.chedong.com/tech/rotate_merge_log.html 本文出自 “yahoon的小屋” 博客,请务必保留此出处http://yahoon.blog.运维网.com/13184/40392
|
|