前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >awstats分析nginx日志「建议收藏」

awstats分析nginx日志「建议收藏」

作者头像
全栈程序员站长
发布2022-09-13 10:17:00
5440
发布2022-09-13 10:17:00
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

看了awstats介绍后,感觉是个好东西,等装好来用的时候,不像那么一回事。awstats说白了就是对nginx,apache产生的日志进行分析。awstats分析出来的数据不准,日志是按照一定的规则来生成的,把访问数据存入到文件中,但是数据存入的时候可能是不全的,awstats分析的时候就有误差。

一,安装awstats

yum install awstats

二,配置awstats

1,nginx的日志格式

查看复制打印?

  1. log_format access_www ‘remote_addr – remote_user [time_local] “request” ‘
  2. status body_bytes_sent “
  3. ‘”http_user_agent” http_x_forwarded_for’;
  4. access_log /var/log/nginx/access.log access_www;

awstats分析现有的日志文件,产生的日志文件名,从头到尾要对得上,要不然会报错的。

2,切割日志脚本cut.sh

查看复制打印?

  1. [root@localhost access] cat /home/zhangy/cut.sh
  2. #!/bin/bash
  3. logs_path=”/var/log/nginx/” #nginx日志位置
  4. # 你的日志名, access_www是web服务器对应log名,accesss_img是图片服务器对应的log名
  5. logs_names=(access_www access_img)
  6. num=${#logs_names[@]} #数组总数
  7. for((i=0;i<num ;i++));do #循环
  8. if [ ! -d {logs_path}/{logs_names[i]} ] #判断目录是不是存在
  9. then
  10. mkdir -p {logs_path}{logs_names[i]} #不存在,建之,日志重新建文件夹,切割后文件很多,所以分目录
  11. fi
  12. log_name=”{logs_path}{logs_names[i]}.log” #原始目录位置
  13. if [ -f “$log_name” ]; then
  14. mv {logs_path}{logs_names[i]}.log {logs_path}{logs_names[i]}/{logs_names[i]}.log-(date -d “yesterday” +”%Y%m%d”)
  15. fi
  16. done
  17. nginx -s reopen #重新生成原始日志文件

3,awstats_configure.pl生成配置文件

查看复制打印?

  1. [root@localhost awstats]# find / -name “awstats_configure.pl” -print
  2. /usr/share/awstats/tools/awstats_configure.pl

yum装的,不知道什么位置查找一下。下面重点说一下,生成过程

查看复制打印?

  1. [root@localhost access]# /usr/share/awstats/tools/awstats_configure.pl
  2. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
  3. Do you want to continue setup from this NON standard directory [yN] ? y
  4. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
  5. Config file path (‘none’ to skip web server setup):
  6. > none #用的是nginx上面提示都不符合,在这里选择none
  7. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
  8. Do you want me to build a new AWStats config/profile
  9. file (required if first install) [y/N] ? y #是否要求新建配置文件
  10. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
  11. Your web site, virtual server or profile name:
  12. > access_www #这里填写的东西,根日志的名称一样就行了,域名,非域名都行
  13. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
  14. #下面一路回车就行了。

这个操作完了以后,会在/etc/awstats下面生成一个配置文件。

  1. [root@localhost awstats]# ls /etc/awstats/ |grep access
  2. awstats.access_www.conf

如果你分析多个域名的日志,就把这一步多操作几次

4,修改生成的配置文件

修改二个部LogFile和LogFormat

查看复制打印?

  1. LogFile=”/var/log/nginx/access/access.log-%YYYY-24%MM-24%DD-24″
  2. LogFormat=”%host %logname %time1 %methodurl %code %bytesd %refererquot %uaquot %other”

-24表示一天前的,-0表示当前,配置文件里面有说明

5,生成awstats的静态html

查看复制打印?

  1. [root@localhost awstats]# mkdir -p /var/www/awstats
  2. [root@localhost awstats]# chown -R nginx:nginx /var/www/awstats #nginx的启动用户

上面建了一个web目录awstats来存生成的html

查看复制打印?

  1. [root@localhost ~]# cat /home/zhangy/static.sh
  2. #!/bin/bash
  3. # The Nginx logs path
  4. logs_names=(access_www access_img) #log的名称
  5. web_path=”/var/www/awstats” #静态html的路径
  6. cgi_path=”/usr/share/awstats/wwwroot/cgi-bin” #awstats可执行文件的存放路径
  7. static_path=”/usr/share/awstats/tools” #awstats可执行文件的存放路径
  8. num=${#logs_names[@]}
  9. for((i=0;i<num ;i++));do
  10. if [ ! -d {web_path}/{logs_names[i]} ]
  11. then
  12. mkdir -p {web_path}/{logs_names[i]}
  13. fi
  14. ${static_path}/awstats_buildstaticpages.pl -update \
  15. -config={logs_names[i]} -lang=cn -dir={web_path}/
  16. -awstatsprog=${cgi_path}/awstats.pl
  17. done

当执行完static.sh后,会在/var/lib/awstats/下面生成一个文件,里面存放了分析后的数据

查看复制打印?

  1. [root@localhost awstats]# ls /var/lib/awstats/ |grep access
  2. awstats052012.access_www.txt

http://www.XXX.com/awstats/access_www/awstats.access_www.html

可以通过这个页面来访问,前提是nginx的root设置的目录是/var/www才行。

看一下效果图

awstats
awstats

awstats

总结:

awstats每次只能分析一个log文件,也就是说我有10日志文件,当我们运行static.sh的时候,只会读取一个。所以每天都要用awstats来分析日志,/var/lib/awstats下面的数据文件会不断的增加,用crontab定时跑一下。

查看复制打印?

  1. 59 23 * * * /home/zhangy/cut.sh #夜里12点切日志
  2. 30 04 * * * /home/zhangy/static.sh #凌晨4点生成静态html

转载请注明 作者:海底苍鹰 地址:awstats分析nginx日志«海底苍鹰(tank)博客

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/160194.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档