日志分析goaccess-nginx


yum install glib2 glib2-devel ncurses-devel -y  && yum groupinstall -y 'Development Tools'

wget http://tar.goaccess.io/goaccess-0.8.5.tar.gz

tar -xvzf goaccess-0.8.5.tar.gz  && cd goaccess-0.8.5/ && ./configure --enable-utf8  && make && make install 

/usr/local/bin/goaccess -f /usr/local/nginx/logs/ac.log

===================================================

 

 

https://www.axivo.com/resources/goaccess-settings-for-nginx.17/

GoAccess Settings for Nginx

Real-time web log analyzer and interactive viewer

  1. GoAccess is an open source real-time web log analyzer and interactive viewer that runs in a terminal in Linux systems. It provides fast and valuable HTTP statistics for system administrators that require a visual server report on the fly.

    Start by installing the RPM from AXIVO repository:
    # yum --enablerepo=axivo install goaccess
    For  Nginx only, replace the following variables:
    # sed -i 's|^#date-format.*|date-format %d/%b/%Y|' /etc/goaccess.conf
    # sed -i 's|^#log-format.*|log-format %h %^[%d:%^] %^ "%r" %s %b "%R" "%u"|' /etc/goaccess.conf
    Process a regular or compressed log file (with  zcat), by running the following command:
    # goaccess -f /var/log/nginx/localhost.access.log
    # zcat -f /var/log/nginx/localhost.access.log-20140819.gz | goaccess
    You are now ready to visualize any Nginx access log statistics.

    Log Format Variables Explained
    We have a custom Nginx log format, so chances are you also do on your server. This is the log format we are currently using at AXIVO:
    log_format    main    '$remote_addr - $remote_user [$time_local] $upstream_cache_status '
                          '"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
    We used the following GoAccess variables:
    Nginx Variable                            GoAccess Variable
    $remote_addr                              %r
    $remote_user                              %^ (ignored)
    $time_local                               %d:%^
    $upstream_cache_status                    %^ (ignored)
    $request                                  %r
    $status                                   %s
    $body_bytes_sent                          %b
    $http_referer                             %R
    $http_user_agent                          %u
    Take a look at  man goaccess for a detailed list of other useful settings and variables.

    If you want to enable GeoIP statistics, install the  GeoIP-update package and enable  geoip-city-dataoption into GoAccess configuration file:
    # sed -i 's|^#geoip-city-data|geoip-city-data|' /etc/goaccess.conf
    You should be all set to pull valuable data on the fly, from your server logs.
 
 
http://adslroot.blogspot.jp/2013/12/goaccess-nginx-accesslog.html

使用 goaccess 分析 nginx access.log

 

​不需要很強大,只需要夠用。
安裝,今天剛好趕上一個新版本的發布:
$ yum -y install glib2 glib2-devel ncurses ncurses-devel GeoIP GeoIP-devel
$ wget http://downloads.sourceforge.net/project/goaccess/0.7/goaccess-0.7.tar.gz
$ tar -xzvf goaccess-0.7.tar.gz
$ cd goaccess-0.7/
$ ./configure --enable-geoip --enable-utf8 
$ make
# make install
因為access 的format 不一樣,所以需要修改一下, 我這里默認的 NCSA 也對不上,看看我的nginx 的 access log format:
 
    log_format  main  '$remote_addr $http_host $upstream_addr [$time_local] $request '
                      '"$status" $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$request_time"';
眼睛看了老半天,終於按空格對上號了(部分字段由於中間有空格,但是沒有引號,所以又被多算了一列), vim ~/.goaccessrc:
date_format %d/%b/%Y
log_format %h %^ %^ %^[%d:%^] %^ %r %^ "%s" %b "%R" "%u" "%^" "%D"
 
上面這個保存為文件,就不用每次去按照下面的提示按 C 去修改了。(至於每個字段的含義,可以去官方查詢,另外發現有些是沒有,
也有可能是我沒發現,比如 $upstream_addr, $http_x_forwarded_for, 沒有暫時用 %^ 啰)
 
