SNMP (Simple Network Management Protocol) 配合 MRTG (Multi Router Traffic Grapher) 是經典的網絡設備流量監控組合。本文基於 RH442 課程實驗環境,詳細記錄在 CentOS/RedHat 上部署 SNMP Agent (被監控端) 與 SNMP Manager + MRTG (監控中心),實現對遠程 Linux 服務器網卡流量的自動採集、RRD 數據存儲與 Web 圖表展示全流程。
| 角色 | 主機名 | IP | 關鍵服務 |
|---|---|---|---|
| SNMP Manager / MRTG Server | station2.example.com | 192.168.32.32 | net-snmp, mrtg, apache |
| SNMP Agent (被監控客戶端) | station3.example.com | 192.168.32.33 | net-snmp (snmpd) |
yum install -y net-snmp net-snmp-utils
# 备份原配置
cp /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.bak
# 生成最小可用配置
cat > /etc/snmp/snmpd.conf <<'EOF'
# 系統信息
syslocation Server Room, Rack 3
syscontact admin@example.com
# 訪問控制:允許 192.168.32.32 (Manager) 以 community "public" 只讀訪問全樹
rocommunity public 192.168.32.32
# 監聽地址 (預設只聽 127.0.0.1,必須開放)
agentAddress udp:161,udp6:[::1]:161
# 開啟標準 MIB-II (介面流量計數器 ifInOctets/ifOutOctets)
view systemonly included .1.3.6.1.2.1.1
view systemonly included .1.3.6.1.2.1.25.1
view all included .1
rocommunity public default -V systemonly
EOF
service snmpd start
chkconfig snmpd on
# 本地測試
snmpwalk -v 2c -c public 127.0.0.1 .1.3.6.1.2.1.2.2.1.10 # ifInOctets
snmpwalk -v 2c -c public 127.0.0.1 .1.3.6.1.2.1.2.2.1.16 # ifOutOctets
yum install -y net-snmp net-snmp-utils mrtg httpd
# MRTG 依賴 Perl、RRDTool,yum 會自動解決依賴
實際生產中 Manager 端通常不運行 snmpd,直接用 snmpwalk/snmpget 採集遠程 Agent。此處實驗環境在 Manager 端也配置 snmpd 以便本地測試。
snmpconf
# 交互選擇:
# 1. Read in existing config: 2 (/etc/snmp/snmpd.conf)
# 2. Select file type: 1 (snmpd.conf)
# 3. Select section: 5 (Access Control Setup)
# 4. Select: 3 (SNMPv1/v2c read-only community)
# 5. Community name: public
# 6. Network: 0.0.0.0/0 (或限制 192.168.32.0/24)
# 7. OID: (留空 = 全樹)
# 8. Finished → quit → overwrite
# 掃描 Agent 介面,生成 mrtg.cfg
cfgmaker --global 'WorkDir: /var/www/mrtg' \
--global 'Options[_]: growright,bits' \
--output /etc/mrtg/mrtg.cfg \
public@192.168.32.33
# 關鍵參數解析:
# WorkDir: Web 可訪問目錄 (Apache DocumentRoot 下)
# Options[_]: growright (流量圖向右增長), bits (顯示 bits/s 而非 bytes/s)
# public@192.168.32.33: community@agent_ip
# 為介面添加友好標題
Title[192.168.32.33_eth0]: "Station3 eth0 Traffic"
PageTop[192.168.32.33_eth0]: Station3 eth0 Network Traffic
# 設置最大帶寬 (用於計算百分比,單位 bits/s)
MaxBytes[192.168.32.33_eth0]: 12500000 # 100Mbps = 12.5MB/s = 100,000,000 bits/s
indexmaker --output=/var/www/mrtg/index.html /etc/mrtg/mrtg.cfg
# 每 5 分鐘採集一次
echo "*/5 * * * * root LANG=C LC_ALL=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg --lock-file /var/lock/mrtg/mrtg_l --confcache-file /var/lib/mrtg/mrtg.ok" > /etc/cron.d/mrtg
service httpd start
chkconfig httpd on
# 瀏覽器訪問 https://192.168.32.32/mrtg/ 查看流量圖
SNMPv1/v2c 明文傳輸 community string,極不安全。生產環境必須啟用 SNMPv3 (認證+加密):
# 停止 snmpd
service snmpd stop
# 創建用戶:認證 SHA,加密 AES
net-snmp-create-v3-user -ro -A AuthPass123! -X PrivPass456! -a SHA -x AES snmpuser
# 會自動更新 /var/lib/net-snmp/snmpd.conf 和 /etc/snmp/snmpd.conf
service snmpd start
snmpwalk -v 3 -u snmpuser -l authPriv -a SHA -A AuthPass123! -x AES -X PrivPass456! 192.168.32.33 .1.3.6.1.2.1.2.2.1.10
# mrtg.cfg 中目標定義
Target[station3_eth0]: /usr/bin/snmpget -v 3 -u snmpuser -l authPriv -a SHA -A AuthPass123! -x AES -X PrivPass456! 192.168.32.33 .1.3.6.1.2.1.2.2.1.10.2 .1.3.6.1.2.1.2.2.1.16.2
# 或使用外部腳本封裝 SNMPv3 邏輯
| 現象 | 排查命令 | 解決方向 |
|---|---|---|
cfgmaker 提示 no response | snmpwalk -v 2c -c public 192.168.32.33 sysDescr.0 | Agent 防火牆阻擋 UDP 161、snmpd 未監聽 0.0.0.0、community 不匹配 |
| MRTG 圖表無數據/平線 | 檢查 /var/log/mrtg/mrtg.log、rrdtool info | MaxBytes 設置過小、採集間隔未生效、RRD 文件損壞 |
| 介面計數器 32 位溢位 (高流量) | 介面速率 > 100Mbps | 啟用 64 位計數器 ifHCInOctets/.1.3.6.1.2.1.31.1.1.1.6,MRTG Options[_]: growright,bits,unknaszero |
| Apache 顯示 403 Forbidden | 目錄權限、SELinux | chcon -R -t httpd_sys_content_t /var/www/mrtg、setsebool -P httpd_unified 1 |
MRTG 最後更新於 2014 年,功能固化。新項目建議採用:
SNMP + MRTG 雖屬經典組合,但在「配置 Agent 訪問控制 → cfgmaker 自動發現介面 → mrtg.cfg 微調 → cron 定時採集 → Apache 展示」流程跑通後,即可獲得直觀的流量趨勢圖。核心在於 rocommunity 與 agentAddress 的正確配置、MaxBytes 精準匹配介面帶寬、growright,bits 選項優化顯示。為安全起見,生產環境務必升級 SNMPv3 並配合防火牆限制來源 IP。長期演進建議遷移至 Prometheus/Grafana 生態。