AWStats

提供: ArchWiki
2019年3月31日 (日) 14:44時点におけるHiromi-mi (トーク | 投稿記録)による版 (英語版と同期)
ナビゲーションに移動 検索に移動

AWStats - Free log file analyzer for advanced statistics より:

AWStats はウェブ・ストリーミング・ftp・メールサーバーの統計をグラフィカルに生成することができるフリーな強力かつ機能豊富なツールです。ログアナライザーとして CGI あるいはコマンドラインからログに含まれている全ての情報をグラフィカルなウェブページに表示できます。部分的な情報ファイルを使うことで巨大なログファイルを高速に処理することが可能です。解析できるログファイルは Apache ログファイル (NCSA combined/XLF/ELF ログ形式や common/CLF ログ形式), WebStar, IIS (W3C log format) あるいはウェブ・プロキシ・WAP・ストリーミングサーバー・メールサーバー・FTP サーバーなどのあらゆるサーバーツールにわたります。

インストール

awstats パッケージをインストールしてください。ウェブサーバーとして Apache を使用するときは、mod_perlAUR パッケージも必要です。

設定

Apache の mod_perl を有効化

Apache で mod_perl を有効にするには、Apache の設定ファイル (/etc/httpd/conf/httpd.conf) に以下の行を追加します:

LoadModule perl_module modules/mod_perl.so

AWStats 用にログを作成するように Apache を設定

デフォルトでは AWStats を使うには Apache のアクセスログを 'combined' で記録する必要があります。挙動を変更するのでないかぎり、アクセスログの形式を 'combined' に設定するようにしてください。Apache の設定を以下のようにします:

<VirtualHost *:80>
    ServerAdmin zxc@returnfalse.net
    DocumentRoot "/srv/http/xxx"
    ServerName www.returnfalse.net
    ErrorLog "/var/log/httpd/returnfalse-error_log"
    CustomLog "/var/log/httpd/returnfalse-access_log" combined
</VirtualHost>

重要な行は以下のところです:

CustomLog "/var/log/httpd/returnfalse-access_log" combined
警告: 既に別の形式でアクセスログを取っていた場合、AWStats はログが読み込めないとエラーを吐きます。Apache のログフォーマットを変更したら、AWStats が読み込めるように古いログファイルを削除してください。

Apache の設定で AWStats の設定を読み込む

ログフォーマットを設定したら、次は AWStats の設定ファイルを Apache に記述します。AUR のパッケージにはデフォルトの設定ファイルが付属しており、問題なく動作します。設定を自分で作成したい場合、デフォルト設定は以下の通りです:

Alias /awstatsclasses "/usr/share/webapps/awstats/classes/"
Alias /awstatscss "/usr/share/webapps/awstats/css/"
Alias /awstatsicons "/usr/share/webapps/awstats/icon/"
ScriptAlias /awstats/ "/usr/share/webapps/awstats/cgi-bin/"

<Directory "/usr/share/webapps/awstats">
    Options None
    AllowOverride None
    Require all granted
</Directory>

次のファイルを Apache のメイン設定でインクルードしてください (AUR の場合、パスは /etc/httpd/conf/extra/httpd-awstats.conf です):

Include conf/extra/httpd-awstats.conf

上記の設定を全て完了したら、Apache を再起動してから http://localhost/awstats/awstats.pl を開くことで AWStats を確認できます。

最後に、AWStats でログを読み込んで統計に変換します。

AWStats の設定

パッケージには AWStats で表示する統計を毎時間更新するスクリプトが付いています。このスクリプトは /etc/awstats にある AWStats の設定ファイルを読み込んで、そこに定義されているサイトの統計を更新します。設定ファイルを作成する代わりに、AWStats の設定ツールを使うことができます。次のコマンドを実行して指示に従ってください:

perl /usr/share/awstats/tools/awstats_configure.pl

設定ファイルを作成したら、手動で修正する箇所が一つだけ存在します。awstats_configure.pl によって作成された設定ファイルをお好きなテキストエディタで開いてください。LogFile 変数が定義されている行を探して、Apache のアクセスログのパスを設定してください (先に 'combined' 形式でログ出力すると設定したパス):

LogFile=/var/log/httpd/returnfalse-access_log

スクリプトを実行して結果を確認してください。たとえば /etc/awstats/awstats.apache.conf であれば、以下のコマンドを実行:

/usr/share/awstats/tools/awstats_buildstaticpages.pl config=apache -update -awstatsprog=/usr/share/webapps/awstats/cgi-bin/awstats.pl -dir=/srv/http/awstats
警告: 上記の設定では誰でも AWStats にアクセスできてしまいます。認証を設定することで統計情報を非公開にできます。

Nginx

使用しているウェブサーバーソフトウェアが nginx の場合、以下の手順に従ってください:

1. 上記の通り awstats をインストールしてください。以下のコマンドでフォルダとファイルの所有者は "http" に、所有グループは "http" にする必要があります:

# chown -R http:http /usr/share/webapps/awstats/

2. awstats 設定ツールを使ってサイト設定ファイルを生成してください。以下の行が正しく設定されていることを確認します:

LogFile="/var/log/nginx/access.log"
LogFormat=1

3. nginx で awstats の Perl スクリプトを動作させるため、以下のコードで /etc/nginx/cgi-bin.php スクリプトを作成:

