成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

日志分析工具Awstats實戰之Nginx篇:分析結果靜態化

運維 系統運維
Awstats 的運行是需要 PERL 環境的支持,從 awstats 的文檔來看,它對 Apache HTTP Server 的支持是非常完美的,而當我們把 Web 服務器換成 Nginx 后,要運行 awstats 變得很麻煩。首先 Nginx 本身對 Perl 的支持是比較弱的,甚至官方也不建議使用;另外在日志格式上有需要修改后才能運行。

前言:

Awstats 是在 SourceForge 上發展很快的一個基于 Perl 的 WEB 日志分析工具,一個充分的日志分析讓 Awstats 顯示您下列資料:

  • 訪問次數、獨特訪客人數,
  • 訪問時間和上次訪問,
  • 使用者認證、最近認證的訪問,
  • 每周的高峰時間(頁數,點擊率,每小時和一周的千字節),
  • 域名/國家的主機訪客(頁數,點擊率,字節,269域名/國家檢測, geoip 檢測),
  • 主機名單,最近訪問和未解析的 IP 地址名單
  • 大多數看過的進出頁面,
  • 檔案類型,
  • 網站壓縮統計表(mod_gzip 或者 mod_deflate),
  • 使用的操作系統 (每個操作系統的頁數,點擊率 ,字節, 35 OS detected),
  • 使用的瀏覽器,
  • 機器人訪問(檢測 319 個機器人),
  • 蠕蟲攻擊 (5 個蠕蟲家族),
  • 搜索引擎,利用關鍵詞檢索找到你的地址,
  • HTTP 協議錯誤(最近查閱沒有找到的頁面),
  • 其他基于 URL 的個性報導,鏈接參數, 涉及綜合行銷領域目的.
  • 貴網站被加入"最喜愛的書簽".次數.
  • 屏幕大小(需要在索引頁補充一些 HTML 標簽).
  • 瀏覽器的支持比例: Java, Flash, RealG2 reader, Quicktime reader, WMA reader, PDF reader.
  • 負載平衡服務器比率集群報告.

Awstats 的運行是需要 PERL 環境的支持,從 awstats 的文檔來看,它對 Apache HTTP Server 的支持是非常完美的,而當我們把 Web 服務器換成 Nginx 后,要運行 awstats 變得很麻煩。首先 Nginx 本身對 Perl 的支持是比較弱的,甚至官方也不建議使用;另外在日志格式上有需要修改后才能運行。

使用awstats可以分析apache日志,同樣也可以分析nginx日志。本文將詳細介紹自動定時切割nginx的訪問日志,并使用awstats來定時分析nginx日志及實現統計結果可供安全便捷的查閱。

環境:

  1. CentOS 6.4 x86_64 
  2. ip:192.168.1.113 域名:www.sunsky.com 
  3. nginx-1.2.9 編譯安裝,路徑/usr/local/nginx,服務開啟狀態 
  4. 日志記錄格式為nginx默認的,切勿更改,否則會造成awstats無法分析日志。 
  5. log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 
  6. '$status $body_bytes_sent "$http_referer" ' 
  7. '"$http_user_agent" "$http_x_forwarded_for"'; 
  8. awstats-7.2.tar.gz 

一、日志自動切割

對于nginx的日志切割,由于沒有像apache一樣去用cronolog工具,這里我們就寫一個腳本,讓它可以在每天00:01自動執行,切割昨天的日志(交由awstats分析),壓縮前天的日志(壓縮日志可減小存儲空間,為防止awstats沒有分析完就被壓縮,所以只壓縮前天的日志)。

  1. vim /server/scripts/cut_nginx_log.sh 

輸入以下內容:

  1. #!/bin/sh 
  2. yesterday=`date -d "yesterday" +"%Y%m%d"` 
  3. before_yesterday=`date -d "-2 day" +"%Y%m%d"` 
  4. Nginx_Dir="/usr/local/nginx" 
  5. Nginx_logs="/app/logs" 
  6. Log_Name="www_access" 
  7. cd /tmp 
  8. [ -d $Nginx_Logs ] && cd $Nginx_logs || exit 1 
  9. [ -f $Log_Name.log ] && /bin/mv $Log_Name.log ${Log_Name}_${yesterday}.log || exit 1 
  10. if [ $? -eq 0 -a -f $Nginx_Dir/logs/nginx.pid ] 
  11. then 
  12. kill -USR1 `cat $Nginx_Dir/logs/nginx.pid` 
  13. fi 
  14. [ -f  ${Log_Name}_${before_yesterday}.log ] && /usr/bin/gzip ${Log_Name}_${before_yesterday}.log|| exit 1 

