From da4539e34cacdc0bd6ecfba98dc48caecc12b104 Mon Sep 17 00:00:00 2001 From: Ilya Etingof Date: Sat, 30 Jun 2018 20:14:57 +0200 Subject: Overhaul SMI/MIB instrumentation API (#161) Overhaul SMI/MIB instrumentation API SMI/MIB managed objects API overhauled for simplicity and flexibility breaking backward compatibility. This change would allow way more control over custom MIB managed objects and also is the prerequisite for asynchronous MIB instrumentation. --- examples/smi/agent/custom-managed-object.py | 13 ++++++++----- examples/smi/agent/operations-on-managed-objects.py | 11 ++++++----- .../v3arch/asyncore/agent/cmdrsp/custom-mib-controller.py | 2 +- .../v3arch/asyncore/agent/cmdrsp/implementing-snmp-table.py | 6 +++--- 4 files changed, 18 insertions(+), 14 deletions(-) (limited to 'examples') diff --git a/examples/smi/agent/custom-managed-object.py b/examples/smi/agent/custom-managed-object.py index 0b175193..73bfa836 100644 --- a/examples/smi/agent/custom-managed-object.py +++ b/examples/smi/agent/custom-managed-object.py @@ -28,9 +28,9 @@ sysLocation, = mibBuilder.importSymbols('SNMPv2-MIB', 'sysLocation') # Custom Managed Object class MySysLocationInstance(MibScalarInstance): # noinspection PyUnusedLocal - def readGet(self, name, *args): + def readGet(self, varBind, **context): # Just return a custom value - return name, self.syntax.clone('The Leaky Cauldron') + return varBind[0], self.syntax.clone('The Leaky Cauldron') sysLocationInstance = MySysLocationInstance( @@ -52,9 +52,12 @@ if __name__ == '__main__': 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] + + varBinds = [((), None)] + + while True: + varBinds = mibInstrum.readNextVars(*varBinds) + oid, val = varBinds[0] if exval.endOfMib.isSameTypeWith(val): break print(oid, val.prettyPrint()) diff --git a/examples/smi/agent/operations-on-managed-objects.py b/examples/smi/agent/operations-on-managed-objects.py index 7d1f300c..e1eb52d6 100644 --- a/examples/smi/agent/operations-on-managed-objects.py +++ b/examples/smi/agent/operations-on-managed-objects.py @@ -28,18 +28,19 @@ print('done') print('Create/update SNMP-COMMUNITY-MIB::snmpCommunityEntry table row: ') varBinds = mibInstrum.writeVars( - ((snmpCommunityEntry.name + (2,) + instanceId, 'mycomm'), - (snmpCommunityEntry.name + (3,) + instanceId, 'mynmsname'), - (snmpCommunityEntry.name + (7,) + instanceId, 'volatile')) + (snmpCommunityEntry.name + (2,) + instanceId, 'mycomm'), + (snmpCommunityEntry.name + (3,) + instanceId, 'mynmsname'), + (snmpCommunityEntry.name + (7,) + instanceId, 'volatile') ) 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') print('Read whole MIB (table walk)') -oid, val = (), None +varBinds = [((), None)] while True: - oid, val = mibInstrum.readNextVars(((oid, val),))[0] + 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())) diff --git a/examples/v3arch/asyncore/agent/cmdrsp/custom-mib-controller.py b/examples/v3arch/asyncore/agent/cmdrsp/custom-mib-controller.py index 1a03e725..94bdb7de 100644 --- a/examples/v3arch/asyncore/agent/cmdrsp/custom-mib-controller.py +++ b/examples/v3arch/asyncore/agent/cmdrsp/custom-mib-controller.py @@ -53,7 +53,7 @@ snmpContext = context.SnmpContext(snmpEngine) # any Managed Objects attached. It supports only GET's and # always echos request var-binds in response. class EchoMibInstrumController(instrum.AbstractMibInstrumController): - def readVars(self, varBinds, acInfo=(None, None)): + def readVars(self, *varBinds, **context): return [(ov[0], v2c.OctetString('You queried OID %s' % ov[0])) for ov in varBinds] diff --git a/examples/v3arch/asyncore/agent/cmdrsp/implementing-snmp-table.py b/examples/v3arch/asyncore/agent/cmdrsp/implementing-snmp-table.py index 0e15599a..ea924fc2 100644 --- a/examples/v3arch/asyncore/agent/cmdrsp/implementing-snmp-table.py +++ b/examples/v3arch/asyncore/agent/cmdrsp/implementing-snmp-table.py @@ -102,9 +102,9 @@ mibBuilder.exportSymbols( rowInstanceId = exampleTableEntry.getInstIdFromIndices('example record one') mibInstrumentation = snmpContext.getMibInstrum() mibInstrumentation.writeVars( - ((exampleTableColumn2.name + rowInstanceId, 'my string value'), - (exampleTableColumn3.name + rowInstanceId, 123456), - (exampleTableStatus.name + rowInstanceId, 'createAndGo')) + (exampleTableColumn2.name + rowInstanceId, 'my string value'), + (exampleTableColumn3.name + rowInstanceId, 123456), + (exampleTableStatus.name + rowInstanceId, 'createAndGo') ) # --- end of SNMP table population --- -- cgit v1.2.1