设为首页 收藏本站
查看: 1066|回复: 0

[经验分享] Apache日志分割及分析

[复制链接]

尚未签到

发表于 2017-1-3 06:11:55 | 显示全部楼层 |阅读模式
相关软件及下载地址:
1、 cronolog-1.6.2.tar.gz
2、 awstats-6.95.tar.gz
3、 GeoIP-1.4.6.tar.gz
4、 Geo-IP-1.38.tar.gz
5、 Net-XWhois-0.90.tar.gz
6、 QQWry.Dat
7、 IP解析插件
软件说明:
Cronolog -apache日志分割
Awstats –apace日志分析主程序
GeoIP –IP地址分析,来至某个国家并显示国旗
Xwhois及后面 –IP反向解析
详细安装过程:
一、安装cronolog日志分割软件:
[iyunv@saas soft]# tar zxvf cronolog-1.6.2.tar.gz
[iyunv@saas cronolog-1.6.2]# ./configure &&make &&make install
[iyunv@saas cronolog-1.6.2]# which cronolog
/usr/local/sbin/cronolog
修改httpd.conf文件
<VirtualHost *>
ServerName www.test.cn
DocumentRoot “/opt/lampp/htdocs/newtest”
CustomLog “|/usr/local/sbin/cronolog /opt/lampp/logs/test_%Y%m%d.log” combined
ErrorLog “|/usr/local/sbin/cronolog /opt/lampp/logs/test_error_%Y%m%d.log”
</VirtualHost>
重启apache后在/opt/lampp/logs/目录下可以看到以下格式日志:
-rw-r–r– 1 root root 12021 6月 25 15:01 test_20100625.log
-rw-r–r– 1 root root 718 6月 25 15:00 test_error_20100625.log
日志分割比较简单。。至此就安装完成了。。。以后每天都会在该目录下生成当日的日志文件。
二、安装awstats主程序
[iyunv@saas local]# tar zxvf awstats-6.95.tar.gz -C /usr/local/
[iyunv@saas local]# mv awstats-6.95/ awstats
[iyunv@saas local]# mkdir /etc/awstats
[iyunv@saas local]# mkdir /var/lib/awstats
[iyunv@saas local]# chown -R apache /var/lib/awstats/
[iyunv@saas local]# chown -R apache /usr/local/awstats/
[iyunv@saas local]# /usr/local/awstats/tools/awstats_configure.pl
Enter full config file path of your Web server.
Example: /etc/httpd/httpd.conf
Example: /usr/local/apache2/conf/httpd.conf
Example: c:\Program files\apache group\apache\conf\httpd.conf
Config file path (‘none’ to skip web server setup):
Ø /opt/lampp/etc/httpd.conf #http.conf文件路径
—–> Need to create a new config file ?
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ? y
—–> Define config file name to create
What is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Your web site, virtual server or profile name:
Ø www.test.cn#生成配置文件的名字
—–> Define config file path
In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directory path to store config file(s) (Enter for default):
>配置文件放置地址 默认即可
—–> Add update process inside a scheduler
Sorry, configure.pl does not support automatic add to cron yet.
You can do it manually by adding the following command to your cron:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.test.cn
Or if you have several config files and prefer having only one command:
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue…
回车继续
A SIMPLE config file has been created: /etc/awstats/awstats.www.test.cn.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for ‘www.test.cn’ with command:
> perl awstats.pl -update -config=www.test.cn
You can also read your statistics for ‘www.test.cn’ with URL:
> http://localhost/awstats/awstats.pl?config=www.test.cn
Press ENTER to finish…
回车结束
Configure.pl配置文件,会将:
CustomLog ……………………..common
改为
CustomLog ……………………..combined #实际上面我们已经更改过来,日志切割的时候
会在apache中增加如下内容:
Alias /awstatsclasses “/usr/local/awstats/wwwroot/classes/”
Alias /awstatscss “/usr/local/awstats/wwwroot/css/”
Alias /awstatsicons “/usr/local/awstats/wwwroot/icon/”
ScriptAlias /awstats/ “/usr/local/awstats/wwwroot/cgi-bin/”

