Search code examples
pythonsnmpnet-snmppysnmp

Is there any right format for adding context name to the snmp agent?


I am new to SNMP Context.I am setting up a SNMP agent in the cloud. This is the basic format we got from the snmplabs.com which uses the context name. But the problem is when we use context name in the snmpwalk request it give reply like

"SNMPv2-SMI::dod = No more variables left in this MIB View (It is past the end of the MIB tree)"

We follow the format http://snmplabs.com/pysnmp/examples/v3arch/asyncore/agent/cmdrsp/agent-side-mib-implementations.html

# Serve non-default MIB tree

# Listen and respond to SNMP GET / SET / GETNEXT / GETBULK queries with
# the following options:

#  SNMPv3
#  with USM username usr - md5 - none
#  using alternative set of Managed Objects addressed by
#  contextName: my - context
#  allow access to SNMPv2 - MIB objects (1.3.6.1.2.1)
#  over IPv4 / UDP, listening at 127.0.0.1:161

from pysnmp.entity import engine, config

from pysnmp.entity.rfc3413 import cmdrsp, context

from pysnmp.carrier.asyncore.dgram import udp

from pysnmp.smi import instrum, builder

from pysnmp.proto.api import v2c

snmpEngine = engine.SnmpEngine()

config.addTransport(
    snmpEngine,
    udp.domainName,
    udp.UdpTransport().openServerMode(('127.0.0.1', 161))
)

# SNMPv3/USM setup

# user: usr-md5-none, auth: MD5, priv NONE
config.addV3User(
    snmpEngine, 'usr-md5-none',
    config.usmHMACMD5AuthProtocol, 'authkey1'
)

# Allow full MIB access for each user at VACM
config.addVacmUser(snmpEngine, 3, 'usr-md5-none', 'authNoPriv', (1, 3, 6, 1, 2, 1), (1, 3, 6, 1, 2, 1))

snmpContext = context.SnmpContext(
    snmpEngine
)

# Create an [empty] set of Managed Objects (MibBuilder), pass it to
# Management Instrumentation Controller and register at SNMP Context
# under ContextName 'my-context'
snmpContext.registerContextName(
    v2c.OctetString('my-context'),  # Context Name
    instrum.MibInstrumController(builder.MibBuilder().loadModules)  # Managed Objects
)

# Register SNMP Applications at the SNMP engine for particular SNMP context
cmdrsp.GetCommandResponder(snmpEngine, snmpContext)
cmdrsp.SetCommandResponder(snmpEngine, snmpContext)
cmdrsp.NextCommandResponder(snmpEngine, snmpContext)
cmdrsp.BulkCommandResponder(snmpEngine, snmpContext)

# Register an imaginary never-ending job to keep I/O dispat#cher running forever
snmpEngine.transportDispatcher.jobStarted(1)

# Run I/O dispatcher which would receive queries and send responses
try:
    snmpEngine.transportDispatcher.runDispatcher()
except:
    snmpEngine.transportDispatcher.closeDispatcher()
    raise

snmpwalk -v3 -u usr-md5-none -l authNoPriv -A authkey1  -n my-context 127.0.0.1 .1.3.6

While using the above snmpwalk, we will get output like

SNMPv2-SMI::dod = No more variables left in this MIB View (It is past the end of the MIB tree)

But if use the following snmpwalk

snmpwalk -v3 -u usr-md5-none -l authNoPriv -A authkey1 127.0.0.1 .1.3.6

we will get output

SNMPv2-MIB::sysDescr.0 = STRING: PySNMP engine version 4.4.11, Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)]

SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::enterprises.20408
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (14) 0:00:00.14

SNMPv2-MIB::sysContact.0 = STRING:

SNMPv2-MIB::sysName.0 = STRING: 
SNMPv2-MIB::sysLocation.0 = STRING: 
SNMPv2-MIB::sysServices.0 = INTEGER: 0
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::snmpInPkts.0 = Counter32: 10
SNMPv2-MIB::snmpOutPkts.0 = Counter32: 0
SNMPv2-MIB::snmpInBadVersions.0 = Counter32: 0
SNMPv2-MIB::snmpInBadCommunityNames.0 = Counter32: 0
SNMPv2-MIB::snmpInBadCommunityUses.0 = Counter32: 0
SNMPv2-MIB::snmpInASNParseErrs.0 = Counter32: 0
SNMPv2-MIB::snmpInTooBigs.0 = Counter32: 0
SNMPv2-MIB::snmpInNoSuchNames.0 = Counter32: 0
SNMPv2-MIB::snmpInBadValues.0 = Counter32: 0
SNMPv2-MIB::snmpInReadOnlys.0 = Counter32: 0
SNMPv2-MIB::snmpInGenErrs.0 = Counter32: 0
SNMPv2-MIB::snmpInTotalReqVars.0 = Counter32: 0
SNMPv2-MIB::snmpInTotalSetVars.0 = Counter32: 0
SNMPv2-MIB::snmpInGetRequests.0 = Counter32: 0
SNMPv2-MIB::snmpInGetNexts.0 = Counter32: 0
SNMPv2-MIB::snmpInGetResponses.0 = Counter32: 0
SNMPv2-MIB::snmpInTraps.0 = Counter32: 0
SNMPv2-MIB::snmpOutTooBigs.0 = Counter32: 0
SNMPv2-MIB::snmpOutNoSuchNames.0 = Counter32: 0
SNMPv2-MIB::snmpOutBadValues.0 = Counter32: 0
SNMPv2-MIB::snmpOutGenErrs.0 = Counter32: 0
SNMPv2-MIB::snmpOutSetRequests.0 = Counter32: 0
SNMPv2-MIB::snmpOutGetResponses.0 = Counter32: 0
SNMPv2-MIB::snmpOutTraps.0 = Counter32: 0
SNMPv2-MIB::snmpEnableAuthenTraps.0 = INTEGER: enabled(1)
SNMPv2-MIB::snmpSilentDrops.0 = Counter32: 0
SNMPv2-MIB::snmpProxyDrops.0 = Counter32: 0

So for getting the same output for context name what change I should take care


Solution

  • Do you have your MIB populated at all? This line:

    instrum.MibInstrumController(builder.MibBuilder())  # Managed Objects
    

    creates an empty MIB tree and registers it under my-context context name. Unless you populate that MIB tree elsewhere in the code, that explains why you are not getting anything in response.

    If you do this:

    instrum.MibInstrumController(builder.MibBuilder().loadModules())
    

    all existing MIB modules in pysnmp search path would get loaded and registered with this new MIB tree.

    BTW, the whole ContextEngineId configuration is irrelevant to ContextName. Unless you want it for some reason, you can just drop everything related to specific ContextEngineId value in your code.