GoAccess是一款開源的實時Web日誌分析器,可在*nix
系統的終端(terminal
)或通過瀏覽器與查看者(viewer)進行交互。
Causality
收到研發經理的需求:統計某具體時間段內某服務請求API接口的數據。
統計緯度有
- 按各接口的總請求次數分類彙總;
- 各接口每分鐘的請求數,取出請求數最多的20個API接口;
數據展示有指定的格式。
|
|
對應的Nginx的log_format
為
|
|
其中的/bususer/flybusLogin.do
即為請求的API接口,指定時間範圍內的數據有 772,528 條。
本人的處理方案是:
- 使用
vim
提取指定時間範圍內的數據存儲到臨時文件中;(也可通過sed
進行) - 使用
scp
將該臨時文件從遠程服務器中下載到本地; - 使用
awk
、bash
進行數據的清洗、提取; - 將提取的數據存入MariaDB數據庫中;
- 另一方案:將提取的數據存入另一臨時文件中;
- 數據入庫完成後,通過SQL語句進行數據分析彙總;
前兩步手動進行,之後的操作通過撰寫Shell Script執行。
數據清洗、提取、入庫 全程耗時將近5個小時,嚴重影響到工作效率。
後聽主管說有一款名為GoAccess
的工具可以處理Nginx日誌,並有圖表展示,故而有了本文。
以下是個人電腦配置信息
item | detail |
---|---|
OS | CentOS Linux release 7.2.1511 (Core) |
Kernel | 3.10.0-327.28.3.el7.x86_64 |
CPU | Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz |
Memory | 8GB |
本文的相關操作也在該機上進行
Introduction
GoAccess
用C語言開發,代碼託管在GitHub,演示頁面。
其在官網網站的介紹如下:
GoAccess is an open source real-time web log analyzer and interactive viewer that runs in a terminal in
*nix
systems or through your browser.It provides fast and valuable HTTP statistics for system administrators that require a visual server report on the fly.
關於其特性及支持的Web日誌格式,具體可見 鏈接
常見問題見 鏈接。
Installation
GoAccess
目前的最新版本是 1.0.2
,其官方的安裝文檔見 GoAccess - Downloads,以下相關內容皆來自該文檔。
Dependence
編譯安裝GoAccess
,其依賴文件只需ncurses
即可
The only dependency is
ncurses
.
Distro | NCurses | GeoIP (optional) | Tokyo Cabinet (optional) |
---|---|---|---|
Ubuntu/Debian | libncursesw5-dev | libgeoip-dev | libtokyocabinet-dev |
Fedora/RHEL/CentOS | ncurses-devel | geoip-devel | tokyocabinet-devel |
Arch Linux | ncurses | geoip | compile from source |
Gentoo | sys-libs/ncurses | dev-libs/geoip | dev-db/tokyocabinet |
Slackware | ncurses | GeoIP | tokyocabinet |
You may need to install tools like
gcc
,make
, etc for compiling/building software from source. e.g.,base-devel
,build-essential
,"Development Tools"
.
安裝依賴包
|
|
Via Compile Installation
編譯安裝步驟見 鏈接。
編譯安裝時如果./configure
未指定--prefix
、--sysconfdir
等參數,則默認執行如下操作:
|
|
即相關目錄為/usr/local/bin
、/usr/local/etc
、/usr/local/share/doc/goaccess
、/usr/local/share/man/man1
如果需要卸載已經編譯安裝好的GoAccess
,執行命令
|
|
即可,具體的操作為
|
|
Configure Options
Multiple options can be used to configure GoAccess
. For a complete up-to-date list of configure options, run ./configure --help
--enable-debug
: Compile with debugging symbols and turn off compiler optimizations. Default is disabled--enable-utf8
: Compile with wide character support. Ncursesw is required.--enable-geoip
: Compile with GeoLocation support. MaxMind’s GeoIP is required. Default is disabled--enable-tcb=<memhash|btree>
: Compile with Tokyo Cabinet storage support. memhash will utilize Tokyo Cabinet’s on-memory hash database. btree will utilize Tokyo Cabinet’s on-disk B+ Tree database. Default is disabled--disable-zlib
: Disable zlib compression on B+ Tree database.--disable-bzip
: Disable bzip2 compression on B+ Tree database.--with-getline
: Use GNUgetline()
to parse full line requests instead of a fixed size buffer of 4096.
其餘選項
--disable-option-checking
: ignore unrecognized –enable/–with options--disable-FEATURE
: do not include FEATURE (same as –enable-FEATURE=no)--enable-FEATURE[=ARG]
: include FEATURE [ARG=yes]--enable-silent-rules
: less verbose build output (undo: “make V=1”)--disable-silent-rules
: verbose build output (undo: “make V=0”)--enable-dependency-tracking
: do not reject slow dependency extractors--disable-dependency-tracking
: speeds up one-time build
執行yum info tokyocabinet
可看到如下信息
Tokyo Cabinet is a library of routines for managing a database. It is the successor of QDBM. Tokyo Cabinet runs very fast. For example, the time required to store 1 million records is 1.5 seconds for a hash database and 2.2 seconds for a B+ tree database. Moreover, the database size is very small and can be up to 8EB. Furthermore, the scalability of Tokyo Cabinet is great.
故而編譯配置選項選擇如下:
|
|
注意:數據存儲在磁盤中要比存儲在內存中耗時。
A dataset of about 52M hits (12GB size) is parsed in 20 mins (in-memory), 60 mins (on-disk storage). – https://goaccess.io/faq
此處指定可執行文件路徑/usr/local
,配置文件路徑/etc/goaccess
,安裝包臨時存放路徑/tmp
。
|
|
以下是安裝後的信息
|
|
Via Package Manager Installation
通過各GNU/Linux發行版的包管理器安裝,具體見 鏈接。
在CentOS中可通過如下命令安裝
|
|
Debian/Ubuntu 可通過官方倉庫安裝最新版本的GoAccess
|
|
Note: .deb packages in the official repo are available through https as well. You may need to install
apt-transport-https
.
Command Line / Config Options
以下是命令參數 鏈接
The following options can be supplied to the command or specified in the configuration file. If specified in the configuration file, long options need to be used without prepending
--
.Removing the query string with
-q
can greatly decrease memory consumption, especially on timestamped requests.
Configuration
GoAccess
官方文檔Manual Page的CUSTOM LOG/DATE FORMAT部分對GoAccess
的配置文件及time-format
、date-format
、log-format
的格式進行了簡要說明。
其配置文件路徑可%sysconfdir%/goaccess.conf
或~/.goaccessrc
,其中%sysconfdir%
可以是/etc/
、/usr/etc/
或/usr/local/etc/
。
The configuration file resides under:
%sysconfdir%/goaccess.conf
or~/.goaccessrc
Note%sysconfdir%
is either/etc/
,/usr/etc/
or/usr/local/etc/
通常只需要設置time-format
、date-format
、log-format
三個參數即可,可在%sysconfdir%/goaccess.conf
中修改,也可直接寫入~/.goaccessrc
。
注:本文的配置文件路徑為/etc/goaccess/goaccess.conf
。
如果直接使用初始配置文件(未設置time-format
、date-format
、log-format
),執行
|
|
會出現如下提示
|
|
進行手動選擇、設置。
在配置文件%sysconfdir%/goaccess.conf
的對應位置按如下格式進行修改 或 直接寫入~/.goaccessrc
|
|
再次執行goaccess -f /tmp/api_access.log
,GoAccess
自動開始分析(parse)日誌中數據,分析完成後即能顯示圖表數據,如何在Shell窗口中使用快捷按鍵進行交互,可參考其官方文檔中的 INTERACTIVE KEYS
部分。
time-format
time-format 的格式%T
或 %H:%M:%S
,如果時間戳(timestamp)設置了微秒(microsecond),則必須設置%f
。具體可通過命令man strftime
查看。
The time-format variable followed by a space, specifies the log format date containing any combination of regular characters and special format specifiers. They all begin with a percentage (
%
) sign. Seeman strftime
.%T
or%H:%M:%S
.Note: If a timestamp is given in microseconds, %f must be used as time-format
%H
: The hour as a decimal number using a 24-hour clock (range 00 to 23).%M
: The minute as a decimal number (range 00 to 59).%S
: The second as a decimal number (range 00 to 60). (The range is up to 60 to allow for occasional leap seconds.)
date-format
具體可通過命令man strftime
查看。
The date-format variable followed by a space, specifies the log format date containing any combination of regular characters and special format specifiers. They all begin with a percentage (
%
) sign. Seeman strftime
.
Note: If a timestamp is given in microseconds, %f
must be used as date-format
%b
: The abbreviated month name according to the current locale.%d
: The day of the month as a decimal number (range 01 to 31).%Y
: The year as a decimal number including the century.
log-format
The log-format variable followed by a space or
\t
for tab-delimited, specifies the log format string.
具體指令解釋說明見CUSTOM LOG/DATE FORMAT。
Usage
使用示例,以日誌文件/tmp/api_access.log
為例
Shell Windows
直接使用-f
參數可在Shell窗口中生成圖表數據
執行
|
|
截圖如下:
Browser
使用sed
命令將指定時間段內的日誌存儲到臨時文件中,使用goaccess
將分析好的數據寫入html文件通過瀏覽器打開
|
|
截圖如下:
本文只進行簡單的測試,更詳細的示例參見 https://github.com/allinurl/goaccess#usage
Conclusion
GoAccess
功能確實強大,對於體積很大的日誌處理起來很方便。但對一些具體的需求並不能滿足,比如我接到的需求:
- 各接口每分鐘的請求數,取出請求數最多的20個API接口
在不動用ELK的前提下,似乎只能自己寫工具處理。
此處附上本人寫的腳本
Shell Script
創建SQL數據表
|
|
|
|
數據提取語句
|
|
Bibliography
- GoAccess (A Real-Time Apache and Nginx) Web Server Log Analyzer
- 使用 GoAccess 分析 Nginx 網頁記錄檔,即時監控伺服器狀態
- GoAccess - 圖形化 Web Log 的分析程式
- 使用GoAccess分析Nginx日誌以及sed/awk手動分析實踐
Change Logs
- 2016.09.20 18:26 Tue Asia/Shanghai
- 初稿完成
- 2019.04.29 10:56 Mon America/Boston
- 勘誤,遷移到新Blog