使用AWSTATS自动分析Nginx日志

环境说明:
Nginx安装目录:/usr/local/nginx
Nginx配置文件存放目录:/usr/local/nginx/conf/nginx.conf
Nginx日志目录:/usr/local/nginx/logs
Awstats安装目录:/usr/local/awstats
Awstats配置文件存放目录:/etc/awstats
日志切割脚本存放目录:/usr/local/nginx/sbin/cut_nginx_log.sh
一、Nginx日志切割
1、修改nginx.conf配置文件,使AWSTATS支持分析日志格式
# vim /usr/local/nginx/conf/nginx.conf
log_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/bash
mv  /usr/local/nginx/logs/access.log /usr/local/nginx/logs/access_`date +%Y%m%d`.log
mv  /usr/local/nginx/logs/error.log /usr/local/nginx/logs/error_`date +%Y%m%d`.log
killall  -s  USER1  nginx
# chmod  +x  /usr/local/nginx/sbin/cut_nginx_log.sh
3、 
二、下载安装Awstats
1、下载安装
# 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 Unix
Warning: AWStats standard directory on Linux OS is '/usr/local/awstats'.
If you want to use standard directory, you should first move all content
of AWStats distribution from current directory:
/tmp/src
to standard directory:
/usr/local/awstats
And then, run configure.pl from this location.
Do you want to continue setup from this NON standard directory [yN] ?输入y
回车
-----> 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
Config 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/profile
file (required if first install) [y/N] ?输入 y #创建一个全新的统计
回车
-----> Define config file name to create
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:
> 192.168.2.143被统计网站的域名 #
回车
-----> 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):
>  #回车继续
-----> 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 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:
/tmp/src/wwwroot/cgi-bin/awstats.pl -update -config=192.168.2.143
Or if you have several config files and prefer having only one command:
/tmp/src/tools/awstats_updateall.pl now
Press ENTER to continue...
A SIMPLE config file has been created: /etc/awstats/awstats.192.168.2.143.conf
You 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.143
You can also build static report pages for '192.168.2.143' with command:
> perl awstats.pl -output=pagetype -config=192.168.2.143
Press 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.143
Create/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: 0
Parsed lines in file: 66466
Found 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.conf
server {
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.sh
23 59 * * * root /usr/local/nginx/sbin/cut_nginx_log.sh
00 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

转载于:https://blog.51cto.com/darren/1081974

使用AWSTATS自动分析Nginx日志相关推荐

  1. 烂泥:利用awstats分析nginx日志

    昨天把nginx的日志进行了切割,关于如何切割nginx日志,可以查看<烂泥:切割nginx日志>这篇文章. 今天打算分析下nginx日志,要分析nginx日志,我们可以通过shell脚本 ...

  2. 利用awstats分析nginx日志

    今天打算分析下nginx日志,要分析nginx日志,我们可以通过shell脚本和第三方软件awstats进行分析,在此我们选择的是通过第三方软件awstats进行分析. 要使用awstats分析ngi ...

  3. awstats分析nginx日志

    看了awstats介绍后,感觉是个好东西,等装好来用的时候,不像那么一回事.awstats说白了就是对nginx,apache产生的日志进行分析.awstats分析出来的数据不准,日志是按照一定的规则 ...

  4. 利用awstats分析nginx日志 简单配置

    1.nginx日志分割 创建日志分割脚本,用crontab定时执行此脚本. #vim /wwwlog/xxx.com/logcron.sh #!/bin/bash log_dir="/www ...

  5. 使用awstats分析nginx日志

    参考文章:http://www.ibm.com/developerworks/cn/linux/l-cn-awstats-nginx/index.html 前言 在实际的网站运营中,我们经常需要了解到 ...

  6. awstats linux日志分析,日志分析:用AWStats分析Nginx的访问日志,它可以统计您站点的如...

    日志分析:用AWStats分析Nginx的访问日志,它可以统计您站点的如 Awstats是一个免费非常简洁而且强大有个性的统计工具.它可以统计您站点的如下信息: 一:访问量,访问次数,页面浏览量,点击 ...

  7. python nginx日志分析_Nginx日志分析及脚本编写

    在我们日常的运维中,当Nginx服务器正常运行后,我们会经常密切关注Nginx访问日志的相关情况,发现有异常的日志信息需要进行及时处理. 那今天我将跟大家一起来研究和分析Nginx日志,nginx默认 ...

  8. 使用logrotate管理nginx日志文件

    本文转载自:http://linux008.blog.51cto.com/2837805/555829 描述:linux日志文件如果不定期清理,会填满整个磁盘.这样会很危险,因此日志管理是系统管理员日 ...

  9. 使用GoAccess分析Nginx日志

    下载 GoAccess 的源代码.编译和安装: http://www.goaccess.io/download # wget http://tar.goaccess.io/goaccess-0.9.6 ...

最新文章

  1. Cocos本地存储LocalStorage
  2. [改善Java代码]优先使用整型池
  3. 吴恩达 coursera ML 第十四课总结+作业答案
  4. react 动态路 嵌套动子路由_2020年,我是如何从一名Vueer转岗到React阵营
  5. 计算机win7内容已满,Win7旗舰版电脑C盘满了怎么清理
  6. 木马藏身于系统进程中
  7. 信安精品课:第3章密码学基本理论精讲笔记
  8. 我眼中的性能测试工程师
  9. qwidget show 是否有信号_c++笔记(信号集处理函数)
  10. Python接口测试
  11. Win11怎么把右键刷新调出来?
  12. FISCO BCOS 同态加密 实例
  13. Unity3D 字体设置
  14. rustdesk:远控工具说明
  15. 一篇文章读懂拿了图灵奖和诺贝尔奖的概率图模型
  16. 台式计算机cpu允许温度,台式机cpu温度多少正常 台式电脑工作时温度范围
  17. 前端屏幕尺寸和分辨率_移动端尺寸基础知识
  18. 北方直播卖货搞钱“第一城“,竟然是临沂,200万人发家快手电商
  19. CentOS系统磁盘的分区格式化和挂载操作
  20. 基于stm32的MAX31865铂电阻PT100测温全套资料

热门文章

  1. vue源码解析pdf_Vue源码全面解析八 callHook函数(触发生命周期函数)
  2. python高级开发面试题_python面试的100题(16)
  3. python2.7环境下“No module named matplotlib.pyplot”的解决办法
  4. Verilog 中的 function
  5. 充分的总结分析系统,来调整CMDB及流程。
  6. [剑指Offer]5.二维数组中的查找
  7. ACE_Task::putq(转)
  8. c语言中较常见的由内存分配引起的错误_内存越界_内存未初始化_内存太小_结构体隐含指针...
  9. 常用Intent调用(摘自网络)
  10. 针对MySQL数据库主从延迟的问题