#contents
*AWStats [#qe8e9838]
apache のアクセス解析ソフト

AWStatsは、httpdのアクセスログを解析し、視覚的にブラウザで閲覧することが出来る形に整形するPerlのスクリプト

 本家サイト http://awstats.sourceforge.net/~
  2006-04-07現在 最新安定板 ver6.5

 完全日本語版 http://www.bflets.dyndns.org/Tools/AWStatsJpn.html~
  2006-04-07現在 ver6.5の日本語修正版

 本家「AWStats」もVer.4.0から正式に日本語に対応したことになってるが、非常にお粗末。~
 より完璧な日本語対応を目指して修正を行ったものを公開しているすばらしい人がいたので、
 使わしてもらう。

*Jcode.pm のインストール [#s3ec162c]
 オリジナルログ内のURLをデコードするために必要

 参照~
 http://openlab.jp/Jcode/index-j.html~
 http://www.mytools.net/memo/JcodePM.html

 「Jcode-0.xx.tar.gz」をダウンロード~
 http://www.ring.gr.jp/pub/lang/perl/CPAN/modules/by-module/Encode/DANKOGAI/

 インストール
 # tar zxvf Jcode-0.88.tar.gz
 # cd Jcode-0.88
 # perl Makefile.PL; make; make install

 確認
 # perl -MJcode -e'print $Jcode::VERSION'
 0.88

*nkfインストール [#p12bb0a8]
 「nkf206a.tar.gz」をダウンロード~
 http://sourceforge.jp/projects/nkf/

 インストール
 # tar zxvf nkf206a.tar.gz
 # cd nkf206
 # make
 # cp nkf /usr/bin/
 # rm -rf nkf206
 # rm -f nkf206a.tar.gz

*環境確認 [#v4d459b7]
-perlのバージョンが5.8.0以降である
 # perl -v
 This is perl, v5.8.5 built for i386-linux-thread-multi
-perlの場所
 # which perl
 /usr/bin/perl
-WWWサーバのCGIディレクトリのパス~
 本文では /usr/local/apache/cgi-bin とする
-Apacheのアクセスログのパス~
 本文では /usr/local/apache/logs/access_log とする
-WWWサーバのルートディレクトリ~
 本文では /home/hoge/public_html とする
-Apacheのログフォーマットがcombinedである
 # vi /usr/local/apache/conf/httpd.conf
 httpd.conf
 CustomLog /usr/local/apache/logs/access_log combined

*AWStats 6.5完全日本語版 インストール [#z34653df]
 参考~
 http://www.akenotori.jp/soft/26.html#SEC9~
 http://cyberam.dip.jp/linux_server/log/awstats60_main.html~
 http://honana.com/awstats~

-「AWStats65_Jpn.tar.gz」をダウンロード~
 http://www.bflets.dyndns.org/Tools/AWStatsJpn.html

-展開
 # tar zxvf AWStats65_Jpn.tar.gz
 # cd AWStats65_Jpn

-インストールディレクトリの準備~
 イメージファイルディレクトリ
 # mkdir /home/hoge/public_html/awstats
 # mkdir /home/hoge/public_html/awstats/images
 AWStatsインストールディレクトリ
 # mkdir /usr/local/apache/cgi-bin/awstats

-インストール
 # cd /usr/local/src/AWStats65_Jpn/wwwroot/
 # cp -rf cgi-bin/* /usr/local/apache/cgi-bin/awstats/
 # cp -rf icon/* /home/qrpnt/public_html/awstats/images/

-設定
 # cd /usr/local/apache/cgi-bin/awstats/
 # nkf -ed awstats.model_jp.conf > awstats.www.hoge.jp.conf
 # vi awstats.www.hoge.jp.conf
 awstats.www.hoge.jp.conf
 LogFile="/usr/local/apache/cgi-bin/awstats/access_log.utf8"
 SiteDomain="www.hoge.jp"
 DNSLookup=1
 DirCgi="/cgi-bin/awstats"
 DirIcons="/awstats/images"
 DetailedReportsOnNewWindows=0
 Lang="jp"

-ログファイル作成
 # /usr/local/apache/cgi-bin/awstats/utf8_decode.pl  </usr/local/apache/logs/access_log >  /usr/local/apache/cgi-bin/awstats/access_log.utf8

-手動でアクセス解析実行
 # cd /usr/local/apache/cgi-bin/awstats
 # perl awstats.pl -config=www.hoge.jp -update
 正常に終了していれば、処理結果が以下のような形で表示されます。
 Lines in file: 225730
 Found 5 dropped records,
 Found 124 corrupted records,
 Found 0 old records,
 Found 225601 new records.

-静的html生成
 # /usr/local/apache/cgi-bin/awstats.pl -config=www.hoge.jp -output -staticlink > /home/hoge/public_html/awstats/index.html

 ブラウザでアクセス解析ファイルにアクセスしてみる~
 http://www.hoge.jp/awstats/

*定期的に実行 [#nec1d314]
 # cd /usr/local/src/AWStats65_Jpn/
 # nkf -ed awstats.cron > /usr/local/apache/cgi-bin/awstats/awstats.cron

awstats.cron
 #! /usr/bin/perl
 
 # 完全日本語版AWStats用サンプルcronファイル
 #
 # ファイルパス等は自分の環境に合わせて書き直して下さい。
 
 # httpのログファイルをデコードした上で一時ディレクトリに格納します。
 # access_log.utf8は一時的にデコードしたログを保存するファイルの名前です。
 
 system("/usr/local/apache/cgi-bin/awstats/utf8_decode.pl <  /usr/local/apache/logs/access_log >  /usr/local/apache/cgi-bin/awstats/access_log.utf8");
 
 # ログファイルが複数の場合、ファイル数の分行う
 #system("/usr/local/apache/cgi-bin/awstats/utf8_decode.pl < /usr/local/apache/logs/ssl_access_log >     #/usr/local/apache/cgi-bin/awstats/access_log.utf8");
 
 # awstats.plが切り詰めないオリジナルのログを切り詰めます。
 #open(LOG,"+</usr/local/apache/logs/access_log");
 #truncate(LOG,0);
 #close(LOG);
 
 #open(LOG,"+</usr/local/apache/logs/ssl_access_log");
 #truncate(LOG,0);
 #close(LOG);
 
 # Ver.5.0から、更新処理を別途走らせる必要ができたらしい....
 system("/usr/local/apache/cgi-bin/awstats/awstats.pl -config=www.hoge.jp -update > /dev/null");
 
 # awstats.plを走らせ、htmlファイルを出力します。
 # 必須ではありませんが、awstats.plを直接参照させるよりも、htmlに出力して表示す
 # る方が、良好なパフォーマンスが得られます。
 system("/usr/local/apache/cgi-bin/awstats/awstats.pl -config=www.hoge.jp -output -staticlink > /home/hoge/public_html/awstats/index.html");
 
 # アクセス可能になるように出力したhtmlファイルの権限を変更
 system("chgrp hoge /home/hoge/public_html/awstats/index.html");
 system("chown hoge /home/hoge/public_html/awstats/index.html");

パーミッションを変更
 # chmod 700 /usr/local/apache/cgi-bin/awstats/awstats.cron

cron でこのスクリプトを自動実行
(例では30分毎に実行)
 # crontab -e
 */30 * * * * perl /usr/local/apache/cgi-bin/awstats/awstats.cron

*AWStatsとlogrotateの問題点解決 [#w33fc0ea]
Apacheのaccess_logをlogrotateによりローテーションしている場合に問題が発生する。

例えば4時10分にaccess_logの ログローテーションが行われるとすると~
「定期的に実行する」で紹介したスクリプトは30時間おきに実行されるので~
4時に実行されたとして次に実行されるのは4時30分であるため、~
4時から4時10分の間にaccess_logに記録されたログはawstatsの統計情報に 反映されない。

この問題を解決するにはaccess_logをローテーションする直前にawstats.plにより統計情報をアップデートします。

/etc/logrotate.d/apacheを編集します。
 # vi /etc/logrotate.d/apache
下記をapache再起動の前に追加
 prerotate
  perl /usr/local/apache/cgi-bin/awstats/awstats.cron
 endscript

*ワームが残すログによるAWStatsのエラー回避 [#i935fba5]
WindowsのIISやWebDAVの脆弱性を突いた攻撃により、Linuxではセキュリティ上は問題にならないが、大きなログを残すためログファイルが増大化する。 またAWStatsなどでアクセスログを元に統計情報を出力している場合、エラーとなり実行できなくなる場合もある。 これらの対策として、http.confを編集します。
 # vi /usr/local/apache/conf/httpd.conf
下記を~
 <IfModule mod_setenvif.c>~
 ・・・~
 </IfModule>~
内に追記 
 SetEnvIf Request_URI "default\.ida" wormlog
 SetEnvIf Request_URI "cmd\.exe" wormlog
 SetEnvIf Request_URI "root\.exe" wormlog
 SetEnvIf Request_URI "Admin\.dll" wormlog
 SetEnvIf Request_URI "NULL\.IDA" wormlog
 SetEnvIf Request_URI "NULL\.printer" wormlog
 SetEnvIf Request_URI "mem_bin" wormlog
 SetEnvIf Request_URI "vti_bin" wormlog
 SetEnvIf Request_URI "MSADC" wormlog
CustomLog を変更
 CustomLog /usr/local/apache/logs/access_log combined env=!wormlog
 CustomLog /usr/local/apache/logs/worm_log combined env=wormlog
これにより、NimdaやCode Red、Code Blueなどによるログはaccess_logではなくworm_logに記録される。~
また、WebDAVの脆弱性を突いた攻撃によるログは大変サイズが大きく、AWStatsも正常に動作しなくなります。
 [12/Mar/2004:20:14:15 +0900] "SEARCH /\x90\x02\xb1\x02\xb1\x02\xb1\x02\xb1\x02 …
というものが延々記録されます。このログは SetEnvIf では回避できません。~
(URIが長すぎることによる414エラー(Request-URI Too Long)に対するログのほうが先に判断されるため。)~
このログをaccess_logファイルに記録しない方法は次のようにhttp.confを編集します。
 # vi /usr/local/apache/conf/httpd.conf
httpd.conf
 LogFormat "%h %l %u %t \"%!414r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
Apache再起動
 # /usr/local/apache/bin/apachectl restart


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS