AWStats日志分析理论与实验
程序员文章站
2022-06-25 20:27:32
...
文章目录
前言:
AWStats是免费的一个非常强大的日志分析工具的Apache日志文件。 从apache分析日志后,它以易于理解的图形格式显示它们。 AWStats是高级Web统计的缩写,它可以在命令行界面或CGI上运行。
一、AWStats概述
1.1 AWStats日志分析系统
- Perl语言开发的一款开源日志分析系统
- 可用来分析Apache、Samba、Vsftpd、IIS等服务器的访问日志
- 信息结合crond等周期性计划任务服务,可对日志内容进行周期性分析
1.2 AWStats主要功能
-
AWStats可以同进站点以下信息:
访问量,访问次数,页面浏览量,点击数,数据流量等
精确到每月、每日、每小时的数据
访问者国家
访问者IP
Robots/Spiders的统计
访客持续时间
对不同File type的统计信息
Pages-URL的统计
访客操作系统浏览器等信息
其他信息(搜索关键字等)
二、AWStats 实验
2.1 实验环境
两台虚拟机,一台配置httpd基础网站和DNS域名解析服务及AWStats日志分析系统,一台模拟用户机进行访问测试
2.2.1 安装dns、httpd服务
[aaa@qq.com ~]# yum install bind httpd -y
2.2.2 配置DNS服务
- 修改全局配置文件
[aaa@qq.com ~]# vim /etc/named.conf
options {
listen-on port 53 { any; }; ##将127.0.0.1改为any
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; }; ##将localhost改为any
..................省略部分内容
- 修改区域配置文件
[aaa@qq.com ~]# vim /etc/named.rfc1912.zones
zone "ceshi" IN { #添加此段信息
type master;
file "ceshi.com.zone";
allow-update { none; };
};
..................省略部分内容
- 修改区域数据配置文件
[aaa@qq.com ~]# cd /var/named/
[aaa@qq.com named]# ls
chroot data dyndb-ldap named.empty named.loopback
chroot_sdb dynamic named.ca named.localhost slaves
[aaa@qq.com named]# cp -p named.localhost ceshi.com.zone
[aaa@qq.com named]# vim ceshi.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
wwww IN A 192.168.181.154 #将原本IPV6的数据删除,添加本地地址
- 关闭防火墙、增强型安全功能,然后重启服务
[aaa@qq.com named]# systemctl stop firewalld.service
[aaa@qq.com named]# setenforce 0
[aaa@qq.com named]# systemctl start named
2.2.3 使用win10虚拟机上测试
在win10网卡上指定dhs地址为192.168.181.154
解析地址www.ceshi.com
2.2.4 配置httpd服务
[aaa@qq.com named]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.181.154:80 ##找到监听地址,开启监听并将地址改为本地IP
#Listen 80 ##关闭ipv6监听端口
..............省略部分内容
ServerName www.ceshi.com:80 ##开启服务名,并将域名改为www.ceshi.com
..............省略部分内容
##保存退出后开启httd服务
[aaa@qq.com named]# systemctl start httpd
2.2.4 在win10上测试
直接在浏览器中输入www.ceshi.com
3.1 配置AWStats
3.1.1 下载、共享、挂载AWStats
- 在主机上下载AWStats软件包并放入LAMP-C7共享文件夹中
- 在linux虚拟机上挂载共享文件夹
[aaa@qq.com named]# mkdir /abc
[aaa@qq.com named]# mount.cifs //192.168.181.1/LAMP-C7 /abc #地址为宿主机IP地址
Password for aaa@qq.com//192.168.181.1/LAMP-C7:
[aaa@qq.com named]# cd /abc
[aaa@qq.com abc]# ls
apr-1.6.2.tar.gz cronolog-1.6.2-14.el7.x86_64.rpm mysql-5.6.26.tar.gz
apr-util-1.6.0.tar.gz Discuz_X2.5_SC_UTF8.zip php-5.6.11.tar.bz2
awstats-7.6.tar.gz httpd-2.4.29.tar.bz2
3.1.2 配置AWStats
- 解压AWStats软件包到/opt目录中
[aaa@qq.com abc]# tar zxvf awstats-7.6.tar.gz -C /opt
awstats-7.6/
awstats-7.6/tools/
awstats-7.6/tools/awstats_buildstaticpages.pl
.............省略部分内容
- 将/opt下的awstats-7.6 移动到/usr/local中并重命名为awstats
[aaa@qq.com abc]# cd /opt
[aaa@qq.com opt]# ls
awstats-7.6 rh
[aaa@qq.com opt]# mv awstats-7.6/ /usr/local/awstats
[aaa@qq.com opt]# cd /usr/local/
[aaa@qq.com local]# ls
awstats bin etc games include lib lib64 libexec sbin share src
此处需注意,如果移动awstats-7.6到/usr/local/中未改名,稍后更新日志分析的时候可能会报错。
- 进入awstats进行配置
进行配置AWStats服务前一定要先开启httpd服务,否则会配置失败
[aaa@qq.com local]# cd awstats/
[aaa@qq.com awstats]# cd tools
[aaa@qq.com tools]# ls
awstats_buildstaticpages.pl dolibarr maillogconvert.pl xslt
awstats_configure.pl geoip_generator.pl nginx
awstats_exportlib.pl httpd_conf urlaliasbuilder.pl
awstats_updateall.pl logresolvemerge.pl webmin
[aaa@qq.com tools]# ./awstats_configure.pl
[aaa@qq.com tools]# ./awstats_configure.pl
..........................省略部分内容
-----> Check for web server install
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):
> /etc/httpd/conf/httpd.conf ####输入apache配置文件路径
..........................省略部分内容
-----> 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 ###提示“是否建立新的httpd的配置文件”这里我们选择yes
-----> 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.ceshi.com ##输入域名
-----> 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):
> ###提示“是否选择默认的配置文件目录”此处直接回车
-----> Create config file '/etc/awstats/awstats.www.ceshi.com.conf'
Config file /etc/awstats/awstats.www.ceshi.com.conf created.
-----> Restart Web server with '/sbin/service httpd restart'
Redirecting to /bin/systemctl restart httpd.service
..........................省略部分内容
A SIMPLE config file has been created: /etc/awstats/awstats.www.ceshi.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.ceshi.com' with command:
> perl awstats.pl -update -config=www.ceshi.com
You can also read your statistics for 'www.ceshi.com' with URL:
> http://localhost/awstats/awstats.pl?config=www.ceshi.com
#####复制以上的路径
Press ENTER to finish...
####完成配置
[aaa@qq.com tools]#
- 修改apache配置文件
[aaa@qq.com tools]# cd /etc/httpd/conf
[aaa@qq.com conf]# ls
httpd.conf magic
[aaa@qq.com conf]# vim httpd.conf
##输入G跳转到尾行
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/"
#######配置完AWStats后,会自动将以上AWStats的控制写入到apache配置文件中,
#
# This is to permit URL access to scripts/files in AWStats directory.
#
<Directory "/usr/local/awstats/wwwroot">
Options None
AllowOverride None
#Order allow,deny ###注释掉此权限配置
#Allow from all ###注释掉此权限配置
Require all granted ##添加允许所有人访问
</Directory>
--------》wq(保存)
- 修改AWStats配置文件
[aaa@qq.com conf]# cd /etc/awstats/
[aaa@qq.com awstats]# ls
awstats.www.ceshi.com.conf
[aaa@qq.com awstats]# vim awstats.www.ceshi.com.conf
#输入/LogFile找到监控的日志文件对象
LogFile="/var/log/httpd/access_log" ##修改监控的日志文件,指向apache的访问日志文件
DirData="/var/lib/awstats" ###查看数据目录
------------》wq(保存)
- 进入/var/lib创建awstats目录给AWStats存放数据
[aaa@qq.com awstats]# cd /var/lib
[aaa@qq.com lib]# mkdir awstats
- 重启httpd服务
[aaa@qq.com lib]# systemctl restart httpd
3.1.2 使用win10虚拟机进行测试
在浏览器中输入我们之前在配置AWStats时复制的路径:http://localhost/awstats/awstats.pl?config=www.ceshi.com (注意要将local改为www.ceshi.com)
即:http://www.ceshi.com/awstats/awstats.pl?config=www.ceshi.com
访问页面如下:
3.1.3 更新日志分析数据
[aaa@qq.com lib]# cd /usr/local/awstats/
[aaa@qq.com awstats]# ls
docs README.md tools wwwroot
[aaa@qq.com awstats]# cd tools
[aaa@qq.com tools]# ls
awstats_buildstaticpages.pl dolibarr maillogconvert.pl xslt
awstats_configure.pl geoip_generator.pl nginx
awstats_exportlib.pl httpd_conf urlaliasbuilder.pl
awstats_updateall.pl logresolvemerge.pl webmin
[aaa@qq.com tools]# ./awstats_updateall.pl now ##执行日志数据分析更新(刷新)
Running '"/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -update -config=www.ceshi.com -configdir="/etc/awstats"' to update config www.ceshi.com
Create/Update database for config "/etc/awstats/awstats.www.ceshi.com.conf" by AWStats version 7.6 (build 20161204)
From data in log file "/var/log/httpd/access_log"...
Phase 1 : First bypass old records, searching new record...
Searching new records from beginning of log file...
Phase 2 : Now process new records (Flush history on disk after 20000 hosts)...
Jumped lines in file: 0
Parsed lines in file: 98
Found 0 dropped records,
Found 0 comments,
Found 0 blank records,
Found 1 corrupted records,
Found 0 old records,
Found 97 new qualified records.
- 返回win10虚拟机刷新网页
更新后数据如下:
3.2 设置周期性计划任务
将日志数据分析周期性计划任务
[aaa@qq.com tools]# crontab -e
*/2 * * * * /usr/local/awstats/tools/awstats_updateall.pl now
###每隔2分钟更新日志数据分析
-----》wq
[aaa@qq.com tools]# systemctl start crond ##开启周期性计划任务
3.3 优化网页网址
[aaa@qq.com tools]# cd /var/www/html/
您在 /var/spool/mail/root 中有邮件
[aaa@qq.com html]# ls
[aaa@qq.com html]# vim aws.html
<html>
<head>
<meta http-equiv=refresh content="0;url=http://www.ceshi.com/awstats/awstats.pl?config=www.ceshi.com">
</head>
<body></body>
</html>
---------->保存
- 返回win10,重新打开浏览器,输入http://www.ceshi.com/aws.html
显示页面如下:
总结
需注意:在本篇博客中也有过强调,开始配置AWStats日志分析服务的时候,最好更改文件包命名,以防之后更新数据时出现erorr。