Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Sarg (Squid Analysis Report Generator) 完整部署與自動化報表指南

編輯:Linux 教學

Sarg 簡介

Sarg (Squid Analysis Report Generator) 是一款經典的 Squid 存取日誌分析工具,將原始 access.log 轉換為直觀的 HTML 報表,支援:

  • 用戶/ IP 訪問統計(流量、次數、時長、排名)
  • 熱門網站/URL/文件類型排行
  • 拒絕/錯誤請求分析
  • 每日/每週/每月彙總報表
  • 多語言、可自定義模板

安裝部署(原始碼編譯)

# 1. 安裝依賴
yum install -y gcc make wget httpd   # CentOS/RHEL
apt install -y build-essential wget apache2  # Debian/Ubuntu

# 2. 下載編譯(以 2.3.11 為例,請至 SourceForge 獲取最新版)
cd /usr/local/src
wget https://sourceforge.net/projects/sarg/files/sarg/sarg-2.3.11/sarg-2.3.11.tar.gz
tar -zxvf sarg-2.3.11.tar.gz
cd sarg-2.3.11

./configure --prefix=/usr/local/sarg
make && make install

核心配置 /usr/local/sarg/sarg.conf

# Squid 存取日誌路徑(支援多檔、管道、gz 壓縮檔)
access_log /var/log/squid/access.log

# 報表輸出目錄(需 Web Server 可讀)
output_dir /var/www/html/sarg

# 解析 IP 為主機名(較慢,大流量建議關閉)
resolve_ip no

# 覆蓋歷史報表(每次運行重新生成全部,或用 date_extend 累加)
overwrite_report yes

# 報表標題與語言
title "Squid Proxy Access Report"
language Chinese_Traditional

# 日期格式
date_format e    # 歐洲格式 DD/MM/YYYY

# 排除項(正則)
exclude_hosts /usr/local/sarg/exclude_hosts.conf
exclude_users /usr/local/sarg/exclude_users.conf

# Top N 列表大小
topuser_sort_field BYTES   # 按流量排序
topuser_num 100
topsites_num 200

# 匿名化用戶(隱私保護)
anonymize_ip no
anonymize_login no

自動化排程:每小時增量更新 + 每日全量重建

1. 每小時增量更新(輕量,適合實時監控)

cat > /etc/cron.hourly/sarg-hourly <<'EOF'
#!/bin/bash
/usr/local/sarg/bin/sarg -d today -o /var/www/html/sarg/daily >/dev/null 2>&1
EOF
chmod +x /etc/cron.hourly/sarg-hourly

2. 每日全量重建(凌晨低峰,生成完整歷史報表)

cat > /etc/cron.daily/sarg-daily <<'EOF'
#!/bin/bash
# 完整重建所有歷史報表
/usr/local/sarg/bin/sarg -o /var/www/html/sarg >/dev/null 2>&1
EOF
chmod +x /etc/cron.daily/sarg-daily

3. 進階:Logrotate 配合日誌輪轉後觸發 Sarg

cat > /etc/logrotate.d/squid-sarg <<'EOF'
/var/log/squid/access.log {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
    create 640 squid squid
    sharedscripts
    postrotate
        # 平滑重載 Squid 釋放舊日誌檔案控制代碼
        [ -f /var/run/squid.pid ] && kill -USR1 $(cat /var/run/squid.pid)
        # 觸發 Sarg 增量分析剛輪轉的日誌
        /usr/local/sarg/bin/sarg -d yesterday -o /var/www/html/sarg/daily >/dev/null 2>&1
    endscript
}
EOF

Web 訪問配置

Apache 虛擬主機範例


    ServerName sarg.example.com
    DocumentRoot /var/www/html/sarg
    
        Options Indexes FollowSymLinks
        AllowOverride None
        Require ip 10.0.0.0/8 192.168.0.0/16 127.0.0.1
        # 或啟用基本認證
        # AuthType Basic
        # AuthName "Sarg Reports"
        # AuthUserFile /etc/httpd/.htpasswd
        # Require valid-user
    
    # 禁止列出目錄索引(可選)
    # DirectoryIndex index.html

Nginx 配置範例

server {
    listen 80;
    server_name sarg.example.com;
    root /var/www/html/sarg;
    index index.html;

    location / {
        allow 10.0.0.0/8;
        allow 192.168.0.0/16;
        allow 127.0.0.1;
        deny all;
        # auth_basic "Sarg Reports";
        # auth_basic_user_file /etc/nginx/.htpasswd;
    }

    # 靜態資源快取
    location ~* \.(css|js|png|gif|ico)$ {
        expires 30d;
        add_header Cache-Control "public";
    }
}

進階功能與調優

需求配置/方案
主機名顯示而非 IPresolve_ip yes + 確保 DNS 反解正確,或編輯 /etc/hosts 映射關鍵 IP
排除內部監控/健康檢查流量exclude_hosts 檔案列入監控 IP;exclude_users 列入系統帳號
多 Squid 節點合併分析將所有節點 access.log 透過 rsync/syslog-ng 集中到分析機,統一跑 Sarg
大日誌分析效能優化啟用 date_extend yes 累加模式;定期 sarg -d lastweek 只分析增量;使用 SSD 存放輸出目錄
自定義報表模板複製 /usr/local/sarg/share/sarg/template 目錄修改 CSS/HTML,配置 template_dir 指向新目錄
整合 Grafana/PrometheusSarg 僅生成靜態 HTML,長期趨勢分析建議改用 squid_exporter + Prometheus + Grafana

常見問題排查

  • 報表為空/數據為零:確認 access_log 路徑正確、Squid 正常寫入日誌、日誌格式為標準 squid 或 combined 格式。
  • 中文亂碼:確認 language Chinese_Traditional、模板 CSS 含 @charset "UTF-8"、日誌檔編碼為 UTF-8。
  • 權限拒絕:output_dir 需 Apache/Nginx 執行用戶可讀;Sarg 執行用戶需可寫 output_dir 及讀取 access_log。
  • 執行極慢:關閉 resolve_ip、啟用 date_extend、排除無關主機/用戶、將輸出目錄遷至 SSD。

總結

Sarg 雖非即時分析利器,但憑藉零依賴、輕量級、標準 HTML 輸出,仍是中小型 Squid 部署的首選審計報表工具。關鍵在於:Logrotate 聯動增量分析、Web 訪問權限管控、大日誌效能調優。若需即時儀表板、告警、長期趨勢,請轉向 squid_exporter + Prometheus + Grafana 現代可觀測棧。

Copyright © Windows教程網 All Rights Reserved