[root@test src]# wget http://prdownloads.sourceforge.net/awstats/awstats-6.95.tar.gz [root@test src]# tar zxvf awstats-6.95.tar.gz [root@test src]# mv awstats-6.95 /usr/local/awstats [root@test src]# mkdir /var/lib/awstats [root@test awsta
[root@test src]# wget http://prdownloads.sourceforge.net/awstats/awstats-6.95.tar.gz
[root@test src]# tar zxvf awstats-6.95.tar.gz
[root@test src]# mv awstats-6.95 /usr/local/awstats
[root@test src]# mkdir /var/lib/awstats
[root@test awstats]# mkdir -m 755 /usr/local/awstats/data/
[root@test src]# cd /usr/local/awstats/
[root@test awstat]# perl /usr/local/awstats/tools/awstats_configure.pl
只填写域名,其它选项全部y或默认
[root@test awstats]# vi/etc/awstats/awstats.www.123.com.conf
修改日志文件路径:
LogFile="/usr/local/awstats/tools/logresolvemerge.pl/home/tool/access.20100817.11*.log |"
此为用awstats脚本自动合并分析所有/home/tool/access.20100817.11*.log日志
如果日志使用tar -zcvf 压缩,logresolvemerge.pl会自动调用zcat
也可以同时列出多个日志文件,如以下格式:
LogFile="/usr/local/awstats/tools/logresolvemerge.pl 1.log2.log |"
合并日志也可用以下方式:
cat a.log b.log c.log | sort -m -t " " –k 4 > log
将三个日志以空格为分隔符,对第四列进行排序,结果保存在log
添加GeoIP插件使awstats可以显示用户地理位置
cpan install Geo::IP
[root@test awstats]# cd /usr/local/src/
[root@test src]# wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
[root@test src]# tar zxvf GeoIP.tar.gz
[root@test src]# cd GeoIP-1.4.6/
[root@test GeoIP-1.4.6]# ./configure && make &&make install
[root@test src]# wget http://geolite.maxmind.com/download/geoip/api/perl/Geo-IP-1.38.tar.gz
[root@test src]# tar zxvf Geo-IP-1.38.tar.gz
[root@test src]#cd Geo-IP-1.38
[root@test Geo-IP-1.38]# perl Makefile.PL && make&& make install
[root@test src]# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
[root@test src]# gunzip GeoLiteCity.dat.gz
[root@test src]# cp GeoLiteCity.dat /usr/local/share/GeoIP/
[root@test src]# vi /etc/awstats/awstats.www.dayu360.com.conf
启用或添加以下选项:
LoadPlugin="tooltips"
LoadPlugin="decodeutfkeys"
LoadPlugin="geoip GEOIP_STANDARD/usr/local/share/GeoIP/GeoIP.dat"
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD/usr/local/share/GeoIP/GeoLiteCity.dat"
[root@test awstats]# perl/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -config=www.dayu360.com –update
更新awstats数据库(即web页面显示的内容)
当登陆到awstats的web界面时出现提示:
Error: Access to statistics is only allowed from anauthenticated session to authenticated users.
则需把/etc/awstats/awstats.www.123.com.conf中:
AllowAccessFromWebToAuthenticatedUsersOnly=0
访问web页:
http://192.168.0.80/awstats/awstats.pl?config=www.123.com
以上完成了基本设置,下面进行一些高级设置:
(1)不同的日志格式对应不同的日志格式的设置:
一、若对运行在apache上的网站产生的日志(combined格式)进行分析则在/etc/awstats/awstats.www.123.com.conf文件中配置:
LogType=W
LogFormat=1
二、若要对运行在nginx上的网站产生的日志进行分析,首先需要在nginx的配置文件nginx.conf中更改产生日志的格式:
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status"$body_bytes_sent "$http_referer" '
'"$http_user_agent"$http_x_forwarded_for';
access_log logs/access.log main;
然后在/etc/awstats/awstats.www.123.com.conf文件中配置:
LogType=W
LogFormat=1
三、若nginx日志格式为:
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status"$body_bytes_sent "$http_referer" '
'"$http_user_agent"';
access_log logs/access.log main;
则在/etc/awstats/awstats.www.123.com.conf文件中配置:
LogType=W
LogFormat=%host %other %logname%time1 %methodurl %code %bytesd %refererquot %uaquot"
注:网上某些文章的nginx.conf中log_format后面加了$http_x_forwarded_for(来源ip),然而其在/etc/awstats/awstats.www.123.com.conf文件中配置还是:
LogFormat=%host %other%logname %time1 %methodurl %code %bytesd %refererquot %uaquot",则因为缺少对应关系而使得awstats报错。
可以通过修改nginx的日志格式(把$http_x_forwarded_for去掉)即可。
最后注意:通过测试发现%methodurl字段必须对应GET的日志,若有POST的日志或“-”则会报错。
Sftp自动上传日志:
Apache服务器:
useradd -u 1000 httplog -d /home/httplog/
passwd httplog
awstat 服务器:
创建get日志的脚本
vi /usr/local/shell/get_weblog.sh
#!/bin/sh
export WEB1=172.16.0.115;
export WEB2=172.16.0.116;
export WEB3=172.16.0.117;
export USER=httplog;
exportPASS="bdGhS3EI4dSH86QBXjV9";
exportCPATH="/home/httpdlog/`date +%Y`/`date --date='1 days ago' +%m`/"
exportweb1="web1.access.`date --date='1 days ago' +%Y%m%d`.log"
exportweb2="web2.access.`date --date='1 days ago' +%Y%m%d`.log"
exportweb3="web3.access.`date --date='1 days ago' +%Y%m%d`.log"
exportSPATH="/opt/weblogs/`date +%Y`/`date --date='1 days ago' +%m`/"
echo "sftp start";
if [ ! -d $SPATH ]; then
mkdir -p $SPATH; fi
lftp -u ${USER},${PASS}sftp://${WEB1} <
lcd $SPATH
get $CPATH/$web1
open sftp://${USER}:${PASS}@$WEB2
get $CPATH/$web2
open sftp://${USER}:${PASS}@$WEB3
get $CPATH/$web3
bye
EOF
echo "GET WEB LOGDONE!!!";
创建awstat更新脚本
vi /usr/local/shell/awstat-update.sh
#!/bin/bash
configfiles="www"
#configfiles="www bbsbuy"
domain=123.com
command=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
for conf in ${configfiles};
do
${command} -update-config=${conf}.${domain} -Databasebreak=month;
${command} -update-config=${conf}.${domain} -Databasebreak=day;
done
日历显示cgi
vi/usr/local/awstat/wwwroot/cgi-bin/calendar.pl
#!/usr/bin/perl
@domain_allow=("www");
#@domain_allow=("www","prom","buy","tag","blog","member");
$site=$ENV{"QUERY_STRING"};
if ($site eq ""){
$site="www";
}else{
$reject=1;
foreach $sub_domain (@domain_allow){
if ($sub_domain eq $site){
$reject=0;
}
}
}
# The awstats config filename
$CONFIG=$site.".123.com";
### Begin program ###
@now=localtime(time);
$today_day=$now[3];
$today_month=$now[4]+1;
$today_year=$now[5]+1900;
@yesterday=localtime(time-3600*24);
$ytd_day=$yesterday[3];
$ytd_month=$yesterday[4]+1;
$ytd_year=$yesterday[5]+1900;
$lastmonth=$today_month-1;
$lastmonth_year=$today_year;
if($lastmonth<1)
{
$lastmonth=1;
$lastmonth_year=$today_year-1;
}
$lastyear=$today_year-1;
print "Content-type:text/html\n\n";
print"<body>\n";
if ($reject==1){
print "subdomain:'$site' is notaccepted, please contact admin";
}else{
print "Choose Subdomain:
foreach $subdomain (@domain_allow){
if($subdomain eq $site) {
$selected="selected='selected'";
}else{
$selected="";
}
print "
}
print"
";
print "
print "
print "
print "
print "
print "
print "\n
\n";
printCal($today_year, $today_month);
print "\n
\n";
printCal($lastmonth_year, $lastmonth);
}
print"\n