前言
本文中使用的是awstats 7.0版本。
此版本增加了對(duì)win7的支持以及一些更新的特性。
New features/improvements:
- Detect Windows 7.
- Can format numbers according to language.
- More mime types.
- Added geoip_asn_maxmind plugin.
- Geoip Maxmind city plugin have now override file capabilities to complete
missing entries in geoip maxmind database.
- Added graphgooglechartapi to use online Google chart api to build graph.
- Can show map of country to report countries when using graphgooglechartapi.
- Part of codes was change to use more functions and have a cleaner code.
- Added parameter to ignore missing log files when merging for a site on
multiple servers where a single server may not have created a log for a given day.
- Update robots database.
- Added Download tracking where certain mime types are defined as downloads
and HTTP status 206 is tracked as download continuation
Awstats 是在 SourceForge 上發(fā)展很快的一個(gè)基于 Perl 的 WEB 日志分析工具,一個(gè)充分的日志分析讓 Awstats 顯示您下列資料:
- 訪問(wèn)次數(shù)、獨(dú)特訪客人數(shù),
- 訪問(wèn)時(shí)間和上次訪問(wèn),
- 使用者認(rèn)證、最近認(rèn)證的訪問(wèn),
- 每周的高峰時(shí)間(頁(yè)數(shù),點(diǎn)擊率,每小時(shí)和一周的千字節(jié)),
- 域名/國(guó)家的主機(jī)訪客(頁(yè)數(shù),點(diǎn)擊率,字節(jié),269域名/國(guó)家檢測(cè), geoip 檢測(cè)),
- 主機(jī)名單,最近訪問(wèn)和未解析的 IP 地址名單
- 大多數(shù)看過(guò)的進(jìn)出頁(yè)面,
- 檔案類型,
- 網(wǎng)站壓縮統(tǒng)計(jì)表(mod_gzip 或者 mod_deflate),
- 使用的操作系統(tǒng) (每個(gè)操作系統(tǒng)的頁(yè)數(shù),點(diǎn)擊率 ,字節(jié), 35 OS detected),
- 使用的瀏覽器,
- 機(jī)器人訪問(wèn)(檢測(cè) 319 個(gè)機(jī)器人),
- 蠕蟲攻擊 (5 個(gè)蠕蟲家族),
- 搜索引擎,利用關(guān)鍵詞檢索找到你的地址,
- HTTP 協(xié)議錯(cuò)誤(最近查閱沒有找到的頁(yè)面),
- 其他基于 URL 的個(gè)性報(bào)導(dǎo),鏈接參數(shù), 涉及綜合行銷領(lǐng)域目的.
- 貴網(wǎng)站被加入”最喜愛的書簽”.次數(shù).
- 屏幕大小(需要在索引頁(yè)補(bǔ)充一些 HTML 標(biāo)簽).
- 瀏覽器的支持比例: Java, Flash, RealG2 reader, Quicktime reader, WMA reader, PDF reader.
- 負(fù)載平衡服務(wù)器比率集群報(bào)告.
Awstats 的運(yùn)行是需要 PERL 環(huán)境的支持,從 awstats 的文檔來(lái)看,它對(duì) Apache HTTP Server 的支持是非常完美的,而當(dāng)我們把 Web 服務(wù)器換成 Nginx 后,要運(yùn)行 awstats 變得很麻煩。首先 Nginx 本身對(duì) Perl 的支持是比較弱的,甚至官方也不建議使用;另外在日志格式上有需要修改后才能運(yùn)行。
日志切割
本文主要介紹通過(guò)讓 awstats 對(duì)日志統(tǒng)計(jì)的結(jié)果生成靜態(tài)頁(yè)面,然后通過(guò) Nginx 輸出以達(dá)到統(tǒng)計(jì) Nginx 訪問(wèn)日志的效果,其中還包括如何讓 Nginx 自動(dòng)切割日志文件。對(duì)于nginx的日志,我的做法是按天切割。然后存入日期形式的目錄中并壓縮。
需要注意的是,nginx的日志應(yīng)該遵循以下格式,才可以被awstats識(shí)別,如定義日志格式
1 2 3 | log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
|
使用日志格式
1 | access_log /home/www/logs/access.log main;
|
這里需要有一個(gè)小技巧的提示:把log_format這段代碼放在你nginx的http的定義段中,可以在下面的每一個(gè)server中引用此格式。不必在每個(gè)server里面都去定義格式。
本文不講如何安裝nginx,稍后我將發(fā)布我的lnmp一鍵安裝包(linux nginx mysql php)。全編譯+優(yōu)化自動(dòng)化安裝,使用php-fpm運(yùn)行php的fastcgi進(jìn)程。
我寫了一個(gè)定時(shí)切割日志的腳本。每天0:00開始執(zhí)行,切割昨天的日志(交由awstats分析),壓縮前天的日志(壓縮日志可減小存儲(chǔ)空間,為防 止awstats沒有分析完就被壓縮,所以只壓縮前天的日志)。如果你的nginx和log文件放的路徑和我的不一樣,請(qǐng)對(duì)應(yīng)修改。
輸入以下內(nèi)容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #!/bin/bash
# This script run at 00:00
# cut yesterday log and gzip the day before yesterday log files.
# yesterday logs to awstats
# The Nginx logs path
logs_path="/home/www/logs/"
date_dir=${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/$(date -d "yesterday" +"%d")/
gzip_date_dir=${logs_path}$(date -d "-2 day" +"%Y")/$(date -d "-2 day" +"%m")/$(date -d "-2 day" +"%d")/
mkdir -p $date_dir
mv ${logs_path}*access.log $date_dir
/usr/local/nginx/sbin/nginx -s reopen
/usr/bin/gzip ${gzip_date_dir}*.log
|
然后讓它每天0時(shí)起開始進(jìn)行,執(zhí)行crontab -e加入以下代碼再按:wq保存退出,這里我將此腳本放在/root/下,切記要給它可執(zhí)行權(quán)限(chmod +x cut_log.sh).
1 | 00 00 * * * /bin/bash /root/cut_log.sh
|
這樣就可以每天凌里自動(dòng)切割昨天的日志到以日期為目錄結(jié)構(gòu)的目錄中。可以留存以后查詢。留著昨天的日志交給下面的awstats來(lái)分析,壓縮前天的日志(前天的已經(jīng)被分析過(guò)了)。
安裝和配置awstats
下載最新的 awstats,我使用的是迄今為止最新的7.0版本
安裝到/usr/local下,這個(gè)路徑是習(xí)慣。大部分人保持的良好習(xí)慣。
1 2 3 | wget http://awstats.sourceforge.net/files/awstats-7.0.tar.gz
tar -zxvf awstats-7.0.tar.gz
mv awstats-7.0 /usr/local/awstats
|
修改權(quán)限,wget下載下來(lái)的包中權(quán)限是非root的,賦予過(guò)權(quán)限之后,.pl的文件也就可以運(yùn)行了。
1 2 3 4 | chown -R root:root /usr/local/awstats
chmod -R =rwX /usr/local/awstats
chmod +x /usr/local/awstats/tools/*.pl
chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl
|
然后執(zhí)行 tools 目錄中的 awstats_configure.pl 配置向?qū)В瑒?chuàng)建一個(gè)新的統(tǒng)計(jì)
運(yùn)行(注意這里要在當(dāng)前目錄運(yùn)行。否則會(huì)有一些關(guān)于標(biāo)準(zhǔn)目錄的提示。)
1 2 | cd /usr/local/awstats/tools
./awstats_configure.pl
|
將會(huì)有如下一些提示:
1 2 3 4 5 6 7 8 9 10 | -----> Running OS detected: Linux, BSD or Unix
-----> 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):
>none #這里添none并回車,因?yàn)槲覀儧]有使用apache
|
回車之后下一個(gè)選項(xiàng)
1 2 3 4 5 6 7 8 9 10 11 | Your web server config file(s) could not be found.
You will need to setup your web server manually to declare AWStats
script as a CGI, if you want to build reports dynamically.
See AWStats setup documentation (file docs/index.html)
-----> Update model config file '/usr/local/awstats/wwwroot/cgi-bin/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,創(chuàng)建一個(gè)新的配置文件
1 2 3 4 5 6 | -----> 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:
>akii.org #這里輸入你要分析的域名,或是隨便一個(gè)你易記的配置名并回車
|
接下來(lái)要定義你的配置文件存放的路徑,可用默認(rèn)
1 2 3 4 5 | -----> 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):
> #直接回車,使用默認(rèn)路徑/etc/awstats
|
回車后的提示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | -----> Create config file '/etc/awstats/awstats.akii.org.conf'
Config file /etc/awstats/awstats.akii.org.conf created.
-----> 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:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=akii.org
Or if you have several config files and prefer having only one command:
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue... #按回車?yán)^續(xù)
A SIMPLE config file has been created: /etc/awstats/awstats.akii.org.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'yuyuanchun.com' with command:
> perl awstats.pl -update -config=akii.org
You can also build static report pages for 'akii.org' with command:
> perl awstats.pl -output=pagetype -config=akii.org
Press ENTER to finish... #回車完成配置文件的創(chuàng)建
|
完成配置文件的創(chuàng)建后,我們還要修改一下。因?yàn)槲覀兪前刺烨懈畹娜罩荆懈钔瓿珊蠼挥蒩wstats去分析。并不是讓awstats去分時(shí)正在時(shí)時(shí) 增長(zhǎng)的也就是正在被寫入的日志,這樣的好處是不至于遺漏數(shù)據(jù),并且分析已經(jīng)切割完成的日志,更不用擔(dān)心會(huì)有沖突。壞處是我一天切割一次日志,你要等第二天 才能看昨天的一些詳細(xì)數(shù)據(jù)。
修改/etc/awstats/awstats.akii.org.conf,執(zhí)行:
1 | vi /etc/awstats/awstats.akii.org.conf
|
找到
1 | LogFile="/var/log/httpd/mylog.log"
|
修改為:
1 | LogFile="/home/www/logs/%YYYY-24/%MM-24/%DD-24/akii.org_access.log"
|
如果你的日志路徑和我的不一樣,請(qǐng)修改成對(duì)應(yīng)的日志文件名。以上的完整路徑是切割后保存的nginx日志文件。其中%YYYY-24/%MM-24/%DD-24表示年月日都減去24小時(shí),也就是昨天的日志目錄。修改完成后按:wq保存退出。
接下來(lái)可以測(cè)試一下awstats分析日志了(前提是你已經(jīng)有了切割過(guò)的日志,沒有的話可以先退行一下切割日志的腳本/root/cut_log.sh)
首先,還要?jiǎng)?chuàng)建一個(gè)awstats用于記錄數(shù)據(jù)的目錄
1 | mkdir -p /var/lib/awstats
|
然后運(yùn)行awstats的wwwroot目錄中的awatsts.pl來(lái)測(cè)試一下
1 | /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=akii.org
|
你如果看到類似下面的提示就說(shuō)明配置文件都正確了。
1 2 3 4 5 6 7 8 9 10 11 12 13 | Create/Update database for config "/etc/awstats/awstats.akii.org.conf" by AWStats version 7.0 (build 1.964)
From data in log file "/home/www/logs/2010/07/24/akii.org_access.log"...
Phase 1 : First bypass old records, searching new record...
Direct access after last parsed record (after line 43260)
Jumped lines in file: 43260
Found 43260 already parsed records.
Parsed lines in file: 0
Found 0 dropped records,
Found 0 comments,
Found 0 blank records,
Found 0 corrupted records,
Found 0 old records,
Found 0 new qualified records
|
統(tǒng)計(jì)分析完成后,結(jié)果還在 Awstats 的數(shù)據(jù)庫(kù)中。在 Apache 上,可以直接打開 Perl 程序的網(wǎng)頁(yè)查看統(tǒng)計(jì)。 但本文開始時(shí)已經(jīng)提到,Nginx 對(duì) Perl 支持并不好,所以我們要換個(gè)方法,利用 awstats 的工具將統(tǒng)計(jì)的結(jié)果生成靜態(tài)文件,具體的步驟如下:
- 首先在 webroot 目錄下創(chuàng)建一個(gè)文件夾。例:/home/www/awstats
- 寫一個(gè)腳本,定期執(zhí)行讓 Awstats 把靜態(tài)頁(yè)面生成到該目錄中
先生成存放awstats生成的靜態(tài)文件的目錄,我這里用的是/home/www/awstats
1 | mkdir -p /home/www/awstats
|
我們來(lái)寫一個(gè)腳本
然后輸入以下內(nèi)容
1 2 3 4 5 | #!/bin/bash
mkdir -p /home/www/awstats/akii.org
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update \
-config=akii.org -lang=cn -dir=/home/www/awstats/akii.org \
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
|
上述命令的具體意思如下:
- /usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats 靜態(tài)頁(yè)面生成工具
- -update -config=akii.org 更新配置項(xiàng)
- -lang=cn 語(yǔ)言為中文
- -dir=/home/www/awstats 統(tǒng)計(jì)結(jié)果輸出目錄
- -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路徑。
然后在你的nginx的配置文件中,在你想要安置awstats或默認(rèn)的ip或域名的server段中,加入關(guān)于awstats和icon的兩個(gè)目錄配置。
如一個(gè)完整案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | server {
listen 80;
server_name localhost;
root /home/www;
index index.html;
location ~ ^/awstats/ { # awstats 靜態(tài)頁(yè)面目錄
root /home/www/awstats;
autoindex on; #可以目錄瀏覽你的多個(gè)域名的目錄用于分析
index index.html;
access_log off;
}
location ~ ^/icon/ { # 圖標(biāo)目錄
root /usr/local/awstats/wwwroot;
index index.html;
access_log off;
}
}
|
接下來(lái)可以測(cè)試一下腳本是否可以正確執(zhí)行
還是別忘了給它可執(zhí)行權(quán)限
1 2 | chmod +x /root/awstats.sh
/root/awstats.sh
|
如果你看到它生成了一堆網(wǎng)頁(yè),那就說(shuō)明成功了。
輸出信息部分例如
1 2 3 4 5 6 | Launch update process : "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=akii.org -update -configdir=
......
Build keywords page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=akii.org -staticlinks -lang=cn -output=keywords
Build errors404 page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=akii.org -staticlinks -lang=cn -output=errors404
20 files built.
Main HTML page is 'awstats.akii.org.html'.
|
然后可以把它加入自動(dòng)運(yùn)行了。
配置awstats腳本自動(dòng)運(yùn)行
加入
1 | 00 1 * * * /root/awstats.sh
|
然后保存退出。
這樣就可以每天在凌晨自動(dòng)分割日志,并且開始自動(dòng)用awstats分析nginx的日志了。
認(rèn)證訪問(wèn)
如果你想給你的awstats加上訪問(wèn)密碼,可以見這里:nginx為目錄或網(wǎng)站加上密碼認(rèn)證
原創(chuàng)文章,寫的辛苦。如果你要轉(zhuǎn)載,請(qǐng)保留出處及鏈接。
參考資料:http://www.ibm.com/developerworks/cn/linux/l-cn-awstats-nginx/index.html