執行crontab -e將該腳本加入定時任務中

  1. 1 0 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1 

這樣每天凌晨00:01就能自動實現日志的切割,壓縮等功能了。

因為本次實驗下的nginx此時已經有日志了,另外為了后文awstats能對切割過的日志進行分析,所以這里我們要運行一下此腳本,來將現有日志進行切割生成昨天的日志方便后文操作。

  1. /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1 

#p#

二、Awstats的安裝與配置

1.部署awstats

首先我們要下載awstats軟件包,并將其放在常規目錄(/usr/local)下:

  1. wget http://awstats.sourceforge.net/files/awstats-7.2.tar.gz 
  2. tar zxf awstats-7.2.tar.gz 
  3. mv awstats-7.2 /usr/local/awstats 

由于wget下載下來的包中權限是非root的,所以這里要修改權限,否則稍后*.pl將無法運行

  1. chown -R root.root /usr/local/awstats 
  2. chmod +x /usr/local/awstats/tools/*.pl 
  3. chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl 

接下來我們要執行awstats/tools下的awstats_configure.pl配置向導,用來生成awstats的配置文件,awstats配置文件的命名規則是awstats.website.conf。

  1. cd /usr/local/awstats/tools/ 
  2. ./awstats_configure.pl 

此時會出現如下提示:

  1. ----- AWStats awstats_configure 1.0 (build 1.9) (c) Laurent Destailleur ----- 
  2. This tool will help you to configure AWStats to analyze statistics for  
  3. one web server. You can try to use it to let it do all that is possible 
  4. in AWStats setup, however following the step by step manual setup 
  5. documentation (docs/index.html) is often a better idea. Above all if: 
  6. - You are not an administrator user, 
  7. - You want to analyze downloaded log files without web server, 
  8. - You want to analyze mail or ftp log files instead of web log files, 
  9. - You need to analyze load balanced servers log files, 
  10. - You want to 'understand' all possible ways to use AWStats... 
  11. Read the AWStats documentation (docs/index.html). 
  12. -----> Running OS detected: Linux, BSD or Unix 
  13. -----> Check for web server install 
  14. Enter full config file path of your Web server. 
  15. Example: /etc/httpd/httpd.conf 
  16. Example: /usr/local/apache2/conf/httpd.conf 
  17. Example: c:\Program files\apache group\apache\conf\httpd.conf 
  18. Config file path ('none' to skip web server setup): 
  19. > none      #這里讓填寫網頁服務器的配置文件路徑,因為我們用的不是apache,所以這里要填none 
  20. Your web server config file(s) could not be found. 
  21. You will need to setup your web server manually to declare AWStats 
  22. script as a CGI, if you want to build reports dynamically. 
  23. See AWStats setup documentation (file docs/index.html) 
  24. -----> Update model config file '/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf' 
  25. File awstats.model.conf updated. 
  26. -----> Need to create a new config file ? 
  27. Do you want me to build a new AWStats config/profile 
  28. file (required if first install) [y/N] ? y          
  29. #詢問是否創建一個新的配置文件,這里填y 
  30. -----> Define config file name to create 
  31. What is the name of your web site or profile analysis ? 
  32. Example: www.mysite.com 
  33. Example: demo 
  34. Your web site, virtual server or profile name: 
  35. > www.sunsky.com       
  36. #這里讓填寫你的網站域名,虛擬主機名或者隨便一個配置名 
  37. -----> Define config file path 
  38. In which directory do you plan to store your config file(s) ? 
  39. Default: /etc/awstats 
  40. Directory path to store config file(s) (Enter for default): 
  41. >#這里要填寫你配置文件存放路徑,我們使用它默認的路徑/etc/awstats,所以直接回車即可 
  42. -----> Create config file '/etc/awstats/awstats.www.sunsky.com.conf' 
  43. Config file /etc/awstats/awstats.www.sunsky.com.conf created. 
  44. -----> Add update process inside a scheduler 
  45. Sorry, configure.pl does not support automatic add to cron yet. 
  46. You can do it manually by adding the following command to your cron: 
  47. /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.sunsky.com 
  48. Or if you have several config files and prefer having only one command: 
  49. /usr/local/awstats/tools/awstats_updateall.pl now 
  50. Press ENTER to continue...   
  51. #提示不能自動加入crontab定時任務,需要稍后自己添加,我們按回車繼續即可 
  52. A SIMPLE config file has been created: /etc/awstats/awstats.www.sunsky.com.conf 
  53. You should have a look inside to check and change manually main parameters. 
  54. You can then manually update your statistics for 'www.sunsky.com' with command: 
  55. > perl awstats.pl -update -config=www.sunsky.com 
  56. You can also build static report pages for 'www.sunsky.com' with command: 
  57. > perl awstats.pl -output=pagetype -config=www.sunsky.com 
  58. Press ENTER to finish... #提示配置文件創建完成和如何更新配置及建立靜態報告頁,這里我們回車即可結束這個配置向導 

2、修改awstats配置文件

完成配置文件的創建之后,我們還需要對/etc/awstats/awstats.www.sunsky.com.conf里的一些參數進行修改。

  1. sed -i 's#LogFile="/var/log/httpd/mylog.log"#LogFile="/app/logs/www_access_%YYYY-24%MM-24%DD-24.log"#g' /etc/awstats/awstats.www.sunsky.com.conf 

這里更改的目的是指定awstats需要分析的nginx的日志文件路徑。這里的路徑大家要按自己的日志路徑來填。

  1. sed -i 's#DirData="/var/lib/awstats"#DirData="/usr/local/awstats/data"#g' /etc/awstats/awstats.www.sunsky.com.conf 

這里更改的目的是指定awstats的數據庫配置文件(即awstats的數據庫(純文本))。

由于,此處沒有/usr/local/awstats/data目錄,所以我們要創建出來

  1. mkdir /usr/local/awstats/data 

以上的兩個替換操作進行完之后一定要用命令查看替換是否成功,以便及早發現紕漏。

  1. grep "LogFile=" /etc/awstats/awstats.www.sunsky.com.conf 
  2. grep "DirData=" /etc/awstats/awstats.www.sunsky.com.conf 

查詢替換結果正確之后,即可進行下面的步驟。

3、創建awstats統計結果存放目錄

現在我們要創建awstats統計結果的數據庫存放目錄:

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

如果屏幕輸出類似下面的提示就說明配置文件都正確無誤了:

  1. Create/Update database for config "/etc/awstats/awstats.www.sunsky.com.conf" by AWStats version 7.2 (build 1.992) 
  2. From data in log file "/app/logs/www_access.log"... 
  3. Phase 1 : First bypass old records, searching new record... 
  4. Searching new records from beginning of log file... 
  5. Jumped lines in file: 0 
  6. Parsed lines in file: 0 
  7. Found 0 dropped records, 
  8. Found 0 comments, 
  9. Found 0 blank records, 
  10. Found 0 corrupted records, 
  11. Found 0 old records, 
  12. Found 0 new qualified records. 

注釋:awstats.pl 會到 /etc/awstats目錄下搜索,根據讀取到的配置文件運行程序,去讀取-config中的參數,把www.sunsky.com擴展成 awstats.www.sunsky.com.conf

分析日志:運行后將這樣的日志統計結果歸檔到一個awstats的數據庫(純文本)里;

然后是輸出:分兩種形式

1、一種是通過cgi程序讀取統計結果數據庫輸出;

2、一種是運行后臺腳本將輸出導出成靜態文件;

統計分析完成后,結果還在 Awstats 的數據庫中。在 Apache 上,可以直接打開 Perl 程序的網頁查看統計。 但Nginx 對 Perl 支持并不好,所以要換個方法,利用 awstats 的工具將統計的結果生成靜態文件,這里方便我們還是用腳本來實現:

  1. vim /server/scripts/awstats.sh 

輸入以下內容:

  1. #!/bin/sh 
  2. Awstats_Dir="/usr/local/awstats" 
  3. [ -d /www/awstats ]||mkdir /www/awstats 
  4. $Awstats_Dir/wwwroot/cgi-bin/awstats.pl -update -config=www.sunsky.com 
  5. $Awstats_Dir/tools/awstats_buildstaticpages.pl -update -config=www.sunsky.com -awstatsprog=$Awstats_Dir/wwwroot/cgi-bin/awstats.pl -lang=cn -dir=/www/awstats 

腳本內容講解:

  1. /usr/local/awstats/tools/awstats_buildstaticpages.pl    Awstats 靜態頁面生成工具 
  2. -update -config=www.sunsky.com  更新配置項 
  3. -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl  Awstats 日志更新程序路徑 
  4. -lang=cn     語言為中文 
  5. -dir= /www/awstats  統計結果輸出目錄 
  6. awstats_buildstaticpages.pl會根據-config的參數去讀取里面的DirData路徑下的數據庫配置文件,然后生成靜態的html文件,生成的文件重定向到/www/awstats目錄下。 

#p#

三、配置nginx實現安全訪問

接下來我們要配置nginx使其能安全的訪問到分析的數據:

  1. vim /usr/local/nginx/conf/nginx.conf 

在server{}內添加如下內容:

  1. server { 
  2. listen       80; 
  3. server_name  www.sunsky.com; 
  4. location / { 
  5. root   /www/sunsky; 
  6. index  index.html index.htm; 
  7. access_log /app/logs/www_access.log commonlog; 
  8. location ~ ^/awstats/ { 
  9. root   /www/; 
  10. index  awstats.www.sunsky.com.html;          #根據自己的網站域名進行更改首頁文件 
  11. autoindex on; 
  12. access_log off; 
  13. charset gb2312; 
  14. auth_basic "Restricted";                                          #有些網站不愿意公開網站流量信息,所以加個認證 
  15. auth_basic_user_file /usr/local/nginx/htpasswd.pass;   #該文件由apache的加密認證工具htpasswd創建 
  16. location ~ ^/icon/ { 
  17. root   /usr/local/awststs/wwwroot; 
  18. index  index.html; 
  19. access_log off; 
  20. charset gb2312; 

由于nginx沒有好的加密認證工具,需要借助apache的htpasswd來實現加密認證功能:

  1. htpasswd -c -m /usr/local/nginx/htpasswd.pass sunskyadmin      #用戶名為sunskyadmin 

配置完畢之后,檢查nginx語法,然后優雅重啟之后,用游覽器訪問www.sunsky.com/awstats,輸入賬號密碼之后即可查看統計信息了。

至此,awstats已經可以實現對Nginx的日志統計及靜態化的安全訪問功能了。

四、配置awstats自動運行

為了讓整個日志的統計過程可以實現自動化,將awstats.sh腳本加入crontab定時任務中去,此時結合上面的定時切割任務,我們的crontab里面會有多出來兩條定時任務

  1. 1 0 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1 
  2. 0 1 * * * /bin/sh /server/scripts/awstats.sh >/dev/null 2>&1 

到此,我們整個日志訪問工具awstats在nginx上的配置就完成了。當然此篇是基于靜態頁面的顯示的,后面會再寫一篇日志分析工具Awstats實戰之Nginx篇-分析結果動態化出來和大家一起交流學習的。

本文作者:sunsky,博客地址:http://sunsky.blog.51cto.com/

責任編輯:黃丹 來源: 博客
相關推薦

2013-10-31 16:17:45

日志分析Awstats實戰Nginx

2013-11-01 10:43:35

日志分析Awstats實戰Apache

2022-06-29 09:19:09

靜態代碼C語言c代碼

2015-07-31 10:57:01

安全日志windows安全日志安全日志分析

2021-06-08 13:56:34

工具靜態代碼

2010-03-25 18:09:23

Nginx配置文件

2017-09-14 10:45:47

PostgreSQL日志分析pgBadger

2012-09-20 10:07:29

Nginx源碼分析Web服務器

2019-03-20 13:44:30

Web 開發代碼

2014-02-12 10:28:50

Hadoop

2021-07-29 06:37:55

KubernetesKubeLinter工具

2016-05-17 13:54:05

2021-04-27 08:57:58

開發技能代碼

2024-08-06 09:40:21

2022-12-09 15:38:54

Cppcheck靜態分析工具

2022-12-13 15:42:56

Clang-Tidy靜態分析工具

2022-03-21 15:02:05

Harmonyhiperf鴻蒙

2024-03-19 08:02:28

集群GaussDB指標

2022-03-25 00:00:00

Splunk搜索SPL

2023-10-30 09:01:08

Nginx日志分析
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲精品自在在线观看 | 一区二区三区免费 | 久久久久国产一区二区三区四区 | 成人片免费看 | 精品一二| 国产精品久久久久久久久免费相片 | 欧美一级二级视频 | 国产精品久久久久久久久久 | 国产高清一区二区三区 | 欧美4p| 国产91久久精品一区二区 | 中文字幕免费在线 | 亚洲一区二区三区视频 | 日韩欧美在线一区 | 超碰人人做 | 国产精品乱码一二三区的特点 | 欧美日韩一区二区三区四区 | 欧美lesbianxxxxhd视频社区 | 国产乱码久久久久久 | 羞羞视频网站在线观看 | 91久操视频 | 鲁大师一区影视 | 成人亚洲视频 | 亚洲69p| 偷拍自拍在线观看 | 国产精品网址 | 成人精品一区二区 | 亚洲视频国产 | 日韩一区二区精品 | 亚洲精品国产第一综合99久久 | 99re国产精品 | 人人鲁人人莫人人爱精品 | 成人午夜免费福利视频 | 国产成人综合亚洲欧美94在线 | 亚洲高清视频在线观看 | 亚洲欧美一区二区三区1000 | 成人国产精品久久 | 亚洲高清视频在线观看 | 精品视频网 | 北条麻妃99精品青青久久 | 久久国产精品免费一区二区三区 |