Linux – MRTG

Loading

MRTG : Multi Router Traffic Grapher 是一套以 SNMP 協定來取得主機各種資訊,並以透過網頁繪製曲線圖的方式來呈現.先來了解一下 SNMP 是什麼?

  • SNMP (Simple Network Management Protocol 簡易網路管理協定)
    用來管理網路設備(伺服器,電源供應器,RAID…)的一種協定 (Protocol).SNMP 使用 UDP Port 161 來傳送及請求訊息,以及 UDP Port 162 等待 trap 訊息.

    [root@localhost ~]# netstat -lnpu | grep -i snmp
    udp        0      0 0.0.0.0:161             0.0.0.0:*                           1023/snmpd      
    

    主要可以分為三部分 1. 管理端 (Manager) , 2. 代理者 (Agent) 以及 3. NMS(Network Management Station) , 後面會一一解釋.

  • MIB (Management information base) , OID (Object Identifier)
    管理資訊庫 MIB 為階層性的資料分類,用來定義所有網路設備的屬性,每一個屬性亦即為 Object Identifier (OID).
  • Agent
    通常運作在被監控的網路設備上,並提供資訊給 管理端,SNMP V1定義了五種指令 Get Request , Get NextRequest , Get Response , Set Request , Trap, SNMP V2 新增了 GETBULK REQUEST , INFORM .
  • NMS(Network Management Station)
    管理端 (Manager) 利用 SNMP 協定向 Agent 查詢被監控網路設備上的資訊,並利用資訊彙整資料(統計圖表)給使用者.

更多關於 SNMP 請參考 https://benjr.tw/8266 說明.

測試環境為 CentOS 7 x86_64 (虛擬機) : IP : 192.168.95.129

所需套件為 httpd ,mrtg , net-snmp 與 net-snmp-utils

HTTP

MRTG 是透過網頁來呈現 SNMP 的資料, Web Server 可以自行選擇 Apache 或是 Nginx .下面使用的是 Apache

[root@localhost ~]# yum install -y httpd

這邊計畫把 mrtg 網頁資料放置於 http 資料夾 /var/www/mrtg 中,但 http 預設並沒有給這個目錄權限,我們需要自行新增 /etc/httpd/conf.d/mrtg.conf 檔案.

[root@localhost ~]# vi /etc/httpd/conf.d/mrtg.conf
Alias /mrtg /var/www/mrtg

<Location /mrtg>
    Require local
    Require ip 192.168.95.0/24
    # Require ip 10.1.2.3
    # Require host example.org
</Location>

參數說明:
Require – 設定哪些網段可以存取.

