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

安装snmp和启用python模块

[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/

配置snmp

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

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

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,')'

 

 

参考链接:https://blog.csdn.net/mirahs/java/article/details/49619729

posted @ 2020-06-30 12:56  泡泡茶壶i  阅读(458)  评论(0编辑  收藏  举报