<?php
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("pipe", "w")   // stderr is a file to write to
);
$newenv = $_SERVER;
$newenv["SCRIPT_FILENAME"] = $_SERVER["X_SCRIPT_FILENAME"];
$newenv["SCRIPT_NAME"] = $_SERVER["X_SCRIPT_NAME"];
if (is_executable($_SERVER["X_SCRIPT_FILENAME"])) {
   $process = proc_open($_SERVER["X_SCRIPT_FILENAME"], $descriptorspec, $pipes, NULL, $newenv);
   if (is_resource($process)) {
       fclose($pipes[0]);
       $head = fgets($pipes[1]);
       while (strcmp($head, "\n")) {
           header($head);
           $head = fgets($pipes[1]);
       }
       fpassthru($pipes[1]);
       fclose($pipes[1]);
       fclose($pipes[2]);
       $return_value = proc_close($process);
   } else {
       header("Status: 500 Internal Server Error");
       echo("Internal Server Error");
   }
} else {
   header("Status: 404 Page Not Found");
   echo("Page Not Found");
}
?> 

4. nginx のドメイン設定ファイルに以下のディレクティブを追加:

location ^~ /awstatsicons {
   alias /usr/share/webapps/awstats/icon/;
   access_log off;
}
location ^~ /awstatscss {
   alias /usr/share/webapps/awstats/examples/css/;
   access_log off;
}
location ^~ /awstatsclasses {
   alias /usr/share/webapps/awstats/examples/classes/;
   access_log off;
}
location ~ ^/cgi-bin/.*\.(cgi|pl|py|rb) {
   gzip off;
   fastcgi_pass  unix:/var/run/php-fpm/php-fpm.sock;
   fastcgi_index cgi-bin.php;
   fastcgi_param SCRIPT_FILENAME    /etc/nginx/cgi-bin.php;
   fastcgi_param SCRIPT_NAME        /cgi-bin/cgi-bin.php;
   fastcgi_param X_SCRIPT_FILENAME  /usr/share/webapps/awstats$fastcgi_script_name;
   fastcgi_param X_SCRIPT_NAME      $fastcgi_script_name;
   fastcgi_param QUERY_STRING       $query_string;
   fastcgi_param REQUEST_METHOD     $request_method;
   fastcgi_param CONTENT_TYPE       $content_type;
   fastcgi_param CONTENT_LENGTH     $content_length;
   fastcgi_param GATEWAY_INTERFACE  CGI/1.1;
   fastcgi_param SERVER_SOFTWARE    nginx;
   fastcgi_param REQUEST_URI        $request_uri;
   fastcgi_param DOCUMENT_URI       $document_uri;
   fastcgi_param DOCUMENT_ROOT      $document_root;
   fastcgi_param SERVER_PROTOCOL    $server_protocol;
   fastcgi_param REMOTE_ADDR        $remote_addr;
   fastcgi_param REMOTE_PORT        $remote_port;
   fastcgi_param SERVER_ADDR        $server_addr;
   fastcgi_param SERVER_PORT        $server_port;
   fastcgi_param SERVER_NAME        $server_name;
   fastcgi_param REMOTE_USER        $remote_user;
}

5. サイトの awstats ページには "http://your_domain.com/cgi-bin/awstats.pl?config=your_domain.com" からアクセスできます。任意で、nginx のサイト設定ファイルに以下の rewrite ルールを追加しても良いでしょう:

location ~ ^/awstats {
   rewrite ^ http://your_domain.com/cgi-bin/awstats.pl?config=your_domain.com;
}

上記の設定で、ブラウザのアドレスバーに "http://your_domain.com/awstats" と入力するだけで awstats のページにアクセスできるようになります。

統計情報の作成

以下のコマンドを実行することで手動でサイトの最新統計を生成できます:

/usr/share/awstats/tools/awstats_updateall.pl now -awstatsprog=/usr/share/webapps/awstats/cgi-bin/awstats.pl

上記の生成は cron で自動化できます。AWStats の cron テンプレートを見てください: /usr/share/doc/awstats-7.5/cron.hourly。ただし、logrotate を使っている場合、cron ジョブは logrotate が実行される前に起動する必要があります。logrotate によってログファイルの名前が変わって awstats からアクセスできなくなると統計情報が生成できません。/etc/logrotate.d に存在するウェブサーバーの logrotate スクリプトを使って awstats を呼び出すと良いでしょう。nginx の logrotate スクリプトの例を以下に示します。prerotate ディレクティブを追加しています:

/var/log/nginx/*log {
 daily
 missingok
 notifempty
 create 640 http log
 compress
 sharedscripts
 prerotate
   # Trigger awstats computation
   /usr/share/awstats/tools/awstats_updateall.pl now -awstatsprog=/usr/share/webapps/awstats/cgi-bin/awstats.pl
 endscript
 postrotate
    test ! -r /run/nginx.pid || kill -USR1 `cat /run/nginx.pid`
 endscript
}

GeoIP (任意)

geo ip のサポートを追加するには、 Geo::IP モジュールを cpan を利用してインストールしてください。さらなる詳細は Perl#CPAN.pm を参照してください。あるいは、AUR から perl-geoipAUR をインストールします。/etc/awstats/ にある awstats のサイト設定ファイルに以下の行を追加してください:

LoadPlugin="geoip GEOIP_STANDARD /usr/share/GeoIP/GeoIP.dat"

参照