SARG(Squid Analysis Report Generator)將難讀的 Squid 原始日誌轉為直觀的 Web 報表,支援按日/週/月統計熱門網站、用戶流量、存取時段等。本文記錄 CentOS 環境下從原始碼編譯安裝、配置優化到 Crontab 自動化的完整流程。
# 安裝編譯工具鏈與依賴
yum install -y gcc make wget httpd crond
# 啟動基礎服務
service httpd start && service crond start
chkconfig httpd on && chkconfig crond on
# 下載最新穩定版(以 2.3.11 為例)
wget https://downloads.sourceforge.net/project/sarg/sarg/sarg-2.3.11/sarg-2.3.11.tar.gz
tar zxvf sarg-2.3.11.tar.gz
cd sarg-2.3.11
# 64 位系統需修補 log.c(防止 PRIi64 格式化錯誤)
# 參考官方補丁或手動將 nbytes 轉為 (int64_t)
./configure --prefix=/usr/local/sarg
make && make install
# Squid access.log 路徑
access_log /var/log/squid/access.log
# 暫存與輸出目錄
temporary_dir /tmp
output_dir /var/www/html/squid-reports
# 日期格式:歐洲制 DD-MM-YYYY
date_format e
# 覆蓋模式:同一天只保留最新報表
overwrite_report yes
# 排除本機/內網流量(視需求)
# exclude_hosts 192.168.1.100
# exclude_codes 200 304
# 語言與標題
language Chinese
title "企業代理伺服器存取報表"
# 偵錯模式檢查配置
sarg -x
# 正常生成報表
sarg
# 指定時間範圍:前一天
sarg -d day-1
# 指定週/月
sarg -d week-1
sarg -d month-1
# 建立排程腳本
cat > /etc/cron.daily/sarg << 'EOF'
#!/bin/bash
/usr/local/sarg/bin/sarg -d day-1
EOF
chmod +x /etc/cron.daily/sarg
# 或使用 crontab 精確控制時間
# 0 6 * * * /usr/local/sarg/bin/sarg -d day-1
瀏覽器開啟:https://
| 錯誤 | 原因 | 解決 |
|---|---|---|
| 報表為空 | access_log 路徑錯誤或格式不符 | 確認 Squid logformat 為 squid 或 common |
| 中文亂碼 | 語系/字型缺失 | yum install -y cjkuni-uming-fonts 並設定 language Chinese |
| 權限拒絕 | output_dir 不可寫 | chown -R apache:apache /var/www/html/squid-reports |
| 64位編譯失敗 | log.c int64 格式化 | 套用補丁將 nbytes 強轉 (int64_t) |
sarg -d day-1 -o /tmp/report && mail -s "SARG Daily" admin@corp.com < /tmp/report/index.htmlSARG 部署一次、長期受益,是網管掌握頻寬使用、審計異常流量的利器。
CentOS wget 進度條多行顯示亂碼修復:locale 翻譯錯誤導致換行、msgunfmt/msgfmt 修正 .mo 檔案
CentOS 部署 SARG 分析 Squid 代理日誌:完整安裝配置與定時報表實戰
Ubuntu sudo 執行緩慢?將主機名加入 /etc/hosts 即時解決
Ubuntu Qt Creator 無法輸入中文?Fcitx5 + Sogou 輸入法完整配置指南
Linux sshd 啟動報錯 Could not load host key 解決:重新產生 ED25519 主機金鑰