实时计算(统计)APACHE每个虚拟主机的流量(ZT)

实时计算(统计)APACHE每个虚拟主机的流量(ZT)

实时计算(统计)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数据库应用到网页上。

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



这些东西就是大家工作起来的时候了。(我对网页的前台制作可是外行人。呵。期待着好作品)

有关这方面的问题、经验,欢迎大家与我交流。
new email:      bendy@qq.com

。。。。。END。。。。。
好文章,应该加精。有时间实验一下。
哈哈。。什么时候转到这里了?想不到我乱写的东西也可以上CU的精华。。多谢转贴
[quote]原帖由 "Bendy"]哈哈。。什么时候转到这里了?想不到我乱写的东西也可以上CU的精华。。多谢转贴[/quote 发表:

未经你同意就转过来,对不起哦!
多多包涵
没关系。我还多谢你转过来呢。
不错的文章,以后应该会用得着.
mod_accounting.h:84: error: syntax error before "pool"
mod_accounting.c:34: error: syntax error before "accounting_module"
mod_accounting.c:34: warning: data definition has no type or storage class
mod_accounting.c:36: error: syntax error before "pool"
mod_accounting.c:80: error: syntax error before "table"
mod_accounting.c: In function `TableLen':
mod_accounting.c:84: error: `tab' undeclared (first use in this function)
mod_accounting.c:84: error: (Each undeclared identifier is reported only once
mod_accounting.c:84: error: for each function it appears in.)
mod_accounting.c: In function `BytesSent':
mod_accounting.c:117: warning: passing arg 1 of `strlen' makes pointer from integer without a cast
mod_accounting.c:129: error: structure has no member named `client'
mod_accounting.c:129: error: `BO_BYTECT' undeclared (first use in this function)
mod_accounting.c: In function `BytesRecvd':
mod_accounting.c:153: warning: assignment makes pointer from integer without a cast
mod_accounting.c: In function `get_user':
mod_accounting.c:167: error: structure has no member named `user'
mod_accounting.c: In function `ignore':
mod_accounting.c:180: error: request for member `sin_addr' in something not a structure or union
mod_accounting.c: In function `set_db':
mod_accounting.c:207: error: request for member `module_index' in something not a structure or union
mod_accounting.c: In function `set_driver':
mod_accounting.c:216: error: request for member `module_index' in something not a structure or union
mod_accounting.c: In function `set_db_host':
mod_accounting.c:236: error: request for member `module_index' in something not a structure or union
mod_accounting.c: In function `set_login_info':
mod_accounting.c:246: error: request for member `module_index' in something not a structure or union
mod_accounting.c: In function `set_query_fmt':
mod_accounting.c:256: error: request for member `module_index' in something not a structure or union
mod_accounting.c: In function `set_timed_updates':
mod_accounting.c:269: error: request for member `module_index' in something not a structure or union
mod_accounting.c: In function `add_ignored_hosts':
mod_accounting.c:282: error: request for member `module_index' in something not a structure or union
mod_accounting.c: At top level:
mod_accounting.c:373: error: syntax error before '*' token
mod_accounting.c: In function `acct_make_state':
mod_accounting.c:375: error: `p' undeclared (first use in this function)
mod_accounting.c: At top level:
mod_accounting.c:385: error: syntax error before "pool"
mod_accounting.c: In function `do_query':
mod_accounting.c:387: error: `cfg' undeclared (first use in this function)
mod_accounting.c:400: error: `server' undeclared (first use in this function)
mod_accounting.c:400: warning: passing arg 5 of `ap_log_error' from incompatible pointer type
mod_accounting.c:400: error: too few arguments to function `ap_log_error'
mod_accounting.c:423: error: `p' undeclared (first use in this function)
mod_accounting.c:423: warning: assignment makes pointer from integer without a cast
mod_accounting.c:427: warning: assignment makes pointer from integer without a cast
mod_accounting.c:431: warning: assignment makes pointer from integer without a cast
mod_accounting.c:435: error: `r' undeclared (first use in this function)
mod_accounting.c:435: warning: assignment makes pointer from integer without a cast
mod_accounting.c:442: warning: assignment makes pointer from integer without a cast
mod_accounting.c:449: warning: assignment makes pointer from integer without a cast
mod_accounting.c: In function `acct_transaction':
mod_accounting.c:462: error: request for member `module_index' in something not a structure or union
mod_accounting.c: At top level:
mod_accounting.c:516: error: syntax error before "pool"
mod_accounting.c: In function `acct_child_exit':
mod_accounting.c:518: error: `s' undeclared (first use in this function)
mod_accounting.c:518: error: request for member `module_index' in something not a structure or union
mod_accounting.c:521: error: `p' undeclared (first use in this function)
mod_accounting.c: At top level:
mod_accounting.c:526: error: syntax error before "pool"
mod_accounting.c: In function `mod_acct_init':
mod_accounting.c:528: warning: passing arg 1 of `ap_add_version_component' from incompatible pointer type
mod_accounting.c:528: error: too few arguments to function `ap_add_version_component'
mod_accounting.c: At top level:
mod_accounting.c:534: error: conflicting types for 'accounting_module'
mod_accounting.c:34: error: previous declaration of 'accounting_module' was here
mod_accounting.c:536: error: `this_module_needs_to_be_ported_to_apache_2_0' undeclared here (not in a function)
mod_accounting.c:536: error: initializer element is not constant
mod_accounting.c:536: error: (near initialization for `accounting_module.version')
mod_accounting.c:537: warning: initialization makes integer from pointer without a cast
mod_accounting.c:538: warning: initialization makes integer from pointer without a cast
mod_accounting.c:542: warning: initialization makes integer from pointer without a cast
mod_accounting.c:550: warning: excess elements in struct initializer
mod_accounting.c:550: warning: (near initialization for `accounting_module')
mod_accounting.c:552: warning: excess elements in struct initializer
mod_accounting.c:552: warning: (near initialization for `accounting_module')
mod_accounting.c:555: warning: excess elements in struct initializer
mod_accounting.c:555: warning: (near initialization for `accounting_module')
mod_accounting.c:558: warning: excess elements in struct initializer
mod_accounting.c:558: warning: (near initialization for `accounting_module')
mod_accounting.c:563: warning: excess elements in struct initializer
mod_accounting.c:563: warning: (near initialization for `accounting_module')
apxs:Error: Command failed with rc=65536
.
make: *** [mod_accounting.so] Error 1



make 出错,怎么处理阿。。谢谢!!!
不错的东东。
用mod-cband是不是更容易实现?