使用awstats分析nginx日志


1.awstats介紹

本文主要是記錄centos6.5下安裝配置awstats,並統計nginx訪問日志

1.1 awstats介紹

awstats是一款日志統計工具,它使用Perl語言編寫,可統計的日志類型包括appache,nginx,ftp,mail等,awstats對nginx日志統計非常詳細,如統計項

  • 按參觀時間:  按月歷史統計   按日期統計   按星期   每小時瀏覽次數 
  • 按參觀者:  國家或地區   城市   IP   最近參觀日期   無法反解譯的IP地址   搜索引擎網站的機器人
  • 瀏覽器統計:  每次參觀所花時間   文件類別   下載  存取次數  入站處   出站處   操作系統版本  瀏覽器版本
  • 反向鏈接:  來源網址   由那些搜索引擎轉介
  • HTTP:  錯誤碼   錯誤次數 (400)   錯誤次數 (403)   錯誤次數 (404) 
  • 搜索:  用以搜索的短語 用以搜索的關鍵詞

 

1.2 awstats目錄

1.2.1 awstats有二進制rpm包,也有源碼包,文中使用源碼包安裝,安裝目錄是/usr/local/awstats, wwwroot是web視圖需要的資源文件,tools是一些統計腳本

awstats/
├── docs
├── README.md
├── tools
└── wwwroot

1.2.2 awstats配置文件目錄/etc/awstats/(默認目錄)

awstats.www.test.com.conf    #站點www.test.com的配置文件
awstats.bbs.test.com.conf    #站點bbs.test.com配置文件

1.2.3 awstats數據文件存放目錄/var/lib/awstats/
awstats從日志統計的數據文件保存在這個目錄,這些數據文件可用於生成最終的html頁面

#以上目錄都可以在配置文件中指定

 

1.3 awstats腳本用法

1.3.1 awstats_configure.pl 主要用它生成各個站點配置文件

/usr/local/awstats/tools/awstats_configure.pl --help
...
 - Create one AWStats config file (if asked) 

1.3.2 awstats.pl用來生成或更新awstats數據文件

它會分析nginx的訪問日志,把分析結果更新到/var/lib/awstats數據文件中去,因此分析完的日志即使刪除,只要/var/lib/awstats中的數據文件不丟失,就不會影響統計結果

/usr/bin/perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.test.com -configdir=/etc/awstats
-update: 更新統計數據文件
-config: 更新www.test.com這個站點
-configdir: 配置文件目錄,/etc/awstats是默認位置,awstats會讀取此目錄下由"-config"參數指定的站點的配置文件(awstats.www.test.com.conf)

1.3.3 awstats_updateall.pl是awstats.pl的封裝,更新所有站點統計數據

/usr/local/awstats/tools/awstats_updateall.pl 
...
Where options are:
  -awstatsprog=pathtoawstatspl
  -configdir=directorytoscan
  -excludeconf=conftoexclude[,conftoexclude2,...] (Note: awstats.model.conf is always excluded)

/usr/local/awstats/tools/awstats_updateall.pl now -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -excludeconf=www.test.com
-awstatsprog: 更新腳本路徑
-excludeconf: 可選項,更新所有站點數據,但是排除www.test.com這個站點

#此腳本最終調用的還是awstats.pl

