TL;NR
本文只關(guān)注一個目的:
使用goaccess
,生成一個可以自動實時刷新的HTML
網(wǎng)頁.
0x00 關(guān)于goaccess
簡單來說goaccess, 是用C
語言開發(fā), 可以高效解析nginx
訪問日志的工具.
- 為什么要用
GoAccess
GoAccess was designed to be a fast, terminal-based log analyzer. Its core idea is to quickly analyze and view web server statistics in real time without needing to use your browser (great if you want to do a quick analysis of your access log via SSH, or if you simply love working in the terminal).
While the terminal output is the default output, it has the capability to generate a complete, self-contained real-time
HTML
report (great for analytics, monitoring and data visualization), as well as aJSON
, andCSV
report.GoAccess是基于終端,快速的日志分析工具. 核心理念是實時查看服務(wù)性能指標.
同時具有命令行窗口輸出模式,還有實現(xiàn)HTM輸出能力.
0x01 安裝goaccess
yum install glib2 glib2-devel GeoIP-devel ncurses-devel zlib zlib-develyum install gcc -y
yum -y install GeoIP-update
yum install goaccess
0x02 配置日志格式
-
/etc/goaccess.conf
修改goacces解析的日志格式
time-format %T
date-format %d/%b/%Y
log-format %h - %^ [%d:%t %^] requesthost:"%v"; "%r" requesttime:"%T"; %s %b "%R" - %^"%u"
-
nginx.conf
修改nginx日志的輸出格式
log_format main '$remote_addr - $remote_user [$time_local] requesthost:"$http_host"; "$request" requesttime:"$request_time"; '
'$status $body_bytes_sent "$http_referer" - $request_body'
'"$http_user_agent" "$http_x_forwarded_for"';
重啟nginx
服務(wù)
# 測試nginx配置文件有效性
nginx -t
# 重啟服務(wù)
nginx -s reload
0x03 生成goaccess
實時頁面
我們的目標是生成一個網(wǎng)頁, 客戶端打開后, 可以自動刷新nginx
訪問日志內(nèi)容.
goaccess /your_nginx_log/access.log -o /your_nginx_static/go.html --real-time-html &
關(guān)于輸出頁面:
把輸出的頁面放到一個遠程可以靜態(tài)訪問路徑即可.
-
nginx
參考配置
server {
listen 80 ;
server_name yourscompany.com;
location /go {
index index.html;
alias /app/nginx/html/;
}
}
0x04 輸出效果
訪問頁面后, 可以看到很炫酷的效果如下:
0x05 后記
其實本打算使用
ELK
+Kibana
+Grafana
來顯示訪問信息的
可是服務(wù)器資源受限, 怕資源不夠用,影響接口性能.
這一套實現(xiàn)下來, 還是比較簡單的, 性價比極高.