MRTG (Multi Router Traffic Grapher) 透過 SNMP 協定讀取設備計數器,生成 PNG 圖表嵌入 HTML,直觀展示流量、負載、記憶體、磁碟等指標趨勢。
yum -y install net-snmp net-snmp-utils
# 配置 /etc/snmp/snmpd.conf
rocommunity public
disk / 13102744
disk /data 41279536
systemctl enable --now snmpd
yum -y install mrtg
使用 cfgmaker 自動生成,再手動微調:
cfgmaker --global 'WorkDir: /var/www/mrtg' \
--global 'Options[_]: growright,bits' \
--output /etc/mrtg/mrtg.cfg \
public@localhost
Target[eth0]: \eth0:public@localhost
MaxBytes[eth0]: 12500000
Title[eth0]: eth0 流量統計
PageTop[eth0]: eth0 網卡流量
Options[eth0]: growright,nopercent
YLegend[eth0]: Bytes/s
ShortLegend[eth0]: B/s
LegendI[eth0]: 入站
LegendO[eth0]: 出站
Target[cpuload]: .1.3.6.1.4.1.2021.11.50.0&.1.3.6.1.4.1.2021.11.53.0:public@localhost
MaxBytes[cpuload]: 100
Options[cpuload]: gauge,nopercent,growright
YLegend[cpuload]: CPU %
ShortLegend[cpuload]: %
LegendI[cpuload]: User
LegendO[cpuload]: Idle
Title[cpuload]: CPU 使用率
Target[mem]: .1.3.6.1.4.1.2021.4.6.0&.1.3.6.1.4.1.2021.4.4.0:public@localhost
MaxBytes1[mem]: 1048576
MaxBytes2[mem]: 2097152
Options[mem]: gauge,growright
YLegend[mem]: Bytes
Title[mem]: 記憶體與 Swap 使用量
Target[disk]: .1.3.6.1.4.1.2021.9.1.8.1&.1.3.6.1.4.1.2021.9.1.8.2:public@localhost
MaxBytes1[disk]: 13102744
MaxBytes2[disk]: 41279536
Options[disk]: gauge,growright
Title[disk]: 磁碟空間使用率
# /etc/cron.d/mrtg 已自動建立,預設 5 分鐘執行一次
# 首次需手動跑 3 次消除初始化錯誤
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
# 生成首頁
indexmaker --output=/var/www/mrtg/index.html --title="伺服器監控儀表板" /etc/mrtg/mrtg.cfg
cfgmaker 指定多個 public@host。snmpwalk 發現廠商私有 OID,擴充監控項(如 RAID 狀態、溫度)。/var/lib/mrtg/*.log 權限、SNMP 連通性(snmpwalk -v2c -c public localhost)。MaxBytes 設定是否符合實際介面速率(位元組 vs 位元)。