一、日志拆分
1、配置apache使用cronolog:
由于实验用的apache开启了虚拟主机功能,所以以下配置都在虚拟主机中进行,这样也是为了方便对多站点的分析操作。
vim /etc/httpd/conf.d/vhosts.conf
将配置文件中的CustomLog和ErrorLog替换为下面的(由于我们要对多站点,所以这里要替换两个虚拟主机的,并且将日志存放名字做有效的区分才好)
www.test.com
CustomLog "|/usr/local/sbin/cronolog logs/www.test.com_access_log_%Y%m%d" combined
ErrorLog "|/usr/local/sbin/cronolog logs/www.test.com_access_log_error_%Y%m%d"
www.test2.com
CustomLog "|/usr/local/sbin/cronolog logs/www.test2.com_access_log_access_%Y%m%d.log" combined
ErrorLog "|/usr/local/sbin/cronolog logs/www.test2.com_error_%Y%m%d.log"
注意:这里一定要注意对两个网站的日志名称配置
2、使用logrotate对apache进行日志拆分:
/var/log/httpd/*log {
daily
dateext
copytruncate
rotate 60
missingok
notifempty
sharedscripts
postrotate
/sbin/service httpd reload > /dev/null 2>/dev/null || true
endscript
}
二、awstats配置及生成:
1、LogFile="/var/log/httpd/www.test.com_access_log-%YYYY-0%MM-0%DD-0"
LogFile="/var/log/httpd/www.test2.com_access_log-%YYYY-0%MM-0%DD-0"
对最新的数据进行分析
2、执行命令:/usr/local/awstats/tools/awstats_updateall.pl now
3、生成静态文件
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=$name -dir=/usr/local/awstats/wwwroot/awstats -awstatsprog=
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
4、针对多域名进行脚本crontab执行
#!/bin/sh
/usr/local/awstats/tools/awstats_updateall.pl now
domain_name="access.mopote.com mopote.com"
for name in $domain_name
do
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=$name -dir=/usr/local/awstats/wwwroot/awstats -awstatsprog=
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
done
crontab添加:
0 9 * * * (time /usr/local/awstats/awstats_cron.sh) &> /usr/local/awstats/logs/
awstats.log
页面访问:
http://localhost/awstats/awstats.pl?config=mopote.com
三、AWStats扩展GeoIP
1、GeoIP插件整合到awstats中,实现IP反向解析归属地功能。
数据库下载地址:http://dev.maxmind.com/zh-hans/geoip/legacy/geolite/
分别下载:
1、国家数据库 2、城市数据库 3、运营商
下载后分别放置到awstats的配置文件目录下。
注: geo已经发布geolite2,但文件格式为mmdb,而不是dat格式。暂时好像awstats不支持。
2、安装相应的perl模块
perl -MCPAN -e 'install "Geo::IP"'
perl -MCPAN -e 'install "Geo::IPfree"'
perl -MCPAN -e 'install "Net::XWhois"'
perl -MCPAN -e 'install "Encode"'
perl -MCPAN -e 'install "URI::Escape"'
3、先下载安装C库
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.8.tar.gz
tar -zxf GeoIP-1.4.8.tar.gz
cd GeoIP-1.4.8/
./configure --prefix=/usr/local/GeoIP
make && make install
由于编译GeoIP时自定义了安装路径,所以这里需要加载关联GeoIP模块库
# echo '/usr/local/GeoIP/lib' >> /etc/ld.so.conf
# /sbin/ldconfig /etc/ld.so.conf
4、下载Perl库
wget http://geolite.maxmind.com/download/geoip/api/perl/Geo-IP-1.40.tar.gz
tar vzxf Geo-IP-1.40.tar.gz
cd Geo-IP-1.40/
perl Makefile.PL LIBS='-L/usr/local/GeoIP/lib' INC='-I/usr/local/GeoIP/include'
make && make install
5、修改配置文件 ,开启相应模块
LoadPlugin="hostinfo"
LoadPlugin="decodeutfkeys"
LoadPlugin="graphgooglechartapi"
LoadPlugin="geoip GEOIP_STANDARD /usr/local/awstats/etc/GeoIP.dat"
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/local/awstats/etc/GeoLiteCity.dat"
LoadPlugin="geoip_org_maxmind GEOIP_STANDARD /usr/local/awstats/etc/GeoIPASNum.dat"
6、更新配置
/usr/local/awstats/tools/awstats_updateall.pl now
四、对nginx日志进行分析统计
1、对nginx日志格式进行变更
log_format awstats '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
2、对awstats配置文件进行变更:
#LogFormat=1 关闭默认的apache格式
LogFormat="%host %other %other %time1 %methodurl %code %bytesd %refererquot %uaquot %otherquot"
3、用awstats生成数据文件
五、结合jawstats显示数据
配置请参考 :http://www.jawstats.com/documentation
// core config parameters
$sDefaultLanguage = "en-gb";
$sConfigDefaultView = "thismonth.all";
$bConfigChangeSites = true; #多站点切换开关
$bConfigUpdateSites = false; #页面更新开关
$sUpdateSiteFilename = "xml_update.php";
// individual site configuration
$aConfig["mopote.com"] = array(
"statspath" => "/usr/local/awstats/data/",
"updatepath" => "//usr/local/awstats/wwwroot/cgi-bin/awstats.pl/",
"siteurl" => "http://mopote.com",
"sitename" => "mopote.com",
"theme" => "default",
"fadespeed" => 250,
"password" => "sky1",
"includes" => "",
"language" => "en-gb"
);
$aConfig["access.mopote.com"] = array(
"statspath" => "/usr/local/awstats/data/",
"updatepath" => "//usr/local/awstats/wwwroot/cgi-bin/awstats.pl/",
"siteurl" => "http://access.mopote.com",
"sitename" => "access.mopote.com",
"theme" => "default",
"fadespeed" => 250,
"password" => "sky2",
"includes" => "",
"language" => "en-gb"
);