net-snmp安装配置
系统是64位Red hat enterprise5。
####################################################
安装
####################################################
1. rpm-ivh net-snmp-5.3.2.2-9.el5.x86_64.rpm
如果出错:
error: Failed dependencies:
libsensors.so.3()(64bit) is needed by net-snmp-5.3.2.2-9.el5.x86_64
解决方案:
rpm -ivh lm_sensors-2.10.7-9.el5.x86_64.rpm
rpm -ivhlm_sensors-devel-2.10.7-9.el5.x86_64.rpm
2. rpm -ivhnet-snmp-utils-5.3.2.2-9.el5.x86_64.rpm
3. 修改/etc/snmp/snmp.conf
# Make at least snmpwalk -v 1 localhost -c public system fast again.
# 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.
# group context sec.model sec.level prefix read write notif
access notConfigGroup "" any noauth exact all none none
#access notConfigGroup "" any noauth exact systemview none none
3. service snmpd start
检查是否正常工作:
[root@bill snmp]# snmpget -v 1 -c public 192.168.1.901.3.6.1.2.1.1.1.0
SNMPv2-MIB::sysDescr.0 = STRING: Linux bill022.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64
[root@bill snmp]# snmpwalk -v 1 -c public 192.168.1.901.3.6.1.2.1.2
################################################
配置
################################################
net-snmp的配置文件在/etc/snmp/snmpd.conf,上面的安装已经进行了一些简单的配置。
其中的community的值其实相当于访问密码,默认是public,为了安全起见,最好修改。
如果想加入自定义的OID,可以参照这里面的例子:
# Note: this has been specifically commented out to prevent
# accidental security holes due to someone else on your system writing
# a /tmp/shtest before you do. Uncomment to use it.
#
# exec .1.3.6.1.4.1.2021.50 shelltest /bin/sh /tmp/shtest
# % snmpwalk -v 1 localhost -c public .1.3.6.1.4.1.2021.50
# enterprises.ucdavis.50.1.1 = 1
# enterprises.ucdavis.50.2.1 = "shelltest"
# enterprises.ucdavis.50.3.1 = "/bin/sh /tmp/shtest"
# enterprises.ucdavis.50.100.1 = 35
# enterprises.ucdavis.50.101.1 = "hello world."
# enterprises.ucdavis.50.101.2 = "hi there."
# enterprises.ucdavis.50.102.1 = 0
exec .1.3.6.1.4.1.2021.50 shelltest /bin/sh /tmp/shtest前面的注释符号去掉,同时在/tmp/下写一个shtest如下:
#!/bin/bash
date
修改完snmpd.conf保存后运行service snmpd restart。
然后可以在本机运行下面的命令验证:
[root@bill tmp]# snmpwalk -v 1 -c public 127.0.0.1 .1.3.6.1.4.1.2021.50
结果如下:
UCD-SNMP-MIB::ucdavis.50.1.1 = INTEGER: 1
UCD-SNMP-MIB::ucdavis.50.2.1 = STRING: "shelltest"
UCD-SNMP-MIB::ucdavis.50.3.1 = STRING: "/bin/sh /tmp/shtest"
UCD-SNMP-MIB::ucdavis.50.100.1 = INTEGER: 0
UCD-SNMP-MIB::ucdavis.50.101.1 = STRING: "Wed Apr 11 22:02:39 CST 2012"
UCD-SNMP-MIB::ucdavis.50.102.1 = INTEGER: 0
UCD-SNMP-MIB::ucdavis.50.103.1 = ""
也可以直接用snmpget验证:
[root@bill tmp]# snmpget -v 1 -c public 127.0.0.1 .1.3.6.1.4.1.2021.50.101.1
UCD-SNMP-MIB::ucdavis.50.101.1 = STRING: "Wed Apr 11 22:03:44 CST 2012"
除了用shell,也可以用其他脚本语言编写OID对应的命令,例如用python,原文出自:
http://hi.baidu.com/tmdniqusi/blog/item/aa71f7fc4fa71451d7887d1d.html
- import os, sys,time
- import re
- from subprocess import Popen, PIPE
- def getDiskUsage():
- diskdata=os.popen("/bin/df | grep -v /boot |grep -v tmpfs |awk 'BEGIN{sum=0;available=0}{sum+=$2;available+=$4}END{print sum,available }'").read()
- fields=re.split('\s+',diskdata.rstrip('\n'))
- total=int(fields[0])
- used=(total-int(fields[1]))*100.0/total
- return used
- def getCPUUsage():
- cpudata=os.popen("cat /proc/stat |head -n 1").read()
- fields=re.split('\s+',cpudata)
- cpuUsage=float(fields[1])+float(fields[2])+float(fields[3])
- cpuTotal=cpuUsage+float(fields[4])
- return (cpuUsage*100.0)/cpuTotal
- def getMemUsage():
- mem=os.popen("free | grep Mem:").read()
- fields=re.split('\s+',mem.rstrip('\n'))
- total=int(fields[1])
- used=total-(int(fields[3])+int(fields[5])+int(fields[6]))
- memUsage=(used*100.0)/total
- return memUsage
- def getTcpConnCnt():
- tcpConnCnt=os.popen("netstat -s -t | grep 'connections established' |awk '{print $1}'").read()
- return int(tcpConnCnt.rstrip('\n'))
- def getNowTimeStr():
- return time.strftime('%Y-%m-%d %H:%M:%S ',time.localtime(time.time()))
- if __name__=='__main__':
- if len(sys.argv) < 2:
- print "usage: python serverusage.py cpu|mem|disk|tcpcnt"
- sys.exit(-1)
- if sys.argv[1]=='cpu':
- print getCPUUsage()
- elif sys.argv[1]=='mem':
- print getMemUsage()
- elif sys.argv[1]=='disk':
- print getDiskUsage()
- elif sys.argv[1]=='tcpcnt':
- print getTcpConnCnt()
这里面计算CPU使用率有问题,因为/proc/stat得到的是CPU累计使用时间,应该取两次抽样的差值。
将上面的python文件命名为serverusage.py并保存在/usr/local/bill/下面。
然后修改snmpd.conf,添加以下语句:
################################################################################
# python example
exec .1.3.6.1.4.1.2652.1.6.1.1.9 cpu /usr/bin/python /usr/local/bill/serverusage.py cpu
exec .1.3.6.1.4.1.2652.1.6.1.1.10 mem /usr/bin/python /usr/local/bill/serverusage.py mem
exec .1.3.6.1.4.1.2652.1.6.1.1.11 disk /usr/bin/python /usr/local/bill/serverusage.py disk
exec .1.3.6.1.4.1.2652.1.6.1.1.12 tcpcnt /usr/bin/python /usr/local/bill/serverusage.py tcpcnt
保存退出,然后service snmpd restart.
验证:
[root@ bill]# snmpwalk -v 1 -c public 127.0.0.1 .1.3.6.1.4.1.2652.1.6.1.1.9
SNMPv2-SMI::enterprises.2652.1.6.1.1.9.1.1 = INTEGER: 1
SNMPv2-SMI::enterprises.2652.1.6.1.1.9.2.1 = STRING: "cpu"
SNMPv2-SMI::enterprises.2652.1.6.1.1.9.3.1 = STRING: "/usr/bin/python /usr/local/bill/serverusage.py cpu"
SNMPv2-SMI::enterprises.2652.1.6.1.1.9.100.1 = INTEGER: 0
SNMPv2-SMI::enterprises.2652.1.6.1.1.9.101.1 = STRING: "0.868230777215"
SNMPv2-SMI::enterprises.2652.1.6.1.1.9.102.1 = INTEGER: 0
SNMPv2-SMI::enterprises.2652.1.6.1.1.9.103.1 = ""