summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorIlya Etingof <etingof@gmail.com>2018-10-13 20:21:31 +0200
committerGitHub <noreply@github.com>2018-10-13 20:21:31 +0200
commit534a5bb8108013c59706c4fb6d195aa332af5e13 (patch)
tree26683f3689386d7ca11216c952861e1184430eee /examples
parent12138b182c82981c5268a18b15178ed564be874f (diff)
downloadpysnmp-git-534a5bb8108013c59706c4fb6d195aa332af5e13.tar.gz
Convert to async MIB instrumentation API (#209)
MIB instrumentation API changed to allow for asynchronous managed objects access. Although built-in SNMPv2-SMI objects are still synchronous, the MIB instrumentation API is async what allows users to replace default MIB instrumentation with their own, potentially asynchronous. CommandResponder refactored to facilitate asynchronous MIB instrumentation routines. The `readVars`, `readNextVars` and `writeVars` MIB controller methods return immediately and deliver their results via a call back. SMI/MIB managed objects API overhauled for simplicity and flexibility breaking backward compatibility.
Diffstat (limited to 'examples')
-rw-r--r--examples/smi/agent/custom-managed-object.py28
-rw-r--r--examples/smi/agent/operations-on-managed-objects.py38
-rw-r--r--examples/v3arch/asyncore/agent/cmdrsp/custom-mib-controller.py4
3 files changed, 49 insertions, 21 deletions
diff --git a/examples/smi/agent/custom-managed-object.py b/examples/smi/agent/custom-managed-object.py
index 73bfa836..f1af0ae3 100644
--- a/examples/smi/agent/custom-managed-object.py
+++ b/examples/smi/agent/custom-managed-object.py
@@ -51,13 +51,23 @@ if __name__ == '__main__':
mibInstrum = instrum.MibInstrumController(mibBuilder)
- print('Remote manager read access to MIB instrumentation (table walk)')
-
- varBinds = [((), None)]
+ def cbFun(varBinds, **context):
+ for oid, val in varBinds:
+ if exval.endOfMib.isSameTypeWith(val):
+ context['state']['stop'] = True
+ print('%s = %s' % ('.'.join([str(x) for x in oid]), not val.isValue and 'N/A' or val.prettyPrint()))
+
+ context['state']['varBinds'] = varBinds
+
+ context = {
+ 'cbFun': cbFun,
+ 'state': {
+ 'varBinds': [((1, 3, 6), None)],
+ 'stop': False
+ }
+ }
- while True:
- varBinds = mibInstrum.readNextVars(*varBinds)
- oid, val = varBinds[0]
- if exval.endOfMib.isSameTypeWith(val):
- break
- print(oid, val.prettyPrint())
+ print('Remote manager read access to MIB instrumentation (table walk)')
+ while not context['state']['stop']:
+ mibInstrum.readNextVars(*context['state']['varBinds'], **context)
+ print('done')
diff --git a/examples/smi/agent/operations-on-managed-objects.py b/examples/smi/agent/operations-on-managed-objects.py
index e1eb52d6..bfcd021e 100644
--- a/examples/smi/agent/operations-on-managed-objects.py
+++ b/examples/smi/agent/operations-on-managed-objects.py
@@ -26,24 +26,40 @@ snmpCommunityEntry, = mibBuilder.importSymbols(
instanceId = snmpCommunityEntry.getInstIdFromIndices('my-router')
print('done')
+
+def cbFun(varBinds, **context):
+ for oid, val in varBinds:
+ print('%s = %s' % ('.'.join([str(x) for x in oid]), not val.isValue and 'N/A' or val.prettyPrint()))
+
print('Create/update SNMP-COMMUNITY-MIB::snmpCommunityEntry table row: ')
-varBinds = mibInstrum.writeVars(
+mibInstrum.writeVars(
(snmpCommunityEntry.name + (2,) + instanceId, 'mycomm'),
(snmpCommunityEntry.name + (3,) + instanceId, 'mynmsname'),
- (snmpCommunityEntry.name + (7,) + instanceId, 'volatile')
+ (snmpCommunityEntry.name + (7,) + instanceId, 'volatile'),
+ cbFun=cbFun
)
-for oid, val in varBinds:
- print('%s = %s' % ('.'.join([str(x) for x in oid]), not val.isValue and 'N/A' or val.prettyPrint()))
print('done')
+
+def cbFun(varBinds, **context):
+ for oid, val in varBinds:
+ if exval.endOfMib.isSameTypeWith(val):
+ context['state']['stop'] = True
+ print('%s = %s' % ('.'.join([str(x) for x in oid]), not val.isValue and 'N/A' or val.prettyPrint()))
+
+ context['state']['varBinds'] = varBinds
+
+context = {
+ 'cbFun': cbFun,
+ 'state': {
+ 'varBinds': [((1, 3, 6), None)],
+ 'stop': False
+ }
+}
+
print('Read whole MIB (table walk)')
-varBinds = [((), None)]
-while True:
- varBinds = mibInstrum.readNextVars(*varBinds)
- oid, val = varBinds[0]
- if exval.endOfMib.isSameTypeWith(val):
- break
- print('%s = %s' % ('.'.join([str(x) for x in oid]), not val.isValue and 'N/A' or val.prettyPrint()))
+while not context['state']['stop']:
+ mibInstrum.readNextVars(*context['state']['varBinds'], **context)
print('done')
print('Unloading MIB modules...'),
diff --git a/examples/v3arch/asyncore/agent/cmdrsp/custom-mib-controller.py b/examples/v3arch/asyncore/agent/cmdrsp/custom-mib-controller.py
index 94bdb7de..7e74df3d 100644
--- a/examples/v3arch/asyncore/agent/cmdrsp/custom-mib-controller.py
+++ b/examples/v3arch/asyncore/agent/cmdrsp/custom-mib-controller.py
@@ -54,7 +54,9 @@ snmpContext = context.SnmpContext(snmpEngine)
# always echos request var-binds in response.
class EchoMibInstrumController(instrum.AbstractMibInstrumController):
def readVars(self, *varBinds, **context):
- return [(ov[0], v2c.OctetString('You queried OID %s' % ov[0])) for ov in varBinds]
+ cbFun = context.get('cbFun')
+ if cbFun:
+ cbFun([(ov[0], v2c.OctetString('You queried OID %s' % ov[0])) for ov in varBinds], **context)
# Create a custom Management Instrumentation Controller and register at