日志分析工具Awstats實戰(zhàn)之Nginx篇:分析結(jié)果靜態(tài)化
前言:
Awstats 是在 SourceForge 上發(fā)展很快的一個基于 Perl 的 WEB 日志分析工具,一個充分的日志分析讓 Awstats 顯示您下列資料:
- 訪問次數(shù)、獨特訪客人數(shù),
- 訪問時間和上次訪問,
- 使用者認證、最近認證的訪問,
- 每周的高峰時間(頁數(shù),點擊率,每小時和一周的千字節(jié)),
- 域名/國家的主機訪客(頁數(shù),點擊率,字節(jié),269域名/國家檢測, geoip 檢測),
- 主機名單,最近訪問和未解析的 IP 地址名單
- 大多數(shù)看過的進出頁面,
- 檔案類型,
- 網(wǎng)站壓縮統(tǒng)計表(mod_gzip 或者 mod_deflate),
- 使用的操作系統(tǒng) (每個操作系統(tǒng)的頁數(shù),點擊率 ,字節(jié), 35 OS detected),
- 使用的瀏覽器,
- 機器人訪問(檢測 319 個機器人),
- 蠕蟲攻擊 (5 個蠕蟲家族),
- 搜索引擎,利用關(guān)鍵詞檢索找到你的地址,
- HTTP 協(xié)議錯誤(最近查閱沒有找到的頁面),
- 其他基于 URL 的個性報導(dǎo),鏈接參數(shù), 涉及綜合行銷領(lǐng)域目的.
- 貴網(wǎng)站被加入"最喜愛的書簽".次數(shù).
- 屏幕大小(需要在索引頁補充一些 HTML 標簽).
- 瀏覽器的支持比例: Java, Flash, RealG2 reader, Quicktime reader, WMA reader, PDF reader.
- 負載平衡服務(wù)器比率集群報告.
Awstats 的運行是需要 PERL 環(huán)境的支持,從 awstats 的文檔來看,它對 Apache HTTP Server 的支持是非常完美的,而當我們把 Web 服務(wù)器換成 Nginx 后,要運行 awstats 變得很麻煩。首先 Nginx 本身對 Perl 的支持是比較弱的,甚至官方也不建議使用;另外在日志格式上有需要修改后才能運行。
使用awstats可以分析apache日志,同樣也可以分析nginx日志。本文將詳細介紹自動定時切割nginx的訪問日志,并使用awstats來定時分析nginx日志及實現(xiàn)統(tǒng)計結(jié)果可供安全便捷的查閱。
環(huán)境:
- CentOS 6.4 x86_64
- ip:192.168.1.113 域名:www.sunsky.com
- nginx-1.2.9 編譯安裝,路徑/usr/local/nginx,服務(wù)開啟狀態(tài)
- 日志記錄格式為nginx默認的,切勿更改,否則會造成awstats無法分析日志。
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- awstats-7.2.tar.gz
一、日志自動切割
對于nginx的日志切割,由于沒有像apache一樣去用cronolog工具,這里我們就寫一個腳本,讓它可以在每天00:01自動執(zhí)行,切割昨天的日志(交由awstats分析),壓縮前天的日志(壓縮日志可減小存儲空間,為防止awstats沒有分析完就被壓縮,所以只壓縮前天的日志)。
- vim /server/scripts/cut_nginx_log.sh
輸入以下內(nèi)容:
- #!/bin/sh
- yesterday=`date -d "yesterday" +"%Y%m%d"`
- before_yesterday=`date -d "-2 day" +"%Y%m%d"`
- Nginx_Dir="/usr/local/nginx"
- Nginx_logs="/app/logs"
- Log_Name="www_access"
- cd /tmp
- [ -d $Nginx_Logs ] && cd $Nginx_logs || exit 1
- [ -f $Log_Name.log ] && /bin/mv $Log_Name.log ${Log_Name}_${yesterday}.log || exit 1
- if [ $? -eq 0 -a -f $Nginx_Dir/logs/nginx.pid ]
- then
- kill -USR1 `cat $Nginx_Dir/logs/nginx.pid`
- fi
- [ -f ${Log_Name}_${before_yesterday}.log ] && /usr/bin/gzip ${Log_Name}_${before_yesterday}.log|| exit 1
執(zhí)行crontab -e將該腳本加入定時任務(wù)中
- 1 0 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1
這樣每天凌晨00:01就能自動實現(xiàn)日志的切割,壓縮等功能了。
因為本次實驗下的nginx此時已經(jīng)有日志了,另外為了后文awstats能對切割過的日志進行分析,所以這里我們要運行一下此腳本,來將現(xiàn)有日志進行切割生成昨天的日志方便后文操作。
- /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1
#p#
二、Awstats的安裝與配置
1.部署awstats
首先我們要下載awstats軟件包,并將其放在常規(guī)目錄(/usr/local)下:
- wget http://awstats.sourceforge.net/files/awstats-7.2.tar.gz
- tar zxf awstats-7.2.tar.gz
- mv awstats-7.2 /usr/local/awstats
由于wget下載下來的包中權(quán)限是非root的,所以這里要修改權(quán)限,否則稍后*.pl將無法運行
- chown -R root.root /usr/local/awstats
- chmod +x /usr/local/awstats/tools/*.pl
- chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl
接下來我們要執(zhí)行awstats/tools下的awstats_configure.pl配置向?qū)?,用來生成awstats的配置文件,awstats配置文件的命名規(guī)則是awstats.website.conf。
- cd /usr/local/awstats/tools/
- ./awstats_configure.pl
此時會出現(xiàn)如下提示:
- ----- AWStats awstats_configure 1.0 (build 1.9) (c) Laurent Destailleur -----
- This tool will help you to configure AWStats to analyze statistics for
- one web server. You can try to use it to let it do all that is possible
- in AWStats setup, however following the step by step manual setup
- documentation (docs/index.html) is often a better idea. Above all if:
- - You are not an administrator user,
- - You want to analyze downloaded log files without web server,
- - You want to analyze mail or ftp log files instead of web log files,
- - You need to analyze load balanced servers log files,
- - You want to 'understand' all possible ways to use AWStats...
- Read the AWStats documentation (docs/index.html).
- -----> 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 #這里讓填寫網(wǎng)頁服務(wù)器的配置文件路徑,因為我們用的不是apache,所以這里要填none
- 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)建一個新的配置文件,這里填y
- -----> 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:
- > www.sunsky.com
- #這里讓填寫你的網(wǎng)站域名,虛擬主機名或者隨便一個配置名
- -----> 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):
- >#這里要填寫你配置文件存放路徑,我們使用它默認的路徑/etc/awstats,所以直接回車即可
- -----> Create config file '/etc/awstats/awstats.www.sunsky.com.conf'
- Config file /etc/awstats/awstats.www.sunsky.com.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=www.sunsky.com
- 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...
- #提示不能自動加入crontab定時任務(wù),需要稍后自己添加,我們按回車繼續(xù)即可
- A SIMPLE config file has been created: /etc/awstats/awstats.www.sunsky.com.conf
- You should have a look inside to check and change manually main parameters.
- You can then manually update your statistics for 'www.sunsky.com' with command:
- > perl awstats.pl -update -config=www.sunsky.com
- You can also build static report pages for 'www.sunsky.com' with command:
- > perl awstats.pl -output=pagetype -config=www.sunsky.com
- Press ENTER to finish... #提示配置文件創(chuàng)建完成和如何更新配置及建立靜態(tài)報告頁,這里我們回車即可結(jié)束這個配置向?qū)?nbsp;
2、修改awstats配置文件
完成配置文件的創(chuàng)建之后,我們還需要對/etc/awstats/awstats.www.sunsky.com.conf里的一些參數(shù)進行修改。
- sed -i 's#LogFile="/var/log/httpd/mylog.log"#LogFile="/app/logs/www_access_%YYYY-24%MM-24%DD-24.log"#g' /etc/awstats/awstats.www.sunsky.com.conf
這里更改的目的是指定awstats需要分析的nginx的日志文件路徑。這里的路徑大家要按自己的日志路徑來填。
- sed -i 's#DirData="/var/lib/awstats"#DirData="/usr/local/awstats/data"#g' /etc/awstats/awstats.www.sunsky.com.conf
這里更改的目的是指定awstats的數(shù)據(jù)庫配置文件(即awstats的數(shù)據(jù)庫(純文本))。
由于,此處沒有/usr/local/awstats/data目錄,所以我們要創(chuàng)建出來
- mkdir /usr/local/awstats/data
以上的兩個替換操作進行完之后一定要用命令查看替換是否成功,以便及早發(fā)現(xiàn)紕漏。
- grep "LogFile=" /etc/awstats/awstats.www.sunsky.com.conf
- grep "DirData=" /etc/awstats/awstats.www.sunsky.com.conf
查詢替換結(jié)果正確之后,即可進行下面的步驟。
3、創(chuàng)建awstats統(tǒng)計結(jié)果存放目錄
現(xiàn)在我們要創(chuàng)建awstats統(tǒng)計結(jié)果的數(shù)據(jù)庫存放目錄:
- /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.sunsky.com
如果屏幕輸出類似下面的提示就說明配置文件都正確無誤了:
- Create/Update database for config "/etc/awstats/awstats.www.sunsky.com.conf" by AWStats version 7.2 (build 1.992)
- From data in log file "/app/logs/www_access.log"...
- Phase 1 : First bypass old records, searching new record...
- Searching new records from beginning of log file...
- Jumped lines in file: 0
- 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.
注釋:awstats.pl 會到 /etc/awstats目錄下搜索,根據(jù)讀取到的配置文件運行程序,去讀取-config中的參數(shù),把www.sunsky.com擴展成 awstats.www.sunsky.com.conf
分析日志:運行后將這樣的日志統(tǒng)計結(jié)果歸檔到一個awstats的數(shù)據(jù)庫(純文本)里;
然后是輸出:分兩種形式
1、一種是通過cgi程序讀取統(tǒng)計結(jié)果數(shù)據(jù)庫輸出;
2、一種是運行后臺腳本將輸出導(dǎo)出成靜態(tài)文件;
統(tǒng)計分析完成后,結(jié)果還在 Awstats 的數(shù)據(jù)庫中。在 Apache 上,可以直接打開 Perl 程序的網(wǎng)頁查看統(tǒng)計。 但Nginx 對 Perl 支持并不好,所以要換個方法,利用 awstats 的工具將統(tǒng)計的結(jié)果生成靜態(tài)文件,這里方便我們還是用腳本來實現(xiàn):
- vim /server/scripts/awstats.sh
輸入以下內(nèi)容:
- #!/bin/sh
- Awstats_Dir="/usr/local/awstats"
- [ -d /www/awstats ]||mkdir /www/awstats
- $Awstats_Dir/wwwroot/cgi-bin/awstats.pl -update -config=www.sunsky.com
- $Awstats_Dir/tools/awstats_buildstaticpages.pl -update -config=www.sunsky.com -awstatsprog=$Awstats_Dir/wwwroot/cgi-bin/awstats.pl -lang=cn -dir=/www/awstats
腳本內(nèi)容講解:
- /usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats 靜態(tài)頁面生成工具
- -update -config=www.sunsky.com 更新配置項
- -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路徑
- -lang=cn 語言為中文
- -dir= /www/awstats 統(tǒng)計結(jié)果輸出目錄
- awstats_buildstaticpages.pl會根據(jù)-config的參數(shù)去讀取里面的DirData路徑下的數(shù)據(jù)庫配置文件,然后生成靜態(tài)的html文件,生成的文件重定向到/www/awstats目錄下。
#p#
三、配置nginx實現(xiàn)安全訪問
接下來我們要配置nginx使其能安全的訪問到分析的數(shù)據(jù):
- vim /usr/local/nginx/conf/nginx.conf
在server{}內(nèi)添加如下內(nèi)容:
- server {
- listen 80;
- server_name www.sunsky.com;
- location / {
- root /www/sunsky;
- index index.html index.htm;
- access_log /app/logs/www_access.log commonlog;
- }
- location ~ ^/awstats/ {
- root /www/;
- index awstats.www.sunsky.com.html; #根據(jù)自己的網(wǎng)站域名進行更改首頁文件
- autoindex on;
- access_log off;
- charset gb2312;
- auth_basic "Restricted"; #有些網(wǎng)站不愿意公開網(wǎng)站流量信息,所以加個認證
- auth_basic_user_file /usr/local/nginx/htpasswd.pass; #該文件由apache的加密認證工具htpasswd創(chuàng)建
- }
- location ~ ^/icon/ {
- root /usr/local/awststs/wwwroot;
- index index.html;
- access_log off;
- charset gb2312;
- }
- }
由于nginx沒有好的加密認證工具,需要借助apache的htpasswd來實現(xiàn)加密認證功能:
- htpasswd -c -m /usr/local/nginx/htpasswd.pass sunskyadmin #用戶名為sunskyadmin
配置完畢之后,檢查nginx語法,然后優(yōu)雅重啟之后,用游覽器訪問www.sunsky.com/awstats,輸入賬號密碼之后即可查看統(tǒng)計信息了。
至此,awstats已經(jīng)可以實現(xiàn)對Nginx的日志統(tǒng)計及靜態(tài)化的安全訪問功能了。
四、配置awstats自動運行
為了讓整個日志的統(tǒng)計過程可以實現(xiàn)自動化,將awstats.sh腳本加入crontab定時任務(wù)中去,此時結(jié)合上面的定時切割任務(wù),我們的crontab里面會有多出來兩條定時任務(wù)
- 1 0 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1
- 0 1 * * * /bin/sh /server/scripts/awstats.sh >/dev/null 2>&1
到此,我們整個日志訪問工具awstats在nginx上的配置就完成了。當然此篇是基于靜態(tài)頁面的顯示的,后面會再寫一篇日志分析工具Awstats實戰(zhàn)之Nginx篇-分析結(jié)果動態(tài)化出來和大家一起交流學(xué)習(xí)的。
本文作者:sunsky,博客地址:http://sunsky.blog.51cto.com/