How to lookup mib-table in pysnmp?
Question
When I use snmpwalk on a OID ( 1.3.6.1.4.1.2021.4), i get the following result as:
UCD-SNMP-MIB::memIndex.0 = INTEGER: 0
UCD-SNMP-MIB::memErrorName.0 = STRING: swap
UCD-SNMP-MIB::memMinimumSwap.0 = INTEGER: 16000 kB
But, when i use the pysnmp module to query the same OID, i get the results as:
(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))
I have put the pysnmp_mibs in os.environ and the code i'm using is:
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
My question is why is snmpwalk and pysnmp results different, and what should i do to get the results same as snmpwalk using pysnmp module?
Edited:
i tried the code,but on querying 1.3.6.1.2.1.4.22.1 i get the following error
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))'})
I have loaded the IP-MIB module as:
mibBuilder = builder.MibBuilder()
mibPath = mibBuilder.getMibSources()+(builder.DirMibSource(path_to_mib_dir),)
mibBuilder.setMibSources(*mibPath)
mibBuilder.loadModules('IP-MIB')
mibView = view.MibViewController(mibBuilder)
What i am confused is how do i use this mibView in
(symName,modName),indices=cmdgen.mibvar.oidToMibName(cmdGen.mibViewController, oid)
val=cmdgen.mibvar.cloneFromMibValue(cmdGen.mibViewController,modName, symName, val)
should the cmdGen.mibViewController be replaced with the mibView?
but it works on OID 1.3.6.1.2.1.1.3: and i get the result as expected as:
SNMPv2-MIB::sysUpTime.0 = 27
Solution
Try printing:
print '%s::%s.%s = %s' % (
modName, symName,
'.'.join(map(lambda v: v.prettyPrint(), indices),
val.prettyPrint()
)
instead of
print varBindTableRow
Also make sure you load appropriate MIB.
OTHER TIPS
Sorry, but the results are the same !
If your problem is about 16000 versus 16000 KB, you must know that SNMP does not return the unit. SNMP V2 MIB allow just a place to give information about it, so SNMPWALK(as a client) is using it, not pysnmp.