[root@localhost ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2018-11-12 02:22:35 EST; 4s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 2607 (httpd)
   Status: "Processing requests..."
    Tasks: 6
   CGroup: /system.slice/httpd.service
           ├─2607 /usr/sbin/httpd -DFOREGROUND
           ├─2608 /usr/sbin/httpd -DFOREGROUND
           ├─2609 /usr/sbin/httpd -DFOREGROUND
           ├─2610 /usr/sbin/httpd -DFOREGROUND
           ├─2611 /usr/sbin/httpd -DFOREGROUND
           └─2612 /usr/sbin/httpd -DFOREGROUND

先行連接到你的 Server IP

如果連不上請先確一下 Firewall 的狀態.

[root@localhost ~]# systemctl stop firewalld

SNMP

接下來就是 mrtg 與 snmp 相關套件.

[root@localhost ~]# yum install -y mrtg net-snmp net-snmp-utils

修改 snmp 設定檔 ro (read Only,只有唯讀券限) community 為 public (功能類似 user id 或是 password 之類的意思,建議改成別的名稱) 並啟動 snmp 服務.

[root@localhost ~]# echo 'rocommunity public' > /etc/snmp/snmpd.conf
[root@localhost ~]# systemctl start snmpd
[root@localhost ~]# systemctl enable snmpd
Created symlink from /etc/systemd/system/multi-user.target.wants/snmpd.service to /usr/lib/systemd/system/snmpd.service.
[root@localhost ~]# systemctl status snmpd
● snmpd.service - Simple Network Management Protocol (SNMP) Daemon.
   Loaded: loaded (/usr/lib/systemd/system/snmpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2018-11-12 02:15:04 EST; 8min ago
 Main PID: 1032 (snmpd)
   CGroup: /system.slice/snmpd.service
           └─1032 /usr/sbin/snmpd -LS0-6d -f

可以先透過指令 snmpwalk 來確認一下 SNMP 服務是否啟動,後面接的字串可以是不完整的,如果是空的會透過 GETNEXT 把設備上的資訊一一輪詢出來 (透過這種方式可以讓我們得知有哪一些裝置可以做查詢).

參數說明:
-Os : 顯示方式(Output) MIB object name (plus any instance 或 other subidentifiers),其它方式請 man snmpcmd 查詢.
-v: SNMP 版本,從 1 , 2c 到 3 .
-c community : SNMPv1/v2c 傳輸時需要設定 community string (類似 user id 或是 password 之類的意思) , 預設通常為 public.

[root@localhost ~]# snmpwalk -Os -c public -v 1 192.168.95.129
sysDescr.0 = STRING: Linux localhost.localdomain 3.10.0-862.14.4.el7.x86_64 #1 SMP Wed Sep 26 15:12:11 UTC 2018 x86_64
sysObjectID.0 = OID: netSnmpAgentOIDs.10
sysUpTimeInstance = Timeticks: (1021) 0:00:10.21
sysContact.0 = STRING: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
sysName.0 = STRING: localhost.localdomain
sysLocation.0 = STRING: Unknown (edit /etc/snmp/snmpd.conf)
sysORLastChange.0 = Timeticks: (8) 0:00:00.08
sysORID.1 = OID: snmpMPDCompliance
sysORID.2 = OID: usmMIBCompliance
sysORID.3 = OID: snmpFrameworkMIBCompliance
sysORID.4 = OID: snmpMIB
sysORID.5 = OID: tcpMIB
sysORID.6 = OID: ip
...

MRTG

MRTG 設定檔位於 /etc/mrtg/mrtg.cfg ,我們可以手動編輯,或是透過 cfgmaker 來產生,我們利用參數 –ifref 來產生 網卡 ens33 的相關資料.

[root@localhost ~]# cfgmaker --snmp-options=:::::2 --ifref=descr 192.168.95.129 > /etc/mrtg/mrtg.cfg
--base: Get Device Info on public@192.168.95.193:::::2
--base: Vendor Id: Unknown Vendor - 1.3.6.1.4.1.8072.3.2.10
--base: Populating confcache
--base: Get Interface Info
--base: Walking ifIndex
--snpd:   public@192.168.95.193:::::2 -> 1 -> ifIndex = 1
--snpd:   public@192.168.95.193:::::2 -> 2 -> ifIndex = 2
--snpd:   public@192.168.95.193:::::2 -> 3 -> ifIndex = 3
--snpd:   public@192.168.95.193:::::2 -> 4 -> ifIndex = 4
--base: Walking ifType
--snpd:   public@192.168.95.193:::::2 -> 1 -> ifType = 24
--snpd:   public@192.168.95.193:::::2 -> 2 -> ifType = 6
--snpd:   public@192.168.95.193:::::2 -> 3 -> ifType = 6
--snpd:   public@192.168.95.193:::::2 -> 4 -> ifType = 6
--base: Walking ifAdminStatus
--snpd:   public@192.168.95.193:::::2 -> 1 -> ifAdminStatus = 1
--snpd:   public@192.168.95.193:::::2 -> 2 -> ifAdminStatus = 1
--snpd:   public@192.168.95.193:::::2 -> 3 -> ifAdminStatus = 1
--snpd:   public@192.168.95.193:::::2 -> 4 -> ifAdminStatus = 2
--base: Walking ifOperStatus
--snpd:   public@192.168.95.193:::::2 -> 1 -> ifOperStatus = 1
--snpd:   public@192.168.95.193:::::2 -> 2 -> ifOperStatus = 1
--snpd:   public@192.168.95.193:::::2 -> 3 -> ifOperStatus = 2
--snpd:   public@192.168.95.193:::::2 -> 4 -> ifOperStatus = 2
--base: Walking ifMtu
--snpd:   public@192.168.95.193:::::2 -> 1 -> ifMtu = 65536
--snpd:   public@192.168.95.193:::::2 -> 2 -> ifMtu = 1500
--snpd:   public@192.168.95.193:::::2 -> 3 -> ifMtu = 1500
--snpd:   public@192.168.95.193:::::2 -> 4 -> ifMtu = 1500
--base: Walking ifSpeed
--snpd: @ifSpeed = 1:10000000 2:1000000000 3:0 4:10000000

--base: Walking ifHighSpeed
--base: snmpget public@192.168.95.193:::::2:v4only for ifHighSpeed.1 -> 10 Mb/s
--base: snmpget public@192.168.95.193:::::2:v4only for ifHCInOctets.1 -> 21602
--base: snmpget public@192.168.95.193:::::2:v4only for ifHighSpeed.2 -> 1000 Mb/s
--base: snmpget public@192.168.95.193:::::2:v4only for ifHCInOctets.2 -> 175860
--base: snmpget public@192.168.95.193:::::2:v4only for ifHighSpeed.3 -> unknown Mb/s
--base: snmpget public@192.168.95.193:::::2:v4only for ifHCInOctets.3 -> unknown
--base: check for HighspeedCounters failed ... Dropping back to V1
--base: snmpget public@192.168.95.193:::::2:v4only for ifHighSpeed.4 -> 10 Mb/s
--base: snmpget public@192.168.95.193:::::2:v4only for ifHCInOctets.4 -> unknown
--base: check for HighspeedCounters failed ... Dropping back to V1

參數說明:
–snmp-options=:::::2
–snmp-options :[port][:[timeout][:[retries][:[backoff][:version]]]] Use this to set the default SNMP options for all routers following on the command line.
–ifref=descr
interface references by Interface Description 或是使用 –ifdesc=descr 是一樣的.
192.168.1.10
設備 IP .
> /etc/mrtg/mrtg.cfg
輸出轉向到 /etc/mrtg/mrtg.cfg 設定檔.

我們還需要設定 MRTG 位於 http 相關檔案位置設定.

[root@localhost ~]# vi /etc/mrtg/mrtg.cfg
#  for UNIX
WorkDir: /var/www//mrtg
#  to get bits instead of bytes and graphs growing to the right
Options[_]: growright, bits

設定說明:
WorkDir: /var/www//mrtg
設定 mrtg 的 http 網頁資料存放的相對應位置.
Options[_]: growright, bits
取樣資料以 bits 為主.

執行 mrtg 與其設定檔 (不知道為什麼一定要執行三次).

[root@localhost ~]# for (( i=1 ; i <= 3 ; i++ )); do env LANG=C mrtg /etc/mrtg/mrtg.cfg; done
2018-11-12 02:33:04, Rateup WARNING: /usr/bin/rateup could not read the primary log file for 192.168.95.129_ens33
2018-11-12 02:33:04, Rateup WARNING: /usr/bin/rateup The backup log file for 192.168.95.129_ens33 was invalid as well
2018-11-12 02:33:04, Rateup WARNING: /usr/bin/rateup Can't rename 192.168.95.129_ens33.log to 192.168.95.129_ens33.old updating log file

透過 indexmaker 產生 index 檔( http 資料夾會去讀取的第一個檔案 index.html)

[root@localhost ~]# indexmaker --columns=1 /etc/mrtg/mrtg.cfg > /var/www/mrtg/index.html

MRTG 利用 cron table – https://benjr.tw/421#cron 來定時執行 mrtg 擷取資料(每五分鐘一次).

[root@localhost ~]# 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

透過瀏覽器 http://192.168.95.129/mrtg 就可以看到如下的畫面.

監控其他裝置

除了網路裝置外,還有其他裝置可以監控嗎?可以透過 sysstat 指令來完成.

沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料