snmp 管理工具net-snmp安裝配置

  

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 = ""

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章