xiaoguozi's Blog
Pay it forword - 我并不觉的自豪,我所尝试的事情都失败了······习惯原本生活的人不容易改变,就算现状很糟,他们也很难改变,在过程中,他们还是放弃了······他们一放弃,大家就都是输家······让爱传出去,很困难,也无法预料,人们需要更细心的观察别人,要随时注意才能保护别人,因为他们未必知道自己要什么·····
转自: https://www.akii.org/use-awstats-automatic-analysis-nginx-log.html
使用awstats可以分析apache日志,同样也可以分析nginx日志。本文将详细介绍自动定时切割nginx的访问日志,并使用awstats来定时分析nginx的日志的实现方法。

前言

本文中使用的是awstats 7.0版本。
此版本增加了对win7的支持以及一些更新的特性。

New features/improvements:
- Detect Windows 7.
- Can format numbers according to language.
- More mime types.
- Added geoip_asn_maxmind plugin.
- Geoip Maxmind city plugin have now override file capabilities to complete
missing entries in geoip maxmind database.
- Added graphgooglechartapi to use online Google chart api to build graph.
- Can show map of country to report countries when using graphgooglechartapi.
- Part of codes was change to use more functions and have a cleaner code.
- Added parameter to ignore missing log files when merging for a site on
multiple servers where a single server may not have created a log for a given day.
- Update robots database.
- Added Download tracking where certain mime types are defined as downloads
and HTTP status 206 is tracked as download continuation

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

  • 访问次数、独特访客人数,
  • 访问时间和上次访问,
  • 使用者认证、最近认证的访问,
  • 每周的高峰时间(页数,点击率,每小时和一周的千字节),
  • 域名/国家的主机访客(页数,点击率,字节,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 对日志统计的结果生成静态页面,然后通过 Nginx 输出以达到统计 Nginx 访问日志的效果,其中还包括如何让 Nginx 自动切割日志文件。对于nginx的日志,我的做法是按天切割。然后存入日期形式的目录中并压缩。

需要注意的是,nginx的日志应该遵循以下格式,才可以被awstats识别,如定义日志格式

1
2
3
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

使用日志格式

1
access_log  /home/www/logs/access.log  main;

这里需要有一个小技巧的提示:把log_format这段代码放在你nginx的http的定义段中,可以在下面的每一个server中引用此格式。不必在每个server里面都去定义格式。
本文不讲如何安装nginx,稍后我将发布我的lnmp一键安装包(linux nginx mysql php)。全编译+优化自动化安装,使用php-fpm运行php的fastcgi进程。

我写了一个定时切割日志的脚本。每天0:00开始执行,切割昨天的日志(交由awstats分析),压缩前天的日志(压缩日志可减小存储空间,为防 止awstats没有分析完就被压缩,所以只压缩前天的日志)。如果你的nginx和log文件放的路径和我的不一样,请对应修改。

1
vim cut_log.sh

输入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
# This script run at 00:00
# cut yesterday log and gzip the day before yesterday log files.
# yesterday logs to awstats
 
# The Nginx logs path
logs_path="/home/www/logs/"
date_dir=${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/$(date -d "yesterday" +"%d")/
gzip_date_dir=${logs_path}$(date -d "-2 day" +"%Y")/$(date -d "-2 day" +"%m")/$(date -d "-2 day" +"%d")/
 
mkdir -p $date_dir
mv ${logs_path}*access.log $date_dir
/usr/local/nginx/sbin/nginx -s reopen
/usr/bin/gzip ${gzip_date_dir}*.log

然后让它每天0时起开始进行,执行crontab -e加入以下代码再按:wq保存退出,这里我将此脚本放在/root/下,切记要给它可执行权限(chmod +x cut_log.sh).

1
00 00 * * * /bin/bash /root/cut_log.sh

这样就可以每天凌里自动切割昨天的日志到以日期为目录结构的目录中。可以留存以后查询。留着昨天的日志交给下面的awstats来分析,压缩前天的日志(前天的已经被分析过了)。

安装和配置awstats

下载最新的 awstats,我使用的是迄今为止最新的7.0版本

安装到/usr/local下,这个路径是习惯。大部分人保持的良好习惯。

1
2
3
wget http://awstats.sourceforge.net/files/awstats-7.0.tar.gz
tar -zxvf awstats-7.0.tar.gz
mv awstats-7.0 /usr/local/awstats

修改权限,wget下载下来的包中权限是非root的,赋予过权限之后,.pl的文件也就可以运行了。

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

然后执行 tools 目录中的 awstats_configure.pl 配置向导,创建一个新的统计

运行(注意这里要在当前目录运行。否则会有一些关于标准目录的提示。)

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

将会有如下一些提示:

1
2
3
4
5
6
7
8
9
10
-----> 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 #这里添none并回车,因为我们没有使用apache

回车之后下一个选项

1
2
3
4
5
6
7
8
9
10
11
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,创建一个新的配置文件

1
2
3
4
5
6
-----> 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:
>akii.org  #这里输入你要分析的域名,或是随便一个你易记的配置名并回车

接下来要定义你的配置文件存放的路径,可用默认

1
2
3
4
5
-----> 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

回车后的提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-----> Create config file '/etc/awstats/awstats.akii.org.conf'
 Config file /etc/awstats/awstats.akii.org.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=akii.org
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.akii.org.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'yuyuanchun.com' with command:
> perl awstats.pl -update -config=akii.org
You can also build static report pages for 'akii.org' with command:
> perl awstats.pl -output=pagetype -config=akii.org
 
Press ENTER to finish... #回车完成配置文件的创建

完成配置文件的创建后,我们还要修改一下。因为我们是按天切割的日志,切割完成后交由awstats去分析。并不是让awstats去分时正在时时 增长的也就是正在被写入的日志,这样的好处是不至于遗漏数据,并且分析已经切割完成的日志,更不用担心会有冲突。坏处是我一天切割一次日志,你要等第二天 才能看昨天的一些详细数据。

修改/etc/awstats/awstats.akii.org.conf,执行:

1
vi /etc/awstats/awstats.akii.org.conf

找到

1
LogFile="/var/log/httpd/mylog.log"

修改为:

1
LogFile="/home/www/logs/%YYYY-24/%MM-24/%DD-24/akii.org_access.log"

如果你的日志路径和我的不一样,请修改成对应的日志文件名。以上的完整路径是切割后保存的nginx日志文件。其中%YYYY-24/%MM-24/%DD-24表示年月日都减去24小时,也就是昨天的日志目录。修改完成后按:wq保存退出。

接下来可以测试一下awstats分析日志了(前提是你已经有了切割过的日志,没有的话可以先退行一下切割日志的脚本/root/cut_log.sh)

首先,还要创建一个awstats用于记录数据的目录

1
mkdir -p /var/lib/awstats

然后运行awstats的wwwroot目录中的awatsts.pl来测试一下

1
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=akii.org

你如果看到类似下面的提示就说明配置文件都正确了。

1
2
3
4
5
6
7
8
9
10
11
12
13
Create/Update database for config "/etc/awstats/awstats.akii.org.conf" by AWStats version 7.0 (build 1.964)
From data in log file "/home/www/logs/2010/07/24/akii.org_access.log"...
Phase 1 : First bypass old records, searching new record...
Direct access after last parsed record (after line 43260)
Jumped lines in file: 43260
 Found 43260 already parsed records.
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 的数据库中。在 Apache 上,可以直接打开 Perl 程序的网页查看统计。 但本文开始时已经提到,Nginx 对 Perl 支持并不好,所以我们要换个方法,利用 awstats 的工具将统计的结果生成静态文件,具体的步骤如下:

  • 首先在 webroot 目录下创建一个文件夹。例:/home/www/awstats
  • 写一个脚本,定期执行让 Awstats 把静态页面生成到该目录中

先生成存放awstats生成的静态文件的目录,我这里用的是/home/www/awstats

1
mkdir -p /home/www/awstats

我们来写一个脚本

1
vim /root/awstats.sh

然后输入以下内容

1
2
3
4
5
#!/bin/bash
mkdir -p /home/www/awstats/akii.org
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update  \
-config=akii.org -lang=cn -dir=/home/www/awstats/akii.org  \
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

上述命令的具体意思如下:

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

然后在你的nginx的配置文件中,在你想要安置awstats或默认的ip或域名的server段中,加入关于awstats和icon的两个目录配置。

如一个完整案例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen       80;
server_name  localhost;
root /home/www;
index index.html;
 
location ~ ^/awstats/ {     # awstats  静态页面目录
        root   /home/www/awstats;
        autoindex on; #可以目录浏览你的多个域名的目录用于分析
        index  index.html;
        access_log off;
}
 
location ~ ^/icon/ {             # 图标目录
        root   /usr/local/awstats/wwwroot;
        index  index.html;
        access_log off;
}
}

接下来可以测试一下脚本是否可以正确执行

还是别忘了给它可执行权限

1
2
chmod +x /root/awstats.sh
/root/awstats.sh

如果你看到它生成了一堆网页,那就说明成功了。

输出信息部分例如

1
2
3
4
5
6
Launch update process : "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=akii.org -update -configdir=
......
Build keywords page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=akii.org -staticlinks -lang=cn -output=keywords
Build errors404 page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=akii.org -staticlinks -lang=cn -output=errors404
20 files built.
Main HTML page is 'awstats.akii.org.html'.

然后可以把它加入自动运行了。

配置awstats脚本自动运行

1
crontab -e

加入

1
00 1 * * * /root/awstats.sh

然后保存退出。

这样就可以每天在凌晨自动分割日志,并且开始自动用awstats分析nginx的日志了。

认证访问

如果你想给你的awstats加上访问密码,可以见这里:nginx为目录或网站加上密码认证

原创文章,写的辛苦。如果你要转载,请保留出处及链接。

参考资料:http://www.ibm.com/developerworks/cn/linux/l-cn-awstats-nginx/index.html

posted on 2013-01-06 18:05 小果子 阅读(3277) 评论(0)  编辑 收藏 引用 所属分类: 学习笔记开源

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理