分类: BSD
2007-03-17 18:55:52
FreeBSD下实现SNMP+MRTG监控网络服务器
Paolo
利用一天时间,研究了一下FreeBSD下的SNMP+MRTG,由于FreeBSD同样是非常流行的服务器操作系统,特整理了一份关于FreeBSD的SNMP+MRTG文档。在实验整理过程中,发现在FreeBSD下确实有一些与在linux下不同的地方,但相当一部分的操作方法 还是相同的。具体实验过程如下(以后还会继续增加其它各方面的监控,到时会再补充这份文档):
■安装前的准备
◆如果没有更新ports,请先更新ports. 具体方法如下:
-bash-2.05b#cp /usr/share/examples/cvsup/ports-supfile /usr/
先复制一份到/usr/下,这样不用更改原来的ports-supfile文件。我们用复制的ports-supfile文件进行ports的更新。
◆编辑ports-supfile文件,把*default host=CHANGE_THIS.FreeBSD.org改为
*default host=freebsd.org
◆把ports-all注释掉
把下面需要的Ports组件打开(把#号去掉),
ports-base
ports-arabic
ports-archivers
…..
…..
ports-x11-wm
◆-bash-2.05b#cvsup -g -L 2 /usr/ports-supfile
进行ports的更新。完成后,就可以使用最新的ports进行相关软件包的安装了。
■安装
○安装SNMP
◆-bash-2.05b# cd /usr/ports/net-mgmt/net-snmp
◆-bash-2.05b# make install clean 进行SNMP的编译,安装,及安装后源码的删除
##此处,我在安装过程中遇到错误。显示snmp_perl.c错误。如果遇到这样的错误,我们可以在安装过程中禁用snmp的perl支持。具体方法是在编译过程中使用如果选项:
-bash-2.05b#make CONFIGURE_ARGS+="WITHOUT_PERL=yes" install clean
这样就可以安装成功了
○安装MRTG
◆-bash-2.05b# cd /usr/ports/net-mgmt/mrtg
◆-bash-2.05b# make install clean 进行编译,安装
■配置
○SNMP配置:
◆修改/etc/rc.conf:
-bash-2.05b# vi /etc/rc.conf
加入:
------------------------------------------------------------------
snmpd_enable="YES" 设置服务自启动
snmpd_flags="-a -p /var/run/snmpd.pid" 指定PID文件位置
snmptrapd_enable="YES"
snmptrapd_flags="-a -p /var/run/snmptrapd.pid"
------------------------------------------------------------------
◆-bash-2.05b# /etc/netstart 重启动网络相关
◆ 编辑snmpd.conf:
-bash-2.05b#vi /usr/local/share/snmp/snmpd.conf
在/usr/local/share/snmp/目录下有个snmpd.conf.example,可以复制一份为snmpd.conf.进行相关设置更改。
具体内容如下:
-bash-2.05b# cat /usr/local/share/snmp/snmpd.conf|grep -Ev "^$|^#"
com2sec notConfigUser default public
group MyRWGroup v1 notConfigUser
group MyRWGroup v2c notConfigUser
view systemview included .1
access MyRWGroup "" any noauth exact systemview none none
view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
syslocation paoloVM
syscontact Paolo
proc mountd
proc ntalkd 4
proc sendmail 10 1
exec echotest /bin/echo hello world
disk / 10000
load 12 14 14
pass .
◆ 启动snmpd:
-bash-2.05b#/usr/local/etc/rc.d/snmpd.sh start
启动后,执行:
-bash-2.05b# tail -f /var/log/snmpd.log
NET-SNMP version
表示没有错误。
然后再执行:
-bash-2.05b# snmpwalk -v 2c -c public
SNMPv2-MIB::sysDescr.0 = STRING: FreeBSD paoloVM 5.2-RELEASE FreeBSD 5.2-RELEASE #0: Sun Jan 11 04:21:45 GMT 2004 root@wv1u.btc.adaptec.com:/usr/obj/usr/src/sys/GENERIC i386
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.8
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (46583) 0:07:45.83
SNMPv2-MIB::sysContact.0 = STRING: Paolo
SNMPv2-MIB::sysName.0 = STRING: paoloVM
SNMPv2-MIB::sysLocation.0 = STRING: paoloVM
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (4) 0:00:00.04
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
。。。。。。。
。。。。。。。
输出类似以上信息,表示snmp安装启动成功,可以在其它服务器上获取信息。
##这个命令在执行时,不能写成snmpwalk -v 2c
○ MRTG配置:
◆进入mrtg的配置文件所在目录:
-bash-2.05b#cd /usr/local/etc/mrtg
生成配置文件:
-bash-2.05b# cfgmaker --global 'WorkDir:/usr/local/www/data/mrtg' --global 'Options[_]:growright,bits' --ifref=ip public@
--base: Get Device Info on public@
--base: Vendor Id:
--base: Populating confcache
--coca: populate confcache public@
--coca: store in confcache public@
--coca: store in confcache public@
--coca: store in confcache public@
--coca: store in confcache public@
--coca: store in confcache public@
--coca: store in confcache public@
--coca: store in confcache public@
--coca: store in confcache public@
--coca: store in confcache public@
--coca: store in confcache public@
--coca: store in confcache public@
--base: Get Interface Info
--base: Walking ifIndex
--base: Walking ifType
--base: Walking ifAdminStatus
--base: Walking ifOperStatus
--base: Walking ifMtu
--base: Walking ifSpeed
该命令中的IP地址(
◆ 生成index.html页面:
-bash-2.05b# indexmaker --output=/usr/local/www/data/mrtg/135/index.html /usr/local/etc/mrtg/mrtg_135.cfg
其中,--output 指定apache存放页面的目录
后面是上一步生成的配置文件,根据配置文件的内容,生成index.html页
在该配置文件中,需要加入你所要监控的具体项目,相关具体内容可以参考如下:
LoadMIBs: /usr/local/share/snmp/mibs/UCD-SNMP-MIB.txt 通过相关MIB,获取信息
Target[paoloVM.cpu]:ssCpuRawUser.0&ssCpuRawUser.0:public@
RouterUptime[paoloVM.cpu]: public@
MaxBytes[paoloVM.cpu]: 100
Title[paoloVM.cpu]: CPU Load
PageTop[paoloVM.cpu]: Active CPU Load %
Unscaled[paoloVM.cpu]: ymwd
ShortLegend[paoloVM.cpu]: %
YLegend[paoloVM.cpu]: CPU Utilization
Legend1[paoloVM.cpu]: Active CPU in % (Load)
Legend2[paoloVM.cpu]:
Legend3[paoloVM.cpu]:
Legend4[paoloVM.cpu]:
LegendI[paoloVM.cpu]: Active
LegendO[paoloVM.cpu]:
Options[paoloVM.cpu]: growright,nopercent,gauge
LoadMIBs: /usr/local/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Target[paoloVM.mem]: .
PageTop[paoloVM.mem]: Free Memory
WorkDir: /usr/local/www/data/mrtg/135/ 定义工作目录,在该目录下生成页面
Options[paoloVM.mem]: nopercent,growright,gauge,noinfo
Title[paoloVM.mem]: Free Memory
MaxBytes[paoloVM.mem]: 1000000
kMG[paoloVM.mem]: k,M,G,T,P,X
YLegend[paoloVM.mem]: bytes
ShortLegend[paoloVM.mem]: bytes
LegendI[paoloVM.mem]: Free Memory:
LegendO[paoloVM.mem]:
Legend1[paoloVM.mem]: Free memory, not including swap, in bytes
LoadMIBs: /usr/local/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[paoloVM.swap]: memAvailSwap.0&memAvailSwap.0:public@paoloVM
PageTop[paoloVM.swap]: Swap Memory
WorkDir: /usr/local/www/data/mrtg/135/
Options[paoloVM.swap]: nopercent,growright,gauge,noinfo
Title[paoloVM.swap]: Free Memory
MaxBytes[paoloVM.swap]: 1000000
kMG[paoloVM.swap]: k,M,G,T,P,X
YLegend[paoloVM.swap]: bytes
ShortLegend[paoloVM.swap]: bytes
LegendI[paoloVM.swap]: Free Memory:
LegendO[paoloVM.swap]:
Legend1[paoloVM.swap]: Swap memory avail, in bytes
WorkDir: /usr/local/www/data/mrtg/135/
Title[paoloVM.ping]: Round Trip Time
PageTop[paoloVM.ping]: Ping Status
Target[paoloVM.ping]: `/usr/local/etc/mrtg/ping.sh`
MaxBytes[paoloVM.ping]: 2000
Options[paoloVM.ping]: growright,unknaszero,nopercent,gauge
LegendI[paoloVM.ping]: Pkt loss %
LegendO[paoloVM.ping]: Avg RTT
YLegend[paoloVM.ping]: RTT (ms)
以上三段分别获取CPU、Free Memory、Swap_Memory以及ping 用以测试连接外网状态的信息。
◆ 最后一步
-bash-2.05b# mrtg /usr/local/etc/mrtg/mrtg_135.cfg
反复执行,前几次会有错误提示,直到没有错误为止。通常第三次就正常了!
把上面的命令写入cron,让其自动执行:
-bash-2.05b# crontab -l
*/5 * * * * /usr/local/bin/mrtg /usr/local/etc/mrtg/mrtg_135.cfg
相关截图