管理介面啟用與存取
# squid.conf 啟用 Cache Manager
acl manager proto cache_object
acl localhost src 127.0.0.1/32 ::1
http_access allow manager localhost
http_access deny manager
# 重載生效
squid -k reconfigure
# 命令列存取(推薦)
/usr/local/squid/bin/squidclient -h 127.0.0.1 -p 3128 mgr:info
# 或 CGI 方式(需 Apache + /usr/local/squid/share/squid/cachemgr.cgi)
核心指標詳細解讀
1. 連接與請求統計
| 指標 | 含義 | 健康基線 |
Number of clients accessing cache | 當前活躍客戶端 IP 數 | 業務高峰預期值 ±20% |
Number of HTTP requests received | 累計接收 HTTP 請求總數 | 單位時間增長率穩定 |
Average HTTP requests per minute since start | 啟動以來平均 QPS | 對比近 5 分鐘 QPS 判斷流量趨勢 |
Select loop called | I/O 多路復用輪詢次數與平均耗時 | 平均 < 1ms,異常增長提示 FD 壓力 |
2. 命中率四大核心指標(最關鍵)
| 指標 | 定義 | 公式 | 目標值 | 異常判讀 |
| Request Hit Ratio (RHR) | 請求層面命中率 | (HIT + Near HIT) / Total Requests | > 90% (靜態站 > 95%) | < 80% 檢查緩存頭、refresh_pattern、動靜分離 |
| Byte Hit Ratio (BHR) | 流量層面命中率 | Bytes from Cache / Total Bytes Served | > 80% | RHR 高但 BHR 低 → 大文件 MISS、Range 請求多 |
| Request Memory Hit Ratio | 記憶體緩存命中佔比 | Memory HIT / Total Requests | 視 cache_mem 大小 | 過低 → cache_mem 不足或對象過大 |
| Request Disk Hit Ratio | 磁碟緩存命中佔比 | Disk HIT / Total Requests | 視 cache_dir 大小 | 過低 → 磁碟 IO 瓶頸、替換算法不當 |
關鍵洞察:RHR 反映「請求數」節省,BHR 反映「頻寬」節省。視頻/下載站 RHR 可能不高但 BHR 極高(單次 HIT 傳輸 GB 級數據);圖片/API 站 RHR 應極高。業務類型決定指標權重。
3. 服務時間分佈(延遲分析)
| 類別 | 5 分鐘均值 | 60 分鐘均值 | 含義 |
HTTP Requests (All) | 2.95s | 3.47s | 所有請求平均耗時(含上游) |
Cache Misses | 0.03s | 0.03s | MISS 請求耗時(主要看上游響應) |
Cache Hits | 5.06s | 4.79s | 異常!HIT 應遠低於 MISS,此處單位可能是 ms 或統計口徑特殊 |
Near Hits | 0.30s | 0.36s | 就近命中(對等節點/父節點)耗時 |
Not-Modified Replies | 0.0018s | 0.0018s | 304 驗證耗時(極快) |
DNS Lookups | 0.00s | 0.00s | DNS 解析耗時(內部緩存極快) |
修正:官方文檔單位為 毫秒。正常情況:Cache Hits < 10ms,Cache Misses ≈ 上游 RTT + 傳輸時間。若 Cache Hits > 50ms,檢查磁碟 IO、CPU、鎖競爭。
4. 儲存與記憶體佔用
| 指標 | 含義 | 告警閾值 |
Storage Swap size | 磁碟緩存佔用 (KB) | > 90% cache_dir 總量 |
Storage Mem size | 記憶體緩存佔用 (KB) | 接近 cache_mem 限制 |
Mean Object Size | 平均對象大小 (KB) | 業務基線 ±50% |
StoreEntries | 緩存條目總數(內存+磁碟索引) | 配合 store_avg_object_size 估算內存佔用 |
Hot Object Cache Items | 熱門對象記憶體副本數 | 通常 < 1000 |
5. 資源使用
- CPU Usage:< 10% 為健康,> 50% 檢查正則 ACL、日誌格式、壓縮模組。
- Memory (mallinfo):
Total in use / Total size < 85%,Holding blocks 過大提示記憶體碎片。
- File Descriptors:
Number of file desc currently in use / Maximum number of file descriptors < 70%。
- I/O Loop Method:應顯示
epoll (Linux)、kqueue (BSD)、IOCP (Windows)。若為 select/poll 則編譯參數缺失。
常用 squidclient 管理命令速查
| 命令 | 用途 | 範例 |
mgr:info | 綜合狀態(上述所有指標) | squidclient -p 3128 mgr:info |
mgr:mem | 記憶體分配器詳細統計 | squidclient -p 3128 mgr:mem |
mgr:objects | 列出所有緩存對象(慎用,大緩存會卡死) | squidclient -p 3128 mgr:objects |
mgr:diskd | diskd 存儲模組統計 | squidclient -p 3128 mgr:diskd |
mgr:filedescriptors | FD 使用詳情 | squidclient -p 3128 mgr:filedescriptors |
mgr:peer_select | 對等節點選擇統計 | squidclient -p 3128 mgr:peer_select |
mgr:refresh | refresh_pattern 規則匹配統計 | squidclient -p 3128 mgr:refresh |
-m PURGE | 強制清除指定 URL 緩存 | squidclient -p 3128 -m PURGE https://example.com/test.jpg |
mgr:shutdown | 優雅關閉 Squid | squidclient -p 3128 mgr:shutdown |
mgr:reconfigure | 熱重載配置 | squidclient -p 3128 mgr:reconfigure |
自動化監控整合:Prometheus + squid_exporter
# 部署 exporter(Go 實現,支援 Squid 3.5+)
wget https://github.com/boynux/squid_exporter/releases/download/v0.9.0/squid_exporter_linux_amd64.tar.gz
tar -xzf squid_exporter_linux_amd64.tar.gz
./squid_exporter --squid.host=127.0.0.1:3128 --web.listen-address=:9301
# Prometheus scrape 配置
- job_name: 'squid'
static_configs:
- targets: ['proxy01:9301', 'proxy02:9301']
# 關鍵告警規則
- alert: SquidRequestHitRatioLow
expr: squid_request_hit_ratio < 0.85
for: 10m
labels:
severity: warning
annotations:
summary: "Squid RHR 低於 85%"
description: "實例 {{ $labels.instance }} RHR = {{ $value | humanizePercentage }}"
- alert: SquidFileDescriptorUsageHigh
expr: squid_file_descriptors_used / squid_file_descriptors_limit > 0.8
for: 5m
labels:
severity: critical
annotations:
summary: "Squid FD 使用率超 80%"
description: "實例 {{ $labels.instance }} 即將耗盡 FD"
總結
Squid 管理介面提供豐富的運行時指標,核心在於建立業務基線(RHR/BHR/延遲/FD/CPU)並持續對比。單次 mgr:info 僅是快照,時間序列監控(Prometheus/Grafana)才是生產級運維標配。掌握四大命中率指標語義、服務時間分佈判讀、資源使用告警閾值,能在用戶感知故障前 30 分鐘發現隱患。