2012-08-31 58 views
1

我在PySNMP代理中實現自定義MIB時遇到困難。在PySNMP代理中實現自定義MIB

我已經開始與:

http://pysnmp.sourceforge.net/examples/4.x/v3arch/agent/cmdrsp.html

創建了自己的MIB文件,用於build-pysnmp-mib,使一個Python模塊,併成功導入符號。

我看不到下一步該去哪裏。我需要以某種方式將導入的符號安裝在所提供的MIB列表中並提供一個實現。 (它目前是具有一個只讀INTEGER屬性的MIB。)

MIB文件通過smilint,但沒有警告,但我必須手動將缺少的MibScalar導入添加到生成的模塊。

MIB:

TRS-MIB DEFINITIONS ::= BEGIN 

internet OBJECT IDENTIFIER ::= { iso(1) org(3) dod(6) 1 } 
enterprises OBJECT IDENTIFIER ::= { internet private(4) 1 } 
thorcom OBJECT IDENTIFIER ::= { enterprises 27817 } 
trs OBJECT IDENTIFIER ::= { thorcom 2 } 
trsEntry OBJECT IDENTIFIER ::= { trs 1 } 

trsDeliveryTime OBJECT-TYPE 
    SYNTAX Integer32 
    ACCESS not-accessible 
    STATUS mandatory 
    DESCRIPTION "Average message delivery time in milliseconds." 
    ::= { trsEntry 1 } 

END 

代碼:

#!/usr/bin/env python 

# Command Responder 
from pysnmp.entity import engine, config 
from pysnmp.carrier.asynsock.dgram import udp 
#from pysnmp.carrier.asynsock.dgram import udp6 
from pysnmp.entity.rfc3413 import cmdrsp, context 
from pysnmp.proto.rfc1902 import OctetString 
from pysnmp.smi import builder 
from pysnmp import debug 

debug.setLogger(debug.Debug('all')) 

# Create SNMP engine with autogenernated engineID and pre-bound 
# to socket transport dispatcher 
snmpEngine = engine.SnmpEngine() 

# Setup UDP over IPv4 transport endpoint 
config.addSocketTransport(
    snmpEngine, 
    udp.domainName, 
    udp.UdpSocketTransport().openServerMode(('127.0.0.1', 161)) 
    ) 

# Start of new code 
mibBuilder = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder 
mibSources = mibBuilder.getMibSources() + (
    builder.DirMibSource('.'), 
) 
mibBuilder.setMibSources(*mibSources) 

# Create and put on-line my managed object 
deliveryTime, = mibBuilder.importSymbols('TRS-MIB', 'trsDeliveryTime') 
Integer32, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('SNMPv2-SMI', 'Integer32') 

MibScalarInstance, = mibBuilder.importSymbols('SNMPv2-SMI', 'MibScalarInstance') 
class MyDeliveryTime(Integer32): 
    def readGet(self, name, val, idx, (acFun, acCtx)): 
    return name, self.syntax.clone(42) 

deliveryTimeInstance = MibScalarInstance(
    deliveryTime.name, (0,), deliveryTime.syntax 
) 
mibBuilder.exportSymbols('TRS-MIB', deliveryTimeInstance=deliveryTimeInstance) # creating MIB 
# End of new code 


# v1/2 setup 
config.addV1System(snmpEngine, 'test-agent', 'public') 

# v3 setup 
config.addV3User(
    snmpEngine, 'test-user', 
    config.usmHMACMD5AuthProtocol, 'authkey1', 
    config.usmDESPrivProtocol, 'privkey1' 
    ) 

# VACM setup 
config.addContext(snmpEngine, '') 
config.addRwUser(snmpEngine, 1, 'test-agent', 'noAuthNoPriv', (1,3,6)) # v1 
config.addRwUser(snmpEngine, 2, 'test-agent', 'noAuthNoPriv', (1,3,6)) # v2c 
config.addRwUser(snmpEngine, 3, 'test-user', 'authPriv', (1,3,6)) # v3 

# SNMP context 
snmpContext = context.SnmpContext(snmpEngine) 

# Apps registration 
cmdrsp.GetCommandResponder(snmpEngine, snmpContext) 
cmdrsp.SetCommandResponder(snmpEngine, snmpContext) 
cmdrsp.NextCommandResponder(snmpEngine, snmpContext) 
cmdrsp.BulkCommandResponder(snmpEngine, snmpContext) 
snmpEngine.transportDispatcher.jobStarted(1) # this job would never finish 
snmpEngine.transportDispatcher.runDispatcher() 

