向PySNMP getCmd传递一个对象类型数组

2024-06-30 16:06:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将PySNMP用于监控系统,但我希望向它传递一个动态的对象列表,以查询每个连接,如下所示:

errorIndication, errorStatus, errorIndex, varBinds = next(
        getCmd(SnmpEngine(),
           CommunityData(self.device.getSNMPCommunity(), mpModel=0),
           UdpTransportTarget((self.device.getHost(), 
self.device.getSNMPPort()),self.device.getSNMPTimeout(),int(1)),
           ContextData(),
           ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)),
           ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)),
           ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysUpTime', 0)),
           ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)),
           ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0)),
        )
    )

相反,我希望能够做如下事情:

^{pr2}$

然后打电话

errorIndication, errorStatus, errorIndex, varBinds = next(
        getCmd(SnmpEngine(),
            CommunityData(device_to_proc.snmp_community, mpModel=0),
            UdpTransportTarget((device_to_proc.host, int(device_to_proc.snmp_port)),int(device_to_proc.snmp_timeout),int(1)),
            ContextData(),
            Sensors
        )
    )

我缺少pysnmp的另一个功能吗,或者有更好的方法来实现这个功能?在


Tags: toselfdeviceprocsnmpnextintmib
1条回答
网友
1楼 · 发布于 2024-06-30 16:06:39

我猜您只需要一个星号*来将收集到的对象序列解压到SNMP GET函数varargs中:

errorIndication, errorStatus, errorIndex, varBinds = next(
    getCmd(SnmpEngine(),
           CommunityData(device_to_proc.snmp_community, mpModel=0),
           UdpTransportTarget((device_to_proc.host, int(device_to_proc.snmp_port)),int(device_to_proc.snmp_timeout),int(1)),
           ContextData(),
           *Sensors
    )
)

另外,请记住,虽然没有明确限制可以发送给SNMP代理的OID数量,但某些代理可能会阻塞和/或拒绝在单个SNMP查询中为太多对象提供服务。在

相关问题 更多 >