MRTG (Multi Router Traffic Grapher) 透過 SNMP 輪詢網路設備介面流量,生成即時圖表。本文以監控 Juniper 防火牆為例,涵蓋 SNMP 啟用、MRTG 安裝、cfgmaker 產生設定、關鍵參數調教、rrdtool 效能優化與 CGI 圖表呈現完整流程。
set snmp community public authorization read-only
set snmp community public clients 192.168.1.100 # MRTG 伺服器 IP
set snmp location "Server Room"
set snmp contact "admin@example.com"
yum -y install mrtg mrtg-libs
# 官網:https://oss.oetiker.ch/mrtg/ 最新版 2.17.4
cfgmaker --global "WorkDir: /var/www/mrtg" \
--output /etc/mrtg/mrtg.cfg \
public@192.168.1.2
/etc/mrtg/mrtg.cfg# 時間軸左至右、單位 bits
Options[_]: growright, bits
# 重新整理頻率 300 秒 (5 分鐘)
Refresh: 300
Interval: 5
# 啟用 rrdtool 儲存(效能優於文字檔)
LogFormat: rrdtool
PathAdd: /usr/bin/
# 關閉 IPv6
EnableIPv6: no
WorkDir: /var/www/mrtg
| 參數 | 用途 |
|---|---|
| Target | 監控目標格式:port:community@router |
| MaxBytes | 介面最大頻寬(bps),超過值視為計數器歸零重算 |
| YLegend / ShortLegend | Y 軸標題與單位標籤 |
| Legend1-4 | 四條曲線說明:平均入、平均出、最大入、最大出 |
| WithPeak | 顯示峰值曲線:w(週)/m(月)/y(年) |
| PageTop | 自訂 HTML 頁首內容 |
啟用 LogFormat: rrdtool 後,需 CGI 程式讀取 rrd 檔生成動態圖。常用插件:
14all.cgi:經典、輕量routers2.cgi:功能豐富、支援多裝置聚合mrtg-rrd:官方提供Apache 設定範例:
ScriptAlias /mrtg/ /var/www/mrtg/
Options +ExecCGI
AddHandler cgi-script .cgi
Require all granted
# /etc/cron.d/mrtg
*/5 * * * * root /usr/bin/mrtg /etc/mrtg/mrtg.cfg --logging /var/log/mrtg.log
MRTG 亦有 Windows 原生版,安裝 ActivePerl 後可直接執行,設定檔格式相同。