转自http://blog.chinaunix.net/u2/61187/showart_689604.html
现在就以用 snmp+mrtg 监控一台局域网内的 redhat 机器( IP : 192.168.13.103 )的网卡、内存、 CPU 、DISKIO 为例子,谈一下如何实现。
基本的法办就是用一台 redhat 监控机器( IP : 192.168.13.105 ),通过 snmpwalk 命令去抓 目标服务器的状态数据,然后用 mrtg 画出图来。
1 、首先我们要把目标 snmpd.conf 文件的配好。这是用 snmpwalk 命令 一抓取数据的关健。下面是目标机器(IP : 192.168.13.103 )上的 /etc/snmp/snmpd.conf 文件部份内容,红色的部份是我对 snmpd.conf 所做的改动。
[root@wy1 root]# cat /etc/snmp/snmpd.conf
####
# First, map the community name "public" into a "security name"
# sec.name source community
com2sec notConfigUser default public # 定义 community 名称为 public ,映射到安全名 notConfigUser 。
####
# Second, map the security name into a group name:
# groupName securityModel securityName
group notConfigGroup v1 notConfigUser # 定义安全用户名 notConfigUser 映射到 notConfigGroup 组。
group notConfigGroup v2c notConfigUser
####
# Third, create a view for us to let the group have rights to: # 定义一个 view, 来决定 notConfigUser 可以操作的范围。
# Make at least snmpwalk -v 1 localhost -c public system fast again. # 定义可查看的 snmp 的范围。
# 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
view all included .1
####
# Finally, grant the group read-only access to the systemview view. # 给 notConfigGroup 组所定义 view 名 all 以只读权限。
# group context sec.model sec.level prefix read write notif
access notConfigGroup "" any noauth exact all none none
#access notConfigGroup "" any noauth exact mib2 none none
# -----------------------------------------------------------------------------
# Here is a commented out example configuration that allows less
# restrictive access.
# YOU SHOULD CHANGE THE "COMMUNITY" TOKEN BELOW TO A NEW KEYWORD ONLY
# KNOWN AT YOUR SITE. YOU *MUST* CHANGE THE NETWORK TOKEN BELOW TO
# SOMETHING REFLECTING YOUR LOCAL NETWORK ADDRESS SPACE.
## sec.name source community
#com2sec local localhost COMMUNITY
#com2sec mynetwork NETWORK/24 COMMUNITY
## group.name sec.model sec.name
#group MyRWGroup any local
#group MyROGroup any mynetwork
#
#group MyRWGroup any otherv3user
#...
## incl/excl subtree mask
#view all included .1 80
## -or just the mib2 tree-
#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
## context sec.model sec.level prefix read write notif
#access MyROGroup "" any noauth 0 all none none
#access MyRWGroup "" any noauth 0 all all all
其实配制一个 snmpd.conf 文件不算太难,
( 1 )首选是定义一个共同体名 (community) ,这里是 public ,及可以访问这个 public 的用户名( sec name ),这里是 notConfigUser 。 Public 相当于用户 notConfigUser 的密码:)
# sec.name source community
com2sec notConfigUser default public
( 2 )定义一个组名( groupName )这里是 notConfigGroup ,及组的安全级别,把 notConfigGroup 这个用户加到这个组中。
groupName securityModel securityName
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
( 3 )定义一个可操作的范围 (view) 名, 这里是 all ,范围是 .1
# name incl/excl subtree mask(optional)
view all included .1
( 4 )定义 notConfigUser 这个组在 all 这个 view 范围内可做的操作,这时定义了 notConfigUser 组的成员可对.1 这个范围做只读操作。
# group context sec.model sec.level prefix read write notif
access notConfigGroup "" any noauth exact all none none
ok, 这样我们的 snmpd.conf 文件就基本配成了,用 service snmpd restart 重启 snmpd 服务。
配置文件范例:
###############################################################################
#
# snmpd.conf:
# An example configuration file for configuring the ucd-snmp snmpd agent.
#
############################################################################### # sec.name source community
#com2sec notConfigUser default public
########################## 新定义一个用户myuser,并设置用户的密码为fistforward #################################################
#项目分类名|用户名 | 可访问的来源地址|社区(就是密码)
com2sec myuser 10.117.186.40 passed
com2sec myuser 127.0.0.1 passed ################################################## 定义组配置 ################################################################# # groupName securityModel securityName
#group notConfigGroup v2c notConfigUser
#定义一个组,将我们的用户加入进这个组,同时指定改组可以使用的snmp的协议版本
#项目分类名|组名|协议版本| 添加的用户
group mygroup v1 myuser
group mygroup v2c myuser ########################################## 定义一个view视图,设置视图的可看范围 ################################################
# name incl/excl subtree mask(optional)
view systemview included .1.3.6.1.2.1.
view systemview included .1.3.6.1.2.1.25.1.
#项目名分类|视图名称|包含/排除|范围
view all included . ###################################################### 定义组可以看到的视图权限 #################################################
#项目分类名|组名|描述|sec.model|sec.level|prefix|可读读视图|可写的视图|notif
access mygroup "" any noauth exact all none none syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf) dontLogTCPWrappersConnects yes
现在我们做一个测试,在监控机上打下面的命令:
# snmpwalk -v 127.0.0.1 -c fistforward system
SNMPv2-MIB::sysDescr. = STRING: Linux dq_dev02 2.6.-573.7..el6.x86_64 # SMP Tue Sep :: UTC x86_64
SNMPv2-MIB::sysObjectID. = OID: NET-SNMP-MIB::netSnmpAgentOIDs.
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: () ::06.63
SNMPv2-MIB::sysContact. = STRING: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
SNMPv2-MIB::sysName. = STRING: dq_dev02
SNMPv2-MIB::sysLocation. = STRING: Unknown (edit /etc/snmp/snmpd.conf)
SNMPv2-MIB::sysORLastChange. = Timeticks: () ::00.06
SNMPv2-MIB::sysORID. = OID: SNMP-MPD-MIB::snmpMPDMIBObjects.3.1.
SNMPv2-MIB::sysORID. = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance
SNMPv2-MIB::sysORID. = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
SNMPv2-MIB::sysORID. = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID. = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID. = OID: IP-MIB::ip
SNMPv2-MIB::sysORID. = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID. = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORDescr. = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr. = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr. = STRING: The SNMP Management Architecture MIB.
SNMPv2-MIB::sysORDescr. = STRING: The MIB module for SNMPv2 entities
SNMPv2-MIB::sysORDescr. = STRING: The MIB module for managing TCP implementations
SNMPv2-MIB::sysORDescr. = STRING: The MIB module for managing IP and ICMP implementations
SNMPv2-MIB::sysORDescr. = STRING: The MIB module for managing UDP implementations
SNMPv2-MIB::sysORDescr. = STRING: View-based Access Control Model for SNMP.
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.06
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.06
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.06
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.06
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.06
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.06
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.06
SNMPv2-MIB::sysORUpTime. = Timeticks: () ::00.06
使用2c版本也可以
# snmpwalk -v 2c 127.0.0.1 -c fistforward system
SNMPv2-MIB::sysDescr. = STRING: Linux dq_dev02 2.6.-573.7..el6.x86_64 # SMP Tue Sep :: UTC x86_64
SNMPv2-MIB::sysObjectID. = OID: NET-SNMP-MIB::netSnmpAgentOIDs.
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: () ::19.26
-v 2c