生成並修改TRS-MIB.py:

# PySNMP SMI module. Autogenerated from smidump -f python TRS-MIB 
# by libsmi2pysnmp-0.1.1 at Fri Aug 31 13:56:45 2012, 
# Python version (2, 6, 6, 'final', 0) 

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

# Imports 

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

# Objects 

internet = MibIdentifier((1, 3, 6, 1)) 
enterprises = MibIdentifier((1, 3, 6, 1, 4, 1)) 
thorcom = MibIdentifier((1, 3, 6, 1, 4, 1, 27817)) 
trs = MibIdentifier((1, 3, 6, 1, 4, 1, 27817, 2)) 
trsEntry = MibIdentifier((1, 3, 6, 1, 4, 1, 27817, 2, 1)) 
trsDeliveryTime = MibScalar((1, 3, 6, 1, 4, 1, 27817, 2, 1, 1), Integer32()).setMaxAccess("noaccess") 
if mibBuilder.loadTexts: trsDeliveryTime.setDescription("Average message delivery time in milliseconds.") 

# Augmentions 

# Exports 

# Objects 
mibBuilder.exportSymbols("TRS-MIB", internet=internet, enterprises=enterprises, thorcom=thorcom, trs=trs, trsEntry=trsEntry, trsDeliveryTime=trsDeliveryTime) 

更新:

我現在有一個錯誤左:

$ snmpget -v2c -c public localhost .1.3.6.1.4.1.27817.2.1.1 
Error in packet 
Reason: noAccess 
Failed object: iso.3.6.1.4.1.27817.2.1.1 

調試是:

DBG: handle_read: transportAddress ('127.0.0.1', 48191) incomingMessage '0,\x02\x01\x01\x04\x06public\xa0\x1f\x02\x04>9\xc4\xa0\x02\x01\x00\x02\x01\x000\x110\x0f\x06\x0b+\x06\x01\x04\x01\x81\xd9)\x02\x01\x01\x05\x00' 
DBG: receiveMessage: msgVersion 1, msg decoded 
DBG: prepareDataElements: Message: 
version='version-2' 
community=public 
data=PDUs: 
    get-request=GetRequestPDU: 
    request-id=1043973280 
    error-status='noError' 
    error-index=0 
    variable-bindings=VarBindList: 
    VarBind: 
    name=1.3.6.1.4.1.27817.2.1.1 
    =_BindValue: 
     unSpecified= 





DBG: value index rebuilt at (1, 3, 6, 1, 6, 3, 18, 1, 1, 1, 2), 1 entries 
DBG: processIncomingMsg: looked up securityName MibScalarInstance((1, 3, 6, 1, 6, 3, 18, 1, 1, 1, 3, 116, 101, 115, 116, 45, 97, 103, 101, 110, 116), test-agent) contextEngineId MibScalarInstance((1, 3, 6, 1, 6, 3, 18, 1, 1, 1, 4, 116, 101, 115, 116, 45, 97, 103, 101, 110, 116), �O�c�@��) contextName MibScalarInstance((1, 3, 6, 1, 6, 3, 18, 1, 1, 1, 5, 116, 101, 115, 116, 45, 97, 103, 101, 110, 116),) by communityName MibScalarInstance((1, 3, 6, 1, 6, 3, 18, 1, 1, 1, 2, 116, 101, 115, 116, 45, 97, 103, 101, 110, 116), public) 
DBG: processIncomingMsg: generated maxSizeResponseScopedPDU 65379 securityStateReference 12831470 
DBG: prepareDataElements: SM returned securityEngineID SnmpEngineID(hexValue='8004fb857f00163de40e2b7') securityName test-agent 
DBG: prepareDataElements: cached by new stateReference 2662033 
DBG: receiveMessage: MP succeded 
DBG: receiveMessage: PDU GetRequestPDU: 
request-id=1043973280 
error-status='noError' 
error-index=0 
variable-bindings=VarBindList: 
    VarBind: 
    name=1.3.6.1.4.1.27817.2.1.1 
    =_BindValue: 
    unSpecified= 



