實時計算(統計)APACHE每個虛擬主機的流量(ZT)

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

實時計算(統計)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。。。。。
《解決方案》

實時計算(統計)APACHE每個虛擬主機的流量(ZT)

好文章,應該加精。有時間實驗一下。
《解決方案》

實時計算(統計)APACHE每個虛擬主機的流量(ZT)

哈哈。。什麼時候轉到這裡了?想不到我亂寫的東西也可以上CU的精華。。多謝轉貼
《解決方案》

實時計算(統計)APACHE每個虛擬主機的流量(ZT)

原帖由 "Bendy"]哈哈。。什麼時候轉到這裡了?想不到我亂寫的東西也可以上CU的精華。。多謝轉貼

未經你同意就轉過來,對不起哦!
多多包涵
《解決方案》

實時計算(統計)APACHE每個虛擬主機的流量(ZT)

沒關係。我還多謝你轉過來呢。
《解決方案》

實時計算(統計)APACHE每個虛擬主機的流量(ZT)

:em02: 不錯的文章,以後應該會用得著.
《解決方案》

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: *** Error 1



make 出錯,怎麼處理阿。。謝謝!!!
《解決方案》

不錯的東東。
《解決方案》

用mod-cband是不是更容易實現?
《解決方案》

就是呀  make報錯 求解決?




[火星人 ] 實時計算(統計)APACHE每個虛擬主機的流量(ZT)已經有647次圍觀

http://coctec.com/docs/service/show-post-13793.html