<Directory “/usr/local/awstats/wwwroot”>
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>
当然你也有可能会发现httpd.conf文件会变成这个样子:(每句后面会多出^M)
# Do NOT simply read the instructions in here without understanding^M
# what they do. They’re here only as hints or reminders. If you are unsure^M
# consult the online docs. You have been warned. ^M
#^M
# Configuration and logfile names: If the filenames you specify for many^M
# of the server’s control files begin with “/” (or “drive:/” for Win32), the^M
# server will use that explicit path. If the filenames do *not* begin^M
# with “/”, the value of ServerRoot is prepended — so “logs/foo.log”^M
# with ServerRoot set to “/opt/lampp” will be interpreted by the^M
# server as “/opt/lampp/logs/foo.log”.^M
^M
#^M
使用dos2unix命令转换下格式:
[iyunv@saas tools]# file /opt/lampp/etc/httpd.conf
/opt/lampp/etc/httpd.conf: ASCII English text, with CRLF, LF line terminators
[iyunv@saas tools]# dos2unix /opt/lampp/etc/httpd.conf
dos2unix: converting file /opt/lampp/etc/httpd.conf to UNIX format …
[iyunv@saas tools]# file /opt/lampp/etc/httpd.conf
/opt/lampp/etc/httpd.conf: ASCII English text
生成的配置文件,配置文件中大概要修改的如下:/etc/awstats/awstats
LogFile=”apache日志文件
DirData=”/var/lib/awstats”
DirCgi=”/awstats”
AllowToUpdateStatsFromBrowser=1
注:由于前面我们使用过了日志分割,固每日的日志都会跟上日期。所以在LogFile处可以写定一个日志文件名称如:”/opt/lamp/logs/Test_acc.log” 。每日凌晨的时候利用脚本将其前日的日志复制覆盖掉此文件再做分析。
现为了测试,先将今日的日志先复制一份命名为”Test_acc.log”
更新数据并测试awstats是否网页访问OK
[iyunv@saas ~]# usr/local/awstats/tools/awstats_updateall.pl now
Running ‘”/usr/local/awstats/wwwroot/cgi-bin/awstats.pl” -update -config=www.test.cn -configdir=”/etc/awstats”‘ to update config www.test.cn
Create/Update database for config “/etc/awstats/awstats.www.test.cn.conf” by AWStats version 6.95 (build 1.943)
From data in log file “/opt/lampp/logs/test_acc.log”…
Phase 1 : First bypass old records, searching new record…
Direct access after last parsed record (after line 880)
Jumped lines in file: 880
Found 880 already parsed records.
Parsed lines in file: 0
Found 0 dropped records,
Found 0 corrupted records,
Found 0 old records,
Found 0 new qualified records.
使用浏览器,察看日志统计情况:http://域名/awstats/awstats.pl?config=配置文件名。。
实例如下:
http://www.test.cn/awstats/awstats.pl?config=www.test.cn
如提示:Forbidden You don’t have permission to access /awstats/awstats.pl on this server.错误。
1、 请确认/usr/local/awstats目录权限
[iyunv@saas ~]# chmod -R 755 /usr/local/awstats
2、 检测httpd.conf文件是否应用支持CGI。。改httpd.conf为如下:
[iyunv@OAServer ~]# tail /opt/lampp/etc/httpd.conf
# This is to permit URL access to scripts/files in AWStats directory.
#
<Directory “/usr/local/awstats/wwwroot”>
Options FollowSymLinks +ExecCGI
AddHandler cgi-script .pl
AllowOverride None
Order allow,deny
Allow from all
</Directory>
三、插件安装:
1、 GeoIP-1.4.6.tar.gz安装
[iyunv@saas soft]# tar zxvf GeoIP-1.4.6.tar.gz
[iyunv@saas GeoIP-1.4.6]#./configure &&make &&make install
2、 Geo-IP-1.38.tar.gz安装
[iyunv@saas soft]# tar zxvf Geo-IP-1.38.tar.gz
[iyunv@saas Geo-IP-1.38]# perl Makefile.PL &&make &&make install
[iyunv@saas Geo-IP-1.38]# /usr/local/bin/geoipupdate -v
安装好Geoip插件后,在配置文件中启用插件:
[iyunv@saas soft]# grep “geoip ” /etc/awstats/awstats.www.test.cn.conf
# geoip or geoipfree plugin instead of enabled reverse DNS lookup.
LoadPlugin=”geoip GEOIP_STANDARD /usr/local/share/GeoIP/GeoIP.dat” #取消掉本行注释
在web页面就能看到如下红色标注部分及国旗了。。
主机 : 0 个解译成功, 763 无法得知(不能反解网域名称)
639 参观者

GeoIP
Country

网页数

文件数

字节

最近参观日期

121.56.215.145
China

205

1070

12.29 M字节

2010年06月27日 13:41

123.179.199.146
China

108

636

51.03 M字节

2010年06月27日 03:45

110.84.174.240
Australia

105

478

19.08 M字节

2010年06月27日 05:48

118.124.2.212
China

102

511

71.00 M字节

2010年06月27日 16:53


国家或地区

网页数

文件数

字节



China

cn

4444

39202

1.83 G字节




Australia

au

311

2174

109.83 M字节



3、Net-XWhois-0.90.tar.gz 安装(反向地址解析)
[iyunv@saas soft]# tar zxvf Net-XWhois-0.90.tar.gz
[iyunv@saas Net-XWhois-0.90]# perl Makefile.PL &&make &&make install
改qqwry.pl文件中
my $ipfile=”./QQWry.Dat”;

my $ipfile=”${DIR}/plugins/QQWry.Dat”;
复制 QQWry.Dat, qqhostinfo.pm, qqwry.pl 等3个文件到 awstats/wwwroot/cgibin/plugins 下面。并授权755
[iyunv@saas soft]# cp qqhostinfo.pm QQWry.Dat qqwry.pl \
>/usr/local/awstats/wwwroot/cgi-bin/plugins/
[iyunv@saas soft]#chmod 755 /usr/local/awstats/wwwroot/cgi-bin/plugins/*
修改配置文件中LoadPlugin如下:
LoadPlugin=”qqhostinfo”
顺带启用以下插件:
LoadPlugin=”tooltips”
LoadPlugin=”decodeutfkeys” #解决搜索关键字乱码问题
一切OK后。重新刷新下WEB查看页面。可以看到如下:
  

主机 : 0 个解译成功, 763 无法得知(不能反解网域名称)
639 参观者

GeoIP
Country

Location

网页数

文件数

字节

最近参观日期

121.56.215.145
China

内蒙古兴安盟 电信

205

1070

12.29 M字节

2010年06月27日 13:41

123.179.199.146
China

内蒙古赤峰市 电信

108

636

51.03 M字节

2010年06月27日 03:45

110.84.174.240
Australia

福建省福州市 电信

105

478

19.08 M字节

2010年06月27日 05:48

在页面的最下面可以看到已经启用的插件:
Advanced Web Statistics 6.95 (build 1.943) -
创建者 awstats (插件: qqhostinfo, geoip, decodeutfkeys, tooltips)
四、编写简单脚本。并设置定时执行
[iyunv@saas soft]#cat /opt/lampp/logs/awstats.sh
#!/bin/bash
logdir=/opt/lampp/logs
date=`date +%Y%m%d -d “-1 days”`
rmdate=`date +%Y%m%d -d “-7 days”`
\cp $logdir/ksense.com.cn_$date.log $logdir/Test_acc.log
rm -rf $logdir/*_$rmdate.log
/usr/local/awstats/tools/awstats_updateall.pl now
[iyunv@saas soft]]# crontab -l
01 01 * * * /opt/lampp/logs/awstatus.sh #每晚一点处理前一天的日志文件
五、关于安全。网上很多都提倡使用密码认证的方式。。我做得比较简单
<Directory “/usr/local/awstats/wwwroot”>
Options FollowSymLinks +ExecCGI
AddHandler cgi-script .pl
AllowOverride None
Order allow,deny
Allow from 192.168.0.12 #只允许本IP访问此页面
</Directory>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-322875-1-1.html 上篇帖子: apache配置域名指向 下篇帖子: apache的access_log问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表