簡體   English   中英

如何在 pysnmp 中查找 mib 表?

[英]How to lookup mib-table in pysnmp?

當我在 OID ( 1.3.6.1.4.1.2021.4) 上使用 snmpwalk 時,我得到以下結果:

UCD-SNMP-MIB::memIndex.0 = INTEGER: 0

UCD-SNMP-MIB::memErrorName.0 = STRING: swap

UCD-SNMP-MIB::memMinimumSwap.0 = INTEGER: 16000 kB

但是,當我使用 pysnmp 模塊查詢相同的 OID 時,我得到的結果如下:

(ObjectName(1.3.6.1.4.1.2021.4.1.0), Integer(0))

(ObjectName(1.3.6.1.4.1.2021.4.2.0), OctetString('swap'))

(ObjectName(1.3.6.1.4.1.2021.4.12.0), Integer(16000))

我已將 pysnmp_mibs 放入 os.environ 中,我使用的代碼是:

from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.smi import builder, view, error

errorIndication, errorStatus, errorIndex, \
             varBindTable = cmdGen.nextCmd (
cmdgen.CommunityData('test-agent', 'public'),
cmdgen.UdpTransportTarget ( (localhost, 161) ),
('1.3.6.1.4.1.2021.4')
)

if errorIndication:
    log.warn(errorIndication)
    return
else:
    if errorStatus:
        log.warn( '%s at %s\n' % (
            errorStatus.prettyPrint (),
            errorIndex and varBindTable[-1][int (errorIndex) - 1] or '?'
            ))
        return
    else:
        for varBindTableRow in varBindTable:
            for oid, val in varBindTableRow:
                    (symName, modName), indices = cmdgen.mibvar.oidToMibName(
                        cmdGen.mibViewController, oid
                        )
                    val = cmdgen.mibvar.cloneFromMibValue(
                                  cmdGen.mibViewController, modName, symName, val
                          )
            print varBindTableRow

我的問題是為什么 snmpwalk 和 pysnmp 結果不同,我應該怎么做才能獲得與使用 pysnmp 模塊的 snmpwalk 相同的結果?


編輯

我嘗試了代碼,但在查詢 1.3.6.1.2.1.4.22.1 時出現以下錯誤

NoSuchObjectError: NoSuchObjectError({'str': 'No MIB info for    (1, 3, 6, 1, 4, 1, 2021, 4, 1, 0) (closest parent (1, 3, 6, 1, 4, 1))'})

我已將 IP-MIB 模塊加載為:

mibBuilder = builder.MibBuilder() 
mibPath = mibBuilder.getMibSources()+(builder.DirMibSource(path_to_mib_dir),)
mibBuilder.setMibSources(*mibPath)
mibBuilder.loadModules('IP-MIB')
mibView = view.MibViewController(mibBuilder)

我感到困惑的是我如何使用這個mibView

(symName,modName),indices=cmdgen.mibvar.oidToMibName(cmdGen.mibViewController, oid)
val=cmdgen.mibvar.cloneFromMibValue(cmdGen.mibViewController,modName, symName, val)

應該將cmdGen.mibViewController替換為mibView嗎?

但它適用於 OID 1.3.6.1.2.1.1.3:我得到的結果符合預期:

SNMPv2-MIB::sysUpTime.0 = 27

抱歉,結果是一樣的!

如果您的問題是關於 16000 與 16000 KB,您必須知道 SNMP 不會返回單位。 SNMP V2 MIB 只允許一個地方提供有關它的信息,因此 SNMPWALK(作為客戶端)正在使用它,而不是 pysnmp。

嘗試打印:

print '%s::%s.%s = %s' % (
                    modName, symName,
                    '.'.join(map(lambda v: v.prettyPrint(), indices),
                    val.prettyPrint()
                    )

代替

print varBindTableRow

還要確保加載適當的 MIB。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM