#author("2018-08-29T09:21:16+09:00","","")

#author("2018-08-29T09:22:17+09:00","","")

[[CentOS7]]

*バーチャルホスト用アクセスログ解析(AWStats) [#o0ff98e5]
''[[AWStats>http://awstats.sourceforge.net/]]''を利用して[[バーチャルホスト>バーチャルホスト設定(Apache)]]のアクセスログを解析し、バーチャルホスト用へのアクセス統計をWebブラウザから参照できるようにする。

なお、ここでの環境条件は以下のとおりとする。

-バーチャルホスト名:usage.com
-バーチャルホストアクセスログ:/var/log/httpd/usage.com-access_log
-バーチャルホストドキュメントルート:/var/www/html/usage.com/
-バーチャルホストアクセス解析結果出力先:/var/www/html/usage.com/awstatsreport

&color(red){※[[バーチャルホスト>バーチャルホスト設定(Apache)]]、[[AWStats>AWStatsインストール]]導入済であること};



*AWStats設定 [#idc023f0]
|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# cp /etc/awstats/awstats.model.conf /etc/awstats/awstats.usage.com.conf &color(lime){← 設定ファイルをサンプルよりコピー};|
|[root@localhost ~]# vi /etc/awstats/awstats.usage.com.conf &color(lime){← 設定ファイル編集};|
|# "LogFile" contains the web, ftp or mail server log file to analyze. &br; # Possible values: A full path, or a relative path from awstats.pl directory. &br; # Example: "/var/log/apache/access.log" &br; # Example: "../logs/mycombinedlog.log" &br; # You can also use tags in this filename if you need a dynamic file name &br; # depending on date or time (Replacement is made by AWStats at the beginning &br; # of its execution). This is available tags : &br; #   %YYYY-n  is replaced with 4 digits year we were n hours ago &br; #   %YY-n    is replaced with 2 digits year we were n hours ago &br; #   %MM-n    is replaced with 2 digits month we were n hours ago &br; #   %MO-n    is replaced with 3 letters month we were n hours ago &br; #   %DD-n    is replaced with day we were n hours ago &br; #   %HH-n    is replaced with hour we were n hours ago &br; #   %NS-n    is replaced with number of seconds at 00:00 since 1970 &br; #   %WM-n    is replaced with the week number in month (1-5) &br; #   %Wm-n    is replaced with the week number in month (0-4) &br; #   %WY-n    is replaced with the week number in year (01-52) &br; #   %Wy-n    is replaced with the week number in year (00-51) &br; #   %DW-n    is replaced with the day number in week (1-7, 1=sunday) &br; #                              use n=24 if you need (1-7, 1=monday) &br; #   %Dw-n    is replaced with the day number in week (0-6, 0=sunday) &br; #                              use n=24 if you need (0-6, 0=monday) &br; #   Use 0 for n if you need current year, month, day, hour... &br; # Example: "/var/log/access_log.%YYYY-0%MM-0%DD-0.log" &br; # Example: "C:/WINNT/system32/LogFiles/W3SVC1/ex%YY-24%MM-24%DD-24.log" &br; # You can also use a pipe if log file come from a pipe : &br; # Example: "gzip -d </var/log/apache/access.log.gz &#124;" &br; # If there is several log files from load balancing servers : &br; # Example: "/pathtotools/logresolvemerge.pl *.log &#124;" &br; # &br; LogFile="/var/log/httpd/access_log" &br; &color(lime){''↓''}; &br; LogFile="/var/log/httpd/usage.com-access_log" &color(lime){← バーチャルホストアクセスログを指定}; &br;  &br; # "SiteDomain" must contain the main domain name, or the main intranet web &br; # server name, used to reach the web site. &br; # If you share the same log file for several virtual web servers, this &br; # parameter is used to tell AWStats to filter record that contains records for &br; # this virtual host name only (So check that this virtual hostname can be &br; # found in your log file and use a personalized log format that include the &br; # %virtualname tag). &br; # But for multi hosting a better solution is to have one log file for each &br; # virtual web server. In this case, this parameter is only used to generate &br; # full URL's links when ShowLinksOnUrl option is set to 1. &br; # If analysing mail log, enter here the domain name of mail server. &br; # Example: "myintranetserver" &br; # Example: "www.domain.com" &br; # Example: "ftp.domain.com" &br; # Example: "domain.com" &br; # &br; SiteDomain="localhost.localdomain" &br; &color(lime){''↓''}; &br; SiteDomain="usage.com" &color(lime){← Webサーバー名を指定}; &br;  &br; # If you want to have hosts reported by name instead of ip address, AWStats &br; # need to make reverse DNS lookups (if not already done in your log file). &br; # With DNSLookup to 0, all hosts will be reported by their IP addresses and &br; # not by the full hostname of visitors (except if names are already available &br; # in log file). &br; # If you want/need to set DNSLookup to 1, don't forget that this will reduce &br; # dramatically AWStats update process speed. Do not use on large web sites. &br; # Note: Reverse DNS lookup is done on IPv4 only (Enable ipv6 plugin for IPv6). &br; # Note: Result of DNS Lookup can be used to build the Country report. However &br; # it is highly recommanded to enable the plugin 'geoipfree' or 'geoip' to &br; # have an accurate Country report with no need of DNS Lookup. &br; # Possible values: &br; # 0 - No DNS Lookup &br; # 1 - DNS Lookup is fully enabled &br; # 2 - DNS Lookup is made only from static DNS cache file (if it exists) &br; # Default: 2 &br; # &br; DNSLookup=2 &br; &color(lime){''↓''}; &br; DNSLookup=1 &color(lime){← アクセス元の名前解決を行う(アクセス元をIPアドレスではなくホスト名で表示する)}; &br;  &br; # Plugin: GeoIPfree &br; # Perl modules required: Geo::IPfree version 0.2+ (from Graciliano M.P.) &br; # Country chart is built from an Internet IP-Country database. &br; # This plugin is useless for intranet only log files. &br; # Note: You must choose between using this plugin (need Perl Geo::IPfree &br; # module, database less up to date) or the GeoIP plugin (need Perl Geo::IP &br; # module from Maxmind, database more up to date). &br; # Note: Activestate provide a corrupted version of Geo::IPfree 0.2 Perl &br; # module, so install it from elsewhere (from www.cpan.org for example). &br; # This plugin reduces AWStats speed of 10% ! &br; # &br; #LoadPlugin="geoipfree" &br; &color(lime){''↓''}; &br; LoadPlugin="geoipfree" &color(lime){← コメント解除(アクセス元国情報を詳細に取得)};|



*AWStatsデータベース作成 [#v078f6a7]
**AWStatsデータベース初期作成 [#hd4396b4]
''Apache''のバーチャルホスト分のログファイルを過去分も含めて全て''AWStats''のデータベースに取り込む

|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# vi awstatsinit.sh &color(lime){← ''AWStats''データベース初期作成スクリプト作成};|
|#!/bin/sh &br;  &br; &color(lime){# データベース作成先ディレクトリ作成}; &br; mkdir -p `grep ^DirData /etc/awstats/awstats.$1.conf &#124; awk -F= '{print $2}'&#124;tr -d \"` &br;  &br; &color(lime){# AWStats設定ファイルよりApacheログファイル名取得}; &br; logfile=`grep ^LogFile /etc/awstats/awstats.$1.conf&#124;sed -e 's/LogFile="\([^ ]*\)"/\1/p' -e d` &br;  &br; &color(lime){# 過去ログを古い順に処理}; &br; grep dateext /etc/logrotate.conf > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br;     &color(lime){# 過去ログファイル名に日付が設定されている場合}; &br;     for log in `ls $logfile-*&#124;sort` &br;     do &br;         `rpm -ql awstats&#124;grep "awstats\.pl"` \ &br;         -config=$1 -update -logfile=$log &br;     done &br; else &br;     &color(lime){# 過去ログファイル名に通番が設定されている場合}; &br;     for log in `ls $logfile.*&#124;sort -r` &br;     do &br;         `rpm -ql awstats&#124;grep "awstats\.pl"` \ &br;         -config=$1 -update -logfile=$log &br;     done &br; fi &br;  &br; &color(lime){# 現在ログ処理}; &br; for log in `ls $logfile&#124;sort` &br; do &br;     `rpm -ql awstats&#124;grep "awstats\.pl"` \ &br;     -config=$1 -update -logfile=$log &br; done|
|[root@localhost ~]# sh awstatsinit.sh usage.com &color(lime){← ''AWStats''データベース初期作成スクリプト実行}; &br; |
|&color(lime){※ログ量により時間がかかる}; &br; Update for config "/etc/awstats/awstats.usage.com.conf" &br; With data in log file "/var/log/httpd/usage.com-access_log.4"... &br; Phase 1 : First bypass old records, searching new record... &br; Searching new records from beginning of log file... &br; Phase 2 : Now process new records (Flush history on disk after 20000 hosts)... &br; Jumped lines in file: 0 &br; Parsed lines in file: 123047 &br;  Found 1821 dropped records, &br;  Found 0 corrupted records, &br;  Found 0 old records, &br;  Found 121226 new qualified records. &br; Update for config "/etc/awstats/awstats.usage.com.conf" &br; With data in log file "/var/log/httpd/usage.com-access_log.3"... &br; Phase 1 : First bypass old records, searching new record... &br; Direct access to last remembered record has fallen on another record. &br; So searching new records from beginning of log file... &br; Phase 2 : Now process new records (Flush history on disk after 20000 hosts)... &br; Jumped lines in file: 0 &br; Parsed lines in file: 130918 &br;  Found 1700 dropped records, &br;  Found 0 corrupted records, &br;  Found 0 old records, &br;  Found 129218 new qualified records. &br; Update for config "/etc/awstats/awstats.usage.com.conf" &br; With data in log file "/var/log/httpd/usage.com-access_log.2"... &br; Phase 1 : First bypass old records, searching new record... &br; Direct access to last remembered record is out of file. &br; So searching it from beginning of log file... &br; Phase 2 : Now process new records (Flush history on disk after 20000 hosts)... &br; Jumped lines in file: 0 &br; Parsed lines in file: 128865 &br;  Found 1680 dropped records, &br;  Found 0 corrupted records, &br;  Found 0 old records, &br;  Found 127185 new qualified records. &br; Update for config "/etc/awstats/awstats.usage.com.conf" &br; With data in log file "/var/log/httpd/usage.com-access_log.1"... &br; Phase 1 : First bypass old records, searching new record... &br; Direct access to last remembered record is out of file. &br; So searching it from beginning of log file... &br; Phase 2 : Now process new records (Flush history on disk after 20000 hosts)... &br; Jumped lines in file: 0 &br; Parsed lines in file: 115202 &br;  Found 1532 dropped records, &br;  Found 0 corrupted records, &br;  Found 0 old records, &br;  Found 113670 new qualified records. &br; Update for config "/etc/awstats/awstats.usage.com.conf" &br; With data in log file "/var/log/httpd/usage.com-access_log"... &br; Phase 1 : First bypass old records, searching new record... &br; Direct access to last remembered record is out of file. &br; So searching it from beginning of log file... &br; Phase 2 : Now process new records (Flush history on disk after 20000 hosts)... &br; Jumped lines in file: 0 &br; Parsed lines in file: 21746 &br;  Found 312 dropped records, &br;  Found 0 corrupted records, &br;  Found 0 old records, &br;  Found 21434 new qualified records.|
|[root@localhost ~]# rm -f awstatsinit.sh &color(lime){← ''AWStats''データベース初期作成スクリプト削除};|

&color(red){※以後の''AWStats''データベースの更新は''/etc/cron.daily/awstats''により毎日自動実行される};



*AWStats確認 [#b582bb6b]
http://usage.com/awstats/awstats.pl へアクセスしてアクセス統計ページが表示されることを確認



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

**アクセス統計ページ''HTML''版格納ディレクトリ設定 [#tae42412]
|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# mkdir /var/www/html/usage.com/awstatsreport &color(lime){← ''HTML''版格納ディレクトリ作成};|
|[root@localhost ~]# vi /etc/httpd/conf.d/virtualhost-usage.com.conf &color(lime){← バーチャルホスト設定ファイル編集};|
|<VirtualHost *:80> &br;     ServerName usage.com &br;     DocumentRoot /var/www/html/usage.com &br;     ErrorLog logs/virtual-error_log &br;     CustomLog logs/usage.com-access_log combined env=!no_log &br;     Alias /awstatsreport /var/www/html/usage.com/awstatsreport &color(lime){← 追加}; &br; </VirtualHost>|
|[root@localhost ~]# systemctl reload httpd &color(lime){← Webサーバー設定反映};|



**awstats.plアクセス制限 [#f02d4a67]
&color(red){※[[Apacheアクセスログ解析(AWStats)>AWStatsインストール]]で既に設定済};



**アクセス統計ページHTML版作成 [#f53f6e5e]
&color(red){※アクセス統計ページHTML版作成スクリプトは[[Apacheアクセスログ解析(AWStats)>AWStatsインストール]]で既に作成済};

|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# ./awstatsreport.sh usage.com /var/www/html/usage.com/awstatsreport &color(lime){← アクセス統計ページHTML版作成スクリプト実行};|



**アクセス統計ページHTML版確認 [#b48edce6]
http://usage.com/awstatsreport/ へアクセスして今月分のアクセス統計ページが表示されることを確認

過去のアクセス統計ページは http://usage.com/awstatsreport/年月(YYYYMM).html (例:http://usage.com/awstatsreport/200601.html)へアクセスする

過去のアクセス統計ページは http://usage.com/awstatsreport/年月(YYYYMM).html (例: http://usage.com/awstatsreport/200601.html )へアクセスする




**アクセス統計ページ'''HTML''版作成定期自動実行設定 [#bd9f69d2]
|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# echo "00 00 * * * root /root/awstatsreport.sh usage.com /var/www/html/usage.com/awstatsreport > /dev/null" >> /etc/cron.d/awstatsreport &color(lime){← cron登録};|
|&color(lime){毎日00:00にアクセス統計ページ''HTML''版作成スクリプト実行};|

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS