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

[经验分享] 使用AWStats自动分析Nginx日志

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-27 13:58:32 | 显示全部楼层 |阅读模式
环境说明:Nginx安装目录:/usr/local/nginxNginx配置文件存放目录:/usr/local/nginx/conf/nginx.confNginx日志目录:/usr/local/nginx/logsAwstats安装目录:/usr/local/awstatsAwstats配置文件存放目录:/etc/awstats日志切割脚本存放目录:/usr/local/nginx/sbin/cut_nginx_log.sh一、Nginx日志切割1、修改nginx.conf配置文件,使AWSTATS支持分析日志格式# vim /usr/local/nginx/conf/nginx.conflog_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log logs/access.log main;  
2、编辑Nginx日志切割脚本# vim /usr/local/nginx/sbin/cut_nginx_log.sh#!/bin/bashmv /usr/local/nginx/logs/access.log /usr/local/nginx/logs/access_`date +%Y%m%d`.logmv /usr/local/nginx/logs/error.log /usr/local/nginx/logs/error_`date +%Y%m%d`.logkillall -s USER1 nginx  
# chmod +x /usr/local/nginx/sbin/cut_nginx_log.sh3、二、下载安装Awstats1、下载安装# 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# mkdir -p /var/lib/awstats  
2、配置# mkdir - p /tmp/src/wwwroot/cgi-bin/# cp /usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf /tmp/src/wwwroot/cgi-bin/# /usr/local/awstats/tools/awstats_configure.pl-----> Running OS detected: Linux, BSD or UnixWarning: AWStats standard directory on Linux OS is '/usr/local/awstats'.If you want to use standard directory, you should first move all contentof AWStats distribution from current directory:/tmp/srcto standard directory:/usr/local/awstatsAnd then, run configure.pl from this location.Do you want to continue setup from this NON standard directory [yN] ?输入y  
回车-----> Check for web server installEnter full config file path of your Web server.Example: /etc/httpd/httpd.confExample: /usr/local/apache2/conf/httpd.confExample: c:\Program files\apache group\apache\conf\httpd.confConfig file path ('none' to skip web server setup):> none 这里不是apache,写none,跳过#  
回车-----> Update model config file '/tmp/src/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/profilefile (required if first install) [y/N] ?输入 y #创建一个全新的统计  
回车-----> Define config file name to createWhat is the name of your web site or profile analysis ?Example: www.mysite.comExample: demoYour web site, virtual server or profile name:> 192.168.2.143被统计网站的域名 #  
回车-----> Define config file pathIn which directory do you plan to store your config file(s) ?Default: /etc/awstatsDirectory path to store config file(s) (Enter for default):> #回车继续-----> Create config file '/etc/awstats/awstats.192.168.2.143.conf'Config file /etc/awstats/awstats.192.168.2.143.conf created.-----> Add update process inside a schedulerSorry, configure.pl does not support automatic add to cron yet.You can do it manually by adding the following command to your cron:/tmp/src/wwwroot/cgi-bin/awstats.pl -update -config=192.168.2.143Or if you have several config files and prefer having only one command:/tmp/src/tools/awstats_updateall.pl nowPress ENTER to continue...A SIMPLE config file has been created: /etc/awstats/awstats.192.168.2.143.confYou should have a look inside to check and change manually main parameters.You can then manually update your statistics for '192.168.2.143' with command:> perl awstats.pl -update -config=192.168.2.143You can also build static report pages for '192.168.2.143' with command:> perl awstats.pl -output=pagetype -config=192.168.2.143Press ENTER to finish...

三、修改统计日志文件的路径# vim /etc/awstats/awstats.192.168.2.143.conf -LogFile="/var/log/httpd/mylog.log"修改成--->LogFile="/usr/local/nginx/logs/access_%YYYY-0%MM-0%DD-0.log"  
这里是对应上面Nginx日志切割所生成的目录存放位置,注意awstats的年月日格式,分析的执行顺序是:Nginx 产生日志 –> 日志切割 –> Nginx 继续产生日志 –> 另存切割日志 –> 交由Awstats统计 –> 生成结果四、执行Awstats 日志更新程序开始统计分析#/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=192.168.2.143Create/Update database for config "/etc/awstats/awstats.192.168.2.143.conf" by AWStats version 7.0 (build 1.971)From data in log file "/usr/local/nginx/logs/access_20121207.log"...Phase 1 : First bypass old records, searching new record...Searching new records from beginning of log file...Phase 2 : Now process new records (Flush history on disk after 20000 hosts)...Jumped lines in file: 0Parsed lines in file: 66466Found 0 dropped records,Found 0 comments,Found 1 blank records,Found 58472 corrupted records,Found 0 old records,Found 7993 new qualified records.  
看到以上显示,证明日志切割和 Awstats 都已经运行无误了。统计分析完成后,结果还在 Awstats 的数据库中。在 Apache 上,可以直接打开 Perl 程序的网页查看统计。但本文开始时已经提到,Nginx 对 Perl 支持并不好,所以我们要换个方法,利用 awstats 的工具将统计的结果生成静态文件。五、Awstats生成静态文件# cp -rf /usr/local/awstats/wwwroot/ /usr/local/nginx/logs/# mkdir /usr/local/nginx/logs/wwwroot/awstats# /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=192.168.2.143 -lang=cn -dir=/usr/local/nginx/logs/wwwroot/awstats/ -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl  
/usr/local/awstats/tools/awstats_buildstaticpages.pl #Awstats 静态页面生成脚本-update -config=192.168.2.143 #更新配置选项-lang=cn #语音中文-dir=/usr/local/nginx/logs/wwwroot/awstats/ #统计结果输出目录-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl #日志更新程序路径六、修改nginx配置文件# vim /usr/local/nginx/conf/nginx.confserver {listen 8080;server_name localhost;location ~ ^/awstats/ { # html 静态页面目录root /usr/local/nginx/logs/wwwroot/awstats;index index.html;access_log off;error_log off;charset gb2312; }location ~ ^/icon/ { # 图标目录root /usr/local/nginx/logs/wwwroot;index index.html;access_log off;error_log off;charset gb2312;}}  
七、配置Awstats自动运行# vim /usr/local/nginx/sbin/cut_nginx_log.sh23 59 * * * root /usr/local/nginx/sbin/cut_nginx_log.sh00 01 * * * /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=192.168.2.143 -lang=cn -dir=/usr/local/nginx/logs/wwwroot/awstats/ -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl  
八、结果验证,URL输入http://192.168.2.143:8080/awstats/awstats.192.168.2.143.html DSC0000.jpg

运维网声明 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-144154-1-1.html 上篇帖子: AWStats: 跨平台的日志分析工具使用简介 下篇帖子: AWStats国家地区扩展GeoIP安装配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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