awstats日志分析

日志分析主要是分析web服务的访问日志,通过日志中包含的信息体现web服务的状态,访问情况等等信息,由此可以对web服务进行调整。

日志分析要分析的主要项
1、客户端的IP了解其所在地,为了提高访问的速度,可以在同地区访问很多的地方建立机房。
2、那些资源访问次数较多,代表资源受欢迎程度高。
3、记录访问时间,了解网站访问的波峰波谷,可以根据这个确定维护时间。
4、客户端使用的浏览器。做兼容。
独立IP:
      定的IP地址访问网站的次数。
独立访客:
      访问网站的自然人。
页面浏览量 pv(page view)
      网站的某个内容或消息,比较受欢迎。

信息的汇总最好形成可视化的图表,方便查看和整理。

日志的轮滚,日志的轮滚周期要根据需求确定好。轮滚就是防止单个日志文件太大,时间跨度太长,造成查看困难的问题而生的。

如果要将轮滚过的日志重新合成一个那么就直接追加到一个文件中就好了。

#!/bin/bash
logdir="/log/nginx"
filename="access.log-"
logfile="${logdir}/all_access.log"
> $logfile
for i in `seq 7 -1 1`
do
    cat ${logdir}/${filename}`date -d "$i day ago" "+%Y%m%d"` >> $logfile
done

合并7天内日志的脚本。

主要用了

date -d "1 day ago" "+%Y%m%d"

这个命令。打印1天前的日期,并格式化输出。

清空日志:如果直接rm -f 删除日志的文件并不会释放空间,它只是切断了用户和文件之间的联系,但是进程没有被杀死,硬盘的容量还是那么多,要重启服务才能生效。

最简单的清空日志。

> filename.log
cat /dev/null > filename.log

就直接重定向把文件内容顶掉。

综合以上情况使用的日志分析工具选用awstats

配置方法:
1、下载安装。
2、配置。
3、生成数据库文件

1、用的是从网上下载的直接就有bin包的awstats,解压就可以用。

下载地址:http://www.awstats.org/

然后解压缩到/usr/local/awstats

[root@localhost local]# tar xf awstats-7.7.tar.gz
[root@localhost local]# mv awstats-7.7/ awstats/

这个是默认的路径,直接解压的是后边有版本号的,最好改下名,否则会有warning警告信息,提示你。

在这里插入图片描述

我下的是awstats7.7,直接就可以有执行权限。如果是7.2还是多少以前的版本,要将目录内的.pl文件加上可执行权限chmod +x /usr/local/awstats/tools/*.plchmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl

安装apache。安装这个是为了能在网页上看到图形的日志汇总,日志分析。

[root@localhost local]# yum install httpd -y 

给awstats整个目录的属组改成apache。

[root@localhost local]# chown -R apache:apache awstats/

2、配置,生成awstats配置文件并修改。

运行向导文件,生成awstats的配置文件:

这个过程基本都是自动生成的,只要注意有注释的就好了。

[root@localhost tools]# ./awstats_configure.pl  //这个就是引导文件。

----- AWStats awstats_configure 1.0 (build 20140126) (c) Laurent Destailleur -----
This tool will help you to configure AWStats to analyze statistics for
one web server. You can try to use it to let it do all that is possible
in AWStats setup, however following the step by step manual setup
documentation (docs/index.html) is often a better idea. Above all if:
- You are not an administrator user,
- You want to analyze downloaded log files without web server,
- You want to analyze mail or ftp log files instead of web log files,
- You need to analyze load balanced servers log files,
- You want to 'understand' all possible ways to use AWStats...
Read the AWStats documentation (docs/index.html).

-----> Running OS detected: Linux, BSD or Unix

-----> 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的配置文件位置。

-----> Check and complete web server config file '/etc/httpd/conf/httpd.conf'
  All AWStats directives are already present.

-----> Update model config file '/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf'
  File awstats.model.conf updated.

-----> 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    //这里需要写下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:
> testawstats     	//这里指定的是访问的地址

-----> 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.testawstats.conf'
 Config file /etc/awstats/awstats.testawstats.conf created.

-----> 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=testawstats
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.testawstats.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'testawstats' with command:
> perl awstats.pl -update -config=testawstats
You can also read your statistics for 'testawstats' with URL:
> http://localhost/awstats/awstats.pl?config=testawstats

Press ENTER to finish...    //按回车结束

到这里就生成了awstats的配置文件。

[root@localhost tools]# vim /etc/awstats/awstats.testawstats.conf

可以看到这个配置文件的中间那段就是在上边写的访问地址。

 50 LogFile="/var/log/httpd/access_log"
 这个是被分析日志的位置,随便写,但是要有这个日志
 220 DirData="/usr/local/awstats/data"
 这个是日志分析数据库文件的位置,也是随便的

创建目录赋予权限

[root@localhost tools]# mkdir /usr/local/awstats/data
[root@localhost tools]# chown apache:apache /usr/local/awstats/data/

配置apache

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf //Apache配置文件
<Directory "/usr/local/awstats/wwwroot">
    Options None
    AllowOverride None
    order allow,deny
    allow from all
    Require all granted
</Directory>

改成这样就可以了。

启动服务

[root@localhost tools]# systemctl restart httpd
[root@localhost tools]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

3、生成数据库文件。

生成数据库文件:

[root@localhost data]# perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -config=testawstats -update
Create/Update database for config "/etc/awstats/awstats.testawstats.conf" by AWStats version 7.7 (build 20180105)
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: 5654
 Found 0 dropped records,
 Found 0 comments,
 Found 0 blank records,
 Found 0 corrupted records,
 Found 0 old records,
 Found 5654 new qualified records.
[root@localhost tools]# 

注意:-config=testawstats =后边的要写在最初配置时写的访问地址

然后查看数据库文件:

[root@localhost tools]# cd /usr/local/awstats/data/
[root@localhost data]# ls
awstats112019.testawstats.txt  awstats122019.testawstats.txt

目录就是配置文件中的那个位置。

注意:如果没有生成数据库文件但是看到那个输出的都是0 ,就是没有日志,空的自然没办法生成数据库文件。

打开浏览器访问。

http://172.16.12.72/awstats/awstats.pl?config=testawstats

然后就能看到这个日志的分析了。

在这里插入图片描述在这里插入图片描述

以上就是apache的日志分析了。

分析其他的东西:

nginx的日志格式和Apache的日志格式一样,不用改东西,直接修改配置文件中的被分析日志就好了。

tomcat 的日志格式不一样,所以要进行修改。

将配置文件中加上这个

LogFormat = "%host %other %logname %time1 %methodurl %code"
这个就是日志的格式,主机名(IP) 访问的人  登录名   访问时间   访问方式  代码块

并修改被监控日志就好了。

注意:在修改被分析日志之后,一定要重新生成数据库文件。否则还是上一个日志的分析情况。

另外,awstats这个软件在epel源中有,如果配置了epel源可以直接yum install 安装。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值