2011-12-22

MRTG on CentOS 5.6

MRTG 是一套老牌的網路流量監控軟體,除了可以統計各個網路 Port 上下傳的流量,更可細分為每 5 分、30 分、兩小時以及每日計算,而老牌的好處就是相關的教學網路上一定不少,能因為工作的關係把這好工具給學起來真是太棒了

成果展示。

1. 安裝套件

jonny@centos:~$ sudo yum install mrtg.x86_64 net-snmp.x86_64 net-snmp-utils.x86_64 [Enter]

2. SNMP 設定

2.1. 備份原有設定。
jonny@centos:~$ sudo cp /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.ori [Enter]

2.2. 加入以下設定,紅字部份請改成當前 IP 網段,而 view 的設定請參考 MRTG - A Table of some interesting OIDs
jonny@centos:~$ sudo vim /etc/snmp/snmpd.conf [Enter]
com2sec local     localhost  public
com2sec mynetwork 192.168.123.0/24 public
group MyRWGroup v1        local
group MyROGroup v1        mynetwork
view all    included  .1        80
view systemview included .1.3.6.1.4.1.2021.4.3.0
view systemview included .1.3.6.1.4.1.2021.4.4.0
view systemview included .1.3.6.1.4.1.2021.4.5.0
view systemview included .1.3.6.1.4.1.2021.4.6.0
access MyROGroup ""      any       noauth    0      all    none   none
access MyRWGroup ""      any       noauth    0      all    all    all

2.2.3. 重新啟動 snmp 服務。
jonny@centos:~$ sudo /etc/init.d/snmpd restart [Enter]

2.2.4. 察看 161 Port 是否已被傾聽。
jonny@centos:~$ sudo netstat -tnulp | grep 161 [Enter]
udp 0 0 0.0.0.0:161 0.0.0.0:* 10427/snmpd

2.2.5. 使用 snmpwalk 指令測試,切忌得先安裝 net-snmp-utils 套件才有該指令。
jonny@centos:~$ sudo snmpwalk localhost -v 1 -c public [Enter]
SNMPv2-MIB::sysDescr.0 = STRING: Linux localhost 2.6.18-238.el5 #1 SMP Sun Dec 19 14:22:44 EST 2010 x86_64
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (165245496) 19 days, 3:00:54.96
SNMPv2-MIB::sysContact.0 = STRING: Root  (configure /etc/snmp/snmp.local.conf)
SNMPv2-MIB::sysName.0 = STRING: localhost
SNMPv2-MIB::sysLocation.0 = STRING: Unknown (edit /etc/snmp/snmpd.conf)
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (1) 0:00:00.01
SNMPv2-MIB::sysORID.1 = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID.2 = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID.3 = OID: IP-MIB::ip
SNMPv2-MIB::sysORID.4 = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID.5 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORID.6 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
SNMPv2-MIB::sysORID.7 = OID: SNMP-MPD-MIB::snmpMPDCompliance
SNMPv2-MIB::sysORID.8 = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance
SNMPv2-MIB::sysORDescr.1 = STRING: The MIB module for SNMPv2 entities
SNMPv2-MIB::sysORDescr.2 = STRING: The MIB module for managing TCP implementations
SNMPv2-MIB::sysORDescr.3 = STRING: The MIB module for managing IP and ICMP implementations
SNMPv2-MIB::sysORDescr.4 = STRING: The MIB module for managing UDP implementations
SNMPv2-MIB::sysORDescr.5 = STRING: View-based Access Control Model for SNMP.
SNMPv2-MIB::sysORDescr.6 = STRING: The SNMP Management Architecture MIB.
SNMPv2-MIB::sysORDescr.7 = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr.8 = STRING: The management information definitions for the SNMP User-based Security Model.
SNMPv2-MIB::sysORUpTime.1 = Timeticks: (1) 0:00:00.01
SNMPv2-MIB::sysORUpTime.2 = Timeticks: (1) 0:00:00.01
SNMPv2-MIB::sysORUpTime.3 = Timeticks: (1) 0:00:00.01
SNMPv2-MIB::sysORUpTime.4 = Timeticks: (1) 0:00:00.01
SNMPv2-MIB::sysORUpTime.5 = Timeticks: (1) 0:00:00.01
SNMPv2-MIB::sysORUpTime.6 = Timeticks: (1) 0:00:00.01
SNMPv2-MIB::sysORUpTime.7 = Timeticks: (1) 0:00:00.01
SNMPv2-MIB::sysORUpTime.8 = Timeticks: (1) 0:00:00.01
HOST-RESOURCES-MIB::hrSystemUptime.0 = Timeticks: (481900607) 55 days, 18:36:46.07

3. MRTG 設定

3.1. 產生 mrtg.cfg 設定檔
jonny@centos:~$ cfgmaker public@localhost > /etc/mrtg/mrtg.cfg [Enter]

3.2. 產生 MRTG 基本網頁,重覆執行到沒有吐訊息出來為止
jonny@centos:~$ env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg [Enter]

3.3. 產生 MRTG 首頁。
jonny@centos:~$ sudo indexmaker /etc/mrtg/mrtg.cfg > /var/www/mrtg/index.html [Enter]

3.4. 加入排程,並每 5 分鐘繪製流量圖。
jonny@centos:~$
crontab -e [Enter]
*/5 * * * * root env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg  > /dev/null 2>&1

4. Apache 設定

4.1. MTRG 是個很棒的工具,但在方便自己的同時也會方便他人,建議只開放給特定 IP 使用就好。
jonny@centos:~$ sudo vim /etc/httpd/conf/httpd.conf [Enter]
NameVirtualHost *:80

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /var/www/mrtg/
    ServerName  *
    ErrorLog /var/log/mrtg-error-log
    CustomLog /var/log/mrtg-access-log common
    <Directory "/var/www/mrtg/">
        Options -Indexes FollowSymLinks
        AllowOverride All
        Order deny,allow
        Allow from 192.168.123.0/24
        Deny from all
        ErrorDocument 403 ok
        RewriteEngine On
    </Directory>
</VirtualHost>

4.2. 重新啟動 Apache 服務。
jonny@centos:~$ sudo /etc/init.d/httpd graceful [Enter]

結束這一回合 :P

註:以上範例的紅字 localhost 可替換成同網段上有開 snmp 服務的 Server 或網路設備。

延伸閱讀:
鳥哥的 Linux 私房菜 -- MRTG 流量偵測法
OHaHa's學習心得 | 用apache限定訪客來源IP
RHEL5 - 21.7. Virtual Hosts

資料來源:
八克里: CentOS 5.3 MRTG 流量分析報表
資安論壇: [教學][研究] MRTG 快速安裝程式 (CentOS 5.1)
Centos Install and Configure MRTG
已經安裝 net-snmp 卻沒有 snmpwalk 指令的解法 | 資訊藝術家