AWStatsでログ解析
zopeとは直接関係ありませんが、Apache 2が出力したバーチャルホストごとのアクセスログをAWStatsを使用して解析する方法について記述します。
Apache 2のログ出力設定 によって、バーチャルホストごとにアクセスログが出力されるようになりました。ここでは、その出力されたログを解析する方法について記述します。
なお、アクセスログは以下のように出力されるという前提で話を進めます。
- www.example.com へのアクセスログが /var/log/httpd/example_log.YYYYMMDD というファイルに出力されます。
- blog.example.com へのアクセスログが /var/log/httpd/blog_log.YYYYMMDD という形式で出力されます。
- YYYYMMDDはそのログの日付が入ります。
目次
インストール[install]
アクセスログの解析には AWStats というログ解析ソフトを使用します。ここでは、AWStatsのインストール手順を記述します。
最初に、AWStatsのアーカイブファイルをダウンロードして /usr/local/awstats に展開します。
% cd /usr/local/ % sudo su # tar xfz awstats-6.3.tgz # mv awstats-6.3 awstats
次に、設定スクリプト awstats_configure.pl を実行します。いくつか質問されるので設定値を入力します。
# cd awstats/tool # perl awstats_configure.pl : : 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): > /etc/apache2/conf/httpd.conf ← Apacheの設定ファイルの場所 : : -----> 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 ←設定ファイルを生成する-----> 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.example.com ←サーバの名前を指定
-----> 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): > ←デフォルトでいいので何も入力しない : :
設定[setting]
設定スクリプトを実行することにより、Apache 2の設定ファイル /etc/apache2/conf/httpd.conf に AWStats 用の設定が記述されます。
また、www.example.com 用の設定ファイルが /etc/awstats/awstats.www.example.com.conf に作成されます。
次に、この設定ファイルを修正します。修正個所は以下の3点です。
- LogFile ログファイル名を日付付きの書式にする
- DNSLookup IPアドレスからDNSの逆引きを行なう
- Lang 言語を日本語とする
# cd /etc/awstats # cp awstats.www.example.com.conf org # vi awstats.www.example.com.conf # diff awstats.www.example.com.conf org # 51c51 < LogFile="/var/log/httpd/example_log.%YYYY-1%MM-1%DD-1" --- > LogFile="/var/log/httpd/mylog.log" 182c182 < DNSLookup=1 --- > DNSLookup=2 862c862 < Lang="jp" --- > Lang="auto"
ログ解析[analyze]
AWStatsを実行する準備が整ったので、過去のログファイルに対して以下のコマンドを実行して解析を行ないます。その前に、ログ解析の結果を保存するディレクトリ /var/lib/awstats を作成します。
# mkdir /var/lib/awstats # tcsh # foreach log ( /var/log/httpd/example_log.* ) foreach? sudo ./awstats.pl -config=www.example.com -LogFile=$log foreach? end
http://127.0.0.1/awstats/awstats.pl?config=www.example.com にアクセスして、ログの解析結果が表示されることを確認します。
blog.example.com用設定[blog]
www.example.com用の設定ファイルをコピーして、blog.example.com用の設定ファイルを作成します。
# cd /etc/awstats # cp awstats.www.example.com.conf awstats.blog.example.com.conf # vi awstats.blog.example.com.conf # diff awstats.www.example.com.conf awstats.blog.example.com.conf 51c51 < LogFile="/var/log/httpd/example_log.%YYYY-1%MM-1%DD-1" --- > LogFile="/var/log/httpd/blog_log.%YYYY-1%MM-1%DD-1" 147c147 < SiteDomain="www.example.com" --- > SiteDomain="blog.example.com"
その後同様にログ解析を行ないます。
# tcsh # foreach log ( /var/log/httpd/blog_log.* ) foreach? sudo ./awstats.pl -config=blog.example.com -LogFile=$log foreach? end
http://127.0.0.1/awstats/awstats.pl?config=blog.example.com にアクセスして、ログの解析結果が表示されることを確認します。
自動実行の設定[cron]
今後はアクセスログの解析を自動で行なうようにするために、cronの設定を行ないます。一時間毎にログ解析を実行するように、/etc/cron.hourly/ に以下のスクリプトを awstats という名前で作成します。なお、スクリプトに実行権限(+x)をつけることを忘れないようにしてください。
# cd /etc/cron.hourly # vi awstats # chmod +x awstats
#!/bin/sh cd /usr/local/awstats/wwwroot/cgi-bin ./awstats.pl -config=www.example.com > /dev/null ./awstats.pl -config=blog.example.com > /dev/null
以上で、ログ解析の設定は終わりです。あとは定期的にログ解析の結果をチェックし、問題がないかをなどを確認します。