net-snmp安装配置

分类: 系统运维 174人阅读 评论(0) 收藏 举报

系统是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

 

[python] view plaincopy
  1. import os, sys,time  
  2. import re  
  3. from subprocess import Popen, PIPE  
  4.   
  5. def getDiskUsage():  
  6.     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()  
  7.     fields=re.split('\s+',diskdata.rstrip('\n'))  
  8.     total=int(fields[0])  
  9.     used=(total-int(fields[1]))*100.0/total  
  10.     return used  
  11. def getCPUUsage():  
  12.     cpudata=os.popen("cat /proc/stat |head -n 1").read()  
  13.     fields=re.split('\s+',cpudata)  
  14.     cpuUsage=float(fields[1])+float(fields[2])+float(fields[3])  
  15.     cpuTotal=cpuUsage+float(fields[4])  
  16.     return (cpuUsage*100.0)/cpuTotal  
  17. def getMemUsage():  
  18.     mem=os.popen("free | grep Mem:").read()  
  19.     fields=re.split('\s+',mem.rstrip('\n'))  
  20.     total=int(fields[1])  
  21.     used=total-(int(fields[3])+int(fields[5])+int(fields[6]))  
  22.     memUsage=(used*100.0)/total  
  23.     return memUsage  
  24. def getTcpConnCnt():  
  25.     tcpConnCnt=os.popen("netstat -s -t | grep 'connections established' |awk '{print $1}'").read()  
  26.     return int(tcpConnCnt.rstrip('\n'))  
  27. def getNowTimeStr():  
  28.     return time.strftime('%Y-%m-%d %H:%M:%S ',time.localtime(time.time()))  
  29. if __name__=='__main__':  
  30.     if len(sys.argv) < 2:  
  31.         print "usage: python serverusage.py cpu|mem|disk|tcpcnt"  
  32.         sys.exit(-1)    
  33.     if sys.argv[1]=='cpu':  
  34.         print getCPUUsage()  
  35.     elif sys.argv[1]=='mem':  
  36.         print getMemUsage()  
  37.     elif sys.argv[1]=='disk':  
  38.         print getDiskUsage()  
  39.     elif sys.argv[1]=='tcpcnt':  
  40.         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 = ""