gpt4 book ai didi

python - 在 PySNMP 代理中实现表

转载 作者:行者123 更新时间:2023-12-01 05:56:26 25 4
gpt4 key购买 nike

我正在使用 PySNMP 作为 SNMP 代理来实现一个表。

我遵循使用自动生成的 TRS-MIB.py 文件和包含实现的手动编写的 __TRS-MIB.py 文件的(PySNMP 源)架构。

我已经制作了一个工作表(2行),但是代码真的很难看,所以必须有更好的方法来做到这一点。 (此外,两行始终具有相同的值,这是一个错误。)

__TRS-MIB.py:

# Imported just in case new ASN.1 types would be created
from pyasn1.type import constraint, namedval
import time

# Imports

( Integer,
ObjectIdentifier,
OctetString, ) = mibBuilder.importSymbols(
"ASN1",
"Integer",
"ObjectIdentifier",
"OctetString"
)
( Bits,
Integer32,
ModuleIdentity,
MibIdentifier,
MibScalar,
MibScalarInstance,
TimeTicks, ) = mibBuilder.importSymbols(
"SNMPv2-SMI",
"Bits",
"Integer32",
"ModuleIdentity",
"MibIdentifier",
"MibScalar",
"MibScalarInstance",
"TimeTicks"
)

class TrsDeliveryTime(Integer32):
def clone(self, **kwargs):
if 'value' not in kwargs:
kwargs['value'] = int(time.time())
return Integer32.clone(self, **kwargs)

class TrsMessagesPerHour(Integer32):
def clone(self, **kwargs):
if 'value' not in kwargs:
kwargs['value'] = -int(time.time())
return Integer32.clone(self, **kwargs)

class TrsGatewayIndex(Integer32):
def clone(self, **kwargs):
if 'value' not in kwargs:
kwargs['value'] = 0
return Integer32.clone(self, **kwargs)

class TrsGatewayName(OctetString):
def clone(self, **kwargs):
if 'value' not in kwargs:
kwargs['value'] = 'SMG0'
return OctetString.clone(self, **kwargs)

class TrsGatewayState(OctetString):
def clone(self, **kwargs):
if 'value' not in kwargs:
kwargs['value'] = 'UP' + str(int(time.time()))
return OctetString.clone(self, **kwargs)

# Objects
( trsDeliveryTime,
trsMessagesPerHour,
trsGatewayTable,
trsGatewayEntry,
trsGatewayIndex,
trsGatewayName,
trsGatewayState, ) = mibBuilder.importSymbols(
'TRS-MIB',
'trsDeliveryTime',
'trsMessagesPerHour',
'trsGatewayTable',
'trsGatewayEntry',
'trsGatewayIndex',
'trsGatewayName',
'trsGatewayState',
)
__trsDeliveryTime = MibScalarInstance(trsDeliveryTime.name, (0,), TrsDeliveryTime(0))
__trsMessagesPerHour = MibScalarInstance(trsMessagesPerHour.name, (0,), TrsMessagesPerHour(0))
__trsGatewayIndex0 = MibScalarInstance(trsGatewayIndex.name, (0,), TrsGatewayIndex(0))
__trsGatewayName0 = MibScalarInstance(trsGatewayName.name, (0,), TrsGatewayName(0))
__trsGatewayState0 = MibScalarInstance(trsGatewayState.name, (0,), TrsGatewayState(0))
__trsGatewayIndex1 = MibScalarInstance(trsGatewayIndex.name, (1,), TrsGatewayIndex(0))
__trsGatewayName1 = MibScalarInstance(trsGatewayName.name, (1,), TrsGatewayName(0))
__trsGatewayState1 = MibScalarInstance(trsGatewayState.name, (1,), TrsGatewayState(0))


# Augmentions

# Exports

# Objects
mibBuilder.exportSymbols("__TRS-MIB",
trsDeliveryTime=__trsDeliveryTime,
trsMessagesPerHour=__trsMessagesPerHour,
trsGatewayTable=trsGatewayTable,
trsGatewayIndex0=__trsGatewayIndex0,
trsGatewayName0=__trsGatewayName0,
trsGatewayState0=__trsGatewayState0,
trsGatewayIndex1=__trsGatewayIndex1,
trsGatewayName1=__trsGatewayName1,
trsGatewayState1=__trsGatewayState1,
)

