• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            xiaoguozi's Blog
            Pay it forword - 我并不覺的自豪,我所嘗試的事情都失敗了······習慣原本生活的人不容易改變,就算現狀很糟,他們也很難改變,在過程中,他們還是放棄了······他們一放棄,大家就都是輸家······讓愛傳出去,很困難,也無法預料,人們需要更細心的觀察別人,要隨時注意才能保護別人,因為他們未必知道自己要什么·····
            轉自: https://www.akii.org/use-awstats-automatic-analysis-nginx-log.html
            使用awstats可以分析apache日志,同樣也可以分析nginx日志。本文將詳細介紹自動定時切割nginx的訪問日志,并使用awstats來定時分析nginx的日志的實現方法。

            前言

            本文中使用的是awstats 7.0版本。
            此版本增加了對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 上發展很快的一個基于 Perl 的 WEB 日志分析工具,一個充分的日志分析讓 Awstats 顯示您下列資料:

            • 訪問次數、獨特訪客人數,
            • 訪問時間和上次訪問,
            • 使用者認證、最近認證的訪問,
            • 每周的高峰時間(頁數,點擊率,每小時和一周的千字節),
            • 域名/國家的主機訪客(頁數,點擊率,字節,269域名/國家檢測, geoip 檢測),
            • 主機名單,最近訪問和未解析的 IP 地址名單
            • 大多數看過的進出頁面,
            • 檔案類型,
            • 網站壓縮統計表(mod_gzip 或者 mod_deflate),
            • 使用的操作系統 (每個操作系統的頁數,點擊率 ,字節, 35 OS detected),
            • 使用的瀏覽器,
            • 機器人訪問(檢測 319 個機器人),
            • 蠕蟲攻擊 (5 個蠕蟲家族),
            • 搜索引擎,利用關鍵詞檢索找到你的地址,
            • HTTP 協議錯誤(最近查閱沒有找到的頁面),
            • 其他基于 URL 的個性報導,鏈接參數, 涉及綜合行銷領域目的.
            • 貴網站被加入”最喜愛的書簽”.次數.
            • 屏幕大小(需要在索引頁補充一些 HTML 標簽).
            • 瀏覽器的支持比例: Java, Flash, RealG2 reader, Quicktime reader, WMA reader, PDF reader.
            • 負載平衡服務器比率集群報告.

            Awstats 的運行是需要 PERL 環境的支持,從 awstats 的文檔來看,它對 Apache HTTP Server 的支持是非常完美的,而當我們把 Web 服務器換成 Nginx 后,要運行 awstats 變得很麻煩。首先 Nginx 本身對 Perl 的支持是比較弱的,甚至官方也不建議使用;另外在日志格式上有需要修改后才能運行。

            日志切割

            本文主要介紹通過讓 awstats 對日志統計的結果生成靜態頁面,然后通過 Nginx 輸出以達到統計 Nginx 訪問日志的效果,其中還包括如何讓 Nginx 自動切割日志文件。對于nginx的日志,我的做法是按天切割。然后存入日期形式的目錄中并壓縮。

            需要注意的是,nginx的日志應該遵循以下格式,才可以被awstats識別,如定義日志格式

            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;

            這里需要有一個小技巧的提示:把log_format這段代碼放在你nginx的http的定義段中,可以在下面的每一個server中引用此格式。不必在每個server里面都去定義格式。
            本文不講如何安裝nginx,稍后我將發布我的lnmp一鍵安裝包(linux nginx mysql php)。全編譯+優化自動化安裝,使用php-fpm運行php的fastcgi進程。

            我寫了一個定時切割日志的腳本。每天0:00開始執行,切割昨天的日志(交由awstats分析),壓縮前天的日志(壓縮日志可減小存儲空間,為防 止awstats沒有分析完就被壓縮,所以只壓縮前天的日志)。如果你的nginx和log文件放的路徑和我的不一樣,請對應修改。

            1
            vim cut_log.sh

            輸入以下內容

            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時起開始進行,執行crontab -e加入以下代碼再按:wq保存退出,這里我將此腳本放在/root/下,切記要給它可執行權限(chmod +x cut_log.sh).

            1
            00 00 * * * /bin/bash /root/cut_log.sh

            這樣就可以每天凌里自動切割昨天的日志到以日期為目錄結構的目錄中。可以留存以后查詢。留著昨天的日志交給下面的awstats來分析,壓縮前天的日志(前天的已經被分析過了)。

            安裝和配置awstats

            下載最新的 awstats,我使用的是迄今為止最新的7.0版本

            安裝到/usr/local下,這個路徑是習慣。大部分人保持的良好習慣。

            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

            修改權限,wget下載下來的包中權限是非root的,賦予過權限之后,.pl的文件也就可以運行了。

            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

            然后執行 tools 目錄中的 awstats_configure.pl 配置向導,創建一個新的統計

            運行(注意這里要在當前目錄運行。否則會有一些關于標準目錄的提示。)

            1
            2
            cd /usr/local/awstats/tools
            ./awstats_configure.pl

            將會有如下一些提示:

            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并回車,因為我們沒有使用apache

            回車之后下一個選項

            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,創建一個新的配置文件

            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  #這里輸入你要分析的域名,或是隨便一個你易記的配置名并回車

            接下來要定義你的配置文件存放的路徑,可用默認

            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):
            > #直接回車,使用默認路徑/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... #按回車繼續
             
            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... #回車完成配置文件的創建

            完成配置文件的創建后,我們還要修改一下。因為我們是按天切割的日志,切割完成后交由awstats去分析。并不是讓awstats去分時正在時時 增長的也就是正在被寫入的日志,這樣的好處是不至于遺漏數據,并且分析已經切割完成的日志,更不用擔心會有沖突。壞處是我一天切割一次日志,你要等第二天 才能看昨天的一些詳細數據。

            修改/etc/awstats/awstats.akii.org.conf,執行:

            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"

            如果你的日志路徑和我的不一樣,請修改成對應的日志文件名。以上的完整路徑是切割后保存的nginx日志文件。其中%YYYY-24/%MM-24/%DD-24表示年月日都減去24小時,也就是昨天的日志目錄。修改完成后按:wq保存退出。

            接下來可以測試一下awstats分析日志了(前提是你已經有了切割過的日志,沒有的話可以先退行一下切割日志的腳本/root/cut_log.sh)

            首先,還要創建一個awstats用于記錄數據的目錄

            1
            mkdir -p /var/lib/awstats

            然后運行awstats的wwwroot目錄中的awatsts.pl來測試一下

            1
            /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=akii.org

            你如果看到類似下面的提示就說明配置文件都正確了。

            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

            統計分析完成后,結果還在 Awstats 的數據庫中。在 Apache 上,可以直接打開 Perl 程序的網頁查看統計。 但本文開始時已經提到,Nginx 對 Perl 支持并不好,所以我們要換個方法,利用 awstats 的工具將統計的結果生成靜態文件,具體的步驟如下:

            • 首先在 webroot 目錄下創建一個文件夾。例:/home/www/awstats
            • 寫一個腳本,定期執行讓 Awstats 把靜態頁面生成到該目錄中

            先生成存放awstats生成的靜態文件的目錄,我這里用的是/home/www/awstats

            1
            mkdir -p /home/www/awstats

            我們來寫一個腳本

            1
            vim /root/awstats.sh

            然后輸入以下內容

            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 靜態頁面生成工具
            • -update -config=akii.org 更新配置項
            • -lang=cn 語言為中文
            • -dir=/home/www/awstats 統計結果輸出目錄
            • -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路徑。

            然后在你的nginx的配置文件中,在你想要安置awstats或默認的ip或域名的server段中,加入關于awstats和icon的兩個目錄配置。

            如一個完整案例:

            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  靜態頁面目錄
                    root   /home/www/awstats;
                    autoindex on; #可以目錄瀏覽你的多個域名的目錄用于分析
                    index  index.html;
                    access_log off;
            }
             
            location ~ ^/icon/ {             # 圖標目錄
                    root   /usr/local/awstats/wwwroot;
                    index  index.html;
                    access_log off;
            }
            }

            接下來可以測試一下腳本是否可以正確執行

            還是別忘了給它可執行權限

            1
            2
            chmod +x /root/awstats.sh
            /root/awstats.sh

            如果你看到它生成了一堆網頁,那就說明成功了。

            輸出信息部分例如

            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'.

            然后可以把它加入自動運行了。

            配置awstats腳本自動運行

            1
            crontab -e

            加入

            1
            00 1 * * * /root/awstats.sh

            然后保存退出。

            這樣就可以每天在凌晨自動分割日志,并且開始自動用awstats分析nginx的日志了。

            認證訪問

            如果你想給你的awstats加上訪問密碼,可以見這里:nginx為目錄或網站加上密碼認證

            原創文章,寫的辛苦。如果你要轉載,請保留出處及鏈接。

            參考資料:http://www.ibm.com/developerworks/cn/linux/l-cn-awstats-nginx/index.html

            posted on 2013-01-06 18:05 小果子 閱讀(3299) 評論(0)  編輯 收藏 引用 所屬分類: 學習筆記開源
            久久精品黄AA片一区二区三区 | 伊人色综合久久| 久久精品中文字幕久久| 久久久无码精品亚洲日韩软件| 久久这里只有精品视频99| 久久久久精品国产亚洲AV无码| 久久久久无码精品国产不卡| 国产精品无码久久久久 | 久久久久久久亚洲Av无码| 久久99精品久久久久久动态图| 久久精品无码一区二区日韩AV| 天天爽天天狠久久久综合麻豆| 久久AⅤ人妻少妇嫩草影院| 亚洲AV日韩精品久久久久| 精品久久久久中文字| 久久本道伊人久久| 亚洲色大成网站www久久九| 精品久久久久久久久久中文字幕| 久久ww精品w免费人成| 亚洲国产精品综合久久一线| 99久久精品免费国产大片| 久久久久久九九99精品| 国内精品伊人久久久久777| 久久精品国产亚洲一区二区三区| 精品久久久久久国产91| 亚洲国产精品无码久久久不卡| 亚洲AV伊人久久青青草原| 久久精品国产一区二区 | 一本久久精品一区二区| 大蕉久久伊人中文字幕| 久久久久夜夜夜精品国产| 国产精品久久影院| 亚洲狠狠综合久久| 久久精品人人做人人爽电影| 久久久精品午夜免费不卡| 91精品国产色综合久久| 狠狠色丁香久久综合五月| AV色综合久久天堂AV色综合在| 久久婷婷五月综合97色| 久久久久人妻精品一区 | 久久国产免费直播|