然后就是執行命令
$goaccess -f access.log
可以重定向輸出為 csv 格式或者 html 格式,羅列的各種 top 分析比較清晰。
 
ps: 不足之處,缺少 hourly 的支持,比如我的想要對比相鄰幾個小時內的訪問情況,來分析異常請求,這個在作者 github 上有個 issue,
     希望能夠早點實現。
https://github.com/allinurl/goaccess/issues/8



http://www.cnblogs.com/yjf512/p/3640346.html


面試的時候一定會被面到的問題是:給出web服務器的訪問日志,請寫一個腳本來統計訪問前10的IP有哪些?訪問前10的請求有哪些?當你領略過goaccess之后,你就明白,這些問題,除了考驗你的腳本背誦記憶能力以外,唯一的作用只有裝A或者裝C了。

對於nginx日志分析,有很多工具,衡量好壞的標准大概就是三快:安裝快,解析快,上手快。滿足這三點的goaccess確實是居家必備良葯。

話說這個標題其實有點委屈GoAccess了,它是一個日志分析工具,並不只是為nginx使用的。你也可以用它來分析apache,IIS的日志,甚至你自己寫的web服務,當然前提是你需要定義好解析的日志格式。誠如見到一個美女,你會一眼喜歡上它的幾個功能有:

1 解析速度快

2 使用簡單

3 能生成html,json,csv

安裝GoAccess

如果你是centos,使用yum:

yum install goaccess

如果你的yum找不到goaccess,就先安裝epel

wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
sudo rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm

 

GoAccess有兩種使用方法,一種是直接在控制台顯示和操作。

當你要立馬分析出今天早上10點到12點的IP訪問情況的時候,直接在控制台敲命令是最快的。

另外一種就是生成html文件或者數據文件。

如果你希望每天管理的服務器集群能給你發送一個昨天訪問日志的情況,這個就是不二選擇了。

控制台使用GoAccess

goaccess的參數:

-f <input-file> : 需要解析的日志文件

-a --agent-list : 在host模塊是不是能點擊IP出現這個IP的user-agentlist,就是出現如圖的功能

Image

-c --conf-dialog: 是否顯示log和format配置對話窗口。

就是下面的這個窗口

Image(1)

這個窗口有很多可以研究的。

 

首先,goaccess支持的日志格式有哪些?

Common Log Format (CLF)

通用日志格式,例子:

127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326

主機   用戶身份 作者 [日期] "請求方法  請求路徑 請求協議" 狀態碼 字節數

 NCSA Commbined Log Format

這個是Common Log Format的擴展,例子:

125.125.125.125 - dsmith [10/Oct/1999:21:15:05 +0500] "GET /index.html HTTP/1.0" 200 1043 "http://www.ibm.com/" "Mozilla/4.05 [en] (WinNT; I)" "USERID=CustomerA;IMPID=01234"

主機 用戶身份 作者 [日期] "請求方法 請求路徑 請求協議" 狀態碼 字節數 referrer 客戶端代理 cookie

 W3C

IIS 4.0和5.0是使用這種格式的,例子如下:

復制代碼
#Software: Microsoft Internet Information Server 4.0

#Version: 1.0

#Date: 1998-11-19 22:48:39

#Fields: date time c-ip cs-username s-ip cs-method cs-uri-stem cs-uri-query sc-status sc-bytes cs-bytes time-taken cs-version cs(User-Agent) cs(Cookie) cs(Referrer)

1998-11-19 22:48:39 206.175.82.5 - 208.201.133.173 GET /global/images/navlineboards.gif - 200 540 324 157 HTTP/1.0 Mozilla/4.0+(compatible;+MSIE+4.01;+Windows+95) USERID=CustomerA;+IMPID=01234 http://yourturn.rollingstone.com/webx?98@@webx1.html
復制代碼

 CloudFront

