FreeBSD 6.1上用net-snmp-5.2.2和mrtg-2.13.2实现多种监控

其实从标题来看,这样的文章已经相当多,不过由于这次使用了最新版的系统和软件,在配置方面比原来的版本有了一些改进,明显更加方便,所以值得记录一下和大家分享。

这次实现监控的目标是系统网络流量、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]:

前端服务器流量监控控

  

    

  

  

    

  

 

System: FreeBSD web server of Toplee.com Col
Maintainer: Michael <toplee@gamil.com>
ifType: ethernetCsmacd (6)
Max Speed: 2.5 MBytes/s

########################### 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]:

Open TCP connections

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]:

Carga de CPU %

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]:

CPU (user and system) Load %

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]:

Memory RAM

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]:

MEMORY USAGE

############################ Swap info #####################################
LoadMIBs: /usr/local/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[swap]: memAvailSwap.0&memAvailSwap.0:public@localhost
PageTop[swap]:

Memory Swap

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]:

LOAD AVERAGE

可以根据具体的需求进行修改上面的配置文件,有什么疑问可以参考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取到监控数据。

欢迎大家一起讨论,有发现我前面的什么地方有问题也欢迎给我指出来,共同进步。

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
7 Responses
  1. wfg says:

    请问启动snmpd时出现以下错误是怎么回事?
    ns# tail /var/log/snmpd.log
    sysctl: physmem: Cannot allocate memory
    [init_smux] bind failed: Address already in use
    Error opening specified endpoint “restart”
    Server Exiting with code 1

  2. Michael says:

    一般来说应该还是conf文件配置问题,官方也很少有这方面完整的文档说明。

  3. isee says:

    Michael 老大我的系统平台是freebsd6.1通过ports安装后发现cpu不能监控,错误如下,麻烦您帮我看一看
    2006-11-10 10:38:21: ERROR: Target[cpu][_IN_] ‘ $target->[2]{$mode} + $target- >[3]{$mode} + $target->[4]{$mode} ‘ (warn): Use of uninitialized value in addi tion (+) at (eval 2234) line 1.
    2006-11-10 10:38:21: ERROR: Target[cpu][_OUT_] ‘ $target->[2]{$mode} + $target ->[3]{$mode} + $target->[4]{$mode} ‘ (warn): Use of uninitialized value in add ition (+) at (eval 2235) line 1.
    2006-11-10 10:38:21: ERROR: Target[usrsys][_IN_] ‘ $target->[5]{$mode} ‘ did not eval into defined data
    2006-11-10 10:38:21: ERROR: Target[usrsys][_OUT_] ‘ $target->[5]{$mode} ‘ did no t eval into defined data
    2006-11-10 10:38:21: ERROR: Target[swap][_IN_] ‘ $target->[8]{$mode} ‘ did not e val into defined data
    2006-11-10 10:38:21: ERROR: Target[swap][_OUT_] ‘ $target->[8]{$mode} ‘ did not eval into defined data

  4. btlcxh says:

    Michael兄,
    小弟在监控cpu时也碰到isee的这个问题
    执行 mrtg /etc/mrtg/cpu.cfg后报错如下:

    [root@unnc-nagios snmp]# mrtg /etc/mrtg/cpu.cfg
    SNMP Error:
    Received SNMP response with error code
    error status: noSuchName
    index 1 (OID: 1.3.6.1.4.1.2021.4.6.0)
    SNMPv1_Session (remote host: “10.2.1.1” [10.2.1.1].161)
    community: “public”
    request ID: 381777074
    PDU bufsize: 8000 bytes
    timeout: 2s
    retries: 5
    backoff: 1)
    at /usr/bin/../lib/mrtg2/SNMP_util.pm line 490
    SNMPGET Problem for .1.3.6.1.4.1.2021.4.6.0 .1.3.6.1.4.1.2021.4.6.0 sysUptime sysName on

    public@10.2.1.1::::::v4only
    at /usr/bin/mrtg line 1920
    ERROR: Target[mem][_IN_] ‘ $target->[0]{$mode} ‘ did not eval into defined data
    ERROR: Target[mem][_OUT_] ‘ $target->[0]{$mode} ‘ did not eval into defined data
    [root@unnc-nagios snmp]#

    如果知道,敬请告知,谢谢!

  5. Michael says:

    mrtg的配置文件太麻烦,出现问题比较棘手,从错误情况来看,还是$target->[0]{$mode}这个变量没有在预定义的数据当中找到匹配的值,但是具体怎么产生的我也没有仔细研究过,多试试几次吧,最近也没有心情去折腾这些东西了,很抱歉不能帮上楼上的朋友。

  6. crazywill says:

    michael, 你的mrtg监控页面效果
    里面有乱码啊
    最大 平均 目前
    5 min: 91 $1$2/秒 11 $1$2/秒 14 $1$2/秒
    15 min: 52 $1$2/秒 9 $1$2/秒 10 $1$2/秒
    这些$1$2是乱码吧?

  7. Michael says:

    [Comment ID #23785 Will Be Quoted Here]

    乱码一方面在在mrtg.cfg里面要指定html的header字符集,另外,$1$2那个是因为没有读到数据,空了我看看

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image