2012-09-12 54 views
1

我執行使用PySNMP作爲SNMP代理表中實現一個表。在PySNMP劑

我已經按照使用自動生成的TRS-MIB.py文件的(PySNMP源)架構,並且包含執行一個手動編寫__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, 
         ) 

回答

2

不假裝這是一個漂亮的代碼,這裏的一些優化,你的最初版本。由於我手頭沒有TRS-MIB文件,因此沒有進行測試。

這個想法是通過重寫它的readGet()方法來專門化MibScalarInstance類(以使其服務於不同的列),它的readGet()方法返回要返回給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()) 
) 
+0

謝謝,這是非常有用的。當我用這個,我得到一個錯誤'AttributeError的:例如位整數。20在我查詢索引列值的時間沒有__call__ method'。如果我查詢八位組串列值之一,我得到'AttributeError的:OctetString和實例沒有__call__ method'。看起來好像比預期的還要多一個函數調用級別,或者類似。 – fadedbee

+0

P.S.我不得不改變'... getName(),0,trs ...'爲'... getName(),(0,),trs ...'使它運行。 – fadedbee

+0

P.P.S.更改'(0,)'的'0'只會使其運行,查詢時代碼仍會產生屬性錯誤。 – fadedbee