萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux 安裝配置 MRTG 監控 Juniper 防火牆流量完整教程

MRTG (Multi Router Traffic Grapher) 是經典的 SNMP 流量圖形化工具,雖然 Prometheus+Grafana 更現代,但 MRTG 輕量、無依賴、CGI 即可查看,仍廣泛用於網絡設備帶寬監控。本文以 **CentOS + Juniper SRX** 為例,覆蓋安裝、配置、Web 生成、定時採集全流程。

前置條件:Juniper 開啟 SNMP

set snmp community public authorization read-only
set snmp community public clients 192.168.1.100  # MRTG 服務器 IP
set snmp location "IDC-Rack-01"
set snmp contact "netadmin@example.com"

1. 安裝 MRTG

yum -y install mrtg mrtg-libs
# 官網:https://oss.oetiker.ch/mrtg/  最新 2.17.4
# Windows 版亦可用,本文以 Linux 為例

2. cfgmaker 生成初始配置

cfgmaker --global "WorkDir:/var/www/mrtg" \
         --output /etc/mrtg/mrtg.cfg \
         public@192.168.1.2   # community@juniper_ip

# 關鍵參數說明:
# --global: 全局選項寫入配置文件頭部
# --output: 指定輸出文件
# public@IP: SNMP community 和目標設備 IP

3. 精調 /etc/mrtg/mrtg.cfg 關鍵參數

# 全局
WorkDir: /var/www/mrtg
Options[_]: growright, bits
# growright: 時間軸左→右;bits: 單位 bits/s(默認 bytes)
Refresh: 300
Interval: 5
# EnableIPv6: no
# LogFormat: rrdtool      # 啟用 RRDTool 存儲(需安裝 rrdtool)
# PathAdd: /usr/bin/      # rrdtool 路徑
Language: chinese         # 中文界面(需字體支持)

# 目標設備段(cfgmaker 自動生成,核對修改)
Target[192.168.1.2_1]: 1:public@192.168.1.2
MaxBytes[192.168.1.2_1]: 12500000  # 100Mbps = 12.5MB/s,按實際帶寬設置
Title[192.168.1.2_1]: Juniper_SRX_FW_Port1
PageTop[192.168.1.2_1]: 

Juniper SRX Firewall - Port 1 Traffic

# LegendI/O, Legend1-4, WithPeak, YLegend 等可按需微調

4. 生成初始 Web 頁面(需運行 3 次消除 Warning)

LANG=C LC_ALL=C mrtg /etc/mrtg/mrtg.cfg
LANG=C LC_ALL=C mrtg /etc/mrtg/mrtg.cfg
LANG=C LC_ALL=C mrtg /etc/mrtg/mrtg.cfg
# 前兩次 WARNING: "Rate up -> down" 屬正常,因無歷史數據計算速率

5. 生成索引首頁

indexmaker /etc/mrtg/mrtg.cfg --output /var/www/mrtg/index.html
# 查看生成文件
ls /var/www/mrtg/
# 應見 *.html, *.png, *.log, index.html, mrtg-l/m/r/ti.gif 等

6. 配置 Web 服務器(Nginx/Apache)

# Nginx 示例
server {
    listen 80;
    server_name mrtg.example.com;
    root /var/www/mrtg;
    location / {
        index index.html;
        auth_basic "MRTG Admin";
        auth_basic_user_file /etc/nginx/.htpasswd;  # 建議加密碼保護
    }
}

7. Crontab 定時採集(每 5 分鐘)

vi /etc/cron.d/mrtg
*/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

8. 進階:RRDTool + CGI 動態圖表(可選)

  • 安裝:yum install rrdtool rrdtool-perl
  • 配置:LogFormat: rrdtool、PathAdd: /usr/bin/
  • CGI 插件:14all.cgi、routers2.cgi、mrtg-rrd 任選一,放入 /var/www/mrtg/ 並配置 Web 執行權限。

常見問題排查

  1. 無數據/圖表空白:snmpwalk -v2c -c public 192.168.1.2 .1.3.6.1.2.1.2.2.1.10 測試 SNMP 連通性與 OID 返回。
  2. MaxBytes 設置過小:導致流量超限被丟棄,圖表平頂 → 按實際帶寬 * 1.2 設置。
  3. 中文亂碼:確保系統安裝中文字體,或改用 Language: english。

完成部署後,訪問 https://mrtg-server/mrtg/ 即可查看 Juniper 防火牆各接口實時、日、周、月、年流量趨勢圖。

copyright © 萬盛學電腦網 all rights reserved