python通过SNMP协议收集服务器监
短信预约 -IT技能 免费直播动态提醒
[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,')'
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341