简体   繁体   English

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

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

I've got 3 switches I'm trying to get SNMP data from.我有 3 个交换机试图从中获取 SNMP 数据。 Every switch does respond from time to time, but which switches respond depends upon the order I'm querying them.每个交换机都会不时响应,但哪些交换机响应取决于我查询它们的顺序。 I'm using easysnmp.Session.我正在使用 easysnmp.Session。

My code (passwords obviously not shown):我的代码(密码显然没有显示):

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

When running this script for all for dictionaries, the only thing changing being the order of the switches queried I'm getting these results:当为所有字典运行此脚本时,唯一改变的是查询的开关顺序我得到这些结果:

====> Testcase 1 switch1 Switch: switch1, Result: <SNMPVariable value='10.160.59.131' (oid='enterprises.37072.302.3.1.1.3.6.0', oid_index='', snmp_type='IPADDR')> switch2 Switch: switch2, Result: <SNMPVariable value='10.160.59.132' (oid='enterprises.37072.302.3.1.1.3.6.0', oid_index='', snmp_type='IPADDR')> switch3 Switch: switch3, result: TIMEOUT ERROR [root@tesu john.roede]# python3 test2.py ====> 测试用例 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

====> Testcase 2 switch3 Switch: switch3, Result: <SNMPVariable value='10.160.59.133' (oid='enterprises.37072.302.3.1.1.3.6.0', oid_index='', snmp_type='IPADDR')> switch1 Switch: switch1, result: TIMEOUT ERROR switch2 Switch: switch2, Result: <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 ====> 测试用例 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

====> Testcase 3 switch2 Switch: switch2, Result: <SNMPVariable value='10.160.59.132' (oid='enterprises.37072.302.3.1.1.3.6.0', oid_index='', snmp_type='IPADDR')> switch3 Switch: switch3, result: TIMEOUT ERROR switch1 Switch: switch1, result: TIMEOUT ERROR [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

====> Testcase 4 switch2 Switch: switch2, Result: <SNMPVariable value='10.160.59.133' (oid='enterprises.37072.302.3.1.1.3.6.0', oid_index='', snmp_type='IPADDR')> switch3 Switch: switch3, Result: <SNMPVariable value='10.160.59.132' (oid='enterprises.37072.302.3.1.1.3.6.0', oid_index='', snmp_type='IPADDR')> switch1 Switch: switch1, result: TIMEOUT ERROR ====> 测试用例 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,结果:超时错误

As you can see, every switch does respond at times, so there shouldn't be an issue at the switch level.如您所见,每个开关有时都会响应,因此开关级别不应该有问题。 Just to be 100% sure I've also written a quick bash script querying all 3 switches in succession.为了 100% 确定,我还编写了一个快速的 bash 脚本来连续查询所有 3 个开关。 This does work:这确实有效:

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

I really can't figure out what's causing the issue with easysnmp.我真的不知道是什么导致了 easysnmp 的问题。 I've tried separating the several SNMP calls with a sleep to rule out I'm firing of the calls in too quick a succession, but this didn't solve the issue.我已经尝试将几个 SNMP 调用与睡眠分开,以排除我连续太快触发调用,但这并没有解决问题。 I've also looked in the easysnmp documentation for a Session.close() function but there doesn't seem to be one.我还查看了 Session.close() function 的 easysnmp 文档,但似乎没有。 There is mention of a update_session() function which I haven't tried since the documentation states: "While it is recommended to create a new Session instance instead, this method has been added for your convenience in case you really need it (we've mis-typed the community string before in our interactive sessions and totally understand your pain)."提到了一个 update_session() function ,我没有尝试过,因为文档指出: “虽然建议创建一个新的 Session 实例,但为了您的方便而添加了此方法,以防您确实需要它(我们'之前在我们的互动会议中输错了社区字符串,完全理解你的痛苦)。”

I'd really appreciate some help with this issue and would like to thank you for your time in advance.非常感谢您在此问题上提供的帮助,并提前感谢您抽出宝贵时间。

PS.附言。 Apologies, I can't seem to get the formatting of the output right:-(抱歉,我似乎无法正确设置 output 的格式:-(

I've been digging a bit deeper and it turns out this issue is caused by several devices using the same engine ID.我一直在深入挖掘,结果发现这个问题是由使用相同引擎 ID 的多个设备引起的。

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

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

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