AWstatsのインストール

概要:AWstatsは高度なウェブ(ftpまたはmailも)サーバーアクセス統計をグラフィックで生成するフリーツールである。AWStatsを使用してApacheのアクセスログを解析し、Webサイトへのアクセス統計をWebブラウザから参照できるようにする。
前提:

1. AWStatsインストール
RPMforgeリポジトリよりインストールする。

# yum -y --enablerepo=rpmforge install awstats ← AWStatsインストール
==========================================================================================
 Package                        Arch          Version               Repository       Size
==========================================================================================
Installing:
 awstats                        noarch        6.95-1.el5.rf         rpmforge        1.1 M
Installing for dependencies:
 perl-Compress-Raw-Bzip2        i386          2.024-1.el5.rf        rpmforge        108 k
 perl-Compress-Raw-Zlib         i386          2.024-1.el5.rf        rpmforge        169 k
 perl-HTML-Parser               i386          3.55-1.fc6            base             92 k
 perl-HTML-Tagset               noarch        3.10-2.1.1            base             15 k
 perl-IO-Compress               noarch        2.024-1.el5.rf        rpmforge        242 k
 perl-libwww-perl               noarch        5.805-1.1.1           base            376 k

Transaction Summary
==========================================================================================
Install      7 Package(s)
Update       0 Package(s)
Remove       0 Package(s)

2. awstats.plの編集 レポートの日付形式を「月 2月 2010」から「2010年 2月」へ変更するためawstats.plを編集する。

# vi /var/www/awstats/awstats.pl ← awstats.pl編集
        if ( $MonthRequired eq 'all' ) {
                print "$Message[6] $YearRequired";
        }
        else {
                print
                  #"$Message[5] $MonthNumLib{$MonthRequired} $YearRequired";
          ← 行頭に#を追加してコメントアウト
                  "$YearRequired$Message[6] ".$MonthNumLib{$MonthRequired};
          ← 追加(日付をYYYY年 MM月形式にする)
        }

print( $MonthRequired eq 'all'
        ? "$Message[6] $YearRequired"
        #: "$Message[5] " ← 行頭に#を追加してコメントアウト
          #. $MonthNumLib{$MonthRequired}
        ← 行頭に#を追加してコメントアウト
          #. " $YearRequired" ← 行頭に#を追加してコメントアウト
        : "$YearRequired$Message[6] $MonthNumLib{$MonthRequired}"
       ← 追加(日付をYYYY年 MM月形式にする)
);

                #print "$MonthNumLib{$monthix}<br />$YearRequired";
         ← 行頭に#を追加してコメントアウト
                print "$YearRequired$Message[6]<br />$MonthNumLib{$monthix}";
         ← 追加(日付をYYYY年 MM月形式にする)
                
                #print "$MonthNumLib{$monthix} $YearRequired";
          ← 行頭に#を追加してコメントアウト
                print "$YearRequired$Message[6] $MonthNumLib{$monthix}";
          ← 追加(日付をYYYY年 MM月形式にする)

3. AWStats設定
/etc/cron.hourly/00awstatsにより1時間おきに自動実行されるAWStatsデータベース更新処理から無駄な処理を省くために、インストール時に自動で作成される次の設定ファイルを削除する。

# rm -f /etc/awstats/awstats.localhost.localdomain.conf ← 不要な設定ファイルを削除する。
awstats設定ファイルを修正する。
# cp /etc/awstats/awstats.model.conf /etc/awstats/awstats.centossrv.com.conf
  ← 設定ファイルをサンプルよりコピー
# vi /etc/awstats/awstats.centossrv.com.conf ← 設定ファイル編集
# "SiteDomain" must contain the main domain name, or the main intranet web
# server name, used to reach the web site.
# If you share the same log file for several virtual web servers, this
# parameter is used to tell AWStats to filter record that contains records for
# this virtual host name only (So check that this virtual hostname can be
# found in your log file and use a personalized log format that include the
# %virtualname tag).
# But for multi hosting a better solution is to have one log file for each
# virtual web server. In this case, this parameter is only used to generate
# full URL's links when ShowLinksOnUrl option is set to 1.
# If analysing mail log, enter here the domain name of mail server.
# Example: "myintranetserver"
# Example: "www.domain.com"
# Example: "ftp.domain.com"
# Example: "domain.com"
#
SiteDomain="localhost.localdomain"
  
SiteDomain="localhost.com" ← Webサーバー名を指定

# Enter here all other possible domain names, addresses or virtual host
# aliases someone can use to access your site. Try to keep only the minimum
# number of possible names/addresses to have the best performances.
# You can repeat the "SiteDomain" value in this list.
# This parameter is used to analyze referer field in log file and to help
# AWStats to know if a referer URL is a local URL of same site or an URL of
# another site.
# Note: Use space between each value.
# Note: You can use regular expression values writing value with REGEX[value].
# Note: You can also use @/mypath/myfile if list of aliases are in a file.
# Example: "www.myserver.com localhost 127.0.0.1 REGEX[mydomain\.(net|org)$]"
#
HostAliases="localhost 127.0.0.1 REGEX[^192\.168\.1\.]"
  ← アクセス元から内部(例:192.168.1.X)を除外する

# If you want to have hosts reported by name instead of ip address, AWStats
# need to make reverse DNS lookups (if not already done in your log file).
# With DNSLookup to 0, all hosts will be reported by their IP addresses and
# not by the full hostname of visitors (except if names are already available
# in log file).
# If you want/need to set DNSLookup to 1, don't forget that this will reduce
# dramatically AWStats update process speed. Do not use on large web sites.
# Note: Reverse DNS lookup is done on IPv4 only (Enable ipv6 plugin for IPv6).
# Note: Result of DNS Lookup can be used to build the Country report. However
# it is highly recommanded to enable the plugin 'geoipfree' or 'geoip' to
# have an accurate Country report with no need of DNS Lookup.
# Possible values:
# 0 - No DNS Lookup
# 1 - DNS Lookup is fully enabled
# 2 - DNS Lookup is made only from static DNS cache file (if it exists)
# Default: 2
#
DNSLookup=2
  
DNSLookup=1 ← アクセス元の名前解決を行う(アクセス元をIPアドレスではなくホスト名で表示する)

# Plugin: GeoIPfree
# Perl modules required: Geo::IPfree version 0.2+ (from Graciliano M.P.)
# Country chart is built from an Internet IP-Country database.
# This plugin is useless for intranet only log files.
# Note: You must choose between using this plugin (need Perl Geo::IPfree
# module, database less up to date) or the GeoIP plugin (need Perl Geo::IP
# module from Maxmind, database more up to date).
# Note: Activestate provide a corrupted version of Geo::IPfree 0.2 Perl
# module, so install it from elsewhere (from www.cpan.org for example).
# This plugin reduces AWStats speed of 10% !
#
#LoadPlugin="geoipfree"
  
LoadPlugin="geoipfree" ← コメント解除(アクセス元国情報を詳細に取得)

4. Geo-IPfreeのインストール

# yum -y install perl-Geo-IPfree
  ← GeoIPfreeプラグイン動作に必要なPerlモジュールインストール
==========================================================================================
 Package                  Arch            Version                 Repository         Size
==========================================================================================
Installing:
 perl-Geo-IPfree          noarch          0.8-1.el5.rf            rpmforge          408 k

Transaction Summary
==========================================================================================
Install      1 Package(s)
Update       0 Package(s)
Remove       0 Package(s)

5. 日本の検索エンジン対応
日本の検索エンジンが検索エンジンとして集計されるようにする。