DBG: receiveMessage: pduType TagSet(Tag(tagClass=128, tagFormat=32, tagId=0)) 
DBG: processPdu: stateReference 2662033, varBinds [(ObjectName(1.3.6.1.4.1.27817.2.1.1), Null(''))] 
DBG: getMibInstrum: contextName "", mibInstum <pysnmp.smi.instrum.MibInstrumController instance at 0x7fcbfe3d5e60> 
DBG: flipFlopFsm: inputNameVals [(ObjectName(1.3.6.1.4.1.27817.2.1.1), Null(''))] 
DBG: flipFlopFsm: state start status ok -> fsmState readTest 
DBG: flipFlopFsm: fun <bound method MibTree.readTest of MibTree((1,), None)> failed NoAccessError({'name': (1, 3, 6, 1, 4, 1, 27817, 2, 1, 1), 'idx': 0}) for 1.3.6.1.4.1.27817.2.1.1=Null('') 
DBG: flipFlopFsm: state readTest status err -> fsmState stop 
DBG: sendRsp: stateReference 2662033, errorStatus noAccess, errorIndex 1, varBinds [(ObjectName(1.3.6.1.4.1.27817.2.1.1), Null(''))] 
DBG: returnResponsePdu: PDU ResponsePDU: 
request-id=1043973280 
error-status='noAccess' 
error-index=1 
variable-bindings=VarBindList: 
    VarBind: 
    name=1.3.6.1.4.1.27817.2.1.1 
    =_BindValue: 
    unSpecified= 



DBG: prepareResponseMessage: cache read msgID 1043973280 transportDomain (1, 3, 6, 1, 6, 1, 1) transportAddress ('127.0.0.1', 48191) by stateReference 2662033 
DBG: prepareResponseMessage: using contextEngineId SnmpEngineID(hexValue='8004fb857f00163de40e2b7') contextName 
DBG: generateResponseMsg: recovered community public by securityStateReference 12831470 
DBG: generateResponseMsg: Message: 
version='version-2' 
community=public 
data=PDUs: 
    response=ResponsePDU: 
    request-id=1043973280 
    error-status='noAccess' 
    error-index=1 
    variable-bindings=VarBindList: 
    VarBind: 
    name=1.3.6.1.4.1.27817.2.1.1 
    =_BindValue: 
     unSpecified= 





DBG: returnResponsePdu: MP suceeded 
DBG: receiveMessage: processPdu succeeded 
DBG: handle_write: transportAddress ('127.0.0.1', 48191) outgoingMessage '0,\x02\x01\x01\x04\x06public\xa2\x1f\x02\x04>9\xc4\xa0\x02\x01\x06\x02\x01\x010\x110\x0f\x06\x0b+\x06\x01\x04\x01\x81\xd9)\x02\x01\x01\x05\x00' 

回答

1

作爲實現管理對象的實例,你有兩個選擇:

  1. 加載和子類MibScalarInstance類,然後覆蓋其readGet()方法使其返回您的實時價值。然後實例化你的新類(確保傳遞適當的OID來標識它)並將它傳遞給exportSymbols(),所以它的OID將在pysnmp Agent中註冊。

  2. 加載Integer32類,對它進行子類化並覆蓋它的「clone()」方法,使其返回實時值。然後加載MibScalarInstance類,實例化它傳遞適當的OID和Integer32子類的實例,然後將MibScalarInstance對象傳遞給exportSymbols(),所以它的OID將在pysnmp Agent中註冊。

將所有代碼保留在您自己的MIB模塊中可能有意義。看看pysnmp/smi/mibs/instances/*。py來獲得一個想法。

從您的Agent應用程序中,調用mibBuilder.loadModules('TRC-MIB')將您的MIB模塊加載到代理中。

在你的代碼中,你似乎以某種方式結合了上述兩種方法:MyDeliveryTime.readGet()將不起作用,但是MyDeliveryTime.clone()或deliveryTimeInstance.readGet()會。

+0

謝謝,我想我快到了。我只剩下一個錯誤...(請參閱原始問題)。謝謝你的幫助。 – fadedbee

+0

解決方案是將'.setMaxAccess(「noaccess」)'改爲'.setMaxAccess(「readonly」)'。 – fadedbee

0

我也遇到了類似的問題:

試圖把MY-MIB [PYC]文件在〜/。pysnmp/mibs文件夾,但我認爲真正起作用的是將這些文件放在文件夾/ usr/lib/pytho */site/pysnmp/smi/mibs /中。

我在pysnmp-4.3.9上工作