日志分析工具Awstats实战之Nginx篇-分析结果静态化

简介:

前言:

Awstats 是在 SourceForge 上发展很快的一个基于 Perl 的 WEB 日志分析工具,一个充分的日志分析让 Awstats 显示您下列资料:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
访问次数、独特访客人数,
访问时间和上次访问,
使用者认证、最近认证的访问,
每周的高峰时间(页数,点击率,每小时和一周的千字节),
域名/国家的主机访客(页数,点击率,字节,269域名/国家检测, geoip 检测),
主机名单,最近访问和未解析的 IP 地址名单
大多数看过的进出页面,
档案类型,
网站压缩统计表(mod_gzip 或者 mod_deflate),
使用的操作系统 (每个操作系统的页数,点击率 ,字节, 35 OS detected),
使用的浏览器,
机器人访问(检测 319 个机器人),
蠕虫攻击 (5 个蠕虫家族),
搜索引擎,利用关键词检索找到你的地址,
HTTP 协议错误(最近查阅没有找到的页面),
其他基于 URL 的个性报导,链接参数, 涉及综合行销领域目的.
贵网站被加入 "最喜爱的书签" .次数.
屏幕大小(需要在索引页补充一些 HTML 标签).
浏览器的支持比例: Java, Flash, RealG2 reader, Quicktime reader, WMA reader, PDF reader.
负载平衡服务器比率集群报告.

Awstats 的运行是需要 PERL 环境的支持,从 awstats 的文档来看,它对 Apache HTTP Server 的支持是非常完美的,而当我们把 Web 服务器换成 Nginx 后,要运行 awstats 变得很麻烦。首先 Nginx 本身对 Perl 的支持是比较弱的,甚至官方也不建议使用;另外在日志格式上有需要修改后才能运行。
使用awstats可以分析apache日志,同样也可以分析ng
inx日志。本文将详细介绍自动定时切割nginx的访问日志,并使用awstats来定时分析nginx日志及实现统计结果可供安全便捷的查阅。


环境:
1
2
3
4
5
6
7
8
9
CentOS  6.4  x86_64
ip: 192.168 . 1.113
域名:www.sunsky.com(server和client都通过hosts文件解析)
nginx- 1.2 . 9  编译安装,路径/usr/local/nginx,服务开启状态
日志记录格式为nginx默认的,切勿更改,否则会造成awstats无法分析日志。
log_format  main   '$remote_addr - $remote_user [$time_local] "$request" '
                   '$status $body_bytes_sent "$http_referer" '
                   '"$http_user_agent" "$http_x_forwarded_for"' ;
awstats- 7.2 .tar.gz


一、日志自动切割

对于nginx的日志切割,由于没有像apache一样去用cronolog工具,这里我们就写一个脚本,让它可以在每天00:01自动执行,切割昨天的日志(交由awstats分析),压缩前天的日志(压缩日志可减小存储空间,为防止awstats没有分析完就被压缩,所以只压缩前天的日志)。

1
vim  /server/scripts/cut_nginx_log .sh

输入以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh
yesterday=` date  -d  "yesterday"  + "%Y%m%d" `
before_yesterday=` date  -d  "-2 day"  + "%Y%m%d" `
Nginx_Dir= "/usr/local/nginx"
Nginx_logs= "/app/logs"
Log_Name= "www_access"
cd  /tmp
[ -d $Nginx_Logs ] &&  cd  $Nginx_logs ||  exit  1
[ -f $Log_Name.log ] &&  /bin/mv  $Log_Name.log ${Log_Name}_${yesterday}.log ||  exit  1
if  [ $? - eq  0 -a -f $Nginx_Dir /logs/nginx .pid ]
     then
        kill  -USR1 ` cat  $Nginx_Dir /logs/nginx .pid`
fi
[ -f  ${Log_Name}_${before_yesterday}.log ] &&  /usr/bin/gzip  ${Log_Name}_${before_yesterday}.log||  exit  1

执行crontab -e将该脚本加入定时任务中

1
1  0  * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/ null  2 >& 1

这样每天凌晨00:01就能自动实现日志的切割,压缩等功能了

因为本次实验下的nginx此时已经有日志了,另外为了后文awstats能对切割过的日志进行分析,所以这里我们要运行一下此脚本,来将现有日志进行切割生成昨天的日志方便后文操作。

1
/bin/sh  /server/scripts/cut_nginx_log .sh > /dev/null  2>&1

二、Awstats的安装与配置

1.部署awstats

首先我们要下载awstats软件包,并将其放在常规目录(/usr/local)下

1
2
3
wget http: //awstats .sourceforge.net /files/awstats-7 .2. tar .gz
tar  zxf awstats-7.2. tar .gz
mv  awstats-7.2  /usr/local/awstats

由于wget下载下来的包中权限是非root的,所以这里要修改权限,否则稍后*.pl将无法运行

1
2
3
chown  -R root.root  /usr/local/awstats
chmod  +x  /usr/local/awstats/tools/ *.pl
chmod  +x  /usr/local/awstats/wwwroot/cgi-bin/ *.pl

