实时计算(统计)APACHE每个虚拟主机的流量==主机服务商必备

参照国外空间商的做法。以流量大小来衡量一个网站的大小、规模。从而实行收费分级。是一个非常值得我们国内空间商所参考的做法。。。
但具体实行的难度在做如何真实地计算每一个虚拟主机用户所占用的流量大小。。。
我所知道的做法有:
一、CPANEL里面的计算流量方法是:先使用APACHE的功能。将每个虚拟主机用户的访问数据全部记录下来。然后再使用某种分析工具。计算出每个用户的总共的流量大小。
二、使用apache的mod_accounting模块。(本文所介绍的).使用这功能可以实时地记录每个虚拟主机用户的访问数据大小(传入多少、传出多少)至指定的MYSQL数据库。

mod_accounting介绍:

mod_accounting is a simple Apache module that can record traffic statistics into a database (bytes in/out per http request).

官方主页为:
http://sourceforge.net/projects/mod-acct/

最新下载页面为:
http://sourceforge.net/project/s ... p;release_id=109989

具体有关介绍请自己慢慢看。。。
(sourceforge.net这网址国内某些地方可能访问不到。或者访问困难。请使用代理访问)

安装方法:
一、服务器下载源代码

#wget 具体地址

二、解压源代码
#tar xzvf mod_accounting-0.5.tar.gz
#cd mod_accounting-0.5

三、更改安装信息

#vi Makefile

#============Makefile=================
## $Id: Makefile,v 1.3 2001/12/30 14:11:43 tellini Exp $
##
##  Makefile -- Build procedure for sample mod_accounting Apache module
##  Autogenerated via ``apxs -n accounting -g''.
##

#   the used tools
APXS=apxs        #加上apache的运行目录。 ex:   /usr/local/apache/bin/apxs
APACHECTL=apachectl      #同上

#   here's what you may need to change
DEF=-DNEED_POSTGRES -DNEED_MYSQL                 #如果不需要postgres数据库。。。将前面那段  " -DNEED_POSTGRES "删除
INC=-I/usr/local/pgsql/include/ -I/usr/local/mysql/include/         #同上。将 -I/usr/local/pgsql/include/ 删除
LIB=-L/usr/local/pgsql/lib -lpq -L/usr/local/mysql/lib/mysql/ -lmysqlclient   #同上。将-L/usr/local/pgsql/lib -lpq 删除

.
.
.
下面的不用更改
#==========================================

四、安装/测试

#make install
#make install test

五、安装完毕。(可能APACHE需要重新启动)设置数据库及模块设置

a:数据库设置
新建一个数据库。或者使用现有的数据库。
添加以下内容:

CREATE TABLE ipaccounting (
bytesin         bigint(20),
bytesout         bigint(20),
host                 varchar(255)
);

INSERT INTO ipaccounting VALUES (0, 0, 'www.domain.com');

我的做法是:一、建一个accounting数据库。二、导入以上内容(当然域名有更改)、三、建立一个用户名为acct(密码为pass)的用户。对该数据库有绝对的权限。(用来更新流量)

b:模块设置。
编辑apache的配置文件httpd.conf
如果配置文件已经添加有相应模块

LoadModule accounting_module  libexec/mod_accounting.so
AddModule mod_accounting.c

就直接继续配置。如果没有添加就手动在相应位置添加。。。
再加上配置信息。。。

官方说明如下:
The module adds these configuration directives:

AccountingQueryFmt: the query to execute to log the transactions.
Available placeholders are %s for sent bytes,
%r for received ones, %h for the virtual host
name, %u for the user name.

AccountingDatabase: the name of the database for logging.

AccountingDatabaseDriver: the name of the database driver.

AccountingDBHost: host and port needed to connect to the database

AccountingLoginInfo: user and password required for logging into
the database

AccountingTimedUpdates: number of seconds to wait between 2 update queries
(performance tuning)

AccountingIgnoreHosts: a list of hosts to ignore. You can specify a
single IP (e.g. 192.168.1.1), a host/mask pair
(e.g. 192.168.1.1/255.255.255.0) or a range
(e.g. 192.168.1.1-192.168.1.5)

而我的实际例子是:
AccountingQueryFmt "UPDATE ipaccounting SET bytesin = bytesin + %r, bytesout = bytesout + %s WHERE LOWER( host ) = LOWER( '%h' )"
AccountingDatabase accounting
AccountingDatabaseDriver mysql
AccountingDBHost localhost 3306
AccountingLoginInfo acct pass
AccountingTimedUpdates 10

详细介绍。。
AccountingQueryFmt    运行的SQL语句。。直接用原来的就OK了。如果你的数据库结构不同。就更改相应位置。
AccountingDatabase  数据库名。自定义
AccountingDatabaseDriver  数据库类型。二个可选(mysql   /   postgres)
AccountingDBHost 数据库地址、端口,必须填完二个信息 一般前者是localhost 后者是端口(mysql/3306   postgres/5432)
AccountingLoginInfo 数据库用户名和密码
AccountingTimedUpdates 更新时间(秒)视具体调试而定。太大不好。太小也不好。

保存、退出!重启apache...

六、查看是否工作。
最简单的做法就是查看error_log文件。如果没有出现accounting的错误信息就基本上正常工作了。。。

七、后期工作。
当然是在MYSQL数据库里。对每一个虚拟主机。都建立一个对应的记录。。。值行注意的是。在登记host栏目时。一定要填写httpd.conf里面<VirtualHost>;信息的ServerName的值。否则无法更新。

八、实际应用工作。
当然。以上的工作都只是系统的安装工作。但实际应用上。需要有程序系统的配全。这就需要我们编写一个简单的PHP系统程序。可以让系统记录出来的mysql数据库应用到网页上。

让客户自己查看自己网站用了多少流量,周期性维护数据库(清空、备份等)添加虚拟主机的记录进数据库系统.

http://bbs.chinaunix.net/viewthread.php?tid=235137

This entry was posted in  服务器类 and tagged  apache流量监控 by  admin. Bookmark the  permalink.