AWS上的日志格式

自定義格式

關於自定義格式所設置的參數說明在這里

 

-d --with-output-resolver 在HTML輸出或者json輸出中開啟IP 解析

如果開啟了-d,goaccess就會使用GEOIP來進行IP解析。

-e --exclude-ip=<ip address>

Host模塊中不需要被統計的IP,比如可以將爬蟲的IP過濾掉

-h --help 幫助文檔

-H --http-protocol 顯示的時候是否帶上HTTP協議信息

-m --with-mouse 控制面板開啟支持鼠標點擊,帶上了m之后,雙擊鼠標就相當於回車的操作

-M --http-method 是否帶上HTTP方法信息

-H和-M帶上之后效果為:

Image(2)

-o --output-format=csv|json 輸出為csv或者json形式

-p --conf-file=<filename>

指定配置文件,如果你有配置文件,配置文件中又恰巧設置了log-format和date,那么你就不會被強制進入到format對話框了。如果你沒有設置的話,默認使用~/.goacessrc。

-q --no-query-string

忽略請求的參數部分

-r --no-term-resolver

在Host模塊中是否進行IP的解析。加上-r是禁止IP解析。

--no-color

輸出無顏色標示

--real-os

展示真實的操作系統。在Operation System模塊中,是否展示更詳細的操作系統信息。

如圖:

Image(3)

下面說說的是控制台的鍵盤操作:

F1是打開幫助文檔

F5是刷新當前窗口

q是退出當前窗口,直到退出程序

o是進入選擇條目

0-9 是選擇模塊

tab和shift+tab是在前后模塊之前切換

j和k是在模塊內部條目切換

s是模塊內部條目排序方式

 

ps:模塊切換在右上角有當前模塊的標示

 

使用GoAccess生成文件

說起GoAccess最讓你爽的就是能生成html了,然后每天就可以看個統計報表。

下面的命令能生成html頁面:

goaccess -d -f web.log -a -p /home/yejianfeng/.goaccessrc > test.html

 

生成的html的截圖如下:

Image(5)

也可以生成json:

goaccess -q -f web.log -a -p /home/yejianfeng/.goaccessrc -o json >test.json

 

和csv

goaccess -q -f web.log -a -p /home/yejianfeng/.goaccessrc -o csv>test.csv

 

等等,還缺點什么?

正如文章,人無完人,工具無完美工具,goaccess也有幾個讓人不夠爽的地方

geoip粒度太粗

它是使用機器自帶的GeoIP,這個自帶的77k左右的IP庫只能判斷出國籍,不能判斷到城市。呃,這個就意味着你的報表中有99%的IP統計是來自:China。你可以升級你的GeoIp,但是,付費。網上有免費的GeoLiteCity.dat庫,但是goaccess自身就不支持國籍-城市的分類,也不支持使用外部的IP庫。

我能想到還有的辦法就是用goaccess生成json,然后自己寫python也好,php也好程序來加載城市的Ip庫來解析地理位置。然后再生成報表。不過,這樣,好像就不美了。

好消息是這個功能在ISSUE中有人提了,TODO LIST。

日期粒度太粗

日期只能粒度到天,如果需要統計一天每個小時的訪問數據,就沒法了。也不是沒辦法,你可以先grep小時的數據,然后再用goaccess解析。不過,這樣,好像很挫。

好消息是這個功能在ISSUE中有人提了,TODO LIST。

 

所以說....TODO LIST真是個好東西,是吧。另外,本文的goaccess使用v0.7.1。

 

 

http://my.oschina.net/mrco/blog/181737

非常小又精悍的 Nginx 日志分析工具 Goaccess,處理速度非常快,我用 XShell 查看,還可以生成 html 報告,先看圖

最佳 Nginx 日志分析工具 GoAccess   Best Nginx log analyzer a0266da1ac649a3a最佳 Nginx 日志分析工具 GoAccess   Best Nginx log analyzer 3a966303ef6baaf7最佳 Nginx 日志分析工具 GoAccess   Best Nginx log analyzer 4a9096e679782f92