接下来我们要执行awstats/tools下的awstats_configure.pl配置向导,用来生成awstats的配置文件,awstats配置文件的命名规则是awstats.website.conf。

1
2
cd  /usr/local/awstats/tools/
. /awstats_configure .pl

此时会出现如下提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
----- AWStats awstats_configure 1.0 (build 1.9) (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):
> none       #这里让填写网页服务器的配置文件路径,因为我们用的不是apache,所以这里要填none
Your web server config  file (s) could not be found.
You will need to setup your web server manually to  declare  AWStats
script as a CGI,  if  you want to build reports dynamically.
See AWStats setup documentation ( file  docs /index .html)
-----> 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:
> www.sunsky.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):
>               #这里要填写你配置文件存放路径,我们使用它默认的路径/etc/awstats,所以直接回车即可
-----> Create config  file  '/etc/awstats/awstats.www.sunsky.com.conf'
  Config  file  /etc/awstats/awstats .www.sunsky.com.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=www.sunsky.com
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 ...    #提示不能自动加入crontab定时任务,需要稍后自己添加,我们按回车继续即可
A SIMPLE config  file  has been created:  /etc/awstats/awstats .www.sunsky.com.conf
You should have a  look  inside to check and change manually main parameters.
You can  then  manually update your statistics  for  'www.sunsky.com'  with  command :
> perl awstats.pl -update -config=www.sunsky.com
You can also build static report pages  for  'www.sunsky.com'  with  command :
> perl awstats.pl -output=pagetype -config=www.sunsky.com
Press ENTER to finish...  #提示配置文件创建完成和如何更新配置及建立静态报告页,这里我们回车即可结束这个配置向导

2、修改awstats配置文件

完成配置文件的创建之后,我们还需要对/etc/awstats/awstats.www.sunsky.com.conf里的一些参数进行修改。

1
sed  -i  's#LogFile="/var/log/httpd/mylog.log"#LogFile="/app/logs/www_access_%YYYY-24%MM-24%DD-24.log"#g'  /etc/awstats/awstats .www.sunsky.com.conf

这里更改的目的是指定awstats需要分析的nginx的日志文件路径。这里的路径大家要按自己的日志路径来填。

1
sed -i  's#DirData="/var/lib/awstats"#DirData="/usr/local/awstats/data"#g'  /etc/awstats/awstats.www.sunsky.com.conf

这里更改的目的是指定awstats的数据库配置文件(即awstats的数据库(纯文本))。
由于,此处没有/usr/local/awstats/data目录,所以我们要创建出来

1
mkdir  /usr/local/awstats/data

以上的两个替换操作进行完之后一定要用命令查看替换是否成功,以便及早发现纰漏。

1
2
grep  "LogFile="  /etc/awstats/awstats .www.sunsky.com.conf
grep  "DirData="  /etc/awstats/awstats .www.sunsky.com.conf

查询替换结果正确之后,即可进行下面的步骤。


3、创建awstats统计结果存放目录

现在我们要创建awstats统计结果的数据库存放目录

1
/usr/local/awstats/wwwroot/cgi-bin/awstats .pl -update -config=www.sunsky.com

如果屏幕输出类似下面的提示就说明配置文件都正确无误了

1
2
3
4
5
6
7
8
9
10
11
12
Create /Update  database  for  config  "/etc/awstats/awstats.www.sunsky.com.conf"  by AWStats version 7.2 (build 1.992)
From data  in  log  file  "/app/logs/www_access.log" ...
Phase 1 : First bypass old records, searching new record...
Searching new records from beginning of log  file ...
Jumped lines  in  file : 0
Parsed lines  in  file : 0
  Found 0 dropped records,
  Found 0 comments,
  Found 0 blank records,
  Found 0 corrupted records,
  Found 0 old records,
  Found 0 new qualified records.

注释:awstats.pl 会到 /etc/awstats目录下搜索,根据读取到的配置文件运行程序,去读取-config中的参数,把www.sunsky.com扩展成 awstats.www.sunsky.com.conf

分析日志:运行后将这样的日志统计结果归档到一个awstats的数据库(纯文本)里;

然后是输出:分两种形式

1、一种是通过cgi程序读取统计结果数据库输出;

2、一种是运行后台脚本将输出导出成静态文件;

统计分析完成后,结果还在 Awstats 的数据库中。在 Apache 上,可以直接打开 Perl 程序的网页查看统计。 但Nginx 对 Perl 支持并不好,所以要换个方法,利用 awstats 的工具将统计的结果生成静态文件,这里方便我们还是用脚本来实现

1
vim  /server/scripts/awstats .sh

输入以下内容

1
2
3
4
5
#!/bin/sh
Awstats_Dir= "/usr/local/awstats"
[ -d  /www/awstats  ]|| mkdir  /www/awstats
$Awstats_Dir /wwwroot/cgi-bin/awstats .pl -update -config=www.sunsky.com
$Awstats_Dir /tools/awstats_buildstaticpages .pl -update -config=www.sunsky.com -awstatsprog=$Awstats_Dir /wwwroot/cgi-bin/awstats .pl -lang=cn - dir = /www/awstats