1.3.4 awstats_buildstaticpages.pl用於讀取數據文件/var/lib/awstats/*並創建或更新html頁面

這個腳本默認只是根據/var/lib/awstats/下的數據文件更新html,如果要在html頁面看到新日志的統計,要先使用awstats.pl更新數據文件,然后再更新html

/usr/local/awstats/tools/awstats_buildstaticpages.pl --help
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=www.test.com -lang=cn -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -dir=/home/awstats-logs/web/
-update:更新html頁面之前,先更新數據文件(調用awstats.pl腳本)
-lang: 輸出語言為cn
-dir: html頁面存放目錄

 

1.4 awstats工作流程

awstats工作流程如下:

  1. 觸發更新進程,並指定此次更新的站點
  2. awstats根據站點名讀取/etc/awstats下相應的配置文件
  3. 根據配置文件中指定的日志路徑分析日志,並把數據文件存放在/var/lib/awstats目錄
  4. 隨后執行構建html頁面腳本,從數據文件構建html到指定目錄中
  5. 配置web服務器,通過web訪問這些html頁面,得到統計結果
  6. 設置定時任務,自動觸發更新進程,循環1,2,3,4,5步驟

 

1.5 awstats統計結果

awstats統計結果可以配置為靜態html,也可以配置使用動態頁面(動態頁面支持網頁端更新日志,指定查看某一個日期的統計結果,但需要cgi支持),兩者的區別如下:

  • 靜態頁面中展示了具體統計結果,awstats提供有腳本用於更新統計結果,可以設置crontab實現定時更新新產生的日志
  • 動態頁面使用GET請求來獲取統計結果,GET請求中帶有查詢參數,這些參數最終會傳遞給awstats.pl腳本,awstats本身並不提供CGI支持,需要自己配置CGI支持

上面是對awstats整體介紹,下面我們以站點www.test.com,bbs.test.com為例說明配置過程,需要建立兩個配置文件,在兩個配置文件中分別指定www,bbs的日志路徑以及其它參數

 

2.awstats配置

2.1 安裝

cd /opt
wget http://prdownloads.sourceforge.net/awstats/awstats-7.4.tar.gz
tar xvf awstats-7.4.tar.gz
mv awstats-7.4 /usr/local/awstats

 

2.2 生成www站點配置文件

由於www站點配置文件和bbs配置文件只是讀取的日志不一樣,其它參數設置可以保持一致,因此本文先建立www站點配置文件,並修改好必要參數,這樣就可以以此配置文件為模板,建立其它站點配置文件

配置文件默認存放於/etc/awstats下,生成配置文件步驟如下

[root@lnmptest tools]# ./awstats_configure.pl 
...
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  #我們這里配置的是nginx,所以輸入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      #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.test.com     #這個配置文件是統計www.test.com這個站點的,名字可以隨便寫,與在awstats.pl中"-config"參數指定的站點名保持一致就好

-----> 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):
>               #直接回車

-----> Create config file '/etc/awstats/awstats.www.testme.com.conf'
 Config file /etc/awstats/awstats.www.testme.com.conf created.
...
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue... 
...
Press ENTER to finish...

 

2.3 修改www站點配置文件

2.3.1 LogFile參數

#vim /etc/awstats/awstats.www.test.com.conf

#LogFile="/usr/local/awstats/tools/logresolvemerge.pl /home/logs/www/*.log |"
#LogFile="/home/logs/www/%YYYY-480%MM-480%DD-480.www.test.access.log"
#LogFile="/home/logs/www/www.test.com.access.log"

#如上,LogFile參數有多種寫法,配置文件中已經解釋很詳細,主要如下
#%YYYY%MM%DD: #此變量表示當前本地時間,比如今天是2015年11月8號,則此變量為"20151108"(不包括雙引號,下同)
#%YYYY-24%MM-24%DD-24: #當前時間的24小時之前,即昨天此刻,"20151107"
#%YYYY-240%MM-240%DD-240: #240小時之前,即10天前此刻,"20151029"
#%YYYY-240%MM-2400%DD-240.www.test.access.log: #會匹配20151029.www.test.access.log 這條日志
#%YYYY-240%MM-2400%DD-240.*.log: #會匹配以"20151029"開頭,以".log"結尾的所有日志
#"/usr/local/awstats/tools/logresolvemerge.pl /home/logs/www/*.log |": 使用logresolvemerge.pl合並統計/home/logs/www下所有以".log"結尾的文件,最后的管道符號"|"不能少

2.3.2  LogFormat參數

不同的web日志具有不同格式,此參數定義awstats要讀取的日志格式,默認情況下,LogFormat值為1

LogFormat=1
#關於具體情況,參考此配置文件中的注釋

如果LogFormat值設為1,則awstats能夠讀取的日志格式為NCSA Combined Log Format, nginx默認的輸出格式也符合NCSA Combined Log Format, 因此awstats能夠讀取nginx的輸出日志,nginx輸出日志格式如下:

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" $http_x_forwarded_for';

注意:nginx日志最后一個字段"$http_x_forwarded_for"並不會被awstats所使用, awstats會使用第一個字段"$remote_addr"作為終端用戶IP進行統計,但是如果你的網站做的CDN, 或者使用代理訪問, 那最后一個字段記錄的IP才是真正終端用戶的IP(此部分內容請參考nginx相關資料),因此如果你的網站做過CDN,或者使用代理,那么就需要做下面的修改以使awstats統計到真正的用戶IP,而非CDN節點或代理的IP

第一種方法,我們可以自定義awstats讀取日志的格式,需要修改LogFormat參數

LogFormat = "%otherquot - %host_r %time1 %methodurl %code %bytesd %refererquot %uaquot %host"
#這一行告訴awstats傳入的nignx日志中每一個字段的意義,第一個參數"%otherquot"告訴awstats不要讀取此字段所代表的內容,即忽略"$remote_addr"的內容,nginx日志的最后一個字段"http_x_forwarded_for"才是awstats應該使用的"%host"

第二種方法不修改LogFormat參數,修改nginx日志記錄格式,把"$http_x_forwarded_for"字段調到首字段,"$remote_addr"調到最后一字段,或者使用腳本把nginx日志中最后一個字段記錄的用戶真實IP和第一個字段對調

2.3.3 其它參數

#日志類型,nginx日志使用W即可
# Enter the log file type you want to analyze.
# Possible values:
#  W - For a web log file
#  S - For a streaming log file
#  M - For a mail log file
#  F - For a ftp log file
# Example: W
# Default: W
#
LogType=W

#數據文件保存目錄
DirData="/var/lib/awstats"

#網頁端支持刷新數據(靜態頁面不需要此參數,只有使用動態頁面才需要)
AllowToUpdateStatsFromBrowser=1

#開啟地理位置支持(顯示IP所在國家,城市統計)
LoadPlugin="geoip GEOIP_STANDARD  /usr/share/GeoIP/GeoIP.dat"
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/share/GeoIP/GeoIPCity.dat"
#yum install GeoIP GeoIP-data GeoIP-devel perl-Geo-IP -y   #先安裝軟件包

#解決中文亂碼(去掉注釋)
LoadPlugin="decodeutfkeys"

2.3.4 生成其它站點配置文件

cp /etc/awstats/awstats.www.test.com.conf /usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf
#/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf 此配置文件為awstats_configure.pl腳本生成配置文件時的模板

生成bbs站點配置文件

cd /usr/local/awstats/tools/
./awstats_configure.pl
...
#參考生成www步驟

由於使用了www的模板,因此上面步驟生成的bbs配置文件將會與前面www配置文件一樣,只需要修改LogFile參數即可

#cat /etc/awstats/awstats.bbs.test.com.conf | grep LogFile
LogFile="/usr/local/awstats/tools/logresolvemerge.pl /home/logs/bbs/*.log |"

 

2.4 生成靜態統計結果

2.4.1 更新所有站點數據文件

/usr/local/awstats/tools/awstats_updateall.pl now -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
#這個腳本會更新所有站點配置文件,默認從/etc/awstats目錄尋找,如果你配置文件不在/etc/awstats目錄,則需要指定目錄

2.4.2 另一種方法是直接使用awstats.pl腳本,逐個站點進行更新

/usr/bin/perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.test.com -configdir=/etc/awstats
/usr/bin/perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=bbs.test.com -configdir=/etc/awstats
-configdir: 這個參數是可選

2.4.3 生成靜態html統計頁面

上面2.4.1或2.4.2都可以統計日志,把結果更新到數據文件,下面是根據數據文件生成最終可以用web訪問的html

/usr/local/awstats/tools/awstats_buildstaticpages.pl -config=www.test.com -lang=cn -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -dir=/home/awstats/web/
/usr/local/awstats/tools/awstats_buildstaticpages.pl -config=bbs.test.com -lang=cn -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -dir=/home/awstats/web/
#/home/awstats/web 為html存放目錄

2.4.4 配置web訪問

我們使用nginx作為web服務器,下面配置一個虛擬主機,nginx配置虛擬主機方法請自行搜索

#cat /usr/local/nginx/conf/vhosts/awstats.conf
server {
    listen       8081;
    server_name  192.168.0.107;
    auth_basic "admin";
    auth_basic_user_file /usr/local/nginx/conf/vhosts/admin.pass;
    access_log  /home/logs/awstats.www.test.com.access.log;
    root /home/awstats/web;    #html存放目錄
    index index.html;

    location /awstats/classes/ {
        alias /usr/local/awstats/wwwroot/classes/;
    }

    location /awstats/css/ {
        alias /usr/local/awstats/wwwroot/css/;
    }

    location /awstats/icon/ {
        alias /usr/local/awstats/wwwroot/icon/;
    }

    location /awstats-icon/ {
        alias /usr/local/awstats/wwwroot/icon/;
    }

    location /awstats/js/ {
        alias /usr/local/awstats/wwwroot/js/;
    }

    location /awstatsicons/ {
        alias /usr/local/awstats/wwwroot/icon/;
    }
 
    location ~ ^/icon/ 
{
        root   /usr/local/awstats/wwwroot;
        index  index.html;
        access_log off;
        error_log off;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM