设为首页 收藏本站
查看: 2052|回复: 0

[经验分享] Nginx日志分析解决方案awstats

[复制链接]

尚未签到

发表于 2015-11-27 16:14:25 | 显示全部楼层 |阅读模式
  

利用Nginx强大的日志功能可以做非常多的事情,比如点击统计、流量分析、在线人数统计以及其他高级数据挖掘等。


现在简单讲解下日志分析的思路。




一,日志的切割


周期性切割日志,比如每天分割一下日志,如果不注意这点日志文件很快就上G,给后续工作带来不便。


分割日志很简单,就是把日志文件复制到别的地方,然后再通知让 Nginx 重新生成日志就可以了。




shell脚本如下:


#!/bin/bash


logs_path="/nginx/logs/"

mv  ${logs_path}access.log   ${logs_path}access_$(date -d "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-24%MM-24%DD-24.log"

这个意思是要去读取nginx昨天的日志文件,关于后边%YYYY-24%MM-24%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, hour




三,开始分析、生成结果


最后,可以执行分析了。


使用这个命令:

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



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


当然这样看起来不太方便,可以再用下面的命令来生成html页面,更加直观。




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





LogFile="/usr/local/nginx/logs/%YYYY-24/%MM-24/expotia2.access_%YYYY-24%MM-24%DD-24.log"


log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';





  


  =====另一篇 =====
  

Awstats基于Perl的WEB日志分析工具,网上关于它的介绍还是比较多的,因为是基于perl开发的,在管在windows还是linux下都需要先安装perl环境。所以,我们先安装perl 。其实,还需要一个apache环境,在上一节中已经进行了安装讲解。


[iyunv@localhost awstats]# tar xvfz perl-5.16.1.tar.gz    解压


[iyunv@localhost awstats]# cd perl-5.16.1    进入目录


[iyunv@localhost perl-5.16.1# rm -f config.sh Policy.sh


[iyunv@localhost perl-5.16.1]# sh Configure -de


[iyunv@localhost perl-5.16.1]#make    编译


[iyunv@localhost perl-5.16.1]# make test


[iyunv@localhost perl-5.16.1]# make install    安装




config.sh Policy.sh   为以前安装时的配置文件,新安装或升级安装时需要将其删除。


sh Configure -de   安装使用默认配置,一般而言将会 ok


make test    执行make命令, 然后make根据test目标规则, 执行规定的操作。


安装完成后 perl 所在目录为 /usr/local/lib/perl5, perl 执行文件在 /usr/local/bin 中。




安装awstats                                                                          





解压awstats


[iyunv@localhost awstats]# tar xvfz awstats-6.4.tgz


[iyunv@localhost awstats]# mkdir /etc/awstats


[iyunv@localhost awstats]# mkdir /var/lib/awstats


上面创建的两个目录都将在后面的配置中用到,一个用于存放站点日志分析的配置文件,一个用于存放日志数据信息。




[iyunv@localhost awstats]# cp -R ./awstats-7.0 /usr/local/   


-R 表示复制目录及目录内的所有项目到/usr/local/目录下


[iyunv@localhost test]# mv  awstats-7.0   awstats     进行一个重命名,便于操作而已。




配置awstats




[iyunv@localhost tools]# pwd


/usr/local/awstats//tools


Awstats的配置有自带的配置工具awstats_configure.pl ,首先用vi打开configure.pl文件,看看第一行有关perl运行环境的配置是否正确。




[iyunv@localhost tools]# vi awstats_configure.pl


#!/usr/bin/perl


.............




[iyunv@localhost tools]# pwd


/usr/local/awstats/tools


[iyunv@localhost tools]# perl awstats_configure.pl  运行配置文件


DSC0000.gif
Do you want to continue setup from this NON standard directory [yN] ?  你是否安装非标准目录【y / n】?
..............
file (required if first install) [y/N] ? 文件(如果需要先安装)【y / n】?
............
your web site ,virtual server or profile name: 你的网站服务器名或文件名
>demo
............
directory path to store config file(s)  (enter for default):  目录路径存储配置文件(填写默认值)
> /etc/awstats
...........
然后,两个回车配置完成。





  


awstats_configure.pl工具会自动修改您的apache配置,在apache的配置文件httpd.conf文件中自动修改下面信息


[iyunv@localhost conf]# pwd


/usr/local/apache/conf


[iyunv@localhost conf]# vi httpd.conf


awstats_configure.pl自动在httpd.conf文件最底部添加了以下内容:



# Directives to allow use of AWStats as a CGI
#
Alias /awstatsclasses "/usr/local/awstats/wwwroot/classes/"
Alias /awstatscss "/usr/local/awstats/wwwroot/css/"
Alias /awstatsicons "/usr/local/awstats/wwwroot/icon/"
ScriptAlias /awstats/ "/usr/local/awstats/wwwroot/cgi-bin/"
#
# This is to permit URL access to scripts/files in AWStats directory.
#
<Directory &quot;/usr/local/awstats/wwwroot&quot;>
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>




  awstats_configure.pl还将根据安装过程中您的回答(要分析的网站名称)信息,自动生产 /etc/awstats/awstats.index.conf文件


[iyunv@localhost awstats]# pwd


/etc/awstats


[iyunv@localhost awstats]# ls


awstats.index.conf


[iyunv@localhost awstats]# vi awstats.index.conf  配置下面文件



................
LogFile=&quot;/usr/local/apache/logs/access_log&quot;     改为apache的日志文件

....





这样就完成了awstats的安装与配置工作。






分析日志




[iyunv@localhost cgi-bin]# pwd


/usr/local/awstats/wwwroot/cgi-bin


[iyunv@localhost cgi-bin]# chmod &#43;x  awstats.pl     加可执行权限


[iyunv@localhost cgi-bin]# perl /awstats.pl -update -config=demo      



Create/Update database for config &quot;/etc/awstats/awstats.index.conf&quot; by AWStats version 7.0 (build 1.971)
From data in log file &quot;/usr/local/apache/logs/access_log&quot;...
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: 3
Found 0 dropped records,
Found 0 comments,
Found 0 blank records,
Found 3 corrupted records,
Found 0 old records,
Found 0 new qualified records.





  




在浏览器里输入:


http://localhost/awstats/awstats.pl?config=demo


然后就可以看到生成的日志报告了




有可能与到下面的错误提示,不管你有没有遇到,反正我是遇到了!:



Not Found
The requested URL /awastats/awastats.pl was not found on this server.
-----------------------------------------------------------------------------
Apache/2.2.3 (CentOS) Server at 192.168.0.105 Port 80





上面的错误一直没找到原因,于是,找了另一种办法来访问日志文件。


[iyunv@localhost cgi-bin]# pwd


/usr/local/awstats/wwwroot/cgi-bin


[iyunv@localhost cgi-bin]# perl awstats.pl -update -config=demo -output -staticlinks >/var/www/html/awastats.html


这样我们把日志文件保存到apache 服务器下面,名为awastats.html


在浏览器里输入:


http://localhost/awstats.html


一样可以访问日志文件噢!



  


  


  


  ----------------------------------------------------------------------------------我本地的用法------------------------------------------------------------------------------------------
  安装perl ,

yum install perl-libwww-perl.noarch

安装awstats

yum install awstats-*   



# wget “http://prdownloads.sourceforge.net/awstats/awstats-7.0-1.noarch.rpm”

# rpm -ivh awstats-7.0-1.noarch.rpm





安装完成,配置可以参考以下资料 !


  


  • /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=demo
  • perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=demo -output -staticlinks >/html/awastats.html




#打开该网址的配置文件,修改其实的log路径,和一些其他参数

vi /etc/awstats/awstats.appcheck.conf





#进行统计并将结果输出到html

perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=appcheck -output -staticlinks >/usr/local/awstats/wwwroot/20140506.html

  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-144232-1-1.html 上篇帖子: 400万pv/日.apache/php/apc/memcache/awstats/jpgraph/cronlog 下篇帖子: Ubuntu下 Awstats的安装以及设置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表