[root@mail doc]# /etc/rc.d/init.d/snmpd start
Starting snmpd: [ OK ]
如果命令输出如上所示,就表示snmp服务器启动正常。
为了配合mrtg使用,还要修改snmpd的配置,以使其允许mrtg读取其interface(网络接口)流量数据。
vi /etc/snmp/snmpd.conf
将
#view mib2 included .iso.org.dod.inte.net.mgmt.mib-2 fc #在90行
的#号去掉,修改为:
view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
然后将
access notConfigGroup "" any noauth exact systemview none none #在62行
修改为:
access notConfigGroup "" any noauth exact mib2 none none
然后再重新启动snmpd:
/etc/rc.d/init.d/snmpd restart
配置MRTG
下一步就是要配置mrtg,实现对网络设备的监控。mrtg的配置信息都是保存在mrtg.cfg文件中的,创建该文件并且在其中定义希望的监控特性。幸运的是一般不需要直接手工编辑该配置文件,因为mrtg软件包提供有cfgmaker配置工具,这是一个脚本文件,根据运行参数可以自动生成mrtg.cfg配置文件。在mrtg源码目录的bin子目录下你可以得到该工具。
因为我的apache是默认安装的,所以DocumentRoot在/var/www/html目录下,同时我在配置的过程中发现如果目录名为mrtg,浏览器就无法访问,总是提示没有权限,但是只要目录名不是mrtg,浏览器就能够访问,百思不得其解,所以我就干脆用/var/www/html作为mrtg的工作目录
RHEL AS 3.0已经默认安装了mrtg,所以mrtg就不用装了
[root@gameserver conf]# rpm -qa | grep mrtg
mrtg-2.9.29-4.ent
cfgmaker --global "WorkDir: /var/www/html" --global "Options[_]: growright,bits" --ifref=ip --output /etc/mrtg/mrtg.cfg public@10.0.0.2 public@10.0.0.3
运行mrtg
一旦生成正确的配置文件,就运行下面的命令:
/usr/bin/mrtg /etc/mrtg/mrtg.cfg
这将查询被监控的设备并在工作目录下创建初始的流量图和web页面,在前三次运行时可能会报告遗失日志文件的告警信息,不要理睬这些信息,只需要连续运行三次以后再运行就不会产生告警信息了。如果仍然出现告警那么就需要察看问题出在哪里了。
运行了三次/usr/bin/mrtg /etc/mrtg/mrtg.cfg命令以后再用indexmaker命令生成mrtg流量图首页
indexmaker /etc/mrtg/mrtg.cfg --output=/var/www/html/index.html
再用浏览器访问http://ip/即可
RHEL AS 3.0会默认5分钟运行一次mrtg,所以我们就不需要把mrtg加入cron任务中了
[root@gameserver conf]# tail -f /var/log/cron
Jan 27 14:55:00 gameserver CROND[31962]: (root) CMD (/usr/bin/mrtg /etc/mrtg/mrtg.cfg)
Jan 27 15:00:00 gameserver CROND[32254]: (root) CMD (/usr/bin/mrtg /etc/mrtg/mrtg.cfg)
Jan 27 15:00:00 gameserver CROND[32256]: (root) CMD (/usr/lib/sa/sa1 1 1)
Jan 27 15:01:00 gameserver CROND[32259]: (root) CMD (run-parts /etc/cron.hourly)
Jan 27 15:05:00 gameserver CROND[32551]: (root) CMD (/usr/bin/mrtg /etc/mrtg/mrtg.cfg)
Jan 27 15:10:00 gameserver CROND[378]: (root) CMD (/usr/bin/mrtg /etc/mrtg/mrtg.cfg)
Jan 27 15:10:00 gameserver CROND[380]: (root) CMD (/usr/lib/sa/sa1 1 1)
Jan 27 15:15:00 gameserver CROND[720]: (root) CMD (/usr/bin/mrtg /etc/mrtg/mrtg.cfg)
Jan 27 15:20:00 gameserver CROND[1012]: (root) CMD (/usr/bin/mrtg /etc/mrtg/mrtg.cfg)
Jan 27 15:20:00 gameserver CROND[1014]: (root) CMD (/usr/lib/sa/sa1 1 1)
Jan 27 15:25:00 gameserver CROND[1308]: (root) CMD (/usr/bin/mrtg /etc/mrtg/mrtg.cfg)
Jan 27 15:30:00 gameserver CROND[1600]: (root) CMD (/usr/bin/mrtg /etc/mrtg/mrtg.cfg)
Jan 27 15:30:00 gameserver CROND[1602]: (root) CMD (/usr/lib/sa/sa1 1 1)
注一:本次实施mrtg的过程中发现一个很奇怪的问题,就是apache用mrtg作为目录名,浏览器就访问不了mrtg目录下的页面,别人目录就没有问题,真是奇怪
注二:如果要mrtg显示中文,则需要编辑/etc/mrtg/mrtg.cfg 文件在
WorkDir: /var/www/html #在18行
Options[_]: growright,bits
配置之间加入
Language: Chinese #注意:不要用Language: gb2312,因为用Language: gb2312会出现流量单位b不能正常显示的问题
修改成如下
WorkDir: /var/www/html
Language: Chinese
Options[_]: growright,bits
再按照前面的方法用/usr/bin/mrtg /etc/mrtg/mrtg.cfg和indexmaker /etc/mrtg/mrtg.cfg --output=/var/www/html/index.html命令重新生成页面即可
注三:cfgmaker命令参数
cfgmaker --global "WorkDir: /var/www/html" --global "Options[_]: growright,bits" --ifref=ip --output /etc/mrtg/mrtg.cfg public@10.0.0.2 public@10.0.0.3
这里的--global参数表示后面的选项是对后面指定的设备都是有效的(如果希望对多个设备进行监控时,该参数就会发生作用)。WorkDir用来指示 mrtg的工作目录;Options用来指定一些特定的选项,这里的growright,bits是用来指定默认options配置的,对于常见的应用来说默认options配置就可以满足需求了。ifref用来指示用什么选项来标识设备接口,这里指定使用IP地址来标识网络设备接口。ifref可以指定为nr、ip、eth、descr、name。nr表示用接口在MIBII库中Interface接口的ifIndex来识别接口;IP表示使用ip地址识别接口;eth表示使用接口的物理地址标识接口;descr表示使用接口的描述信息来标识接口;name表示使用接口名来标识接口。一般来说ip地址是唯一的,但是有些情况下接口是没有IP地址的,例如交换机就会出现这种情况。对于接口来说nr(接口号)是唯一的,因此对于一般情况使用IP地址就可以了,而对于其他一些情况则需要采用nr了。"--output /etc/mrtg.cfg"标识将生成的配置文件存放在/etc/目录下。"public@192.168.0.1"表示监控IP地址为 192.168.0.1的设备,采用public作为共同体名通过snmp协议来监控设备192.168.0.1。
对于希望使用mrtg来对多个设备进行监控的情况,举例如下:(本次实例监控了10.0.0.2和10.0.0.3两台服务器)
cfgmaker --global "WorkDir: /var/www/html/mrtg"
--global "Options[_]: growright,bits"
--ifref=descr
--ifdesc=alias
public@router1.place.xyz
public@router2.place.xyz
--global "Options[_]: growright"
--ifref=name
--ifdesc=descr
public@switch1.place.xyz
--ifdesc=name
public@switch2.place.xyz > mrtg.cfg
这里指示监控四个设备:router1.place.xyz、router2.place.xyz、switch1.place.xyz
和switch2.place.xyz,所有的设备都采用共同体名public来进行监控。并且两个路由器采用descr来作为设备的描述信息,而两个交换机则采用alias作为设备描述(这两者是不同的,例如对于cisco路由器来说,对于descr来说设备描述为"Serial0",而对于aliasl来说则为"Link to HQ")。
注四:/etc/mrtg/mrtg.cfg 内容实例
[root@game html]# cat /etc/mrtg/mrtg.cfg
# Created by
# /usr/bin/cfgmaker --global 'WorkDir: /var/www/html' --global 'Options[_]: growright,bits' --ifref=ip --output /etc/mrtg/mrtg.cfg public@10.0.0.2 public@10.0.0.3
### Global Config Options
# for UNIX
# WorkDir: /home/http/mrtg
# or for NT
# WorkDir: c:\mrtgdata
### Global Defaults
# to get bits instead of bytes and graphs growing to the right
# Options[_]: growright, bits
WorkDir: /var/www/html
Options[_]: growright,bits
######################################################################
# System: gameserver
# Description: Linux gameserver 2.4.21-20.ELsmp #1 SMP Wed Aug 18 20:46:40 EDT 2004 i686
# Contact: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
# Location: Unknown (edit /etc/snmp/snmpd.conf)
######################################################################
### Interface 1 >> Descr: 'lo' | Name: '' | Ip: '127.0.0.1' | Eth: '' ###
### The following interface is commented out because:
### * it is a Software Loopback interface
#
# Target[10.0.0.2_127.0.0.1]: /127.0.0.1:public@10.0.0.2:
# SetEnv[10.0.0.2_127.0.0.1]: MRTG_INT_IP="127.0.0.1" MRTG_INT_DESCR="lo"
# MaxBytes[10.0.0.2_127.0.0.1]: 1250000
# Title[10.0.0.2_127.0.0.1]: Traffic Analysis for 127.0.0.1 -- gameserver
# PageTop[10.0.0.2_127.0.0.1]: <H1>Traffic Analysis for 127.0.0.1 -- gameserver</H1>
# <TABLE>
# <TR><TD>System:</TD> <TD>gameserver in Unknown (edit /etc/snmp/snmpd.conf)</TD></TR>
# <TR><TD>Maintainer:</TD> <TD>Root <root@localhost> (configure /etc/snmp/snmp.local.conf)</TD></TR>
# <TR><TD>Description:</TD><TD>lo </TD></TR>
# <TR><TD>ifType:</TD> <TD>softwareLoopback (24)</TD></TR>
# <TR><TD>ifName:</TD> <TD></TD></TR>
# <TR><TD>Max Speed:</TD> <TD>10.0 Mbits/s</TD></TR>
# <TR><TD>Ip:</TD> <TD>127.0.0.1 (game)</TD></TR>
# </TABLE>
### Interface 2 >> Descr: 'eth0' | Name: '' | Ip: '61.144.222.111' | Eth: '00-11-43-d3-c5-d3' ###
Target[10.0.0.2_61.144.222.111]: /61.144.222.111:public@10.0.0.2:
SetEnv[10.0.0.2_61.144.222.111]: MRTG_INT_IP="61.144.222.111" MRTG_INT_DESCR="eth0"
MaxBytes[10.0.0.2_61.144.222.111]: 12500000
Title[10.0.0.2_61.144.222.111]: Traffic Analysis for 61.144.222.111 -- gameserver
PageTop[10.0.0.2_61.144.222.111]: <H1>Traffic Analysis for 61.144.222.111 -- gameserver</H1>
<TABLE>
<TR><TD>System:</TD> <TD>gameserver in Unknown (edit /etc/snmp/snmpd.conf)</TD></TR>
<TR><TD>Maintainer:</TD> <TD>Root <root@localhost> (configure /etc/snmp/snmp.local.conf)</TD></TR>
<TR><TD>Description:</TD><TD>eth0 </TD></TR>
<TR><TD>ifType:</TD> <TD>ethernetCsmacd (6)</TD></TR>
<TR><TD>ifName:</TD> <TD></TD></TR>
<TR><TD>Max Speed:</TD> <TD>100.0 Mbits/s</TD></TR>
<TR><TD>Ip:</TD> <TD>61.144.222.111 ()</TD></TR>
</TABLE>
### Interface 3 >> Descr: 'eth1' | Name: '' | Ip: '10.0.0.2' | Eth: '00-11-43-d3-c5-d4' ###
Target[10.0.0.2_10.0.0.2]: /10.0.0.2:public@10.0.0.2:
SetEnv[10.0.0.2_10.0.0.2]: MRTG_INT_IP="10.0.0.2" MRTG_INT_DESCR="eth1"
MaxBytes[10.0.0.2_10.0.0.2]: 12500000
Title[10.0.0.2_10.0.0.2]: Traffic Analysis for 10.0.0.2 -- gameserver
PageTop[10.0.0.2_10.0.0.2]: <H1>Traffic Analysis for 10.0.0.2 -- gameserver</H1>
<TABLE>
<TR><TD>System:</TD> <TD>gameserver in Unknown (edit /etc/snmp/snmpd.conf)</TD></TR>
<TR><TD>Maintainer:</TD> <TD>Root <root@localhost> (configure /etc/snmp/snmp.local.conf)</TD></TR>
<TR><TD>Description:</TD><TD>eth1 </TD></TR>
<TR><TD>ifType:</TD> <TD>ethernetCsmacd (6)</TD></TR>
<TR><TD>ifName:</TD> <TD></TD></TR>
<TR><TD>Max Speed:</TD> <TD>100.0 Mbits/s</TD></TR>
<TR><TD>Ip:</TD> <TD>10.0.0.2 ()</TD></TR>
</TABLE>
######################################################################
# System: game
# Description: Linux game 2.6.9 #4 SMP Wed Dec 15 16:42:24 CST 2004 i686
# Contact: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
# Location: Unknown (edit /etc/snmp/snmpd.conf)
######################################################################
### Interface 1 >> Descr: 'eth0' | Name: '' | Ip: '61.144.222.112' | Eth: '00-11-43-d3-b7-cc' ###
Target[10.0.0.3_61.144.222.112]: /61.144.222.112:public@10.0.0.3:
SetEnv[10.0.0.3_61.144.222.112]: MRTG_INT_IP="61.144.222.112" MRTG_INT_DESCR="eth0"
MaxBytes[10.0.0.3_61.144.222.112]: 12500000
Title[10.0.0.3_61.144.222.112]: Traffic Analysis for 61.144.222.112 -- game
PageTop[10.0.0.3_61.144.222.112]: <H1>Traffic Analysis for 61.144.222.112 -- game</H1>
<TABLE>
<TR><TD>System:</TD> <TD>game in Unknown (edit /etc/snmp/snmpd.conf)</TD></TR>
<TR><TD>Maintainer:</TD> <TD>Root <root@localhost> (configure /etc/snmp/snmp.local.conf)</TD></TR>
<TR><TD>Description:</TD><TD>eth0 </TD></TR>
<TR><TD>ifType:</TD> <TD>ethernetCsmacd (6)</TD></TR>
<TR><TD>ifName:</TD> <TD></TD></TR>
<TR><TD>Max Speed:</TD> <TD>100.0 Mbits/s</TD></TR>
<TR><TD>Ip:</TD> <TD>61.144.222.112 ()</TD></TR>
</TABLE>
### Interface 2 >> Descr: 'eth1' | Name: '' | Ip: '10.0.0.3' | Eth: '00-11-43-d3-b7-cd' ###
Target[10.0.0.3_10.0.0.3]: /10.0.0.3:public@10.0.0.3:
SetEnv[10.0.0.3_10.0.0.3]: MRTG_INT_IP="10.0.0.3" MRTG_INT_DESCR="eth1"
MaxBytes[10.0.0.3_10.0.0.3]: 12500000
Title[10.0.0.3_10.0.0.3]: Traffic Analysis for 10.0.0.3 -- game
PageTop[10.0.0.3_10.0.0.3]: <H1>Traffic Analysis for 10.0.0.3 -- game</H1>
<TABLE>
<TR><TD>System:</TD> <TD>game in Unknown (edit /etc/snmp/snmpd.conf)</TD></TR>
<TR><TD>Maintainer:</TD> <TD>Root <root@localhost> (configure /etc/snmp/snmp.local.conf)</TD></TR>
<TR><TD>Description:</TD><TD>eth1 </TD></TR>
<TR><TD>ifType:</TD> <TD>ethernetCsmacd (6)</TD></TR>
<TR><TD>ifName:</TD> <TD></TD></TR>
<TR><TD>Max Speed:</TD> <TD>100.0 Mbits/s</TD></TR>
<TR><TD>Ip:</TD> <TD>10.0.0.3 ()</TD></TR>
</TABLE>
### Interface 3 >> Descr: 'lo' | Name: '' | Ip: '127.0.0.1' | Eth: '' ###
### The following interface is commented out because:
### * it is a Software Loopback interface
#
# Target[10.0.0.3_127.0.0.1]: /127.0.0.1:public@10.0.0.3:
# SetEnv[10.0.0.3_127.0.0.1]: MRTG_INT_IP="127.0.0.1" MRTG_INT_DESCR="lo"
# MaxBytes[10.0.0.3_127.0.0.1]: 1250000
# Title[10.0.0.3_127.0.0.1]: Traffic Analysis for 127.0.0.1 -- game
# PageTop[10.0.0.3_127.0.0.1]: <H1>Traffic Analysis for 127.0.0.1 -- game</H1>
# <TABLE>
# <TR><TD>System:</TD> <TD>game in Unknown (edit /etc/snmp/snmpd.conf)</TD></TR>
# <TR><TD>Maintainer:</TD> <TD>Root <root@localhost> (configure /etc/snmp/snmp.local.conf)</TD></TR>
# <TR><TD>Description:</TD><TD>lo </TD></TR>
# <TR><TD>ifType:</TD> <TD>softwareLoopback (24)</TD></TR>
# <TR><TD>ifName:</TD> <TD></TD></TR>
# <TR><TD>Max Speed:</TD> <TD>10.0 Mbits/s</TD></TR>
# <TR><TD>Ip:</TD> <TD>127.0.0.1 (game)</TD></TR>
# </TABLE>
### Interface 4 >> Descr: 'sit0' | Name: '' | Ip: '' | Eth: '' ###
### The following interface is commented out because:
### * it is administratively DOWN
### * it is operationally DOWN
### * has a speed of 0 which makes no sense
### * --ifref=ip is not unique for this interface
#
# Target[10.0.0.3_4]: 4:public@10.0.0.3:
# SetEnv[10.0.0.3_4]: MRTG_INT_IP="" MRTG_INT_DESCR="sit0"
# MaxBytes[10.0.0.3_4]: 0
# Title[10.0.0.3_4]: Traffic Analysis for 4 -- game
# PageTop[10.0.0.3_4]: <H1>Traffic Analysis for 4 -- game</H1>
# <TABLE>
# <TR><TD>System:</TD> <TD>game in Unknown (edit /etc/snmp/snmpd.conf)</TD></TR>
# <TR><TD>Maintainer:</TD> <TD>Root <root@localhost> (configure /etc/snmp/snmp.local.conf)</TD></TR>
# <TR><TD>Description:</TD><TD>sit0 </TD></TR>
# <TR><TD>ifType:</TD> <TD>Encapsulation Interface (131)</TD></TR>
# <TR><TD>ifName:</TD> <TD></TD></TR>
# <TR><TD>Max Speed:</TD> <TD>0.0 bits/s</TD></TR>
# </TABLE>
参考:http://oldsite.linuxaid.com.cn/solution/showsol.jsp?i=393 网络流量监控器mrtg全攻略一文