# vi /var/www/awstats/lib/search_engines.pm ← search_engines.pm編集
@SearchEnginesSearchIDOrder_list1=(
以下を追加
# Japanese Search Engines
'google\.co\.jp',
'66\.249\.(89\.99|93\.104)',
'66\.102\.(7\.99|7\.104|9\.104|11\.104)',
'64\.233\.(161\.104|179\.104|167\.104|183\.104|187\.104)',
'72\.14\.(203|205|207|209|221|235|253)\.104',
'216\.239\.(37\.104|39\.104|41\.104|51\.104|57\.104|59\.104)',
'search\.biglobe\.ne\.jp',
'goo\.ne\.jp',
'nifty\.com',
'search\.msn\.co\.jp',
'search\.odn\.ne\.jp',
'excite\.ne\.jp',
'naver\.co\.jp',
'fresheye\.com',
'tocc\.co\.jp',
'infoseek\.co\.jp',
'cache\.yahoofs\.jp',
'search\.livedoor\.com',
'www\.ceek\.jp',
'www\.bing\.com',
's\.luna\.tv',
'72\.14\.207\.99',
'209\.85\.165\.104',
'209\.85\.129\.104',
'209\.85\.129\.132',
'209\.85\.(173|175)\.104',
'74\.125\.47\.132',
'74\.125\.77\.132',
'74\.125\.95\.132',
'74\.125\.153\.132',
'74\.125\.155\.132',
'72\.14\.235\.132',
'66\.249\.89\.104',

%NotSearchEnginesKeys=(
以下を追加
# For Japanese Search Engines
'infoseek\.co\.jp'=>'at\.infoseek\.co\.jp',
'goo\.ne\.jp'=>'members\.goo\.ne\.jp',

%SearchEnginesHashID = (
以下を追加
# Japanese Search Engines
'google\.co\.jp','googlejapan',
'66\.249\.(89\.99|93\.104)','googlejapan',
'66\.102\.(7\.99|7\.104|9\.104|11\.104)','googlejapan',
'64\.233\.(161\.104|179\.104|167\.104|183\.104|187\.104)','googlejapan',
'72\.14\.(203|205|207|209|221|235|253)\.104','googlejapan',
'216\.239\.(37\.104|39\.104|41\.104|51\.104|57\.104|59\.104)','googlejapan',
'search\.biglobe\.ne\.jp','biglobe',
'goo\.ne\.jp','goo',
'nifty\.com','nifty',
'search\.msn\.co\.jp','msnjapan',
'search\.odn\.ne\.jp','odn',
'excite\.co\.jp','excitejapan',
'naver\.co\.jp','naver',
'fresheye\.com','fresheye',
'tocc\.co\.jp','tocc',
'infoseek\.co\.jp','infoseek',
'cache\.yahoofs\.jp','yahoocache',
'search\.livedoor\.com','livedoor',
'www\.ceek\.jp','ceek',
'www\.bing\.com','bing',
's\.luna\.tv','lunascape',
'72\.14\.207\.99','googlejapan',
'209\.85\.165\.104','googlejapan',
'209\.85\.129\.104','googlejapan',
'209\.85\.129\.132','googlejapan',
'209\.85\.(173|175)\.104','googlejapan',
'74\.125\.47\.132','googlejapan',
'74\.125\.77\.132','googlejapan',
'74\.125\.95\.132','googlejapan',
'74\.125\.153\.132','googlejapan',
'74\.125\.155\.132','googlejapan',
'72\.14\.235\.132','googlejapan',
'66\.249\.89\.104','googlejapan',

%SearchEnginesKnownUrl=(
以下を追加
# Japanese Search Engines
'googlejapan','(p|q)=',
'biglobe','q=',
'goo','MT=',
'nifty','Text=',
'msnjapan','(q|MT)=',
'odn','Querystring=',
'excitejapan','search=',
'naver','query=',
'fresheye','kw=',
'tocc','QRY=',
'infoseek','qt=',
'yahoocache','w=',
'livedoor','q=',
'ceek','q=',
'clusty','query=',

@WordsToCleanSearchUrl= ('act=','annuaire=','btng=','cat=','categoria=','cfg=','cof=','cou
=','count=','cp=','dd=','domain=','dt=','dw=','enc=','exec=','geo=','hc=','height=','hits=
','hl=','hq=','hs=','id=','kl=','lang=','loc=','lr=','matchmode=','medor=','message=','met
a=','mode=','order=','page=','par=','pays=','pg=','pos=','prg=','qc=','refer=','sa=','safe
=','sc=','sort=','src=','start=','style=','stype=','sum=','tag=','temp=','theme=','type=',
'url=','user=','width=','what=','\\.x=','\\.y=','y=','look=');
   変更
@WordsToCleanSearchUrl= ('act=','annuaire=','btng=','cat=','categoria=','cfg=','cof=','cou
=','count=','cp=','dd=','domain=','dt=','dw=','enc=','exec=','geo=','hc=','height=','hits=
','hl=','hq=','hs=','id=','kl=','lang=','loc=','lr=','matchmode=','medor=','message=','met
a=','mode=','order=','page=','par=','pays=','pg=','pos=','prg=','qc=','refer=','sa=','safe
=','sc=','sort=','src=','start=','style=','stype=','sum=','tag=','temp=','theme=','type=',
'url=','user=','width=','what=','\\.x=','\\.y=','y=','look=','target=','collection=','show
Summary=','perPage=','next=','DB=','OPE=','Max=','base=','submit=','SearchType=','SESSIONI
D=','QUERYRULE=','DISPLAYMIN=','RELURLSWITCH=','SORT=','start=','direct=','relwd=','lk=','
svx=','nh=','internet=','DC=','submit0=','DEST=','where=','CCM=','NRS=','UNI=','UD0=','FRS
=','sv=','rf=','oq=','col=','act\.search=');

%SearchEnginesHashLib=(
以下を追加
# Japanese Search Engines
'googlejapan','Google Japan',
'biglobe','Biglobe',
'goo','Goo',
'nifty','Nifty',
'msnjapan','MSN Japan',
'odn','ODN',
'excitejapan','Excite Japan',
'naver','Naver',
'fresheye','Fresheye',
'tocc','TOCC/Search',
'infoseek','Infoseek',
'yahoocache','Yahoo Cache',
'livedoor','Livedoor',
'ceek','CEEK.JP',
'bing','Bing',
'lunascape','Lunascape',

6. Apache設定

# vi /etc/httpd/conf.d/awstats.conf ← Apache用AWStats設定ファイル編集
<Directory /var/www/awstats/>
        DirectoryIndex awstats.pl
        Options ExecCGI
        order deny,allow
        deny from all
        allow from all ← 追加
        #allow from 127.0.0.1 ← コメントアウト
</Directory>
# /etc/rc.d/init.d/httpd reload  ←  Apache設定再読込み
httpd を再読み込み中:                                      [  OK  ]

7. AWStatsデータベース作成

  1. ) AWStatsデータベース初期作成
    Apacheのログファイルを過去分も含めて全てAWStatsのデータベースに取り込む。
    # vi awstatsinit.sh ← AWStatsデータベース初期作成スクリプト作成
    
    #!/bin/sh
    
    logfile=`grep ^LogFile /etc/awstats/awstats.$1.conf|sed -e 's/LogFile="\([^ ]*\)"/\1/p' -e d`
    for log in `ls $logfile*|sort -r`
    do
        `rpm -ql awstats|grep "awstats\.pl"` \
        -config=$1 -update -logfile=$log
    done
    
    # sh awstatsinit.sh centos5 ← AWStatsデータベース初期作成スクリプト実行
    ※ログ量により時間がかかる
    Update for config "/etc/awstats/awstats.centossrv.com.conf"
    With data in log file "/var/log/httpd/access_log.4"...
    Phase 1 : First bypass old records, searching new record...
    Searching new records from beginning of log file...
    Phase 2 : Now process new records (Flush history on disk after 20000 hosts)...
    Jumped lines in file: 0
    Parsed lines in file: 123047
     Found 1821 dropped records,
     Found 0 corrupted records,
     Found 0 old records,
     Found 121226 new qualified records.
    Update for config "/etc/awstats/awstats.centossrv.com.conf"
    With data in log file "/var/log/httpd/access_log.3"...
    Phase 1 : First bypass old records, searching new record...
    Direct access to last remembered record has fallen on another record.
    So searching new records from beginning of log file...
    Phase 2 : Now process new records (Flush history on disk after 20000 hosts)...
    Jumped lines in file: 0
    Parsed lines in file: 130918
     Found 1700 dropped records,
     Found 0 corrupted records,
     Found 0 old records,
     Found 129218 new qualified records.
    Update for config "/etc/awstats/awstats.centossrv.com.conf"
    With data in log file "/var/log/httpd/access_log.2"...
    Phase 1 : First bypass old records, searching new record...
    Direct access to last remembered record is out of file.
    So searching it from beginning of log file...
    Phase 2 : Now process new records (Flush history on disk after 20000 hosts)...
    Jumped lines in file: 0
    Parsed lines in file: 128865
     Found 1680 dropped records,
     Found 0 corrupted records,
     Found 0 old records,
     Found 127185 new qualified records.
    Update for config "/etc/awstats/awstats.centossrv.com.conf"
    With data in log file "/var/log/httpd/access_log.1"...
    Phase 1 : First bypass old records, searching new record...
    Direct access to last remembered record is out of file.
    So searching it from beginning of log file...
    Phase 2 : Now process new records (Flush history on disk after 20000 hosts)...
    Jumped lines in file: 0
    Parsed lines in file: 115202
     Found 1532 dropped records,
     Found 0 corrupted records,
     Found 0 old records,
     Found 113670 new qualified records.
    Update for config "/etc/awstats/awstats.centossrv.com.conf"
    With data in log file "/var/log/httpd/access_log"...
    Phase 1 : First bypass old records, searching new record...
    Direct access to last remembered record is out of file.
    So searching it from beginning of log file...
    Phase 2 : Now process new records (Flush history on disk after 20000 hosts)...
    Jumped lines in file: 0
    Parsed lines in file: 21746
     Found 312 dropped records,
     Found 0 corrupted records,
     Found 0 old records,
     Found 21434 new qualified records.
    # rm -f awstatsinit.sh ← AWStatsデータベース初期作成スクリプト削除
    
    注:以後のAWStatsデータベースの更新は/etc/cron.hourly/00awstatsにより1時間おきに自動実行される

8. Apacheログローテーション設定
Apacheログファイル切替え時、AWStatsのデータベースに取り込んでから切替えを行うようにする。

# vi /etc/logrotate.d/httpd ← Apacheログロテート設定ファイル編集
/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    -------------追加(ここから)-------------
    prerotate
        `rpm -ql awstats|grep "awstats_updateall\.pl"` now -confdir="/etc/awstats" \
        -awstatsprog="`rpm -ql awstats|grep "awstats\.pl"`" >/dev/null
    endscript
    -------------追加(ここまで)-------------
    postrotate
        /bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true
    endscript
}

9. AWStats確認
http://Webサーバー名/awstats/awstats.plへアクセスしてアクセス統計ページが表示されることを確認

10. アクセス統計ページHTML版の作成
awstats.plは、実行するたびにAWStatsのデータベースからログ情報を入力してアクセス統計ページを作成するため、サーバーに負荷がかかることから、awstats.plは内部からのみ実行できるようにする。
また、万が一、AWStatsのデータベースが失われた場合、アクセス統計が見れなくなってしまうため、アクセス統計ページをHTMLで作成するようにする。

  1. ) アクセス統計ページHTML版格納ディレクトリ設定
    # mkdir /var/www/awstatsreport ← HTML版格納ディレクトリ作成
    # vi /etc/httpd/conf.d/awstatsreport.conf ← HTML版格納ディレクトリアクセス設定ファイル作成
    
    Alias /awstatsreport "/var/www/awstatsreport"
    
  2. ) awstats.plアクセス制限
    # vi /etc/httpd/conf.d/awstats.conf ← Apache用AWStats設定ファイル編集
    
    以下を最終行へ追加
    <Files "awstats.pl">
        Order deny,allow
        Deny from all
        Allow from 127.0.0.1
        Allow from 192.168.1.0/24 ← 内部ネットワークアドレスを指定
    </Files>
    
  3. ) Apache設定反映
    # /etc/rc.d/init.d/httpd reload  ←  Apache設定再読込み
    httpd を再読み込み中:                                      [  OK  ]
    
  4. ) アクセス統計ページHTML版作成
    # vi awstatsreport.sh ← アクセス統計ページHTML版作成スクリプト作成
    
    #!/bin/bash
    
    site=$1
    dir=$2
    
    reportbuild() {
        # AWStatsデータベース更新中断時ロックファイル残存対処(ここから)
        if [ -f /tmp/awstats.$site.lock ]; then
            ps -p `cat /tmp/awstats.$site.lock |awk '{print $6}'`
            if [ $? -ne 0 ]; then
                echo /tmp/awstats.$site.lock removed >&2
                rm -f /tmp/awstats.$site.lock 
            fi
        fi
        # AWStatsデータベース更新中断時ロックファイル残存対処(ここまで)
        `rpm -ql awstats|grep "awstats_buildstaticpages\.pl"` \
        -awstatsprog="`rpm -ql awstats|grep "awstats\.pl"`"\
        -config=$site -update -lang=jp -dir=$dir \
        -year=$YEAR -month=$MONTH -builddate=$YEAR$MONTH
        if [ "$YEAR$MONTH" = $(date +%Y%m) ]; then
            mv $dir/awstats.$site.$YEAR$MONTH.html $dir/index.html
        else
            mv $dir/awstats.$site.$YEAR$MONTH.html $dir/$YEAR$MONTH.html
        fi
    }
    
    ls $dir/* > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        YEAR=`date --date '1 days ago' +%Y`
        MONTH=`date --date '1 days ago' +%m`
        reportbuild
    else
        DirData=`grep ^DirData /etc/awstats/awstats.model.conf|awk -F= '{print $2}'|tr -d \"`
        for log in `ls $DirData/awstats*.$site.txt`
        do
            YEAR=`echo $log|cut -d / -f 5|cut -d . -f 1|sed -e 's/awstats..\([^ ]*\)/\1/p' -e d`
            MONTH=`echo $log|cut -d / -f 5|cut -d . -f 1|sed -e 's/awstats\([^ ]*\)..../\1/p' -e d`
            reportbuild
        done
    fi
    
    # chmod 700 awstatsreport.sh ← アクセス統計ページHTML版作成スクリプトへ実行権限付加
    # ./awstatsreport.sh centos54.com /var/www/awstatsreport
      ← アクセス統計ページHTML版作成スクリプト実行
    
  5. ) アクセス統計ページHTML版確認
    http://Webサーバー名/awstatsreport/へアクセスして今月分のアクセス統計ページが表示されることを確認する。
    過去のアクセス統計ページはhttp://Webサーバー名/awstatsreport/年月(YYYYMM).html(例:http://Webサーバー名/awstatsreport/200601.html)へアクセスする。
  6. ) アクセス統計ページHTML版作成定期自動実行設定
    # echo "00 00 * * * root /root/awstatsreport.sh centossrv.com /var/www/awstatsreport > /de
    v/null" > /etc/cron.d/awstatsreport ← cron登録
    
    毎日00:00にアクセス統計ページHTML版作成スクリプト実行

最終更新のRSS
Last-modified: 2014-03-11 (火) 01:59:57 (3700d)