最佳答案

不要假装这是一段漂亮的代码,这里是对您的初始版本的一些优化。由于我手头没有您的 TRS-MIB 文件,因此尚未对其进行测试。

这个想法是通过重写其 readGet() 方法来专门化 MibScalarInstance 类(以使其服务于不同的列),该方法返回要报告回 SNMP 管理器的 var-bind 对。

您应该能够使用库存 CommandResponder 运行此 MIB ,只需确保在启动时加载您的 MIB。

import time

# Import managed objects
( trsDeliveryTime,
trsMessagesPerHour,
trsGatewayIndex,
trsGatewayName,
trsGatewayState, ) = mibBuilder.importSymbols(
'TRS-MIB',
'trsDeliveryTime',
'trsMessagesPerHour',
'trsGatewayIndex',
'trsGatewayName',
'trsGatewayState',
)

# Columnar managed objects instances implementation

class TrsDeliveryTimeInstance(MibScalarInstance):
def readGet(self, name, val, *args):
if name[-1] == 0: # Row #0
return self.name, self.syntax(int(time.time()))
elif name[-1] == 1: # Row #1
return self.name, self.syntax(time.time()//2)
else:
MibScalarInstance.readGet(self, name, val, *args)

class TrsMessagesPerHourInstance(MibScalarInstance):
def readGet(self, name, val, *args):
if name[-1] == 0: # Row #0
return self.name, self.syntax(-int(time.time()))
elif name[-1] == 1: # Row #1
return self.name, self.syntax(-time.time()//2)
else:
MibScalarInstance.readGet(self, name, val, *args)

class TrsGatewayIndexInstance(MibScalarInstance):
def readGet(self, name, val, *args):
if name[-1] == 0: # Row #0
return self.name, self.syntax(0)
elif name[-1] == 1: # Row #1
return self.name, self.syntax(1)
else:
MibScalarInstance.readGet(self, name, val, *args)

class TrsGatewayNameInstance(MibScalarInstance):
def readGet(self, name, val, *args):
if name[-1] == 0: # Row #0
return self.name, self.syntax('SMG0')
elif name[-1] == 1: # Row #1
return self.name, self.syntax('SMG1')
else:
MibScalarInstance.readGet(self, name, val, *args)

class TrsGatewayStateInstance(MibScalarInstance):
def readGet(self, name, val, *args):
if name[-1] == 0: # Row #0
return self.name, self.syntax('UP' + str(time.time()))
elif name[-1] == 1: # Row #1
return self.name, self.syntax('DOWN' + str(time.time()))
else:
MibScalarInstance.readGet(self, name, val, *args)

# Instantiate and export managed objects instances
mibBuilder.exportSymbols(
"__TRS-MIB",
# Row #0
TrsDeliveryTimeInstance(trsDeliveryTime.getName(), 0, trsDeliveryTime.getSyntax()),
TrsMessagesPerHourInstance(trsMessagesPerHour.getName(), 0, trsMessagesPerHour.getSyntax()),
TrsGatewayIndexInstance(trsGatewayIndex.getName(), 0, trsGatewayIndex.getSyntax()),
TrsGatewayNameInstance(trsGatewayName.getName(), 0, trsGatewayName.getSyntax()),
TrsGatewayStateInstance(trsGatewayState.getName(), 0, trsGatewayState.getSyntax()),
# Row #1
TrsDeliveryTimeInstance(trsDeliveryTime.getName(), 1, trsDeliveryTime.getSyntax()),
TrsMessagesPerHourInstance(trsMessagesPerHour.getName(), 1, trsMessagesPerHour.getSyntax()),
TrsGatewayIndexInstance(trsGatewayIndex.getName(), 1, trsGatewayIndex.getSyntax()),
TrsGatewayNameInstance(trsGatewayName.getName(), 1, trsGatewayName.getSyntax()),
TrsGatewayStateInstance(trsGatewayState.getName(), 1, trsGatewayState.getSyntax())
)

关于python - 在 PySNMP 代理中实现表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12386675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com