在PySNMP代理中实现表

时间:2012-09-12 10:59:29

标签: python snmp pysnmp

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

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

我已经制作了一个工作(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,
                         )

1 个答案:

答案 0 :(得分:2)

不假装这是一个漂亮的代码,这里是对您的初始版本的一些优化。它没有经过测试,因为我手头没有你的TRS-MIB文件。

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

您应该可以使用库存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())
)