脚本内容讲解:

1
2
3
4
5
/usr/local/awstats/tools/awstats_buildstaticpages .pl    Awstats 静态页面生成工具
-update -config=www.sunsky.com  更新配置项
-awstatsprog= /usr/local/awstats/wwwroot/cgi-bin/awstats .pl  Awstats 日志更新程序路径
-lang=cn     语言为中文
- dir /www/awstats   统计结果输出目录

awstats_buildstaticpages.pl会根据-config的参数去读取里面的DirData路径下的数据库配置文件,然后生成静态的html文件,生成的文件重定向到/www/awstats目录下。

三、配置nginx实现安全访问

接下来我们要配置nginx使其能安全的访问到分析的数据

1
vim  /usr/local/nginx/conf/nginx .conf

在server{}内添加如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
         listen       80;
         server_name  www.sunsky.com;
         location / {
             root    /www/sunsky ;
             index  index.html index.htm;
             access_log  /app/logs/www_access .log commonlog;
}
location ~ ^ /awstats/  {
             root    /www/ ;
             index  awstats.www.sunsky.com.html;           #根据自己的网站域名进行更改首页文件
             autoindex on;
             access_log off;
             charset gb2312;
             auth_basic  "Restricted" ;                                           #有些网站不愿意公开网站流量信息,所以加个认证
             auth_basic_user_file  /usr/local/nginx/htpasswd .pass;    #该文件由apache的加密认证工具htpasswd创建
}
location ~ ^ /icon/  {
             root    /usr/local/awststs/wwwroot ;
             index  index.html;
             access_log off;
             charset gb2312;
}
     }

由于nginx没有好的加密认证工具,需要借助apache的htpasswd来实现加密认证功能

1
htpasswd -c -m  /usr/local/nginx/htpasswd .pass sunskyadmin       #用户名为sunskyadmin

配置完毕之后,检查nginx语法,然后优雅重启之后,用游览器访问www.sunsky.com/awstats,输入账号密码之后即可查看统计信息了。

至此,awstats已经可以实现对Nginx的日志统计及静态化的安全访问功能了。

四、配置awstats自动运行

为了让整个日志的统计过程可以实现自动化,将awstats.sh脚本加入crontab定时任务中去,此时结合上面的定时切割任务,我们的crontab里面会有多出来两条定时任务

1
2
1  0  * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/ null  2 >& 1
0  1  * * * /bin/sh /server/scripts/awstats.sh >/dev/ null  2 >& 1

到此,我们整个日志访问工具awstats在nginx上的配置就完成了。当然此篇是基于静态页面的显示的,后面会再写一篇博文日志分析工具Awstats实战之Nginx篇-分析结果动态化出来和大家一起交流学习的。










本文转自 aaao 51CTO博客,原文链接:http://blog.51cto.com/nolinux/1316979,如需转载请自行联系原作者

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
18天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
23 0
|
29天前
|
存储 JSON 应用服务中间件
Higress的日志收集中,底层用的是Envoy,可不可以实现类似NGINX的rsyslog发送?
【2月更文挑战第30天】Higress的日志收集中,底层用的是Envoy,可不可以实现类似NGINX的rsyslog发送?
15 2
|
17天前
|
运维 负载均衡 应用服务中间件
LNMP详解(九)——Nginx虚拟IP实战
LNMP详解(九)——Nginx虚拟IP实战
30 2
|
1天前
工具变量法(两阶段最小二乘法2SLS)线性模型分析人均食品消费时间序列数据和回归诊断2
工具变量法(两阶段最小二乘法2SLS)线性模型分析人均食品消费时间序列数据和回归诊断
|
2天前
|
机器学习/深度学习 前端开发 数据挖掘
R语言计量经济学:工具变量法(两阶段最小二乘法2SLS)线性模型分析人均食品消费时间序列数据和回归诊断
R语言计量经济学:工具变量法(两阶段最小二乘法2SLS)线性模型分析人均食品消费时间序列数据和回归诊断
32 0
|
10天前
|
网络协议 应用服务中间件 Linux
centos7 Nginx Log日志统计分析 常用命令
centos7 Nginx Log日志统计分析 常用命令
23 2
|
11天前
|
运维 监控 应用服务中间件
LNMP详解(十四)——Nginx日志详解
LNMP详解(十四)——Nginx日志详解
16 2
|
18天前
|
存储 监控 数据可视化
Nginx+Promtail+Loki+Grafana Nginx日志展示
通过这些步骤,你可以将Nginx的日志收集、存储、查询和可视化整合在一起。这样,你就可以在Grafana中轻松地创建和展示Nginx日志的图表和面板。
27 3
|
1月前
|
域名解析 网络协议 应用服务中间件
nginx-ingress通过ipv6暴露服务,并在nginx ingress日志中记录客户端真实ipv6的ip地址
本文主要通过阿里云提供的clb和nlb来实现,建议是提前创建好双栈的vpc和vsw(使用clb可以不用双栈vpc和vsw)
175 1
|
17天前
|
Java
使用Java代码打印log日志
使用Java代码打印log日志
73 1