熱門分類
載入中…
目錄0%

📊 Linux Log 分析與視覺化:GoAccess 即時監控

    📊 Linux Log 分析與視覺化:GoAccess 即時監控

    想快速掌握伺服器訪問狀況與異常流量嗎? GoAccess 是一款輕量級且即時的 Linux Log 分析工具, 透過終端介面或 Web Dashboard,就能快速看出 熱門頁面、來源地、User-Agent 與攻擊行為。 本篇將帶你從安裝、配置到 HTML 即時報表的完整流程。

    📘 一、GoAccess 是什麼?

    GoAccess 是以 C 語言開發的開源工具,能即時分析 Apache、Nginx、Squid 等常見伺服器日誌。 支援 命令列互動介面(ncurses)Web 視覺化報表, 並可輸出 JSON、HTML、CSV 等格式供後續分析。

    # 安裝(Rocky / CentOS / Ubuntu)
    sudo dnf install goaccess -y
    # 或
    sudo apt install goaccess -y
    
    # 檢查版本
    goaccess --version
      

    🧩 二、設定日誌來源與格式

    GoAccess 預設分析 /var/log/nginx/access.log/var/log/httpd/access_log。 若使用自訂格式,可在 /etc/goaccess/goaccess.conf 修改 log-format

    # Nginx 常見格式(建議加入 log_format 於 nginx.conf)
    log_format main '$remote_addr - $remote_user [$time_local] '
                     '"$request" $status $body_bytes_sent '
                     '"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
      
    # 執行分析
    sudo goaccess /var/log/nginx/access.log --log-format=COMBINED
      

    🧠 三、即時 Web 報表模式

    GoAccess 可直接輸出即時可視化報表,適合放入內部監控頁面:

    # 生成即時監控 HTML
    sudo goaccess /var/log/nginx/access.log \
      --log-format=COMBINED \
      --real-time-html \
      --ws-url=ws://your-server:7890 \
      -o /var/www/html/report.html
      
    • --real-time-html:啟用即時 WebSocket 報表。
    • --ws-url:設定伺服器連線位址。
    • -o:輸出 HTML 檔案路徑。
    💡 提示:若你的伺服器為外網公開環境,建議搭配 Nginx Basic Auth 或 Fail2Ban 封鎖惡意來源,避免報表外洩。

    🔍 四、常見分析重點

    • General Statistics:總訪問數、請求成功率。
    • Visitor Hostnames & IPs:可判斷攻擊來源。
    • Requested Files:熱門頁面與錯誤頁。
    • HTTP Status Codes:如 404、403、500 頻率。
    • Referrers / User Agents:分析流量來源。

    🧭 五、與安全機制整合

    GoAccess 可與你先前部署的 Fail2Ban / Firewalld 整合形成閉環:

    # 每日自動匯出高風險 IP 並封鎖
    sudo goaccess /var/log/nginx/access.log -a -o report.json --log-format=COMBINED
    cat report.json | jq '.data | map(select(.status >= 400)) | .[].host' | sort | uniq > bad_ip.txt
    for ip in $(cat bad_ip.txt); do sudo firewall-cmd --add-rich-rule="rule family='ipv4' source address='$ip' reject"; done
      

    搭配 crontab 可每日自動清理封鎖名單,形成「監控 → 封鎖 → 報表」循環。

    ⚙️ 六、延伸應用

    • 整合 Grafana LokiElasticSearch 進行長期趨勢分析。
    • 建立 systemd service 常駐執行 GoAccess 即時分析。
    • 搭配 Proxy / Fail2Ban 記錄,進行全方位安全可視化。

    📘 結語

    GoAccess 是低成本、高即時性的 Linux Log 分析利器, 對中小型伺服器或個人站點而言,是了解流量與攻擊行為的最佳起點。 搭配 systemd、Fail2Ban 與 Firewalld,你將擁有一套完整的「安全 × 可視化監控體系」。


    🔗 延伸閱讀

    — WWFandy・Linux 安全系列第四篇

    🔗 分享這篇 LINE Facebook X

    沒有留言:

    字級