python通过SNMP协议收集服务器监

2023-01-31 02:01:04 服务器 协议 收集
[root@test ~]# wget Http://nchc.dl.sourceforge.net/project/net-snmp/net-snmp/5.7.3/net-snmp-5.7.3.tar.gz
[root@test ~]# tar xf net-snmp-5.7.3.tar.gz
[root@test ~]# cd net-snmp-5.7.3
[root@test ~]# ./configure --prefix=/usr/local/net-snmp --with-Python-modules --with-persistent-directory=/usr/local/net-snmp
[root@test ~]# make
[root@test ~]# make install
[root@test ~]# echo '/usr/local/net-snmp/lib/' >> /etc/ld.so.conf.d/server.conf
[root@test ~]# ldconfig
[root@test ~]# echo 'export PATH=/usr/local/net-snmp/sbin/:${PATH}' >> /etc/profile
[root@test ~]# echo 'export PATH=/usr/local/net-snmp/bin/:${PATH}' >> /etc/profile
[root@test ~]# source /etc/profile
[root@test ~]# cd python
[root@test ~]# python setup.py install
[root@test ~]# cp -r netsnmp /usr/local/python/lib/python2.7/site-packages/
[root@test ~]# cp build/lib.linux-x86_64-2.7/netsnmp/client_intf.so /usr/local/python/lib/python2.7/site-packages/netsnmp/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

配置snmp

[root@test ~]# net-snmp-config --create-snmpv3-user -ro -a passWord -x DES -X passworddes snmpuser
[root@test ~]# snmpd
  • 1
  • 2

snmpuser:用户名 
password:密码,密码必须大于8个字符 
DES:加密方式,这边支持AES、DES两种 
passworddes:DES口令,必须大于8位

测试

[root@test ~]# snmpwalk -v 3 -u snmpuser -a MD5 -A password -l authPriv -x DES -X passworddes 127.0.0.1
[root@test ~]# snmpwalk -v 3 -u snmpuser -a MD5 -A password -l authPriv -x DES -X passworddes 127.0.0.1 .1.3.6.1.4.1.2021.10.1.3.1
[root@test ~]# snmpwalk -v 3 -u snmpuser -a MD5 -A password -l authPriv -x DES -X passworddes 127.0.0.1 .1.3.6.1.4.1.2021.4.11.0
[root@test ~]# snmpwalk -v 3 -u snmpuser -a MD5 -A password -l authPriv -x DES -X passworddes 127.0.0.1 sysName
  • 1
  • 2
  • 3
  • 4

python测试脚本test.py

#!/usr/bin/env python
#cofing:utf-8
import netsnmp


print "---v3 setup-------------------------------------\n"
sess = netsnmp.Session(Version=3,
                       DestHost='localhost',
                       SecLevel='authPriv',
                       SecName='snmpuser',
                       PrivPass='passworddes',
                       AuthPass='password')

sess.UseSprintValue = 1

vars = netsnmp.VarList(netsnmp.Varbind('sysUpTime', 0),
                       netsnmp.Varbind('sysContact', 0),
                       netsnmp.Varbind('sysLocation', 0))
print "---v3 get-------------------------------------\n"
vals = sess.get(vars)
print "v3 sess.get result: ", vals, "\n"

for var in vars:
    print var.tag, var.iid, "=", var.val, '(',var.type,')'
print "\n"

print "---v3 getnext-------------------------------------\n"

vals = sess.getnext(vars)
print "v3 sess.getnext result: ", vals, "\n"

for var in vars:
    print var.tag, var.iid, "=", var.val, '(',var.type,')'
print "\n"

vars = netsnmp.VarList(netsnmp.Varbind('sysUpTime'),
                       netsnmp.Varbind('sysORLastChange'),
                       netsnmp.Varbind('sysORID'),
                       netsnmp.Varbind('sysORDescr'),
                       netsnmp.Varbind('sysORUpTime'))

vals = sess.getbulk(2, 8, vars)
print "v3 sess.getbulk result: ", vals, "\n"

for var in vars:
    print var.tag, var.iid, "=", var.val, '(',var.type,')'
print "\n"

print "---v3 set-------------------------------------\n"

vars = netsnmp.VarList(
    netsnmp.Varbind('sysLocation','0', 'my final destination'))
res = sess.set(vars)
print "v3 sess.set result: ", res, "\n"

print "---v3 walk-------------------------------------\n"
vars = netsnmp.VarList(netsnmp.Varbind('system'))

vals = sess.walk(vars)
print "v3 sess.walk result: ", vals, "\n"

for var in vars:
    print "  ",var.tag, var.iid, "=", var.val, '(',var.type,')'

相关文章