nginx日志分析解决方案- Awstats

很多PHP搭建的网站都在由apache转向了nginx。nginx的日志信息如何分析呢? 当然你可以自己写一个,但是这里也推荐一款结果信息非常详尽的开源工具——Awstats ,它基于perl编写,它的介绍如下:

AWStats is a free powerful and featureful tool that generates advanced web, streaming, ftp or mail server statistics, graphically. This log analyzer works as a CGI or from command line and shows you all possible information your log contains, in few graphical web pages. It uses a partial information file to be able to process large log files, often and quickly. It can analyze log files from all major server tools like Apache log files (NCSA combined/XLF/ELF log format or common/CLF log format), WebStar, IIS (W3C log format) and a lot of other web, proxy, wap, streaming servers, mail servers and some ftp servers.

点击这里去官方网站

 

第一步,日志的处理

最好每天分割一下日志,一开始没注意这个工作,结果不久日志文件就上G了,很痛苦。分割日志很简单,首先把日志文件复制到别的地方,然后再通知nginx重新生成日志就可以了。shell脚本如下:

 

# !/bin/bash

logs_path
= " /nginx/logs/ "
mv ${logs_path}access . log  ${logs_path}access_$(date  - " yesterday "   + " %Y%m%d " ) . log
kill   - USR1 `cat  / nginx / logs / nginx . pid`

 

代码中的/nginx/logs指的是nginx的log日志文件所在目录,生成了以昨天日期命名的日志文件。

为了达到每天自动分割的目的,在crontab中加入以下部分:

 

1   0   *   *   *  sh  / home / zyf / sh / cut_nginx_log . sh

这样就每天的0点1分把nginx日志重命名为日期格式,并重新生成今天的新日志文件。

 

第二步,Awstats的配置。

日志文件分割好了,接下来就是分析了,也就是Awstats的使用了。
Awstats的配置文件默认会存储在/etc/awstats/目录下,包括你安装时设置的域名如:awstats.www.xxxxke.com.conf。在这个配置文件中修改这个地方:
LogFile = " /nginx/logs/access_%YYYY-0%MM-0%DD-24.log "

这个意思是要去读取nginx昨天的日志文件,关于后边%YYYY-0%MM-0%DD-24的设置,规则如下:

 

#  You can also use tags in this filename if you need a dynamic file name
# depending on date or time (Replacement is made by AWStats at the beginning
# of its execution). This is available tags :
#   %YYYY-n  is replaced with 4 digits year we were n hours ago
#   %YY-n    is replaced with 2 digits year we were n hours ago
#   %MM-n    is replaced with 2 digits month we were n hours ago
#   %MO-n    is replaced with 3 letters month we were n hours ago
#   %DD-n    is replaced with day we were n hours ago
#   %HH-n    is replaced with hour we were n hours ago
#   %NS-n    is replaced with number of seconds at 00:00 since 1970
#   %WM-n    is replaced with the week number in month (1-5)
#   %Wm-n    is replaced with the week number in month (0-4)
#   %WY-n    is replaced with the week number in year (01-52)
#   %Wy-n    is replaced with the week number in year (00-51)
#   %DW-n    is replaced with the day number in week (1-7, 1=sunday)
#                              use n=24 if you need (1-7, 1=monday)
#   %Dw-n    is replaced with the day number in week (0-6, 0=sunday)
#                              use n=24 if you need (0-6, 0=monday)
#   Use 0 for n if you need current year, month, day, hourdot.gif

 

第三步,开始分析、生成结果。

最后,可以执行分析了。使用这个命令:
/ usr / local / awstats / wwwroot / cgi - bin / awstats . pl  - update  - config = www . xxxxke . com

 

这个命令会把结果生成到/var/lib/awstats 目录下 awstatsXXXX.www.XXXX.com.txt文件。

当然啦,这样看起来不太方便哦,呵呵,可以再用下面的命令来生成html页面,相当漂亮:

 

ContractedBlock.gif ExpandedBlockStart.gif
perl /usr/local/awstats/tools/awstats_buildstaticpages.pl -update \
 
-config=www.xxxxoke.com -lang=cn \ 
-dir=/html/awstats \
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

 

这样就会在/html/awstats目录下生成很漂漂的分析结果页,很暴力很强大。

 

第四步,自动化。

要是每天都去服务器上运行几条命令肯定是件令人烦燥的事情,所以呢,linux的世界里有crontab这样的好东东,很简单,下面是我的crontab

 

1   0   *   *   *  sh  / home / zyf / sh / cut_nginx_log . sh

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

0   2   *   *   *  perl  / usr / local / awstats / tools / awstats_buildstaticpages . pl  - update  - config = www . xxxxke . com  - lang = cn  - dir =/ html / awstats  - awstatsprog =/ usr / local / awstats / wwwroot / cgi - bin / awstats . pl

 

-------------------------------------------------------------------------

大功告成,打完收功……

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值