From f96e5db5fb2bd5df2d4848c803c410e8dc11ecc2 Mon Sep 17 00:00:00 2001 From: Ilya Etingof Date: Wed, 11 May 2016 12:50:44 +0200 Subject: better examples on MIB compiler --- ...onfigure-mib-viewer-and-resolve-pdu-varbinds.py | 37 ++++++++ ...convert-between-pdu-varbinds-and-mib-objects.py | 104 +++++++++++++++++++++ examples/smi/manager/var-binds-mib-resolution.py | 101 -------------------- 3 files changed, 141 insertions(+), 101 deletions(-) create mode 100644 examples/smi/manager/configure-mib-viewer-and-resolve-pdu-varbinds.py create mode 100644 examples/smi/manager/convert-between-pdu-varbinds-and-mib-objects.py delete mode 100644 examples/smi/manager/var-binds-mib-resolution.py (limited to 'examples') diff --git a/examples/smi/manager/configure-mib-viewer-and-resolve-pdu-varbinds.py b/examples/smi/manager/configure-mib-viewer-and-resolve-pdu-varbinds.py new file mode 100644 index 00000000..91d7e5ee --- /dev/null +++ b/examples/smi/manager/configure-mib-viewer-and-resolve-pdu-varbinds.py @@ -0,0 +1,37 @@ +""" +SNMP var-binds MIB resolution (configure MIB compiler) +++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +This script explains how Python application could turn SNMP PDU +variable-bindings into MIB objects or the other way around. + +The code that configures MIB compiler is similar to what +happens inside the pysnmp.hlapi API. +"""# +from pysnmp.smi import builder, view, compiler, rfc1902 + +# Assemble MIB browser +mibBuilder = builder.MibBuilder() +mibViewController = view.MibViewController(mibBuilder) +compiler.addMibCompiler(mibBuilder, sources=['file:///usr/share/snmp/mibs', + 'http://mibs.snmplabs.com/asn1/@mib@']) + +# Pre-load MIB modules we expect to work with +mibBuilder.loadModules('SNMPv2-MIB', 'SNMP-COMMUNITY-MIB') + +# This is what we can get in TRAP PDU +varBinds = [ + ('1.3.6.1.2.1.1.3.0', 12345), + ('1.3.6.1.6.3.1.1.4.1.0', '1.3.6.1.6.3.1.1.5.2'), + ('1.3.6.1.6.3.18.1.3.0', '0.0.0.0'), + ('1.3.6.1.6.3.18.1.4.0', ''), + ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'), + ('1.3.6.1.2.1.1.1.0', 'my system') +] + +# Run var-binds through MIB resolver +# You may want to catch and ignore resolution errors here +varBinds = [rfc1902.ObjectType(rfc1902.ObjectIdentity(x[0]), x[1]).resolveWithMib(mibViewController) for x in varBinds] + +for varBind in varBinds: + print(varBind.prettyPrint()) diff --git a/examples/smi/manager/convert-between-pdu-varbinds-and-mib-objects.py b/examples/smi/manager/convert-between-pdu-varbinds-and-mib-objects.py new file mode 100644 index 00000000..ecc798d3 --- /dev/null +++ b/examples/smi/manager/convert-between-pdu-varbinds-and-mib-objects.py @@ -0,0 +1,104 @@ +""" +SNMP var-binds MIB resolution ++++++++++++++++++++++++++++++ + +This script explains how Python application (typically pysnmp-based +SNMP Manager) could turn SNMP PDU variable-bindings into MIB objects +or the other way around. + +The code below does not explicitly add MIB compiler - that happens +behind the scenes. Examples below try to demo different kinds +of MIB objects to work with. +"""# +from pysnmp.smi import builder, view, rfc1902, error + +# MIB Builder manages pysnmp MIBs +mibBuilder = builder.MibBuilder() + +# MIB View Controller implements various queries to loaded MIBs +mibView = view.MibViewController(mibBuilder) + +# Obtain MIB object information by MIB object name +mibVar = rfc1902.ObjectIdentity('IF-MIB', 'ifInOctets', 1) + +# Optionally attach PySMI MIB compiler to MIB Builder that would +# create pysnmp MIBs on demand from ASN.1 sources downloaded from +# a web site. +try: + mibVar.addAsn1MibSource('http://mibs.snmplabs.com/asn1/@mib@') +except error.SmiError: + print('WARNING: not using MIB compiler (PySMI not installed)') + +mibVar.resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Obtain MIB object information by its [sequence] OID +mibVar = rfc1902.ObjectIdentity(tuple(mibVar)).resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Obtain MIB object information by its [string] OID +mibVar = rfc1902.ObjectIdentity(str(mibVar)).resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Obtain MIB object information by a mix of OID/label parts +mibVar = rfc1902.ObjectIdentity((1, 3, 6, 1, 2, 'mib-2', 1, 'sysDescr')).resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Obtain MIB object information by a label +mibVar = rfc1902.ObjectIdentity('iso.org.dod.internet.mgmt.mib-2.system.sysDescr').resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Obtain the first MIB object in given MIB module +mibVar = rfc1902.ObjectIdentity('SNMPv2-MIB').resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Obtain the last MIB object in given MIB module +mibVar = rfc1902.ObjectIdentity('SNMPv2-MIB', last=True).resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Another way to obtain the first (or last) symbol in MIB module +mibVar = rfc1902.ObjectIdentity('SNMPv2-MIB', '').resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Obtain MIB symbol from whatever MIB it is defined at (MIB should be loaded) +mibVar = rfc1902.ObjectIdentity('', 'sysDescr', 0).resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Create an OID-value pair (called variable-binding in SNMP) +varBind = rfc1902.ObjectType( + rfc1902.ObjectIdentity('SNMPv2-MIB', 'sysObjectID', 0), '1.3.6.1' +).resolveWithMib(mibView) + +print(varBind[0].prettyPrint(), varBind[1].__class__.__name__, varBind[1].prettyPrint()) + +# Create just OID +varBind = rfc1902.ObjectType( + rfc1902.ObjectIdentity('SNMPv2-MIB', 'sysObjectID', 0) +).resolveWithMib(mibView) + +print(varBind[0].prettyPrint(), varBind[1].__class__.__name__, varBind[1].prettyPrint()) + +# Create var-binds from MIB notification object (without OBJECTS clause) +varBinds = rfc1902.NotificationType( + rfc1902.ObjectIdentity('SNMPv2-MIB', 'coldStart') +).resolveWithMib(mibView) + +print(['%s = %s(%s)' % (x[0].prettyPrint(), x[1].__class__.__name__, x[1].prettyPrint()) for x in varBinds]) + +# Create var-binds from MIB notification object (with OBJECTS clause) +varBinds = rfc1902.NotificationType( + rfc1902.ObjectIdentity('IF-MIB', 'linkUp'), + instanceIndex=(1,), + objects={('IF-MIB', 'ifOperStatus'): 'down'} +).resolveWithMib(mibView) + +print(varBinds.prettyPrint()) diff --git a/examples/smi/manager/var-binds-mib-resolution.py b/examples/smi/manager/var-binds-mib-resolution.py deleted file mode 100644 index cee8b7c0..00000000 --- a/examples/smi/manager/var-binds-mib-resolution.py +++ /dev/null @@ -1,101 +0,0 @@ -""" -SNMP var-binds MIB resolution -+++++++++++++++++++++++++++++ - -This script explains how Python application (typically pysnmp-based SNMP Manager) -could enrich SNMP PDU variable-bindings with MIB information or convert MIB objects -into variable-bindings. - -"""# -from pysnmp.smi import builder, view, rfc1902, error - -# MIB Builder manages pysnmp MIBs -mibBuilder = builder.MibBuilder() - -# MIB View Controller implements various queries to loaded MIBs -mibView = view.MibViewController(mibBuilder) - -# Obtain MIB object information by MIB object name -mibVar = rfc1902.ObjectIdentity('IF-MIB', 'ifInOctets', 1) - -# Optionally attach PySMI MIB compiler to MIB Builder that would -# create pysnmp MIBs on demand from ASN.1 sources downloaded from -# a web site. -try: - mibVar.addAsn1MibSource('http://mibs.snmplabs.com/asn1/@mib@') -except error.SmiError: - print('WARNING: not using MIB compiler (PySMI not installed)') - -mibVar.resolveWithMib(mibView) - -print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) - -# Obtain MIB object information by its [sequence] OID -mibVar = rfc1902.ObjectIdentity(tuple(mibVar)).resolveWithMib(mibView) - -print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) - -# Obtain MIB object information by its [string] OID -mibVar = rfc1902.ObjectIdentity(str(mibVar)).resolveWithMib(mibView) - -print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) - -# Obtain MIB object information by a mix of OID/label parts -mibVar = rfc1902.ObjectIdentity((1, 3, 6, 1, 2, 'mib-2', 1, 'sysDescr')).resolveWithMib(mibView) - -print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) - -# Obtain MIB object information by a label -mibVar = rfc1902.ObjectIdentity('iso.org.dod.internet.mgmt.mib-2.system.sysDescr').resolveWithMib(mibView) - -print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) - -# Obtain the first MIB object in given MIB module -mibVar = rfc1902.ObjectIdentity('SNMPv2-MIB').resolveWithMib(mibView) - -print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) - -# Obtain the last MIB object in given MIB module -mibVar = rfc1902.ObjectIdentity('SNMPv2-MIB', last=True).resolveWithMib(mibView) - -print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) - -# Another way to obtain the first (or last) symbol in MIB module -mibVar = rfc1902.ObjectIdentity('SNMPv2-MIB', '').resolveWithMib(mibView) - -print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) - -# Obtain MIB symbol from whatever MIB it is defined at (MIB should be loaded) -mibVar = rfc1902.ObjectIdentity('', 'sysDescr', 0).resolveWithMib(mibView) - -print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) - -# Create an OID-value pair (called variable-binding in SNMP) -varBind = rfc1902.ObjectType( - rfc1902.ObjectIdentity('SNMPv2-MIB', 'sysObjectID', 0), '1.3.6.1' -).resolveWithMib(mibView) - -print(varBind[0].prettyPrint(), varBind[1].__class__.__name__, varBind[1].prettyPrint()) - -# Create just OID -varBind = rfc1902.ObjectType( - rfc1902.ObjectIdentity('SNMPv2-MIB', 'sysObjectID', 0) -).resolveWithMib(mibView) - -print(varBind[0].prettyPrint(), varBind[1].__class__.__name__, varBind[1].prettyPrint()) - -# Create var-binds from MIB notification object (without OBJECTS clause) -varBinds = rfc1902.NotificationType( - rfc1902.ObjectIdentity('SNMPv2-MIB', 'coldStart') -).resolveWithMib(mibView) - -print(['%s = %s(%s)' % (x[0].prettyPrint(), x[1].__class__.__name__, x[1].prettyPrint()) for x in varBinds]) - -# Create var-binds from MIB notification object (with OBJECTS clause) -varBinds = rfc1902.NotificationType( - rfc1902.ObjectIdentity('IF-MIB', 'linkUp'), - instanceIndex=(1,), - objects={('IF-MIB', 'ifOperStatus'): 'down'} -).resolveWithMib(mibView) - -print(varBinds.prettyPrint()) -- cgit v1.2.1