pysnmp Agent with HOST-RESOURCES-MIB

580 views Asked by At

I'm trying to monitor a python process using opennms. To do this I need to implement an agent that supports the HOST-RESOURCES-MIB. Opennms checks the state of the process by inspecting the hrSwRunTable of the HOST-RESOURCES-MIB. The test is done by matching a given process as hrSwRunName against the numeric value of the hrSwRunState.

pysnmp gives a few examples of writing an agent that I'm trying to modify but I'm not having much success.

the relevant part of my code is as follows

import logging

from pysnmp import debug
from pysnmp.carrier.asyncore.dgram import udp
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import cmdrsp, context
from pysnmp.proto.api import v2c
from pysnmp.smi import builder, instrum, exval


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

formatting = '[%(asctime)s-%(levelname)s]-(%(module)s) %(message)s'
logging.basicConfig(level=logging.DEBUG, format=formatting, )

logging.info("Starting....")

# Create SNMP engine
snmpEngine = engine.SnmpEngine()

# Transport setup

# UDP over IPv4
config.addTransport(
    snmpEngine,
    udp.domainName,
    udp.UdpTransport().openServerMode(('mypc', 12345))
)

# SNMPv2c setup

# SecurityName <-> CommunityName mapping.
config.addV1System(snmpEngine, 'my-area', 'public')

# Allow read MIB access for this user / securityModels at VACM
config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (1, 3, 6, 1, 2, 1, 25, 4, 2), (1, 3, 6, 1, 2, 1, 25, 4, 2))

# Create an SNMP context
snmpContext = context.SnmpContext(snmpEngine)

# --- define custom SNMP Table within a newly defined EXAMPLE-MIB ---

# ==================================================================
logging.debug('Loading SNMP-TARGET-MIB module...'),
mibBuilder1 = builder.MibBuilder().loadModules('SNMP-TARGET-MIB')
logging.debug('done')

logging.debug('Building MIB tree...'),
mibInstrum1 = instrum.MibInstrumController(mibBuilder1)
logging.debug('done')

logging.debug('Building table entry index from human-friendly representation...')

snmpTargetAddrEntry, = mibBuilder1.importSymbols('SNMP-TARGET-MIB', 'snmpTargetAddrEntry')
instanceId1 = snmpTargetAddrEntry.getInstIdFromIndices('my-area')
# ==================================================================


logging.debug('Loading HOST-RESOURCES-MIB module...'),
mibBuilder = builder.MibBuilder().loadModules('HOST-RESOURCES-MIB')
logging.debug('done')

logging.debug('Building MIB tree...'),
mibInstrum = instrum.MibInstrumController(mibBuilder)
logging.debug('done')

logging.debug('Building table entry index from human-friendly representation...')

# see http://www.oidview.com/mibs/0/HOST-RESOURCES-MIB.html
hostRunTable, = mibBuilder.importSymbols('HOST-RESOURCES-MIB', 'hrSWRunEntry')
instanceId = hostRunTable.getInstIdFromIndices('my-area')
logging.debug('done')

You will see that at the end of the code I'm trying generate an instance of 'SNMP-TARGET-MIB->snmpTargetAddrEntry' and 'HOST-RESOURCES-MIB->hrSWRunEntry'. The code for the SNMP-TARGET-MIB (which is in the pysnmp documentation) works fine however the code that tries to generate the HOST-RESOURCES-MIB fails when I try to generate an instance on the line instanceId = hostRunTable.getInstIdFromIndices('my-area')

The error is pyasn1.error.PyAsn1Error: Can't coerce 'my-area' into integer: invalid literal for int() with base 10: 'my-area'

Can anyone shed any light on what I'm doing wrong? I realise I'm new to SNMP and so it's quite possible its a stupid error

1

There are 1 answers

1
Ilya Etingof On BEST ANSWER

According to the HOST-RESOURCES-MIB, hrSWRunTable is indexed by hrSWRunIndex column, its values belong to the Integer32 type:

hrSWRunEntry OBJECT-TYPE
    SYNTAX     HrSWRunEntry
    INDEX { hrSWRunIndex }
    ::= { hrSWRunTable 1 }

hrSWRunIndex OBJECT-TYPE
    SYNTAX     Integer32 (1..2147483647)
    ::= { hrSWRunEntry 1 }

You are trying to build an OID index from index value which is string type, not integer. That leads to string->int conversion error:

instanceId = hostRunTable.getInstIdFromIndices('my-area')

So you probably want your first row to have 1 as an index value:

instanceId = hostRunTable.getInstIdFromIndices(1)

Here I assume that you compute instanceId for the purpose of building OIDs for your new tabular objects (e.g. MibScalarInstance).