日志分割与日志分析系统(缓存与压缩)
一、日志分割介绍
■随着网站的访问量增加,默认情况下Apache的单个日
志文件也会越来越大
●日志文件占用磁盘空间很大查看相关信息不方便
■ 对日志文件进行分割
● Apache自带rotatelogs分割工具实现
● 第三方工具cronolog分割
性 第经塞能xo视要0主题T
二、日志分割实验
2.1配置DNS服务
2.1.1安装bind
2.1.2配置主配置文件
vim /etc/named/named.conf
2.1.3配置区域文件
[root@localhost ~]# vim /etc/named.rfc1912.zones
2.1.4配置区域数据配置文件
[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
data named.ca named.localhost slaves
dynamic named.empty named.loopback
[root@localhost named]# cp -p named.localhost kgc.com.zone 将区域数据配置文件模板复制到kgc.com.zone
[root@localhost named]# vim kgc.com.zone
2.2日志分割
2.2.1安装Apache
yum -y install httpd
2.2.2配置httpd服务
1)设置监听地址
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
2)设置域名
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
3)配置访问日志
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
4)配置错误日志
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
2.2.3重启httpd服务
[root@localhost var]# systemctl restart httpd
测试
先访问Apache页面
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HyGjOSGK-1596768861370)(C:\Users\LIU\AppData\Roaming\Typora\typora-user-images\image-20200805191449856.png)]
2.2.4查看日志文件
[root@localhost log]# cd /var/log/httpd
[root@localhost httpd]# ls
access_log www.kgc.com.access_20200805.log
error_log www.kgc.com.error_20200804log
2.3使用外部日志分割软件进行日志分割
2.3.1安装cronolog工具
[root@localhost opt]# ls
cronolog-1.6.2-14.el7.x86_64.rpm rh
[root@localhost opt]# rpm -ivh cronolog-1.6.2-14.el7.x86_64.rpm
警告:cronolog-1.6.2-14.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID 352c64e5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:cronolog-1.6.2-14.el7 ################################# [100%]
2.3.3 修改配置文件
[root@localhost opt]# which cronolog 查看工具路径
/usr/sbin/cronolog
[root@localhost opt]# vim /etc/httpd/conf/httpd.conf
1)配置访问日志
2)配置错误日志
2.3.4测试
访问Apache
产生日志文件
[root@localhost httpd]# ls
access_log www.kgc.com.access_20200805.log
error_log www.kgc.com.error_20200805log
[root@localhost httpd]#
三、日志分析系统
AWStats日志分析系统介绍
■Perl语言开发的一款开源日志分析系统
■可用来分析Apache、Samba、Vsftpd、IS等服务器的
访问日志
■结合crond等计划任务服务,可对日志内容定期进行分析
3.1安装dns和Apache服务并配置,上面已做过不再阐述
3.2编辑Apache首页文件
[root@localhost named]# cd /var/www/html/ 切换到httpd首页目录
[root@localhost html]# ls
[root@localhost html]# vim index.html
3.3安装AWStats软件包
3.3.1将AWStats软件包放在/opt目录
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# ls
awstats-7.6.tar.gz cronolog-1.6.2-14.el7.x86_64.rpm rh
3.3.2解压压缩包
[root@localhost opt]# tar zxvf awstats-7.6.tar.gz
awstats-7.6/
awstats-7.6/tools/
awstats-7.6/tools/awstats_buildstaticpages.pl
awstats-7.6/tools/awstats_updateall.pl
3.3.3将加压后的文件移到/usr/local/awstats 目录下
[root@localhost opt]# ls
awstats-7.6 awstats-7.6.tar.gz cronolog-1.6.2-14.el7.x86_64.rpm rh
[root@localhost opt]# mv awstats-7.6 /usr/local/awstats
[root@localhost opt]# cd /usr/local
[root@localhost local]# ls
awstats bin etc games include lib lib64 libexec sbin share src
[root@localhost local]# cd awstats/
[root@localhost awstats]# ls
docs README.md tools wwwroot
[root@localhost awstats]# cd tools/
[root@localhost tools]# ls 查看
awstats_buildstaticpages.pl geoip_generator.pl urlaliasbuilder.pl
awstats_configure.pl httpd_conf webmin
awstats_exportlib.pl logresolvemerge.pl xslt
awstats_updateall.pl maillogconvert.pl
dolibarr nginx
3.3.4执行awstats_configure.pl 进行安装
[root@localhost tools]# ./awstats_configure.pl
其他选项直接默认,按回车键
3.3.5配置Apache主配置文件
[root@localhost tools]# vim /etc/httpd/conf/httpd.conf
3.3.6修改访问数据文件位置
查看/var/lib/下有没有awstats文件
[root@localhost awstats]# cd /var/lib/
[root@localhost lib]# ls
AccountsService dhclient ipa-client PackageKit stateless
alsa dhcpd iscsi plymouth systemd
alternatives dnsmasq libvirt polkit-1 tftpboot
authconfig flatpak lldpad postfix tpm
bluetooth fprint logrotate pulse tuned
boltd fwupd machines rpcbind udisks2
certmonger fwupdate misc rpm unbound
chrony games mlocate rpm-state upowe
color gdm net-snmp rsyslog vmware
colord geoclue NetworkManager samba xkb
cs gssproxy nfs selinux yum
dav hyperv ntp setroubleshoot
dbus initramfs os-prober sss
发现没有awstats文件,所有需要创建一个
[root@localhost lib]# mkdir awstats
[root@localhost lib]# ls
AccountsService dbus initramfs os-prober sss
alsa dhclient ipa-client PackageKit stateless
alternatives dhcpd iscsi plymouth systemd
authconfig dnsmasq libvirt polkit-1 tftpboot
awstats flatpak lldpad postfix tpm
bluetooth fprint logrotate pulse tuned
boltd fwupd machines rpcbind udisks2
certmonger fwupdate misc rpm unbound
chrony games mlocate rpm-state upower
color gdm net-snmp rsyslog vmware
colord geoclue NetworkManager samba xkb
cs gssproxy nfs selinux yum
dav hyperv ntp setroubleshoot
3.3.7重启httpd
[root@localhost lib]# systemctl restart httpd
3.3.8验证统计网页访问情况
访问Apache首页
刷新
[root@localhost tools]# ./awstats_configure.pl now
登录http://localhost/awstats/awstats.pl?config=www.kgc.com
查看页面访问统计
通过计划性任务,自动统计日志信息
开启计划性任务
[root@localhost tools]#systemctl start crond
[root@localhost tools]# crontab -e
http://localhost/awstats/awstats.pl?config=www.kgc.com
3.4优化网页地址
[root@localhost tools]# cd /var/www/html
[root@localhost html]# ls
index.html
[root@localhost html]# vim aws.html
stemctl start crond
[root@localhost tools]# crontab -e
[外链图片转存中…(img-6CCukVvK-1596768861387)]
http://localhost/awstats/awstats.pl?config=www.kgc.com
3.4优化网页地址
[root@localhost tools]# cd /var/www/html
[root@localhost html]# ls
index.html
[root@localhost html]# vim aws.html
[外链图片转存中…(img-ybHH8XFe-1596768861388)]
使用http://localhost/aws.html网址便可代替http://localhost/awstats/awstats.pl?config=www.kgc.com进行网页访问统计了