最佳 Nginx 日志分析工具 GoAccess   Best Nginx log analyzer 9a8e462964e4da52最佳 Nginx 日志分析工具 GoAccess   Best Nginx log analyzer 39ca1719fc1c0c9c最佳 Nginx 日志分析工具 GoAccess   Best Nginx log analyzer 639a4ff93e05e519

GoAccess 在 CentOS 上的安裝方法:
1. 需要安裝 GeoIP, ncurses, glib2, 一句搞定
yum -y install glib2 glib2-devel ncurses ncurses-devel geoIP geoIP-devel

2. 下載 GoAccess 解壓編譯安裝

# wget http://jaist.dl.sourceforge.net/project/goaccess/0.6.1/goaccess-0.6.1.tar.gz 
# tar zxvf goaccess-0.6.1.tar.gz 
# cd goaccess-0.6.1 
# ./configure –enable-geoip –enable-utf8 
# make && make install 
# make clean

 

3. GoAccess 使用方法
進入 Nginx 日志所在目錄

命令行直接查看

# goaccess -f access.log -c -a

出來圖了,日志格式選 NCSA
最佳 Nginx 日志分析工具 GoAccess   Best Nginx log analyzer 03a6e8d1142c6916

也可以生成 HTML 報告

goaccess -f /var/log/nginx/access.log -a > report.html
zcat -f /var/log/nginx/access.log* | goaccess -a > report.html
zcat access.log.1.gz | goaccess
 

或者干脆分析目前下所有日志

zcat access.log* | goaccess

如果需要分析某天的日志,例如10月5號那天的日志,我們讓linux管道命令來大顯身手^_^。

sed -n ‘/05\/Dec\/2010/,$ p’ access.log | goaccess -s –b

分析從11月5號到12月5號一個月內的日志

sed -n ‘/5\/Nov\/2010/,/5\/Dec\/2010/ p’ access.log | goaccess -s –b

當你不希望在服務器上安裝goaccess程序,可以通過調用本地的goaccess程序來分析服務器上的日志(很神奇吧^_^):

ssh user@server ‘cat /var/log/apache2/access.log’ | goaccess -s -a

 

最讓我興奮的功能是goaccess支持排序!這樣你就可以tab到request這個模塊,而后按s,
跳出來的對話框選擇“Time Serverd”  “DESC”  這樣你就可以非常清楚的知道你的網站是哪些頁面響應很慢了!
 

附下我的NGINX日志格式:

log_format  main  ’$remote_addr – $remote_user [$time_local] “$request” ‘

            ‘$status $body_bytes_sent “$http_referer” ‘
            ‘”$http_user_agent”   $request_time‘;
 
 
 
再附下我的~/.goaccessrc配置:

color_scheme 0
date_format %d/%b/%Y
log_format %h %^[%d:%^] “%r” %s %b “%R” “%u” %T

 
============================================================================================
goaccess高級設定:

for Apache HTTP Server 預設設定:

CustomLog logs/access_log common可選Common Log Format (CLF)
CustomLog logs/access_log combined可選NCSA Combined Log Format

for Lighttpd mod_accesslog預設格式 : (需於GoAccess設定畫面手動編輯Log Format)

Log Format: %h %^ %^ [%d:%^] “%r” %s %b “%R” “%u”
Date Format: %d/%b/%Y

完成設定后產生~/.goaccessrc,日后執行goaccess -f /var/log/httpd/access_log不會再跳出這個設定畫面;若要修改設定,可加上-c參數,例如: goaccess -c -f / var/log/httpd/access_log或直接編輯設定檔案vi ~/.goaccessrc

基本操作

# goaccess -f /var/log/httpd/access_log

t:回到頂端
b:卷到最末
q:關閉視窗或離開程式
上下方向鍵:卷動畫面
數字鍵0 ~ 9,接着按英文字母o或右方向鍵:查看某項目的細節(*注)

