繁体   English   中英

Python3 easysnmp查询多个开关导致部分随机开关随机超时

[英]Python3 easysnmp querying multiple switches results in a random timeout for some random switches

我有 3 个交换机试图从中获取 SNMP 数据。 每个交换机都会不时响应,但哪些交换机响应取决于我查询它们的顺序。 我正在使用 easysnmp.Session。

我的代码(密码显然没有显示):

import easysnmp

switches_dns1 = {
            "switch1": "switch-mBvk1-1.obis.ns.nl",
            "switch2": "switch-mBvk1-2.obis.ns.nl",
            "switch3": "switch-abv5-1.obis.ns.nl"
            }

switches_dns2 = {
            "switch3": "switch-abv5-1.obis.ns.nl",
            "switch1": "switch-mBvk1-1.obis.ns.nl",
            "switch2": "switch-mBvk1-2.obis.ns.nl"          
            }

switches_dns3 = {
            "switch2": "switch-mBvk1-2.obis.ns.nl",
            "switch3": "switch-abv5-1.obis.ns.nl",
            "switch1": "switch-mBvk1-1.obis.ns.nl"                      
            }

switches_dns4 = {
            "switch2": "switch-abv5-1.obis.ns.nl",
            "switch3": "switch-mBvk1-2.obis.ns.nl",
            "switch1": "switch-mBvk1-1.obis.ns.nl"                      
            }

print("====> Testcase 1")   
for switch, hostname_dns in  switches_dns1.items():
    print(switch)
    try:
        snmp_session = easysnmp.Session(hostname=hostname_dns, security_username='test_user', auth_protocol='MD5', auth_password='blahblahblah', version=3, security_level='auth_with_privacy', privacy_password='blahblahblah', privacy_protocol='DES')
        result=snmp_session.get('.1.3.6.1.4.1.37072.302.3.1.1.3.6.0')
        print("Switch: {}, Result: {}".format(switch, result))
    except easysnmp.exceptions.EasySNMPTimeoutError as err:
        print("Switch: {}, result: TIMEOUT ERROR".format(switch))

当为所有字典运行此脚本时,唯一改变的是查询的开关顺序我得到这些结果:

====> 测试用例 1 switch1 开关:switch1,结果:<SNMPVariable value='10.160.59.131' (oid='enterprises.37072.302.3.1.1.3.6.0', oid_index='', snmp_type='IPADDR')> switch2 开关:switch2,结果:<SNMPVariable value='10.160.59.132' (oid='enterprises.37072.302.3.1.1.3.6.0', oid_index='', snmp_type='IPADDR')> switch3 开关:switch3,结果:超时错误 [root@tesu john.roede]# python3 test2.py

====> 测试用例 2 switch3 开关:switch3,结果:<SNMPVariable value='10.160.59.133' (oid='enterprises.37072.302.3.1.1.3.6.0', oid_index='', snmp_type='IPADDR')> switch1 开关:switch1,结果:TIMEOUT ERROR switch2 开关:switch2,结果:<SNMPVariable value='10.160.59.132' (oid='enterprises.37072.302.3.1.1.3.6.0', oid_index='', snmp_type='IPADDR' )> [root@tesu john.roede]# python3 test2.py

====> 测试用例 3 switch2 开关:switch2,结果:<SNMPVariable value='10.160.59.132' (oid='enterprises.37072.302.3.1.1.3.6.0', oid_index='', snmp_type='IPADDR')> switch3 开关:switch3,结果:超时错误 switch1 开关:switch1,结果:超时错误 [root@tesu john.roede]# python3 test2.py

====> 测试用例 4 switch2 开关:switch2,结果:<SNMPVariable value='10.160.59.133' (oid='enterprises.37072.302.3.1.1.3.6.0', oid_index='', snmp_type='IPADDR')> switch3 开关:switch3,结果:<SNMPVariable value='10.160.59.132' (oid='enterprises.37072.302.3.1.1.3.6.0', oid_index='', snmp_type='IPADDR')> switch1 开关:switch1,结果:超时错误

如您所见,每个开关有时都会响应,因此开关级别不应该有问题。 为了 100% 确定,我还编写了一个快速的 bash 脚本来连续查询所有 3 个开关。 这确实有效:

./test.sh
SNMPv2-SMI::enterprises.37072.302.3.1.1.3.6.0 = IpAddress: 10.160.59.131
SNMPv2-SMI::enterprises.37072.302.3.1.1.3.6.0 = IpAddress: 10.160.59.132
SNMPv2-SMI::enterprises.37072.302.3.1.1.3.6.0 = IpAddress: 10.160.59.133

我真的不知道是什么导致了 easysnmp 的问题。 我已经尝试将几个 SNMP 调用与睡眠分开,以排除我连续太快触发调用,但这并没有解决问题。 我还查看了 Session.close() function 的 easysnmp 文档,但似乎没有。 提到了一个 update_session() function ,我没有尝试过,因为文档指出: “虽然建议创建一个新的 Session 实例,但为了您的方便而添加了此方法,以防您确实需要它(我们'之前在我们的互动会议中输错了社区字符串,完全理解你的痛苦)。”

非常感谢您在此问题上提供的帮助,并提前感谢您抽出宝贵时间。

附言。 抱歉,我似乎无法正确设置 output 的格式:-(

我一直在深入挖掘,结果发现这个问题是由使用相同引擎 ID 的多个设备引起的。

有关详细信息,请参阅https://github.com/easysnmp/easysnmp/issues/156

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM