使用awstats自动分析Nginx日志.doc_第1页
使用awstats自动分析Nginx日志.doc_第2页
使用awstats自动分析Nginx日志.doc_第3页
使用awstats自动分析Nginx日志.doc_第4页
使用awstats自动分析Nginx日志.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

移动钢铁使用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 completemissing 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 onmultiple 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 downloadsand HTTP status 206 is tracked as download continuationAwstats 是在 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识别,如定义日志格式123log_format main $remote_addr - $remote_user $time_local $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for;使用日志格式1access_log /home/www/logs/access.log main;本文不讲如何安装nginx,稍后我将发布我的lnmp一键安装包(linux nginx mysql php)。全编译+优化自动化安装,使用php-fpm运行php的fastcgi进程。我写了一个定时切割日志的脚本。每天0:00开始执行,切割昨天的日志(交由awstats分析),压缩前天的日志(压缩日志可减小存储空间,为防止awstats没有分析完就被压缩,所以只压缩前天的日志)。如果你的nginx和log文件放的路径和我的不一样,请对应修改。1vim cut_log.sh输入以下内容1234567891011121314#!/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).100 00 * * * /bin/bash /root/cut_log.sh这样就可以每天凌里自动切割昨天的日志到以日期为目录结构的目录中。可以留存以后查询。留着昨天的日志交给下面的awstats来分析,压缩前天的日志(前天的已经被分析过了)。安装和配置awstats下载最新的 awstats,我使用的是迄今为止最新的7.0版本安装到/usr/local下,这个路径是习惯。大部分人保持的良好习惯。123wget /files/awstats-7.0.tar.gz tar -zxvf awstats-7.0.tar.gz mv awstats-7.0 /usr/local/awstats修改权限,wget下载下来的包中权限是非root的,赋予过权限之后,.pl的文件也就可以运行了。12chown -R root:root /usr/local/awstatschmod -R =rwX /usr/local/awstats然后执行 tools 目录中的 awstats_configure.pl 配置向导,创建一个新的统计运行1/usr/local/awstats/tools/awstats_configure.pl将会有如下一些提示:12345678910- Running OS detected: Linux, BSD or Unix - Check for web server installEnter full config file path of your Web server. Example: /etc/httpd/httpd.conf Example: /usr/local/apache2/conf/httpd.conf Example: c:Program filesapache groupapacheconfhttpd.conf Config file path (none to skip web server setup): none #这里添none并回车,因为我们没有使用apache回车之后下一个选项1234567891011Your 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.confFile awstats.model.conf updated. - Need to create a new config file ? Do you want me to build a new AWStats config/profilefile (required if first install) y/N ?#这里选Y,创建一个新的配置文件123456- Define config file name to create What is the name of your web site or profile analysis ? Example: Example: demo Your web site, virtual server or profile name: #这里输入你要分析的域名,或是随便一个你易记的配置名并回车接下来要定义你的配置文件存放的路径,可用默认12345- Define config file path In which directory do you plan to store your config file(s) ? Default: /etc/awstatsDirectory path to store config file(s) (Enter for default): #直接回车,使用默认路径/etc/awstats回车后的提示12345678910111213141516171819- Create config file /etc/awstats/.confConfig file /etc/awstats/.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= 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/.conf You should have a look inside to check and change manually main parameters. You can then manually update your statistics for with command: perl awstats.pl -update -config= You can also build static report pages for with command: perl awstats.pl -output=pagetype -config= Press ENTER to finish. #回车完成配置文件的创建完成配置文件的创建后,我们还要修改一下。因为我们是按天切割的日志,切割完成后交由awstats去分析。并不是让awstats去分时正在时时增长的也就是正在被写入的日志,这样的好处是不至于遗漏数据,并且分析已经切割完成的日志,更不用担心会有冲突。坏处是我一天切割一次日志,你要等第二天才能看昨天的一些详细数据。修改/etc/awstats/.conf,执行:1vi /etc/awstats/.conf找到1LogFile=/var/log/httpd/mylog.log修改为:1LogFile=/home/www/logs/%YYYY-24/%MM-24/%DD-24/_access.log如果你的日志路径和我的不一样,请修改成对应的日志文件名。以上的完整路径是切割后保存的nginx日志文件。其中%YYYY-24/%MM-24/%DD-24表示年月日都减去24小时,也就是昨天的日志目录。修改完成后按:wq保存退出。接下来可以测试一下awstats分析日志了(前提是你已经有了切割过的日志,没有的话可以先退行一下切割日志的脚本/root/cut_log.sh)首先,还要创建一个awstats用于记录数据的目录1mkdir -p /var/lib/awstats然后运行awstats的wwwroot目录中的awatsts.pl来测试一下1/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=你如果看到类似下面的提示就说明配置文件都正确了。12345678910111213Create/Update database for config /etc/awstats/.conf by AWStats version 7.0 (build 1.964) From data in log file /home/www/logs/2010/07/24/_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 把静态页面生成到该目录中 先生成目录mkdir -p /home/www/awstats我们来写一个脚本1vim /root/awstats.sh然后输入以下内容12345#!/bin/bash mkdir -p /home/www/awstats/ /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config= -lang=cn -dir=/home/www/awstats/ -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl上述命令的具体意思如下: /usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats 静态页面生成工具 -update -config= 更新配置项 -lang=cn 语言为中文 -dir=/home/www/awstats 统计结果输出目录 -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路径。 然后在你的nginx的配置文件中,在你想要安置awstats或默认的ip或域名的server段中,加入关于awstats和icon的两个目录配置。如一个完整案例:12345678910111213141516171819server 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/

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论