*注: 0 表示第十項; Shift + 1 表示第十一項

排除統計某來源IP 
# goaccess -e 123.123.123.123 -f /var/log/httpd/access_log

檢視Host詳細資料的時候顯示來自該Host的User-Agents資訊
# goaccess -a -f /var/log/httpd/access_log

產生HTML報表(靜態報表) 
# goaccess -a -f /var/log/httpd/access_log > result.html

只統計來自某IP的記錄
# grep ^123.123.123.123 /var/log/httpd/access_log | goaccess

注:目前GoAccess無法即時統計pipe進來的資料,日后改版或許會補強這部份(Ref: man goaccess )

Trouble Shooting

無法使用F1 檢視help 訊息的替代方案

Ctrl + h

無法使用F10 完成設定畫面的替代方案

# vi ~/.goaccessrc

color_scheme 1
date_format %d/%b/%Y
log_format %h %^ %^ [%d:%^] "%r" %s %b "%R" "%u"

其他log_format 參考:

Common Log Format (CLF): %h %^[%d:%^] "%r" %s %b
Common Log Format (CLF) with Virtual Host: %^:%^ %h %^[%d:%^] "%r" %s %b
NCSA Combined Log Format: %h %^[%d:%^] "%r" %s %b "%R" "%u"
NCSA Combined Log Format with Virtual Host: %^:%^ %h %^[%d:%^] "%r" %s %b "%R" "%u"
W3C: %d %^ %h %^ %^ %^ %^ %r %^ %s %b %^ %^ %u %R

變更熱鍵

# cd /path/to/goaccess-0.5

# vi goaccess.c

#將F1熱鍵改為F2
找到[F1]Help [O]pen detail view 
改為[ F2 ]Help [O]pen detail view

找到Case 265: 
改為Case 266 :

# vi commons.c

#將Help訊息的F1改為F2
找到” ^F1^ or ^CTRL^ + ^h^ [main help]“, 
改為” ^ F2 ^ or ^CTRL^ + ^h^ [main help]“,

# vi goaccess.1

#將manpage的F1字眼改為F2
找到.IP “F1″ 
改為.IP “ F2 ”

# vi settings.c

#將F10熱鍵改為F9
找到mvwprintw (win, 2, 2, “[SPACE] to toggle – [F10] to proceed”); 
改為mvwprintw (win, 2, 2, “[SPACE] to toggle – [ F9 ] to proceed”);

找到Case 274: 
改為Case 273 :

 

 
 
http://siwei.me/blog/posts/analyze-nginx-log-using-goaccess

使用 goaccess分析nginx日志(analyze Nginx log using GoAccess)

2014-04-13 20:41分類:  技術

nginx 日志無法用rails-request-analyzer 來分析。 需要使用 goaccess 。 因為前者一分析就死機,后者速度更快,分析1.2G的日志大約20秒。 ( if you want to analyze nginx log, use GoAccess)

用法非常簡單:  (quite simple to use) 

$ goaccess -f <your_log_file> -a > result.html

但是goaccess的一個缺點是:  分析大日志時,如果你的機器內存太小,就會報錯退出。例如,你的機器是4G內存,但是要分析的內容是7G大小,這時候就會 在機器運行2,3分鍾,接近死機是,出現 Killed 的結果(還好GoAccess會自動 干掉這個進程 )  ( but it's a weakpoint that goaccess can't analyze big file, e.g. 7G size.  ) 

所以解決辦法是: 1. 把大日志切成小文件。  2. 分析小文件。

$ split -b 2G <your_log_file>

 

http://www.cnphp.info/goaccess-nginx-log-stat-tool-intro.html

http://gaocheng.me/articles/20.html

http://www.fancycoding.com/log-analyse-using-goaccess/

http://hao360.blog.51cto.com/5820068/1344435

http://www.phodal.com/blog/use-goaccess-analyse-nginx-log/



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM