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

SNMP + MRTG 監控 Linux 服務器網絡流量完整實戰:從配置到圖表生成

編輯:Linux 教學

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 Serverstation2.example.com192.168.32.32net-snmp, mrtg, apache
SNMP Agent (被監控客戶端)station3.example.com192.168.32.33net-snmp (snmpd)

Agent 端 (station3) 配置:被監控側

1. 安裝 net-snmp

yum install -y net-snmp net-snmp-utils

2. 配置 snmpd.conf (核心:訪問控制 + 系統信息)

# 备份原配置
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

3. 啟動並驗證

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

Manager 端 (station2) 配置:監控中心

1. 安裝 SNMP 工具與 MRTG

yum install -y net-snmp net-snmp-utils mrtg httpd
# MRTG 依賴 Perl、RRDTool,yum 會自動解決依賴

2. 使用 snmpconf 交互式生成 snmpd.conf (Manager 端也需 snmpd 做本地採集或作為 proxy)

實際生產中 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

3. 生成 MRTG 目標配置文件 (cfgmaker)


# 掃描 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

4. 編輯 mrtg.cfg 微調 (可選)


# 為介面添加友好標題
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

5. 生成首頁索引 (indexmaker)


indexmaker --output=/var/www/mrtg/index.html /etc/mrtg/mrtg.cfg

6. 設置 MRTG 定時採集 (cron)


# 每 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

7. 啟動 Apache 展示圖表


service httpd start
chkconfig httpd on
# 瀏覽器訪問 https://192.168.32.32/mrtg/ 查看流量圖

進階:SNMPv3 安全模式 (生產必備)

SNMPv1/v2c 明文傳輸 community string,極不安全。生產環境必須啟用 SNMPv3 (認證+加密):

Agent 端創建用戶


# 停止 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

Manager 端採集測試


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 配合 SNMPv3 (需 mrtg 2.17.4+ 支援)


# 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 responsesnmpwalk -v 2c -c public 192.168.32.33 sysDescr.0Agent 防火牆阻擋 UDP 161、snmpd 未監聽 0.0.0.0、community 不匹配
MRTG 圖表無數據/平線檢查 /var/log/mrtg/mrtg.log、rrdtool infoMaxBytes 設置過小、採集間隔未生效、RRD 文件損壞
介面計數器 32 位溢位 (高流量)介面速率 > 100Mbps啟用 64 位計數器 ifHCInOctets/.1.3.6.1.2.1.31.1.1.1.6,MRTG Options[_]: growright,bits,unknaszero
Apache 顯示 403 Forbidden目錄權限、SELinuxchcon -R -t httpd_sys_content_t /var/www/mrtg、setsebool -P httpd_unified 1

現代化替代方案建議

MRTG 最後更新於 2014 年,功能固化。新項目建議採用:

  • Prometheus + node_exporter + Grafana:多維指標、強大查詢語言、豐富儀表板、原生告警。
  • Zabbix / Prometheus SNMP Exporter:原生 SNMP 採集、自動發現、模板化。
  • Telegraf + InfluxDB + Grafana:輕量級代理、插件化、高性能時序庫。

總結

SNMP + MRTG 雖屬經典組合,但在「配置 Agent 訪問控制 → cfgmaker 自動發現介面 → mrtg.cfg 微調 → cron 定時採集 → Apache 展示」流程跑通後,即可獲得直觀的流量趨勢圖。核心在於 rocommunity 與 agentAddress 的正確配置、MaxBytes 精準匹配介面帶寬、growright,bits 選項優化顯示。為安全起見,生產環境務必升級 SNMPv3 並配合防火牆限制來源 IP。長期演進建議遷移至 Prometheus/Grafana 生態。

Copyright © Windows教程網 All Rights Reserved