其实从标题来看,这样的文章已经相当多,不过由于这次使用了最新版的系统和软件,在配置方面比原来的版本有了一些改进,明显更加方便,所以值得记录一下和大家分享。
这次实现监控的目标是系统网络流量、TCP连接数、CPU负载、system和user的CPU负载、内存消耗、swap使用,另外还可以在此基础上实现磁盘监控、Mailscanner的垃圾邮件等监控。
首先通过cvsup更新系统源码和ports数到当前的FreeBSD 6.1Release版本,保持系统和软件包的最新版本,然后使用极为方便的ports进行软件安装。
1、安装net-snmp-5.2.2
#cd /usr/ports/net-mgmt/net-snmp
#make install clean
然后就等着系统自动为您下载net-snmp-5.2.2.tar.gz软件包以及关联软件包,ports会自动完成后续的安装。
2、配置snmp
等安装完成后会看到成功的提示以及后续配置的说明。接下来我们进行简单的几个配置即可完成snmp的安装配置。
#mkdir /usr/local/etc/snmp
#cp /usr/local/share/snmp/snmpd.conf /usr/local/etc/snmp/
#vim /usr/local/etc/snmp/snmpd.conf
下面是我的配置文件内容
com2sec local localhost private
com2sec mynet 192.168.0.0/24 public
com2sec public default public
group mygroup v1 mynet
group mygroup v2c mynet
group mygroup usm mynet
group local v1 local
group local v2c local
group local usm local
group public v1 public
group public v2c public
group public usm public
view all included .1 80
access mygroup "" any noauth exact mib2 none none
access public "" any noauth exact all none none
access local "" any noauth exact all all all
syslocation Right here, right now.
syscontact Me
proc mountd
proc ntalkd 4
proc sendmail 10 1
exec echotest /bin/echo hello world
disk / 10000
load 12 14 14
其实里面只有很少的部分是自己修改的,大部分是默认值,具体的您可以对比一下您的snmpd.conf文件,建议把您的配置文件使用 grep -v # snmpd.conf > snmpd.conf.new 去掉所有的注释行再进行配置,这样看起来更清晰一些。
完成上面的配置,snmp的安装和配置已经完成,此时我们可以使用 snmpwalk -v 1 -c public localhost system 来测试看看返回结果,正常应该返回类似下面的内容。
SNMPv2-MIB::sysDescr.0 = STRING: FreeBSD www.toplee.com 6.1-RELEASE FreeBSD 6.1-RELEASE #0: Wed May 24 13:32:37 CST 2006 root@www.toplee.com:/usr/src/sys/i386/compile/TOPLEE i386
SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::dod.0.0.0.0.0.0.0
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (8548737) 23:44:47.37
SNMPv2-MIB::sysContact.0 = STRING: Me
SNMPv2-MIB::sysName.0 = STRING: www.toplee.com
SNMPv2-MIB::sysLocation.0 = STRING: Right here, right now.
SNMPv2-MIB::sysServices.0 = INTEGER: 72
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (11) 0:00:00.11
SNMPv2-MIB::sysORID.1 = OID: IF-MIB::ifMIB
SNMPv2-MIB::sysORID.2 = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID.3 = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID.4 = OID: IP-MIB::ip
SNMPv2-MIB::sysORID.5 = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID.6 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORID.7 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
SNMPv2-MIB::sysORID.8 = OID: SNMP-MPD-MIB::snmpMPDCompliance
SNMPv2-MIB::sysORID.9 = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance
SNMPv2-MIB::sysORDescr.1 = STRING: The MIB module to describe generic objects for network interface sub-layers
SNMPv2-MIB::sysORDescr.2 = STRING: The MIB module for SNMPv2 entities
SNMPv2-MIB::sysORDescr.3 = STRING: The MIB module for managing TCP implementations
SNMPv2-MIB::sysORDescr.4 = STRING: The MIB module for managing IP and ICMP implementations
SNMPv2-MIB::sysORDescr.5 = STRING: The MIB module for managing UDP implementations
SNMPv2-MIB::sysORDescr.6 = STRING: View-based Access Control Model for SNMP.
SNMPv2-MIB::sysORDescr.7 = STRING: The SNMP Management Architecture MIB.
SNMPv2-MIB::sysORDescr.8 = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr.9 = STRING: The management information definitions for the SNMP User-based Security Model.
SNMPv2-MIB::sysORUpTime.1 = Timeticks: (9) 0:00:00.09
SNMPv2-MIB::sysORUpTime.2 = Timeticks: (11) 0:00:00.11
SNMPv2-MIB::sysORUpTime.3 = Timeticks: (11) 0:00:00.11
SNMPv2-MIB::sysORUpTime.4 = Timeticks: (11) 0:00:00.11
SNMPv2-MIB::sysORUpTime.5 = Timeticks: (11) 0:00:00.11
SNMPv2-MIB::sysORUpTime.6 = Timeticks: (11) 0:00:00.11
SNMPv2-MIB::sysORUpTime.7 = Timeticks: (11) 0:00:00.11
SNMPv2-MIB::sysORUpTime.8 = Timeticks: (11) 0:00:00.11
SNMPv2-MIB::sysORUpTime.9 = Timeticks: (11) 0:00:00.11
您可以去掉后面的system参数得到所有的snmp返回数据,通过分析这些数据就可以实现对系统的各种监控,这是mrtg通过snmp实现监控的途径。
为了让系统在启动的时候自动启动snmpd监听服务,我们要对/etc/rc.conf进行配置,在该文件种添加下面内容:
snmpd_enable=”YES”
snmpd_flags=”-a -p /var/run/snmpd.pid”
snmptrapd_enable=”YES”
snmptrapd_flags=”-a -p /var/run/snmptrapd.pid”
下面参数可选
NET_SNMP_SYS_CONTACT=”Michael@toplee.com”
NET_SNMP_SYS_LOCATION=”Beijing,China”
DEFAULT_SNMP_VERSION=3
NET_SNMP_MIB_MODULES=”host smux mibII/mta_sendmail ucd-snmp/diskio”
NET_SNMP_LOGFILE=/var/log/snmpd.log
NET_SNMP_PERSISTENTDIR=/var/net-snmp
3、安装mrtg-2.13.2
在完成了上面的snmp安装配置之后,开始进行mrtg的安装配置,也使用非常方便的ports进行安装
#cd /usr/ports/net-mgmt/mrtg/
#make install clean
4、配置mrtg
#vim /usr/local/etc/mrtg/mrtg.cfg
下面是我的配置文件
WorkDir: /var/www/mrtg
Language: gb2312
Options[_]: growright, bits
Target[traffic_1]: 1:public@localhost:
SetEnv[traffic_1]: MRTG_INT_IP="localhost" MRTG_INT_DESCR="流量分析"
Xsize[traffic_1]: 380
MaxBytes[traffic_1]: 2500000
Title[traffic_1]: 前端服务器流量监控
PageTop[traffic_1]: <h1>前端服务器流量监控控</h1>
<table>
<tr><td>System:</td> <td>FreeBSD web server of Toplee.com Col </td></tr>
<tr><td>Maintainer:</td> <td>Michael <toplee@gamil.com> </td></tr>
<tr><td>ifType:</td> <td>ethernetCsmacd (6)</td></tr>
<tr><td>Max Speed:</td> <td>2.5 MBytes/s</td></tr>
</table>
########################### TCP Cons ######################################
Target[tcpopen]: .1.3.6.1.2.1.6.9.0&.1.3.6.1.2.1.6.9.0:public@localhost
Options[tcpopen]: nopercent,growright,gauge,noinfo
Title[tcpopen]: Open TCP connections
PageTop[tcpopen]: <h1>Open TCP connections</h1>
MaxBytes[tcpopen]: 1000000
YLegend[tcpopen]: # conns
ShortLegend[tcpopen]: connections
LegendI[tcpopen]: Connections:
LegendO[tcpopen]:
Legend1[tcpopen]: Open TCP connections
######################### CPU ############################################
LoadMIBs: /usr/local/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[cpu]:ssCpuRawUser.0&ssCpuRawUser.0:public@localhost + ssCpuRawSystem.0&ssCpuRawSystem.0:public@localhost + ssCpuRawNice.0&ssCpuRawNice.0:public@localhost
RouterUptime[cpu]: public@localhost
MaxBytes[cpu]: 100
Title[cpu]: CPU Load
PageTop[cpu]: <h1>Carga de CPU %</h1>
Unscaled[cpu]: ymwd
ShortLegend[cpu]: %
YLegend[cpu]: Uso de CPU
Legend1[cpu]: CPU Activa % (Carga)
Legend2[cpu]:
Legend3[cpu]:
Legend4[cpu]:
LegendI[cpu]: Active
LegendO[cpu]:
Options[cpu]: growright,nopercent
######################## CPU UsrSys ####################################
LoadMIBs: /usr/local/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[usrsys]:ssCpuRawUser.0&ssCpuRawSystem.0:public@localhost
RouterUptime[usrsys]: public@localhost
MaxBytes[usrsys]: 100
Title[usrsys]: CPU LOAD
PageTop[usrsys]: <h1>CPU (user and system) Load %</h1>
Unscaled[usrsys]: ymwd
ShortLegend[usrsys]: %
YLegend[usrsys]: CPU Utilization
Legend1[usrsys]: User CPU in % (Load)
Legend2[usrsys]: System CPU in % (Load)
Legend3[usrsys]:
Legend4[usrsys]:
LegendI[usrsys]: User
LegendO[usrsys]: System
Options[usrsys]: growright,nopercent
########################### Mem ###########################################
LoadMIBs: /usr/local/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Target[mem]: .1.3.6.1.4.1.2021.4.6.0&.1.3.6.1.4.1.2021.4.6.0:public@localhost
PageTop[mem]: <h1>Memory RAM</h1>
Options[mem]: nopercent,growright,gauge,noinfo
Title[mem]: Memory Libre
MaxBytes[mem]: 1055309824
kMG[mem]: k,M,G,T,P,X
YLegend[mem]: bytes
ShortLegend[mem]: bytes
LegendI[mem]: Free Memory:
LegendO[mem]:
Legend1[mem]: Free memory, not including swap, in bytes
####################### MEM1 ############################################
LoadMIBs: /usr/local/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Target[mem1]:.1.3.6.1.4.1.2021.4.6.0&.1.3.6.1.4.1.2021.4.15.0:public@localhost * 1024
MaxBytes[mem1]: 1055309824
Title[mem1]: MEMORY USAGE
kilo[mem1]: 1024
WithPeak[mem1]: my
Options[mem1]: gauge,growright
YLegend[mem1]: Memory Usage
LegendI[mem1]: Used:
LegendO[mem1]: Cached:
Legend1[mem1]: Used Memory Size
Legend2[mem1]: Cached Memory Size
Legend3[mem1]: 5min Max Used Memory Size
Legend4[mem1]: 5min Max Cached Memory Size
ShortLegend[mem1]: B
PageTop[mem1]: <h1>MEMORY USAGE</h1>
############################ Swap info #####################################
LoadMIBs: /usr/local/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[swap]: memAvailSwap.0&memAvailSwap.0:public@localhost
PageTop[swap]: <h1>Memory Swap</h1>
Options[swap]: nopercent,growright,gauge,noinfo
Title[swap]: Memory Libre
MaxBytes[swap]: 3000000
kMG[swap]: k,M,G,T,P,X
YLegend[swap]: bytes
ShortLegend[swap]: bytes
LegendI[swap]: Memory Libre:
LegendO[swap]:
Legend1[swap]: Swap memory avail, in bytes
######################## Load Avarage ####################################
LoadMIBs: /usr/local/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Target[load]:.1.3.6.1.4.1.2021.10.1.3.2&.1.3.6.1.4.1.2021.10.1.3.3:public@localhost * 100
WithPeak[load]: my
MaxBytes[load]: 10000
Title[load]: SYSTEM LOAD AVG.
Options[load]: growright,gauge,integer,nopercent
YLegend[load]: Load Average
LegendI[load]: 5 min:
LegendO[load]: 15 min:
Legend1[load]: Load average for 5 Min
Legend2[load]: Load average for 15 Min
Legend3[load]: Max load average for 5 Min
Legend4[load]: Max Load average for 15 Min
YTicsFactor[load]: 0.01
#ShortLegend[load]: _
PageTop[load]: <h1>LOAD AVERAGE </h1>
可以根据具体的需求进行修改上面的配置文件,有什么疑问可以参考mrtg的官方文档,比如http://net-snmp.sourceforge.net/tutorial/tutorial-5/mrtg/index.html 有当前版本的snmp下mrtg.cfg配置的范例可以参考。
下面是我的mrtg监控页面效果 http://toplee:toplee@mrtg.toplee.com
以上的讨论基本都是基于在本机上进行监控而言,如果你有多台机器进行监控,可以根据具体需要修改snmpd.conf文件中public的权限,让您的mrtg监控机器可以通过snmp取到监控数据。
欢迎大家一起讨论,有发现我前面的什么地方有问题也欢迎给我指出来,共同进步。