首先再來總結下awstats的處理過程以及查看分析結果的兩種方式,來看官方版說明:
Process logs:Building/updating statistics database,建立/更新統計數據庫(包含統計結果的文本文件)命令如下
perl awstats.pl -config=mysite -update
Run reports:Building and reading reports(生成并閱讀報告)
1.The first option is to build the main reports, in a static HTML page, from the command line, using the following syntax
第一種方式,通過命令行生成html文件,然后瀏覽器展示。命令如下
perl awstats.pl -config=mysite -output -staticlinks > awstats.mysite.html
2.The second option is to dynamically view your statistics from a browser. To do this, use the URL:
第二種方式,通過如下的url“動態”的生成該站點的分析報告
總體思路就是,既然“動態生成”這個過程耗時,那就在服務器上定時通過curl 請求每個站點對應的url將生成的html頁面存儲到特定位置,然后瀏覽器訪問時直接讀取html文件即可(可能有同學要問了,這么費事,那為啥不直接用上面的第一種方式,用awstats.pl提供的參數直接生成html文件呢?這也就回歸到上篇文章中討論過的兩種方式的差別了,awstats.pl生成的靜態html頁面從易用性和美觀性都不如通過CGI動態生成的html頁面)
思路有了,接下來就是“嘗試”和“分析特征”。我們直接以
curl -o /tmp/mysite.html http://www.myserver.mydomain/awstats/awstats.pl?config=mysite
得到的頁面源代碼如下
Statistics for www.mysite.com (2015-08) - main
可以看到動態生成的頁面實際上是一個包含了兩個frame(mainleft和mainright)的html文件,也就是說,如果我們想還原一個動態生成的報告頁面,需要通過如下三條命令來生成對應的三個文件
curl -s -o main.html "http://www.myserver.mydomain/awstats/awstats.pl?config=mysite" #取得主頁面curl -s -o left.html "http://www.myserver.mydomain/awstats/awstats.pl?config=mysite&framename=mainleft" #取得左framecurl -s -o right.html "http://www.myserver.mydomain/awstats/awstats.pl?config=mysite&framename=mainright" #取得右frame
然后,需要在 main.html中修改mainleft和mainright兩個frame的src屬性,將其指定到我們生成的left.html和right.html。如此我們就實現了將動態頁面靜態化(實際上是把動態生這個等待時間放到腳本里定時執行了)。
接下來,就是具體的實現過程了,涉及到對上篇文章中“ cron_awstats_update.sh ”腳本的改進,修改后的腳本內容如下(注釋還算豐富,也能幫助理解思路)
#!/bin/sh#awstats日志分析basedir=/usr/local/awstatsdate_y_m=$(date +%Y%m -d '1 day ago') #因為該腳本是第二天凌晨分析前一天的日志cd $basedir#循環更新所有站點日志統計信息echo -e "\e[1;31m-------`date "+%F %T"` 開始處理---------\n\e[0m" >>logs/cron.logfor i in `ls result/`do echo -e "\e[1;32m -----`date "+%F %T"` 處理 $i 日志-----\e[0m" >>logs/cron.log perl wwwroot/cgi-bin/awstats.pl -config=etc/$i.conf -lang=cn -update &>>logs/cron.log #將動態頁面靜態化,查看展示頁面結構可得:主頁面基本沒內容,主要靠左右兩個frame來生成內容 #所以可以將每一個站點的展示頁分為三部分來緩存 echo -e "\e[1;32m -----`date "+%F %T"` 生成 $i 分析靜態頁面-----\n\e[0m" >>logs/cron.log cd wwwroot #進入wwwroot目錄,并以 '站點/日期'為格式創建目錄用來存儲html文件 if [ ! -d $i/$date_y_m ];then mkdir -p $i/$date_y_m;fi cd $i/$date_y_m curl -s -o main.html "http://www.myserver.mydomain/cgi-bin/awstats.pl?config=$basedir/etc/$i.conf" #取得主頁面 curl -s -o left.html "http://www.myserver.mydomain/cgi-bin/awstats.pl?config=$basedir/etc/$i.conf&framename=mainleft" #取得左frame curl -s -o right.html "http://www.myserver.mydomain/cgi-bin/awstats.pl?config=$basedir/etc/$i.conf&framename=mainright" #取得右frame #修改main.html里關于左右兩個frame的引用 sed -i -e 's/awstats.pl.*left/left.html/g' -e 's/awstats.pl.*right/right.html/g' main.html #接下來修改上面三個文件中的超鏈接部分以及字符集 sed -i -e 's#awstats.pl#http://www.myserver.mydomain/cgi-bin/awstats.pl#g'\ -e 's/charset=.*/charset=utf-8">/g'\ -e 's/lang="cn"http://g'\ main.html left.html right.html cd $basedirdoneecho -e "\e[1;33m-------`date "+%F %T"` 處理完成---------\n\e[0m" >>logs/cron.log
經過腳本處理之后,在wwwroot目錄下,站點目錄與html文件會是這個樣子
到此,我們對上篇文章中的nginx配置部分做相應修改后就可以通過如下url來訪問了
http://www.myserver.mydomain/www/201605 #表示www站2016年5月的統計頁面
但是,改造到這里并不算完,在動態生成的頁面里,有選擇年和月的下拉框,可以查看指定年月的統計頁面,如下圖
這個功能會產生一個如下的請求
http://www.myserver.mydomain/cgi-bin/awstats.pl?month=04&year=2016&output=main&config=www.conf&framename=index
仍然是動態請求(即仍然會慢),但按照我們的設計,每個月應該都已經生成了靜態文件,所以是不需要動態生成的。如何將這個功能點修改為也按照上面靜態url的格式呢,這里作者首先想到了兩個方案:
一個是通過js獲取年和月的值,然后在表單的action處拼出所需的url
另一個是通過nginx的rewrite來實現
經過嘗試和對比,第二種方案更適合這里的場景,因為第一種涉及到對生成的html文件內容進行修改,且不止一處,實現起來??嗦一些;而第二種方案只需要在nginx里做配置即可(這里如何從nginx獲取到參數值并且引用該值算是一個小技巧吧)。
最終,修改之后的nginx配置文件如下
server { listen 800; root /usr/local/awstats/wwwroot; access_log /tmp/awstats_access_log access; error_log /tmp/awstats_nginx.error_log notice; location / { index index.html main.html; } # Static awstats files: HTML files stored in DOCUMENT_ROOT/awstats/ location /awstats/classes/ { alias classes/; } location /awstats/css/ { alias css/; } location /awstats/icon/ { alias icon/; } location /awstats-icon/ { alias icon/; } location /awstats/js/ { alias js/; } # Dynamic stats. location ~ ^/cgi-bin/(awredir|awstats)\.pl.* { gzip off; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root/cgi-bin/fcgi.php; fastcgi_param X_SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param X_SCRIPT_NAME $fastcgi_script_name; include fastcgi_params; fastcgi_send_timeout 300; #為了讓頂部根據時間篩選功能也能用上之前生成的靜態頁面, 其中%2F部分為url編碼后的/,為了取得站點名 if ($query_string ~* "^month=(\d+)&year=(\d+)&output=main&config=.+etc%2F(.+)\.conf&framename=index$") { set $month $1; set $year $2; set $site $3; rewrite ^/cgi-bin/awstats\.pl /$site/$year$month? permanent; } } expires 12h;}
ok,到這里整個改進過程完畢。每個月份的統計結果的主頁面都已經實現了靜態化,查看時再也不用經歷漫長的等待了!PS: 工具再好,也不見得完全適合或者滿足自己的需求,大部分情況下作為“軟件使用者”的運維同胞,應該有這個意識:不只會用,必要時還能改。共勉!
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com