Goaccess 實現 access 日誌的可視化

官網
https://goaccess.cc/?mod=man

nginx 配置

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        access_log  logs/host.access.log  main;

        location / {
            alias  /hexohtml/;
            autoindex on;
            #index  index.html index.htm;
        }

安裝依賴包

yum install glib2 glib2-devel GeoIP-devel  ncurses-devel zlib zlib-devel
# 如果出現:configure: error: *** Missing development files for the GeoIP library,則需要執行下邊安裝geip
yum install geoip-devel ncurses ncurses-devel glib2-devel

下載源碼並編譯安裝

mkdir -p /soft/goaccess
cd /soft/goaccess
wget -c https://tar.goaccess.io/goaccess-1.3.tar.gz
tar -xzvf goaccess-1.3.tar.gz
cd goaccess-1.3/
./configure --enable-utf8 --enable-geoip=legacy
make && make install
# 二進制文件默認安裝路徑在 /usr/local/bin

靜態日誌分析

goaccess -d -f access.log

動態實時分析(http ws)

設置 report.html 路徑

本例將 report.html 文件放在 /usr/local/nginx/logs 下。

vi /usr/local/nginx/conf/nginx.conf 添加:
location /report.html {
    alias  /usr/local/nginx/logs/report.html;
    autoindex on;             
    #index  index.html index.htm;
}
# nginx -s reload

goaccess 實時分析

goaccess /usr/local/nginx/logs/access.log -o /usr/local/nginx/logs/report.html --real-time-html --log-format=COMBINED --time-format='%H:%M:%S' --date-format='%d/%b/%Y' --daemonize
  • –daemonize 以守護進程運行 websocket
  • 僅在 nginx.conf 中 log_format 格式默認的時候,goaccess 可以使用 log-format=COMBINED 參數,否則,log-format 也要相應的指定格式。
  • log/date/time format 可以在 goaccess 命令後直接指定,也可以提前在 goaccess.conf 文件中配置好。
  • –log-format 支持的格式:官網鏈接
COMBINED     | 聯合日誌格式
VCOMBINED    | 支持虛擬主機的聯合日誌格式
COMMON       | 通用日誌格式
VCOMMON      | 支持虛擬主機的通用日誌格式
W3C          | W3C 擴展日誌格式
SQUID        | Native Squid 日誌格式
CLOUDFRONT   | 亞馬遜 CloudFront Web 分佈式系統
CLOUDSTORAGE | 谷歌雲存儲
AWSELB       | 亞馬遜彈性負載均衡
AWSS3        | 亞馬遜簡單存儲服務 (S3)

瀏覽器查看 report

登錄 http://服務器IP/report.html 查看 report

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章