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

CentOS 部署 SARG 分析 Squid 代理日誌:完整安裝配置與定時報表實戰

編輯:關於 Linux

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

SARG 原始碼編譯安裝

# 下載最新穩定版(以 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

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

# 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

Web 介面存取

瀏覽器開啟:https:///squid-reports/

  • 首頁:彙總統計(總位元組、請求數、用戶數、網站數)
  • 用戶報表:個別 IP 流量、前 10 大網站、時間分布
  • 網站報表:熱門域名、分類統計
  • 拒絕/錯誤代碼分析

常見問題排查

錯誤原因解決
報表為空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)

進階應用

  • 多 Squid 節點:集中收集日誌(rsync/syslog-ng)再統一分析
  • 郵件發送報表:sarg -d day-1 -o /tmp/report && mail -s "SARG Daily" admin@corp.com < /tmp/report/index.html
  • 整合 AWStats:SARG 專注代理行為,AWStats 專注 Web 伺服器,互補

SARG 部署一次、長期受益,是網管掌握頻寬使用、審計異常流量的利器。

Copyright © Windows教程網 All Rights Reserved