Mrtg(Multi Router Traffic Grapher,MRTG)是一个监控网络链路流量负载的工具软件, 它通过snmp协议从设备得到设备的流量信息,并将流量负载以包含PNG格式的图形的HTML 文档方式显示给用户,以非常直观的形式显示流量负载、CPU、MEM、SWAP、进程情况.
安装前的准备:
∙安裝前的準備工作:
MRTG 為使用 Perl 程式寫成的,並且使用到 zlib 、 gd 及 png 的函式庫(zlib 用來壓縮圖表、gd 用來繪製圖表),且由於 MRTG 乃使用 SNMP 協定,並且最後是以 HTTP 的網頁型態輸出成圖表,因此,你需要確定 Linux 主機中已經含有下列的套件:
operl (perl-5.0xx 以上)
ozlib (zlib-1.1.3-xx 以上)
ogd (gd-1.3.xx 以上)
olibpng
oapache
確定的方法就以 RPM 來確認吧:
orpm -qa | grep perl
orpm -qa | grep zlib
orpm -qa | grep gd
orpm -qa | grep libpng
一、安装.
1.RPM格式安装:
安装目录为/usr/local/mrtg-2
Rpm –ivh mrtg -2.9.29-4.ent.i386.rpm
2.编译格式安装:
可以在http://sunfreeware.com/indexintel10.html下载mrtg-2.16.2.tar.gz编译文件
安装MRTG:
#Tar –zxvf mrtg-2.16.2.tar.gz
#cd mrtg-2.16.2
#./configure –profix=/usr/local/mrtg-2
#make & make install
二、 配置SNMP
1.编辑/etc/snmp/snmpd.conf
找到如下代码:
[code]
# Third, create a view for us to let the group have rights to:
# Make at least snmpwalk -v 1 localhost -c public system fast again.
# name incl/excl subtree mask(optional)
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.25.1.1
[/code]
在后面加上:
[code]
# haijd Add start
view all included .1.3.6
# haijd Add end
[/code]
(使SNMP的MIB库支持搜索OID为.1.3.6下的所有OID)
把下面的#号去掉
#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
把下面的语句
access notConfigGroup "" any noauth exact systemview none none
改为:
access notConfigGroup "" any noauth exact mib2 none none
(使SNMP支持MIB2库搜索。)
2.改完重启一下snmp
Service snmpd restart
3.检查snmp服务是否正常
snmpwalk -v 1 172.29.13.123 -c public interface
snmpwalk -v 1 172.29.13.123 -c public system
如果snmp工作不正常收集不到interface信息则mrtg无法工作正常,删除/etc/snmp/snmpd.conf,执行snmpconf来创建新的snmp MIB库和帐号
4.获取MIB
利用linux的snmpwalk工具获取目标机器上的MIB信息:
snmpwalk -v 1 172.29.13.123 -c public system
windows系统下要安装snmp补丁,才能获取到MIB信息,这将在6.1介绍。
5.获取OID
在上图中,通过snmpwalk -v 1 172.29.13.123 -c public system,看到许多系统的MIB信息,通过系统特定的MIB就可以得到所需的OID了。如要得到目标IP的系统时间,其对应的MIB为sysUpTime,这就要用到下面命令:
snmptranslate -On 192.168.0.26 SNMPv2-MIB::sysUpTime.0
(下图为192.168.0.26机器上的sysUpTime信息)
得到的.1.3.6.1.2.1.1.3.0就是192.168.0.26的系统时间OID
6.客户端安装
6.1.windows系统:
上面的例子中,IP192.168.0.26的机器是windows2003系统,默认情况下windows2003和windows2000中,snmp的MIB信息没有windows NT项,要经过打SNMP补丁才能看到windows NT项下面的信息,可以到http://www.wtcs.org/informant/download.htm下载
informant-std-16.zip安装到客户机上。找到补丁目录下的informant-std-tree.txt文件,里面有系统的MIB对应表。如下
--standard(1.3.6.1.4.1.9600.1.1)
|
+--logicalDiskTable(1)
| |
| +--logicalDiskEntry(1) [lDiskInstance]
| +-- r-n Gauge32 lDiskPercentDiskReadTime(2)
| +-- r-n Gauge32 lDiskPercentDiskTime(3)
| +-- r-n Gauge32 lDiskPercentDiskWriteTime(4)
| +-- r-n Gauge32 lDiskPercentFreeSpace(5)(磁盘剩余空间百分比)
| +-- r-n Gauge32 lDiskPercentIdleTime(6)
| +-- r-n Gauge32 lDiskAvgDiskQueueLength(7)
| +-- r-n Gauge32 lDiskAvgDiskReadQueueLength(8)
| +-- r-n Gauge32 lDiskAvgDiskWriteQueueLength(9)
| +-- r-n Gauge32 lDiskAvgDiskSecPerRead(10)
| +-- r-n Gauge32 lDiskAvgDiskSecPerTransfer(11)
| +-- r-n Gauge32 lDiskAvgDiskSecPerWrite(12)
| +-- r-n Gauge32 lDiskCurrentDiskQueueLength(13)
| +-- r-n Gauge32 lDiskDiskBytesPerSec(14)
| +-- r-n Gauge32 lDiskDiskReadBytesPerSec(15)
| +-- r-n Gauge32 lDiskDiskReadsPerSec(16)
| +-- r-n Gauge32 lDiskDiskTransfersPerSec(17)
| +-- r-n Gauge32 lDiskDiskWriteBytesPerSec(18)
| +-- r-n Gauge32 lDiskDiskWritesPerSec(19)
| +-- r-n Gauge32 lDiskFreeMegabytes(20)(磁盘剩余空间,单位为MB)
| +-- r-n Gauge32 lDiskSplitIOPerSec(21)
|
+--memory(2)
| |
(剩余内存,单位为Byte)
(剩余内存,单位为KB)
(剩余内存,单位为MB)
|
+--networkInterfaceTable(3)
| |
| +--networkInterfaceEntry(1) [netInstance]
| +-- r-n Gauge32 netBytesReceivedPerSec(2)
| +-- r-n Gauge32 netBytesSentPerSec(3)
| +-- r-n Gauge32 netBytesTotalPerSec(4)
| +-- r-n Gauge32 netCurrentBandwidth(5)
| +-- r-n Gauge32 netOutputQueueLength(6)
| +-- r-n Gauge32 netPacketsOutboundDiscarded(7)
| +-- r-n Gauge32 netPacketsOutboundErrors(8)
| +-- r-n Gauge32 netPacketsReceivedDiscarded(9)
| +-- r-n Gauge32 netPacketsReceivedErrors(10)
| +-- r-n Gauge32 netPacketsReceivedUnknown(11)
| +-- r-n Gauge32 netPacketsReceivedPerSec(12)
| +-- r-n Gauge32 netPacketsSentPerSec(13)
| +-- r-n Gauge32 netPacketsPerSec(14)
|
+--objects(4)
| |
|
+--processorTable(5)
| |
| +--processorEntry(1) [cpuInstance]
| +-- r-n Gauge32 cpuPercentDPCTime(2)
| +-- r-n Gauge32 cpuPercentInterruptTime(3)
| +-- r-n Gauge32 cpuPercentPrivilegedTime(4)
| +-- r-n Gauge32 cpuPercentProcessorTime(5)(CPU负载)
| +-- r-n Gauge32 cpuPercentUserTime(6)
| +-- r-n Gauge32 cpuAPCBypassesPerSec(7)
| +-- r-n Gauge32 cpuDPCBypassesPerSec(8)
| +-- r-n Gauge32 cpuDPCRate(9)
| +-- r-n Gauge32 cpuDPCsQueuedPerSec(10)
| +-- r-n Gauge32 cpuInterruptsPerSec(11)
|
+--system(6)
+-- r-n Gauge32 systemSystemUpTime(1)
补丁目录下的INFORMANT-STD.xml文件为各个MIB说明和对应的OID。
常用MIB信息:
几个公用的MIB:
hrProcessorLoad:CPU负载
hrSystemProcesses:进程
hrMemorySize:总物理内存
如要抓IP为192.168.0.26的磁盘,可用下面的语句:
Snmpwalk –v 2c 192.168.0.26 –c public .1.3.6.1.4.1.9600.1.1.1.1.20
(.1.3.6.1.4.1.9600.1.1为--standard(1.3.6.1.4.1.9600.1.1)树,其后面的.1.1.20是lDiskFreeMegabytes的值,括号里的数值就是OID值)
snmptranslate -On 192.168.0.77 HOST-RESOURCES-MIB::hrProcessorLoad
snmptranslate -On 192.168.3.76 --standard
Diskused: HOST-RESOURCES-MIB::hrStorageUsed
CPU: HOST-RESOURCES-MIB::hrProcessorLoad
RUN Proc: HOST-RESOURCES-MIB::hrSWRunName
SYS Uptime: HOST-RESOURCES-MIB::hrSystemUptime SNMPv2-MIB::sysUpTime
SYS Date: HOST-RESOURCES-MIB::hrSystemDate
SYS Device: HOST-RESOURCES-MIB::hrDeviceDescr
SYS Descr: SNMPv2-MIB::sysDescr
SYS Name: SNMPv2-MIB::sysName
netcard speed: IF-MIB::ifSpeed
netcard physcal address: IF-MIB::ifPhysAddress
6.2.linux系统:
下载netsnmp-5.4.1.1.tar.gz
http://sunfreeware.com/indexintel10.html
安装:
#tar –zxvf netsnmp-5.4.1.1.tar.gz
#cd netsnmp-5.4.1.1
#./configure
#make & make install
协议用v2的
(net-snmp在FC6上可以正确编译通过,在FC4上编译时却发现二个错误,一个是找不到libbeencrypt.la这个文件,第二个错误是无法链接到elf库.
如果出现这二个错误,去网络上下载以下二个软件包进行编译就行了:
1.beecrypt-4.1.2.tar.gz
2.libelf-0.8.10.tar.gz
默认安装的时候libbeencrypt.la是安装在/usr/local/lib下面的,要做一个到/usr/lib的软连接 )
7.客户端的SNMP设置
Windows系统安装完SNMP后安装informant-std-16.zip补丁,打开SNMP Service属性—》安全,按实际情况添加团体,选添加“来自任何主机的SNMP数据包”,下的“添加来自这些主机的SNMP”则安全一些,但要输入实际的MRTG服务端IP。
在SNMP Service属性—》代理,在服务下面的5个选项都打上勾,保存。
Linux系统的SNMP设置与服务端一样
三、 配置apache
用MRTG对目标计算机进行监控,并用页面显示给用户
1.创建一个用于存放MRTG生成文件的文件夹
mkdir /usr/local/mrtg
2.把mkdir /usr/local/mrtg设为默认网站.
#vi /etc/httpd/http.conf
把Allow from localhost 改为
Allow from all(接受来自任何主机的数据包)
把ServerName www.exemple.com:80改为
ServerName 172.29.13.123:80(172.29.13.123为本机IP,如有域名,写域名也可以)
把DocumentRoot "/var/www/html" 改为
DocumentRoot "/usr/local/mrtg"(文件存放目录)
使MRTG生成的页面支持简体中文
把# DefaultLanguage nl 改为: DefaultLanguage zh-cn
增加: AddLanguage zh-cn .cn
把AddDefaultCharset ISO-8859-1 改为:AddDefaultCharset GB2312
增加AddCharset GB2312 .gb2312 .gb(如果有了就不用加了)
3.重启apache
#service httpd restart ,
四:配置MRTG
MRTG通过运行*.cfg配置文件来抓取计算机数据,生成png图表,这就需要配置一个*.cfg文件来抓取所需的数据,如要抓取IP为172.29.13.123的数据:
1.生成一个配置文件123.cfg,保存在/usr/local/mrtg下
# /usr/local/mrtg-2/bin/cfgmaker --output=/usr/local/mrtg/123.cfg --snmp-option=:::::2 public@172.29.13.123
--snmp-option=:::::2 表示采集时使用snmp2(采用bit),可支持155M以上的大流量,而缺省是snmp1(采用32bit),流量超过100M后就不准确
(或直接在/usr/local/mrtg/下新建一个123.cfg文件)
2.编辑MRTG的配置文件123.cfg
#下面的代码使用了简洁模式,去掉了一些说明
EnableIPv6: no
WorkDir: /usr/local/mrtg 保存目录
Options[_]:growright,bits
Language: gb2312 ###简体中文
RunAsDaemon: yes ###每5分钟执行一次
interval:5
LoadMIBs:/usr/share/snmp/mibs/UCD-SNMP-MIB.txt,/usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt,/usr/share/snmp/mibs/TCP-MIB.txt
################ Traffic ######################
Target[eth0_lan-1]: /172.29.13.123:public@172.29.13.123:
Options[eth0_lan-1]: growright
Directory[eth0_lan-1]: eth0
MaxBytes[eth0_lan-1]: 100000000
Kmg[eth0_lan-1]: ,k,M,G,T,P
YLegend[eth0_lan-1]: Bytes per Second
ShortLegend[eth0_lan-1]: Bytes/s
Legend1[eth0_lan-1]: Inflows B/s(Units Bytes)
Legend2[eth0_lan-1]: Outflows B/s(Units Bytes)
LegendI[eth0_lan-1]: Inflows:
LegendO[eth0_lan-1]: Outflows:
Title[eth0_lan-1]: 172.29.13.123 Network traffic
PageTop[eth0_lan-1]:
##如果是多台计算机,可以在下面添加:
Target[eth0_lan-2]: /IP:public@IP:
Options[eth0_lan-2]: growright
Directory[eth0_lan-2]: eth0
MaxBytes[eth0_lan-2]: 100000000
Kmg[eth0_lan-2]: ,k,M,G,T,P
YLegend[eth0_lan-2]: Bytes per Second
ShortLegend[eth0_lan-2]: Bytes/s
Legend1[eth0_lan-2]: Inflows B/s(Units Bytes)
Legend2[eth0_lan-2]: Outflows B/s(Units Bytes)
LegendI[eth0_lan-2]: Inflows:
LegendO[eth0_lan-2]: Outflows:
Title[eth0_lan-2]: eth0 Network traffic
PageTop[eth0_lan-2]:
############### CPU#################
Target[cpuload-1]: .1.3.6.1.4.1.2021.11.50.0&1.3.6.1.4.1.2021.11.53.0:public@172.29.13.123:
Options[cpuload-1]: nopercent,growright
Directory[cpuload-1]: cpu
MaxBytes[cpuload-1]: 100
Unscaled[cpuload-1]: dwym
YLegend[cpuload-1]: CPU Utilization
ShortLegend[cpuload-1]: %;
Legend1[cpuload-1]: CPU load(%)
Legend2[cpuload-1]: CPU Idle(%)
LegendI[cpuload-1]: User:
LegendO[cpuload-1]: Idle:
Title[cpuload-1]: CPU load
PageTop[cpuload-1]:
##如果是多台计算机,可以在下面添加:
Target[cpuload-2]: .1.3.6.1.4.1.2021.11.50.0&1.3.6.1.4.1.2021.11.53.0:public@IP:(OID按实际情况设置)
Options[cpuload-2]: nopercent,growright
Directory[cpuload-2]: cpu
MaxBytes[cpuload-2]: 100
Unscaled[cpuload-2]: dwym
YLegend[cpuload-2]: CPU Utilization
ShortLegend[cpuload-2]: %;
Legend1[cpuload-2]: CPU load(%)
Legend2[cpuload-2]: CPU Idle(%)
LegendI[cpuload-2]: User:
LegendO[cpuload-2]: Idle:
Title[cpuload-2]: CPU load
PageTop[cpuload-2]:
############### MEM AN SWAP#################
Target[memory-1]: .1.3.6.1.4.1.2021.4.6.0&.1.3.6.1.2.1.25.2.3.1.6.3:public@172.29.13.123:
Options[memory-1]: gauge,growright
Directory[memory-1]: mem
MaxBytes1[memory-1]: 1024000 本例设置为1G,可按实际情况设置
MaxBytes2[memory-1]: 1024000
Kmg[memory-1]: k,M,G,T,P
Kilo[memory-1]: 1024
Unscaled[memory-1]: dwym
YLegend[memory-1]: Bytes
ShortLegend[memory-1]: Bytes
Legend1[memory-1]: used Mem (Bytes)
Legend2[memory-1]: used Swap(Bytes)
LegendI[memory-1]: used Mem :
LegendO[memory-1]: used Swap:
Title[memory-1]: memory-1 usage[Mem+Swap]
PageTop[memory-1]:
##如果是多台计算机,可以在下面添加:
Target[memory-2]: .1.3.6.1.4.1.2021.4.6.0&.1.3.6.1.2.1.25.2.3.1.6.3:public@IP: (OID按实际情况设置)
Options[memory-2]: gauge,growright
Directory[memory-2]: mem
MaxBytes1[memory-2]: 1024000
MaxBytes2[memory-2]: 1024000
Kmg[memory-2]: k,M,G,T,P
Kilo[memory-2]: 1024
Unscaled[memory-2]: dwym
YLegend[memory-2]: Bytes
ShortLegend[memory-2]: Bytes
Legend1[memory-2]: used Mem (Bytes)
Legend2[memory-2]: used Swap(Bytes)
LegendI[memory-2]: used Mem :
LegendO[memory-2]: used Swap:
Title[memory-2]: Memory usage[Mem+Swap]
PageTop[memory-2]:
################### Process#######################
Target[process-1]: .1.3.6.1.2.1.25.1.6.0&.1.3.6.1.2.1.25.1.6.0:public@172.29.13.123:
Options[process-1]: gauge,nopercent,growright
Directory[process-1]: process
MaxBytes[process-1]: 1000
YLegend[process-1]: Processes
ShortLegend[process-1]:a
Legend1[process-1]: system processes
Legend2[process-1]:
LegendI[process-1]: processes :
LegendO[process-1]:
Title[process-1]: system processes
PageTop[process-1]:
##如果是多台计算机,可以在下面添加:
Target[process-2]: .1.3.6.1.2.1.25.1.6.0&.1.3.6.1.2.1.25.1.6.0:public@IP: (OID按实际情况设置)
Options[process-2]: gauge,nopercent,growright
Directory[process-2]: process
MaxBytes[process-2]: 1000
YLegend[process-2]: Processes
ShortLegend[process-2]:a
Legend1[process-2]: system processes
Legend2[process-2]:
LegendI[process-2]: processes :
LegendO[process-2]:
Title[process-2]: system processes
PageTop[process-2]:
################### disk#######################
Target[disk-1]: .1.3.6.1.4.1.9600.1.1.1.1.20.2.67.58&.1.3.6.1.4.1.9600.1.1.1.1.20.2.67.58:**************.0.224:
Options[disk-1]: gauge,growright
Directory[disk-1]: disk
MaxBytes[disk-1]: 37500
Kmg[disk-1]: M,G,T,P
Kilo[disk-1]: 1024
Unscaled[disk-1]: dwym
YLegend[disk-1]: disk
ShortLegend[disk-1]: bytes
Legend1[disk-1]: idledisk
Legend2[disk-1]:
LegendI[disk-1]: idledisk:
LegendO[disk-1]:
Title[disk-1]: idledisk
PageTop[disk-1]:
##如果是多台计算机,可以在下面添加:
Target[disk-2]: .1.3.6.1.4.1.9600.1.1.1.1.20.2.67.58&.1.3.6.1.4.1.9600.1.1.1.1.20.2.67.58:public@IP:
Options[disk-2]: gauge,growright
Directory[disk-2]: disk
MaxBytes[disk-2]: 37500
Kmg[disk-2]: M,G,T,P
Kilo[disk-2]: 1024
Unscaled[disk-2]: dwym
YLegend[disk-2]: disk
ShortLegend[disk-2]: bytes
Legend1[disk-2]: idledisk
Legend2[disk-2]:
LegendI[disk-2]: idledisk:
LegendO[disk-2]:
Title[disk-2]: idledisk
PageTop[disk-2]:
3.运行123.cfg
# env LANG=C /usr/local/mrtg-2/bin/mrtg /usr/local/mrtg/123.cfg
一般前面两次会有警告,多运行几次就没有问题.
env LANG=C /usr/local/mrtg-2/bin/mrtg /var/www/html/test.cfg
4. 生成html文件.
# /usr/local/mrtg-2/bin/indexmaker --output=/usr/local/mrtg/index.html /usr/local/mrtg/123.cfg
# /usr/local/mrtg-2/bin/indexmaker --output=/var/www/html/index.html /var/www/html/test.cfg
5. 配置自运行脚本,每五分钟自动运行一次.
在root 下启动 crontab –e 添加计划
*/5 * * * * env LANG=C /usr/local/mrtg-2/bin/mrtg /usr/local/mrtg/123.cfg
*/2 * * * * env LANG=C /usr/local/mrtg-2/bin/mrtg /var/www/html/test.cfg
6.浏览MRTG页面
在浏览器里输入http://172.29.13.123,就可以浏览到172.29.13.123的状态了。
硬盘剩余空间:
五.名词解释
MIB: SNMP的管理信息库。MIB主要用途是SMNP通过查讯代理MIB中相应对象的值,实现对对网络设备状态的监视。
OID: 对象标识符。在MIB中,每个对象都有唯一的OID,它是从MIB树的根开始到此对象所对应的节点沿途路径上所有节点的数字标识,中间以“.”间隔而成。Windows系统下的OID都可以在snmp补丁中的INFORMANT-STD.xml中找到。
Public: snmp中的团体名,可以新建一个
Target:是要执行的脚本
Xsize:生成图表的横向宽度(最大600)
Ysize:生成图表的纵向高度(最大200)
Title:标题
kMG: 改变单位
Ytics:纵向划分为几个块(格子)
MaxBytes:图表纵向数值的最大上限 (对于硬盘、内存要按实际情况设置,要不得到的百分比是不准确的)
PageTop:页面上面的提示
kilo:一般是写1024,如果需要的话,是1000在计算机里的单位
LegendI:从SHELL返回的数据中的第一个 (即&符号的前面数字)
LegendO:从SHELL返回的数据中的第二个 (即&符号后面的数字)
Options: growright,表示图表向右延展