Asynchronous SNMP (asyncore, v1arch)

Applications build around asyncore get CPU time on socket events being watched for by select dispatcher. User code lives mostly in isolated functions (or any callable objects).

As it is with any asynchronous I/O system, asyncore lets you run many SNMP queries in parallel and/or sequentially, interleave SNMP queries with other I/O operations for as long as they are managed within the same event loop.

The pysnmp.hlapi.v1arch.asyncore package implements asyncore binding to pysnmp’s v1arch services.

In most examples approximate analogues of well known Net-SNMP snmp* tools command line options are shown. That may help those readers who, by chance are familiar with Net-SNMP tools, better understanding what example code doe

Here’s a quick example on a simple SNMP GET by high-level API:

  • with SNMPv2c, community ‘public’
  • over IPv4/UDP
  • to an Agent at demo.snmplabs.com:161
  • for the 1.3.6.1.2.1.1.1.0 OID (e.g. SNMPv2-MIB::sysDescr.0 MIB object)

from pysnmp.hlapi.v1arch.asyncore import *


def cbFun(errorIndication, errorStatus, errorIndex, varBinds, **context):
    if errorIndication:
        print(errorIndication)
        return
    elif errorStatus:
        print('%s at %s' % (errorStatus.prettyPrint(),
                            errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?'))
        return
    else:
        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))


snmpDispatcher = SnmpDispatcher()

stateHandle = getCmd(
    snmpDispatcher,
    CommunityData('public'),
    UdpTransportTarget(('demo.snmplabs.com', 161)),
    ('1.3.6.1.2.1.1.1.0', None),
    cbFun=cbFun
)

snmpDispatcher.transportDispatcher.runDispatcher()

The following code sends SNMP TRAP:

  • SNMPv1
  • with community name ‘public’
  • over IPv4/UDP
  • send TRAP notification
  • with Uptime 12345
  • with Generic Trap #1 (warmStart) and Specific Trap 0
  • with Agent Address 127.0.0.1
  • with Enterprise OID 1.3.6.1.4.1.20408.4.1.1.2
  • include managed object information ‘1.3.6.1.2.1.1.1.0’ = ‘my system’
from pysnmp.hlapi.v1arch.asyncore import *


def cbFun(errorIndication, errorStatus, errorIndex, varBinds, **context):
    if errorIndication:
        print(errorIndication)


snmpDispatcher = SnmpDispatcher()

sendNotification(
    snmpDispatcher,
    CommunityData('public', mpModel=0),
    UdpTransportTarget(('demo.snmplabs.com', 162)),
    'trap',
    # SNMPv2-MIB::sysUpTime.0 = 12345
    ('1.3.6.1.2.1.1.3.0', TimeTicks(12345)),
    # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::warmStart
    ('1.3.6.1.6.3.1.1.4.1.0', ObjectIdentifier('1.3.6.1.6.3.1.1.5.2')),
    # SNMP-COMMUNITY-MIB::snmpTrapAddress.0 = 127.0.0.1
    ('1.3.6.1.6.3.18.1.3.0', IpAddress('127.0.0.1')),
    # SNMP-COMMUNITY-MIB::snmpTrapCommunity.0 = public
    ('1.3.6.1.6.3.18.1.4.0', OctetString('public')),
    # SNMP-COMMUNITY-MIB::snmpTrapEnterprise.0 = 1.3.6.1.4.1.20408.4.1.1.2
    ('1.3.6.1.6.3.1.1.4.3.0', ObjectIdentifier('1.3.6.1.4.1.20408.4.1.1.2')),
    # SNMPv2-MIB::sysName.0
    ('1.3.6.1.2.1.1.1.0', OctetString('my system')),
    cbFun=cbFun
)

snmpDispatcher.transportDispatcher.runDispatcher()

More examples on Notification Originator API usage follow.

More sophisticated SNMP operations can still be performed with PySNMP via its Native API to Standard SNMP Applications.