summaryrefslogtreecommitdiff
path: root/examples/smi
diff options
context:
space:
mode:
authorelie <elie>2005-10-26 22:29:11 +0000
committerelie <elie>2005-10-26 22:29:11 +0000
commit61944d704d105f405110b33c10b24732788b8490 (patch)
treefc076b58dca1bc282f624faa70ac586edce16f7e /examples/smi
parent1aa1c0eeff00fd1918205924f4fa4e8aaaf488f5 (diff)
downloadpysnmp-61944d704d105f405110b33c10b24732788b8490.tar.gz
re-written to demonstrate custom Managed Objects handling
Diffstat (limited to 'examples/smi')
-rw-r--r--examples/smi/instrum.py64
1 files changed, 44 insertions, 20 deletions
diff --git a/examples/smi/instrum.py b/examples/smi/instrum.py
index b6e63ec..51d2a06 100644
--- a/examples/smi/instrum.py
+++ b/examples/smi/instrum.py
@@ -1,26 +1,50 @@
-# SNMP agent backend
-from pysnmp.smi import builder, instrum, exval
+# Managed Objects implementation
+from pysnmp.smi import builder
-print 'Loading MIB modules...',
-mibBuilder = builder.MibBuilder().loadModules(
- 'SNMPv2-MIB', 'SNMP-FRAMEWORK-MIB', 'SNMP-COMMUNITY-MIB'
+# MIB Builder is normally pre-created by SNMP engine
+mibBuilder = builder.MibBuilder()
+
+#
+# This may be done in a stand-alone file and then loaded up
+# by SNMP Agent
+#
+
+# A base class for a custom Managed Object
+MibScalarInstance, = mibBuilder.importSymbols(
+ 'SNMPv2-SMI', 'MibScalarInstance'
)
-print 'done'
-print 'Building MIB tree...',
-mibInstrum = instrum.MibInstrumController(mibBuilder)
-print 'done'
+# Managed object specification
+sysLocation, = mibBuilder.importSymbols('SNMPv2-MIB', 'sysLocation')
+
+# Custom Managed Object
+class MySysLocationInstance(MibScalarInstance):
+ def readGet(self, name, val, idx, (acFun, acCtx)):
+ # Just return a custom value
+ return name, self.syntax.clone('The Leaky Cauldron')
+
+sysLocationInstance = MySysLocationInstance(
+ sysLocation.name, (0,), sysLocation.syntax
+ )
-print 'Remote manager write/create access to MIB instrumentation: ',
-print mibInstrum.writeVars(
- (((1,3,6,1,6,3,18,1,1,1,2,109,121,110,109,115), 'mycomm'),
- ((1,3,6,1,6,3,18,1,1,1,3,109,121,110,109,115), 'mynmsname'))
+# Register Managed Object with a MIB tree
+mibBuilder.exportSymbols(
+ # '__' prefixed MIB modules take precedence on indexing
+ '__MY-LOCATION-MIB', sysLocationInstance=sysLocationInstance
)
-print 'Remote manager read access to MIB instrumentation (table walk)'
-oid, val = (), None
-while 1:
- oid, val = mibInstrum.readNextVars(((oid, val),))[0]
- if exval.endOfMib.isSameTypeWith(val):
- break
- print oid, val
+if __name__ == '__main__':
+ #
+ # This is what is done internally by Agent.
+ #
+ from pysnmp.smi import instrum, exval
+
+ mibInstrum = instrum.MibInstrumController(mibBuilder)
+
+ print 'Remote manager read access to MIB instrumentation (table walk)'
+ oid, val = (), None
+ while 1:
+ oid, val = mibInstrum.readNextVars(((oid, val),))[0]
+ if exval.endOfMib.isSameTypeWith(val):
+ break
+ print oid, val