自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

日志分析工具Awstats實戰(zhàn)之Nginx篇:分析結(jié)果靜態(tài)化

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

前言:

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

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

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

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

環(huán)境:

  1. CentOS 6.4 x86_64 
  2. ip:192.168.1.113 域名:www.sunsky.com 
  3. nginx-1.2.9 編譯安裝,路徑/usr/local/nginx,服務(wù)開啟狀態(tài) 
  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自動執(zhí)行,切割昨天的日志(交由awstats分析),壓縮前天的日志(壓縮日志可減小存儲空間,為防止awstats沒有分析完就被壓縮,所以只壓縮前天的日志)。

  1. vim /server/scripts/cut_nginx_log.sh 

輸入以下內(nèi)容:

  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 

執(zhí)行crontab -e將該腳本加入定時任務(wù)中

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

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

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

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

#p#

二、Awstats的安裝與配置

1.部署awstats

首先我們要下載awstats軟件包,并將其放在常規(guī)目錄(/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下載下來的包中權(quán)限是非root的,所以這里要修改權(quán)限,否則稍后*.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 

接下來我們要執(zhí)行awstats/tools下的awstats_configure.pl配置向?qū)?,用來生成awstats的配置文件,awstats配置文件的命名規(guī)則是awstats.website.conf。

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

此時會出現(xiàn)如下提示:

  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      #這里讓填寫網(wǎng)頁服務(wù)器的配置文件路徑,因為我們用的不是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. #詢問是否創(chuàng)建一個新的配置文件,這里填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. #這里讓填寫你的網(wǎng)站域名,虛擬主機名或者隨便一個配置名 
  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定時任務(wù),需要稍后自己添加,我們按回車繼續(xù)即可 
  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... #提示配置文件創(chuàng)建完成和如何更新配置及建立靜態(tài)報告頁,這里我們回車即可結(jié)束這個配置向?qū)?nbsp;

2、修改awstats配置文件

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

  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的數(shù)據(jù)庫配置文件(即awstats的數(shù)據(jù)庫(純文本))。

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

  1. mkdir /usr/local/awstats/data 

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

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

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

3、創(chuàng)建awstats統(tǒng)計結(jié)果存放目錄

現(xiàn)在我們要創(chuàng)建awstats統(tǒng)計結(jié)果的數(shù)據(jù)庫存放目錄:

  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目錄下搜索,根據(jù)讀取到的配置文件運行程序,去讀取-config中的參數(shù),把www.sunsky.com擴展成 awstats.www.sunsky.com.conf

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

然后是輸出:分兩種形式

1、一種是通過cgi程序讀取統(tǒng)計結(jié)果數(shù)據(jù)庫輸出;

2、一種是運行后臺腳本將輸出導(dǎo)出成靜態(tài)文件;

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

  1. vim /server/scripts/awstats.sh 

輸入以下內(nèi)容:

  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 

腳本內(nèi)容講解:

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

#p#

三、配置nginx實現(xiàn)安全訪問

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

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

在server{}內(nèi)添加如下內(nèi)容:

  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;          #根據(jù)自己的網(wǎng)站域名進行更改首頁文件 
  11. autoindex on; 
  12. access_log off; 
  13. charset gb2312; 
  14. auth_basic "Restricted";                                          #有些網(wǎng)站不愿意公開網(wǎng)站流量信息,所以加個認證 
  15. auth_basic_user_file /usr/local/nginx/htpasswd.pass;   #該文件由apache的加密認證工具htpasswd創(chuàng)建 
  16. location ~ ^/icon/ { 
  17. root   /usr/local/awststs/wwwroot; 
  18. index  index.html; 
  19. access_log off; 
  20. charset gb2312; 

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

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

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

至此,awstats已經(jīng)可以實現(xiàn)對Nginx的日志統(tǒng)計及靜態(tài)化的安全訪問功能了。

四、配置awstats自動運行

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

  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上的配置就完成了。當然此篇是基于靜態(tài)頁面的顯示的,后面會再寫一篇日志分析工具Awstats實戰(zhàn)之Nginx篇-分析結(jié)果動態(tài)化出來和大家一起交流學(xué)習(xí)的。

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

責(zé)任編輯:黃丹 來源: 博客
相關(guān)推薦

2013-10-31 16:17:45

日志分析Awstats實戰(zhàn)Nginx

2013-11-01 10:43:35

日志分析Awstats實戰(zhàn)Apache

2022-06-29 09:19:09

靜態(tài)代碼C語言c代碼

2015-07-31 10:57:01

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

2021-06-08 13:56:34

工具靜態(tài)代碼

2010-03-25 18:09:23

Nginx配置文件

2017-09-14 10:45:47

PostgreSQL日志分析pgBadger

2012-09-20 10:07:29

Nginx源碼分析Web服務(wù)器

2019-03-20 13:44:30

Web 開發(fā)代碼

2016-05-17 13:54:05

2014-02-12 10:28:50

Hadoop

2021-07-29 06:37:55

KubernetesKubeLinter工具

2021-04-27 08:57:58

開發(fā)技能代碼

2022-12-09 15:38:54

Cppcheck靜態(tài)分析工具

2022-12-13 15:42:56

Clang-Tidy靜態(tài)分析工具

2024-08-06 09:40:21

2023-10-30 09:01:08

Nginx日志分析

2023-07-07 06:53:56

遠程軟件日志向日葵

2024-03-19 08:02:28

集群GaussDB指標

2022-03-25 00:00:00

Splunk搜索SPL
點贊
收藏

51CTO技術(shù)棧公眾號