如何在 pysnmp 中获取 oid 的类型,即它是 COUNTER、INTEGER...等

问题描述 投票:0回答:1

我有 oid 名称,当我将该 oid 名称传递给 api 时,它应该使用 pysnmp 给出该 oid_name 的数据类型。

假设我通过了 case1:ssCpuRawSystem 它应该给我 Integer32 。 case2 : ssCpuRawUser 它应该给我 Counter32 case3 : ssErrorName 它应该给我 DisplayString

snmp pysnmp
1个回答
0
投票

根据您首选的粒度,您可以使用 PySNMP 的 MIB API,

from pysnmp.proto.rfc1902 import ObjectIdentifier
from pysnmp.smi import builder, view, compiler

# Create MIB builder
mibBuilder = builder.MibBuilder()

# Optionally compile MIBs
compiler.addMibCompiler(mibBuilder, sources=["/usr/share/snmp/mibs"])

mibBuilder.loadTexts = True

# Load MIB modules
mibBuilder.loadModules("SNMPv2-MIB")
# mibBuilder.addMibSources(builder.DirMibSource('/Users/lextm/pysnmp.com/pysnmp/mibs'))
# mibBuilder.loadModule('LEXTUDIO-MIB')

# Create MIB view controller
mibViewController = view.MibViewController(mibBuilder)

# Create an OID object
oid = ObjectIdentifier("1.3.6.1.2.1.1.3.0")

# Get the MIB name and symbol name for the OID
modName, symName, suffix = mibViewController.getNodeLocation(oid)

# Get the MIB node for the OID
(mibNode,) = mibBuilder.importSymbols(modName, symName)

# Get the description of the MIB node
description = mibNode.getDescription()

# Print the results
print("OID: %s" % oid)
print("MIB name: %s" % modName)
print("Symbol name: %s" % symName)
print("Description: %s" % description)
print("Syntax: %s" % mibNode.getSyntax().__class__.__name__) # <- The syntax object contains everything you want to know

取自官方示例

© www.soinside.com 2019 - 2024. All rights reserved.