FreeBsd下安裝和配置MRTG
FreeBsd下MRTG安裝和配置全過程
由于MRTG 不象APACHE 那么龐大, 精密。所以決定用PORTS 安裝。
首先要安裝的是SNMP ,因為MRTG就是通過SNMP 監(jiān)視流量來制作成可視化圖表
- pub# cd /usr/ports/net-mgmt/net-snmp/
- pub# make install && make clean
- pub# rehash
測試一下
- pub# snmpd
- pub# ps waux |grep snmpd
- root 614 0.0 0.8 5612 4308 ?? S 12:45AM 0:00.23 snmpd
這一步好了,假如系統(tǒng)和PORTS TREE 本身沒問題 ,這一步也應(yīng)該沒問題
接下來 就是PORTS 安裝MRTG
- pub# cd /usr/ports/net-mgmt/mrtg/
- pub# make install && make clean
- pub#rehash
測試一下
- pub# mrtg
- Usage: mrtg
- mrtg-2.11.1 is the Multi Router Traffic Grapher.
- If you want to know more about this tool, you might want
- to read the docs. They came together with mrtg!
看到這個就應(yīng)該是沒問題了
做完上面以后
最后就是配置工作 使MRTG 能為我們工作 ,這個是最“復(fù)雜”的一步 , 其實前面安裝也挺復(fù)雜的,只是借助了PORTS 才會覺得“簡單” 有機(jī)會可以全部編譯安裝試一下 以求深入理解MRTG && snmp
1. 在WEB 發(fā)布目錄/pub/site 建立一mrtg 目錄,為的是能把生成的圖表發(fā)布在瀏覽器里面
- pub# mkdir mrtg
2. 編輯 snmpd.conf 文件在里面添加訪問用戶 ,以求能順利獲得snmp上面的流量信息 ,這一步一定要做 ,而且好象在freebsd 和red hat下面它們的信息不太一樣 ,也許是版本不一樣導(dǎo)致 ,我就是吃了悶虧 ,以為都是一樣 ,結(jié)果耗費了很長時間
- pub# cd /usr/local/share/snmp/
- pub# ls
- mib2c-data mib2c.iterate_access.conf
- mib2c.access_functions.conf mib2c.mfd.conf
- mib2c.array-user.conf mib2c.notify.conf
- mib2c.check_values.conf mib2c.old-api.conf
- mib2c.check_values_local.conf mib2c.scalar.conf
- mib2c.column_defines.conf mibs
- mib2c.column_enums.conf mrtg.prowork
- mib2c.column_storage.conf snmp_perl.pl
- mib2c.conf snmp_perl_trapd.pl
- mib2c.create-dataset.conf snmpconf-data
- mib2c.genhtml.conf snmpd.conf.example
- mib2c.int_watch.conf mib2c.iterate.conf
看一下里面有個 snmpd.conf.example , copy 一下
- pub# cp snmpd.conf.example snmpd.conf
這樣里面就有了 snmpd.conf 的配置文件了
- pub# ee snmpd.conf
#######################################################################
#
- # EXAMPLE.conf:
- # An example configuration file for configuring the ucd-snmp snmpd ag
#
#######################################################################
#
- # This file is intended to only be an example. If, however, you want
找一下
- # sec.name source community
- com2sec local localhost COMMUNITY
- com2sec mynetwork NETWORK/24 COMMUNITY
- rocommunity prowork
在這個地方添加 ocommunity prowork
添加在這里比較容易記憶 ,方便以后查找 ,它也可以放在其它的地方 ,注意不要在前面加 # ^_^ ,其中的prowork 就是等會我們要使用的訪問名字 ,
這一步和別的朋友寫的文檔上面有點不太一樣 , 注意多點沒壞處
2. 生成 mrtg 配置文件 ,用于初始化,獲取 snmp 信息
- pub# cd /usr/local/etc/mrtg
- pub# cfgmaker prowork@192.168.1.28 > mrtg.prowork
- --base: Get Device Info on prowork@192.168.1.28:
- --base: Vendor Id:
- --base: Populating confcache
- --snpo: confcache prowork@192.168.1.28: Descr vr0 --> 1
- --snpo: confcache prowork@192.168.1.28: Descr lo0 --> 2
- --snpo: confcache prowork@192.168.1.28: Type 6 --> 1
- --snpo: confcache prowork@192.168.1.28: Type 24 --> 2
- --snpo: confcache prowork@192.168.1.28: Ip 127.0.0.1 --> 2
- --snpo: confcache prowork@192.168.1.28: Ip 192.168.1.28 --> 1
- --snpo: confcache prowork@192.168.1.28: Eth 00-05-5d-a1-b6-7d --> 1
- --snpo: confcache prowork@192.168.1.28: Eth --> 2
- --base: Get Interface Info
- --base: Walking ifIndex
- --base: Walking ifType
- --base: Walking ifAdminStatus
- --base: Walking ifOperStatus
出現(xiàn)以上信息就證明通過了
在這個地方有幾點要注意, 一個是prowork@后面的 192.168.1.28 在很多時候盡量用IP地址來代替,當(dāng)然假如是放在公網(wǎng)上面的機(jī)器是動態(tài)IP 就不得不使用域名,那也是沒辦法的事情,總之目的就是要獲得要抓的網(wǎng)卡上面的流量,得要給MRTG知道是哪塊網(wǎng)卡才行 ,所以一定要給它正確的地址 。
還有一個就是前面prowork@中的prowork ,這個就是在前面snmpd.conf 建立的訪問者拉 ,假如這個訪問者不存在或者是寫錯 就會出現(xiàn)
- pub# cfgmaker com2sec@192.168.1.28 > mrtg.prowork
- --base: Get Device Info on com2sec@192.168.1.28:
- SNMP Error:
- no response received
- SNMPv1_Session (remote host: "192.168.1.28" [192.168.1.28].161)
- community: "com2sec"
- request ID: 1181035357
- PDU bufsize: 8000 bytes
- timeout: 2s
- retries: 5
- backoff: 1)
- at /usr/local/lib/perl5/site_perl/5.005/SNMP_util.pm line 627
- SNMPWALK Problem for 1.3.6.1.2.1.1 on com2sec@192.168.1.28::::::v4only
- at /usr/local/bin/cfgmaker line 796
- WARNING: Skipping com2sec@192.168.1.28: as no info could be retrieved
這個上面的信息就是我錯誤理解snmpd.conf 導(dǎo)致出錯的信息
3 .在正確生成 mrtg.prowork的配置文件后就該修改修改里面的
內(nèi)容以求能符合自己的習(xí)慣 ,
- pub# cat mrtg.prowork
- # Created by
- # /usr/local/bin/cfgmaker prowork@192.168.1.28
- ### Global Config Options
- # for UNIX
- #這是設(shè)定要存放MRTG的信息發(fā)布頁面的路徑 ,也就是存放能
- #打開頁面看流量信息頁面的那個地方
- WorkDir: /pub/site/mrtg/
- # or for NT
- # WorkDir: c:\mrtgdata
- ### Global Defaults
- # to get bits instead of bytes and graphs growing to the right
- # Options[_]: growright, bits
- EnableIPv6: no
######################################################################
- # System: pub.porwork.com.cn
- # Description: FreeBSD pub.porwork.com.cn 4.11-STABLE FreeBSD 4.11-STABLE #5: Tue Apr i386
- # Contact: Me
- # Location: Right here, right now.
######################################################################
- ### Interface 1 >> Descr: 'vr0' | Name: '' | Ip: '192.168.1.28' | Eth: '00-05-5d-a1-b6-7d' ###
- Target[192.168.1.28_1]: 1:prowork@192.168.1.28:
- SetEnv[192.168.1.28_1]: MRTG_INT_IP="192.168.1.28" MRTG_INT_DESCR="vr0"
- MaxBytes[192.168.1.28_1]: 1250000
- Title[192.168.1.28_1]: Traffic Analysis for 1 -- pub.porwork.com.cn
- PageTop[192.168.1.28_1]:
- System: pub.porwork.com.cn in Right here, right now.
- Maintainer: Admin
- Description: vr0
- ifType: ethernetCsmacd (6)
- ifName: prowork nic
- Max Speed: 1250.0 kBytes/s
- Ip: 192.168.1.28 (pub.porwork.com.cn)
- ### Interface 2 >> Descr: 'lo0' | Name: '' | Ip: '127.0.0.1' | Eth: '' ###
- ### The following interface is commented out because:
- ### * it is a Software Loopback interface
- ### * has a speed of 0 which makes no sense
- #
- # Target[192.168.1.28_2]: 2:prowork@192.168.1.28:
- # SetEnv[192.168.1.28_2]: MRTG_INT_IP="127.0.0.1" MRTG_INT_DESCR="lo0"
- # MaxBytes[192.168.1.28_2]: 0
- # Title[192.168.1.28_2]: Traffic Analysis for 2 -- pub.porwork.com.cn
- # PageTop[192.168.1.28_2]:
- Traffic Analysis for 2 -- pub.porwork.com.cn
- #
- # System: pub.porwork.com.cn in Right here, right now.
- # Maintainer: Me
- # Description: lo0
- # ifType: softwareLoopback (24)
- # ifName:
- # Max Speed: 0.0 Bytes/s
- # Ip: 127.0.0.1 (localhost.porwork.com.cn)
- #
- Options[_]: growright, bits
- Language:gb2312
- WithPeak[_]: wmy
藍(lán)色為修改的地方 紅色為添加的地方,這里面的內(nèi)容不是很難
可以修改讓它符合自己的習(xí)慣因為里面有些 等會會在WEB頁面
顯示出來
注意的是任何指令左側(cè) 都不要有空格出現(xiàn) 否則會提示 :
- ERROR: Line 8 ( WorkDir: /var/www/html/mrtg) in CFG file (mrtg)does not make sense
這個一點都不假 ,不相信試試看
4. 生成WEB 頁面 index.html
- indexmaker --title 'prowork net' -output /pub/site/mrtg/index.html mrtg.prowork
--title 為 index.html 的標(biāo)題 /pub/site/mrtg/為發(fā)布頁面的
絕對路徑 mrtg.prowork 為配置文件
這時候應(yīng)該在 /pub/site/mrtg/里面生成了一個配置頁面
打開看的時候 ,應(yīng)該是看到有 幾個紅叉叉的頁面 并沒有預(yù)想
的那樣 ,原因是差了最后一步 :運行MRTG
5.運行 MRTG
- pub# mrtg /usr/local/etc/mrtg/mrtg.prowork
- Rateup WARNING: /usr/local/bin/rateup could not read the primary log file for 192.168.1.28_1
- Rateup WARNING: /usr/local/bin/rateup The backup log file for 192.168.1.28_1 was invalid as well
- Rateup WARNING: /usr/local/bin/rateup Can't remove 192.168.1.28_1.old updating log file
- Rateup WARNING: /usr/local/bin/rateup Can't rename 192.168.1.28_1.log to 192.168.1.28_1.old updating log file
- pub# mrtg /usr/local/etc/mrtg/mrtg.prowork
- Rateup WARNING: /usr/local/bin/rateup Can't remove 192.168.1.28_1.old updating log file
- pub# mrtg /usr/local/etc/mrtg/mrtg.prowork
- pub# mrtg /usr/local/etc/mrtg/mrtg.prowork
- pub# ls
果然不假 ,真如別的朋友上面寫的一樣會“抱怨”三次
到這一步 應(yīng)該是一個MRTG 該可以正常使用了
就看到了令人驚奇的一面
結(jié)尾:
排程 ,因為流量是不住變化的所以每隔一段時間就要去探測
一次 , 時間長了 精確度不夠 ,時間短 頻繁去探測 ,又會
加重系統(tǒng)負(fù)擔(dān) , 別人告訴我 ,一般是 5-10分鐘 最佳
- # mrtg crontab
- */5 * * * * /usr/local/bin/mrtg /usr/local/etc/mrtg/mrtg.prowork
到這里也就完全結(jié)束了。
【編輯推薦】
如何在FreeBSD下用MRTG監(jiān)測網(wǎng)路流量