From a4fed0601ca63ed85ff9b87efd0e8d621e1c5cb2 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Mon, 21 May 2018 15:53:12 -0400 Subject: [PATCH] Fix unprintable characters in some chassisid Some switches send raw octets back, some printable. Try to normalize when unprintable chassis id are detected. This is not 100%, if the hex would be all between 20 and 80 throughout the string, then this will fail to do the right thing. Hopefully, the amount of times when lldp partners disagree on how to implement LLDP-MIB will be limited. Currently it is known than Lenovo and Juniper switches disagree, and both of those have what would be unprintable values in the mfg portion of the chassis id. --- confluent_server/confluent/networking/lldp.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/confluent_server/confluent/networking/lldp.py b/confluent_server/confluent/networking/lldp.py index 7641ccd7..e8fdc009 100644 --- a/confluent_server/confluent/networking/lldp.py +++ b/confluent_server/confluent/networking/lldp.py @@ -151,6 +151,9 @@ def _extract_extended_desc(info, source, integritychecked): info['peerdescription'] = source def sanitize(val): + # This is pretty much the same approach net-snmp takes. + # if the string is printable as-is, then just give it as-is + # if the string has non-printable, then hexify it val = str(val) for x in val.strip('\x00'): if ord(x) < 32 or ord(x) > 128: @@ -161,7 +164,7 @@ def sanitize(val): def _init_lldp(data, iname, idx, idxtoportid, switch): if iname not in data: data[iname] = {'port': iname, 'portid': str(idxtoportid[idx]), - 'chassisid': str(_chassisidbyswitch[switch])} + 'chassisid': _chassisidbyswitch[switch]} def _extract_neighbor_data_b(args): """Build LLDP data about elements connected to switch @@ -180,7 +183,8 @@ def _extract_neighbor_data_b(args): sid = str(sysid[1][6:]) idxtoifname = {} idxtoportid = {} - _chassisidbyswitch[switch] = list(conn.walk('1.0.8802.1.1.2.1.3.2'))[0][1] + _chassisidbyswitch[switch] = sanitize(list( + conn.walk('1.0.8802.1.1.2.1.3.2'))[0][1]) for oidindex in conn.walk('1.0.8802.1.1.2.1.3.7.1.3'): idx = oidindex[0][-1] idxtoportid[idx] = sanitize(oidindex[1])