From fd7ecb8e0b1bbf941107db1fb26fbdaeb4a74859 Mon Sep 17 00:00:00 2001 From: thijs snijder <snijder@astron.nl> Date: Thu, 31 Mar 2022 08:57:39 +0200 Subject: [PATCH] added named_value support --- .../clients/snmp_client.py | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/tangostationcontrol/tangostationcontrol/clients/snmp_client.py b/tangostationcontrol/tangostationcontrol/clients/snmp_client.py index aac0e603f..5878ffa55 100644 --- a/tangostationcontrol/tangostationcontrol/clients/snmp_client.py +++ b/tangostationcontrol/tangostationcontrol/clients/snmp_client.py @@ -111,7 +111,7 @@ class SNMP_client(CommClient): def load_mib(mib_name): mibBuilder = builder.MibBuilder() mibBuilder.loadModule(mib_name) - + class snmp_attribute: @@ -200,19 +200,39 @@ class snmp_attribute: varBinds = varBinds[0] for varBind in varBinds: - # class 'DisplayString' doesnt want to play along for whatever reason - if "DisplayString" in str(type(varBind[1])): - vals.append(varBind[1].prettyPrint()) - elif type(varBind[1]) == hlapi.IpAddress: + + # Get all the types. Custom and base. Custom types are merely wrapped base types. + varbind_types = varBind[1].__class__.__bases__ + (type(varBind[1]),) + + snmp_type = None + + for i in varbind_types: + if i in snmp_to_numpy_dict.keys(): + snmp_type = i + + if snmp_type is None: + TypeError(f"Error: did not find a valid snmp type. Got: {varbind_types}, expected one of: {snmp_to_numpy_dict.keys()}") + + if snmp_type == hlapi.IpAddress: # IpAddress values get printed as their raw value but in hex (7F 20 20 01 for 127.0.0.1 for example) vals.append(varBind[1].prettyPrint()) + + elif snmp_type == hlapi.Integer32 and self.dtype == str: + # Integers can have 'named values', Where a value can be translated to a specific name. A dict basically + # Example: {1: "other", 2: "invalid", 3: "dynamic", 4: "static",} + + if varBind[1].namedValues == {}: + # An empty dict {} means no namedValue's are present. + vals.append(snmp_to_numpy_dict[snmp_type](varBind[1])) + + else: + # append the named values string instead of the raw number. + vals.append(varBind[1].prettyPrint()) else: # convert from the funky pysnmp types to numpy types and then append - vals.append(snmp_to_numpy_dict[type(varBind[1])](varBind[1])) + vals.append(snmp_to_numpy_dict[snmp_type](varBind[1])) if self.is_scalar: vals = vals[0] return vals - - -- GitLab