热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

设置Nginx日志自动割切并使用awstats分析

说明:awstats可以分析apache日志,同样也可以分析nginx日志。由于Awstats是Perl写的,从awstats的文档来看,它对ApacheHTTPServer的支持是非常完美的。因此apache可以直接打开Perl程序的网页查看统计。但nginx对Perl支持并不好,所以我们需要利用aws
说明:
awstats可以分析apache日志,同样也可以分析nginx日志。由于Awstats是Perl写的,从 awstats 的文档来看,它对 Apache HTTP Server 的支持是非常完美的。因此apache可以直接打开 Perl程序的网页查看统计。 但nginx对Perl支持并不好,所以我们需要利用awstats的工具将统计结果生成静态文件,方便nginx的查看。
本文将详细介绍自动定时切割nginx访问日志,并使用awstats来定时分析nginx日志的实现方法。

实现:
一. 设置nginx日志格式,日志按天切割部分
1.设置nginx日志格式,使更好的支持awstats。默认的nginx access格式就可以了。
log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
access_log  /data1/logs/access.log  access;
 
2.编写shell,定时切割每天的访问日志,压缩一周前的访问日志
# cat /root/logrotate.sh //填写以下内容
#!/bin/bash
# This script will run at 00:00,cut yesterday's logs and gzip log files a week ago.
# yesterday's logs for awstats
source_path="/var/log/nginx/"
logs_path="/home/awstats/logs/"
date_dir=${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/$(date -d "yesterday" +"%d")/
gzip_date_dir=${logs_path}$(date -d "1 week ago" +"%Y")/$(date -d "1 week ago" +"%m")/$(date -d "1 week ago" +"%d")/
mkdir -p $date_dir
mv ${source_path}*.access.log $date_dir
kill -USR1 `cat /usr/local/nginx/nginx.pid`
/usr/bin/gzip ${gzip_date_dir}*.access.log
 
3.修改crontal,使日志切割每天凌晨00:00运行
# crontab -e  //添加以下内容
00 00 * * * /bin/bash /root/logrotate.sh

二. awstats的安装与配置
1.下载与安装
# wget http://sourceforge.net/projects/awstats/files/AWStats/7.1.1/awstats-7.1.1.tar.gz/download
# tar -zxvf awstats-7.1.1.tar.gz
# mv awstats-7.1.1 /usr/local/awstats
//修改权限,wget下载下来的包权限转为为1000,需要修改权限,使.pl的文件可以运行了。
# 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
 
2.针对网站,创建配置文件
# cd /usr/local/awstats/tools  //切换到awstats/tools目录运行。否则会有一些关于标准目录的提示。
# ./awstats_configure.pl  //运行./awstats_configure.pl配置向导,创建一个新的统计
-----> 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
>
#这里输入none并回车,因为我们没有使用apache
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] ?
#这里是说没有web server配置,是否新建一个。选y,创建一个新的配置文件
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.server110.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.server110.com.conf'
 Config file /etc/awstats/awstats.www.server110.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.server110.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...
#按回车继续
A SIMPLE config file has been created: /etc/awstats/awstats.www.server110.com.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=www.server110.com
You can also build static report pages for 'www.server110.com' with command:
> perl awstats.pl -output=pagetype -cOnfig=www.server110.com
Press ENTER to finish...
#回车完成配置文件的创建
 
3.修改/etc/awstats/awstats.www.server110.com.conf,将LogFile="/var/log/httpd/mylog.log" 修改为:
LogFile="/home/www/logs/%YYYY-24/%MM-24/%DD-24/server110.com.access.log"
//YYYY-24这里的24是指24小时前,即昨天;server110.com.access.log为你nginx的访问日志,注意修改。

4.测试新的统计是否配置正确,这一步主要是对访问日志进行统计分析
# mkdir -p /var/lib/awstats  //创建一个awstats用于记录数据的目录,不懂是不是一定要。
# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -cOnfig=www.server110.com
运行awstats安装目录wwwroot下的awatsts.pl测试。如果看到类似下面的提示就说明配置文件都正确了。注意这里-cOnfig=www.server110.com要与上一步创建配置文件时输入的域名一致。
Create/Update database for config "/etc/awstats/awstats.www.server110.com.conf" by AWStats version 7.1.1 (build 1.964)
From data in log file "/home/www/logs/2010/07/24/server110.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
 
5.利用Awstats工具生成静态页面,方便nginx查看。统计分析完成后,结果还在 Awstats 的数据库中。apache可以直接打开 Perl 程序的网页查看统计。 但本文开始时已经提到,Nginx 对 Perl 支持并不好,因此我们需要利用awstats的工具将统计的结果生成静态文件
a.新建目录/home/www/awstats,定期执行shell,让 Awstats 把静态页面生成到该目录中
# mkdir -p /home/www/awstats
# vim /root/awstats.sh  //然后输入以下内容
#!/bin/bash
mkdir -p /home/www/awstats/server110.com
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -cOnfig=www.server110.com -lang=cn -dir=/home/www/awstats/server110.com -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
具体参数说明如下:
/usr/local/awstats/tools/awstats_buildstaticpages.pl    Awstats静态页面生成工具
-update -cOnfig=www.server110.com        更新配置项
-lang=cn         语言为中文
-dir=/home/www/awstats/server110.com    统计结果输出目录
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl      Awstats 日志更新程序路径。
# chmod +x /root/awstats.sh
# /root/awstats.sh
如果看到输出信息,那就说明成功了,例如:
Build keywords page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -cOnfig=www.server110.com -staticlinks -lang=cn -output=keywords
Build errors404 page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -cOnfig=www.server110.com -staticlinks -lang=cn -output=errors404
21 files built.
Main HTML page is 'awstats.www.server110.com.html'.
 
b.修改crontab,让awstats输出静态文件自动运行
# crontab -e  //添加以下内容
00 1 * * * /root/awstats.sh


推荐阅读
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文详细介绍了如何在云服务器上配置Nginx、Tomcat、JDK和MySQL。涵盖从下载、安装到配置的完整步骤,帮助读者快速搭建Java Web开发环境。 ... [详细]
  • 在哈佛大学商学院举行的Cyberposium大会上,专家们深入探讨了开源软件的崛起及其对企业市场的影响。会议指出,开源软件不仅为企业提供了新的增长机会,还促进了软件质量的提升和创新。 ... [详细]
  • 本文将深入探讨PHP编程语言的基本概念,并解释PHP概念股的含义。通过详细解析,帮助读者理解PHP在Web开发和股票市场中的重要性。 ... [详细]
  • 在Python开发过程中,随着项目数量的增加,不同项目依赖于不同版本的库,容易引发依赖冲突。为了避免这些问题,并保持开发环境的整洁,可以使用Virtualenv和Virtualenvwrapper来创建和管理多个隔离的Python虚拟环境。 ... [详细]
  • 阿里云ecs怎么配置php环境,阿里云ecs配置选择 ... [详细]
  • 本文将详细介绍如何在ThinkPHP6框架中实现多数据库的部署,包括读写分离的策略,以及如何通过负载均衡和MySQL同步技术优化数据库性能。 ... [详细]
  • 使用LVS与ldirectord实现高可用负载均衡
    本文介绍了如何通过LVS(Linux Virtual Server)结合ldirectord工具来实现服务器的健康检查及负载均衡功能。环境设置包括一个LVS节点和两个真实服务器节点,通过配置ldirectord进行健康状态监测,确保系统的高可用性。 ... [详细]
  • iTOP4412开发板QtE5.7源码编译指南
    本文详细介绍了如何在iTOP4412开发板上编译QtE5.7源码,包括所需文件的位置、编译器设置、触摸库编译以及QtE5.7的完整编译流程。 ... [详细]
  • 本文介绍如何在现有网络中部署基于Linux系统的透明防火墙(网桥模式),以实现灵活的时间段控制、流量限制等功能。通过详细的步骤和配置说明,确保内部网络的安全性和稳定性。 ... [详细]
  • 本文详细介绍了 Linux 系统中用户、组和文件权限的设置方法,包括基本权限(读、写、执行)、特殊权限(SUID、SGID、Sticky Bit)以及相关配置文件的使用。 ... [详细]
  • 随着技术社区的发展,越来越多的技术爱好者选择通过撰写博客来分享自己的学习经验和项目进展。本文将介绍一个具体案例,即将一套原本运行于Windows平台的代码成功移植到Linux(Redhat)环境下的过程与挑战。 ... [详细]
  • Windows 环境下安装 Git 并连接 GitHub 的详细步骤
    本文详细介绍了如何在 Windows 系统中安装 Git 工具,并通过配置 SSH 密钥实现与 GitHub 的安全连接。包括下载、安装、环境配置及验证连接等关键步骤。 ... [详细]
  • 近期,考虑到在Vim内部进行GDB调试、运行Python脚本和数据库连接等多样化需求,思考是否可以通过集成终端来简化这些操作,而非逐一编写Vim脚本来实现。通过研究发现,确实存在一种高效的方法——利用特定插件实现终端功能的整合。 ... [详细]
author-avatar
淑富世廷789
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有