From 83fa32f61590be62c76757521a463a0b369a9cf0 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 8 Mar 2024 16:39:23 -0800 Subject: [PATCH] ethtool - convert units --- jc/parsers/ethtool.py | 61 +++++++++++--- tests/fixtures/generic/ethtool--default1.json | 2 +- tests/fixtures/generic/ethtool--default2.json | 2 +- .../ethtool--module-info-convert-units.json | 1 + .../ethtool--module-info-convert-units.out | 79 +++++++++++++++++++ 5 files changed, 131 insertions(+), 14 deletions(-) create mode 100644 tests/fixtures/generic/ethtool--module-info-convert-units.json create mode 100644 tests/fixtures/generic/ethtool--module-info-convert-units.out diff --git a/jc/parsers/ethtool.py b/jc/parsers/ethtool.py index 7616c3c2..2b442c68 100644 --- a/jc/parsers/ethtool.py +++ b/jc/parsers/ethtool.py @@ -161,6 +161,7 @@ Examples: "br_margin_min": "0%" } """ +import re from typing import List, Dict from jc.jc_types import JSONDictType import jc.utils @@ -202,6 +203,39 @@ def _process(proc_data: JSONDictType) -> JSONDictType: if key in proc_data: proc_data[key] = jc.utils.convert_to_bool(proc_data[key]) + # find and convert units + # "0.0468 mW / -13.30 dBm" or "-40.00 degrees C / -40.00 degrees F" + degrees_re = re.compile(r'(?P.*?) degrees C \/ (?P.*?) degrees F') + power_re = re.compile(r'(?P.*?) mW \/ (?P.*?) dBm') + + for key, val in proc_data.copy().items(): + if isinstance(val, str): + degrees_match = re.match(degrees_re, val) + if degrees_match: + degrees_dict = degrees_match.groupdict() + proc_data[key + '_celsius'] = float(degrees_dict['deg_c']) + proc_data[key + '_farenheit'] = float(degrees_dict['deg_f']) + del proc_data[key] + continue + + power_match = re.match(power_re, val) + if power_match: + power_dict = power_match.groupdict() + proc_data[key + '_mw'] = float(power_dict['pow_mw']) + proc_data[key + '_dbm'] = float(power_dict['pow_dbm']) + del proc_data[key] + continue + + if val.endswith(' V'): + proc_data[key + '_v'] = jc.utils.convert_to_float(val) + del proc_data[key] + continue + + if val.endswith(' mA'): + proc_data[key + '_ma'] = jc.utils.convert_to_float(val) + del proc_data[key] + continue + return proc_data @@ -304,18 +338,21 @@ def _parse_default(data: str) -> JSONDictType: val = val.strip() raw_output[key] = val - if supported_ports: - raw_output['supported_ports'] = supported_ports - if supported_link_modes: - raw_output['supported_link_modes'] = supported_link_modes - if supported_fec_modes: - raw_output['supported_fec_modes'] = supported_fec_modes - if advertised_link_modes: - raw_output['advertised_link_modes'] = advertised_link_modes - if advertised_fec_modes: - raw_output['advertised_fec_modes'] = advertised_fec_modes - if current_message_level: - raw_output['current_message_level'] = current_message_level + list_vals = [ + (supported_ports, 'supported_ports'), + (supported_link_modes, 'supported_link_modes'), + (supported_fec_modes, 'supported_fec_modes'), + (advertised_link_modes, 'advertised_link_modes'), + (advertised_fec_modes, 'advertised_fec_modes'), + (current_message_level, 'current_message_level') + ] + + for obj_list, obj_key in list_vals: + if raw_output.get(obj_key, '').lower() == 'not reported': + raw_output[obj_key] = [] + else: + if obj_list: + raw_output[obj_key] = obj_list return raw_output diff --git a/tests/fixtures/generic/ethtool--default1.json b/tests/fixtures/generic/ethtool--default1.json index 75d39613..57e3f847 100644 --- a/tests/fixtures/generic/ethtool--default1.json +++ b/tests/fixtures/generic/ethtool--default1.json @@ -1 +1 @@ -{"name":"enp0s3","supported_pause_frame_use":"No","supports_auto_negotiation":true,"supported_fec_modes":"Not reported","advertised_pause_frame_use":false,"advertised_auto_negotiation":true,"advertised_fec_modes":"Not reported","speed":"1000Mb/s","duplex":"Full","port":"Twisted Pair","phyad":"0","transceiver":"internal","auto_negotiation":false,"mdi_x":"off (auto)","supports_wake_on":"umbg","wake_on":"d","link_detected":true,"supported_ports":["TP"],"supported_link_modes":["10baseT/Half","10baseT/Full","100baseT/Half","100baseT/Full","1000baseT/Full"],"advertised_link_modes":["10baseT/Half","10baseT/Full","100baseT/Half","100baseT/Full","1000baseT/Full"],"current_message_level":["0x00000007 (7)","drv probe link"],"speed_bps":1000000000} +{"name":"enp0s3","supported_pause_frame_use":"No","supports_auto_negotiation":true,"supported_fec_modes":[],"advertised_pause_frame_use":false,"advertised_auto_negotiation":true,"advertised_fec_modes":[],"speed":"1000Mb/s","duplex":"Full","port":"Twisted Pair","phyad":"0","transceiver":"internal","auto_negotiation":false,"mdi_x":"off (auto)","supports_wake_on":"umbg","wake_on":"d","link_detected":true,"supported_ports":["TP"],"supported_link_modes":["10baseT/Half","10baseT/Full","100baseT/Half","100baseT/Full","1000baseT/Full"],"advertised_link_modes":["10baseT/Half","10baseT/Full","100baseT/Half","100baseT/Full","1000baseT/Full"],"current_message_level":["0x00000007 (7)","drv probe link"],"speed_bps":1000000000} diff --git a/tests/fixtures/generic/ethtool--default2.json b/tests/fixtures/generic/ethtool--default2.json index 87e9b69f..ff9ba7c9 100644 --- a/tests/fixtures/generic/ethtool--default2.json +++ b/tests/fixtures/generic/ethtool--default2.json @@ -1 +1 @@ -{"name":"enp1s0","supported_pause_frame_use":"Symmetric Receive-only","supports_auto_negotiation":true,"supported_fec_modes":"Not reported","advertised_pause_frame_use":false,"advertised_auto_negotiation":true,"advertised_fec_modes":"Not reported","speed":"40000Mb/s","duplex":"Full","auto_negotiation":false,"port":"FIBRE","phyad":"0","transceiver":"internal","supports_wake_on":"d","wake_on":"d","link_detected":true,"supported_ports":["FIBRE"],"supported_link_modes":["10000baseKX4/Full","40000baseCR4/Full","40000baseSR4/Full","56000baseCR4/Full","56000baseSR4/Full","1000baseX/Full","10000baseCR/Full","10000baseSR/Full"],"advertised_link_modes":["10000baseKX4/Full","40000baseCR4/Full","40000baseSR4/Full","1000baseX/Full","10000baseCR/Full","10000baseSR/Full"],"current_message_level":["0x00000014 (20)","link ifdown"],"speed_bps":40000000000} +{"name":"enp1s0","supported_pause_frame_use":"Symmetric Receive-only","supports_auto_negotiation":true,"supported_fec_modes":[],"advertised_pause_frame_use":false,"advertised_auto_negotiation":true,"advertised_fec_modes":[],"speed":"40000Mb/s","duplex":"Full","auto_negotiation":false,"port":"FIBRE","phyad":"0","transceiver":"internal","supports_wake_on":"d","wake_on":"d","link_detected":true,"supported_ports":["FIBRE"],"supported_link_modes":["10000baseKX4/Full","40000baseCR4/Full","40000baseSR4/Full","56000baseCR4/Full","56000baseSR4/Full","1000baseX/Full","10000baseCR/Full","10000baseSR/Full"],"advertised_link_modes":["10000baseKX4/Full","40000baseCR4/Full","40000baseSR4/Full","1000baseX/Full","10000baseCR/Full","10000baseSR/Full"],"current_message_level":["0x00000014 (20)","link ifdown"],"speed_bps":40000000000} diff --git a/tests/fixtures/generic/ethtool--module-info-convert-units.json b/tests/fixtures/generic/ethtool--module-info-convert-units.json new file mode 100644 index 00000000..54f32780 --- /dev/null +++ b/tests/fixtures/generic/ethtool--module-info-convert-units.json @@ -0,0 +1 @@ +{"identifier":"0x03 (SFP)","extended_identifier":"0x04 (GBIC/SFP defined by 2-wire interface ID)","connector":"0x07 (LC)","transceiver_codes":"0x20 0x00 0x00 0x00 0x44 0x40 0x00 0x00 0x03","transceiver_type":["10G Ethernet: 10G Base-LR","FC: short distance (S)","FC: Shortwave laser, linear Rx (SA)","FC: Shortwave laser w/o OFC (SN)","Extended: 100G Base-LR4 or 25GBase-LR"],"encoding":"0x03 (NRZ)","br_nominal":"25750MBd","rate_identifier":"0x00 (unspecified)","length_smf_km":"10km","length_smf":"10000m","length_50um":"0m","length_62_5um":"0m","length_copper":"0m","length_om3":"0m","laser_wavelength":"1310nm","vendor_name":"EDGE","vendor_oui":"00:1b:21","vendor_pn":"SFP-10/25GLR-31","vendor_rev":"1A","option_values":"0x08 0x1a","option":["RX_LOS implemented","TX_FAULT implemented","TX_DISABLE implemented","Retimer or CDR implemented"],"br_margin_max":"0%","br_margin_min":"0%","vendor_sn":"EO11111111111","date_code":"230619","optical_diagnostics_support":"Yes","alarm_warning_flags_implemented":"Yes","laser_bias_current_high_alarm":"Off","laser_bias_current_low_alarm":"Off","laser_bias_current_high_warning":"Off","laser_bias_current_low_warning":"Off","laser_output_power_high_alarm":"Off","laser_output_power_low_alarm":"Off","laser_output_power_high_warning":"Off","laser_output_power_low_warning":"Off","module_temperature_high_alarm":"Off","module_temperature_low_alarm":"Off","module_temperature_high_warning":"Off","module_temperature_low_warning":"Off","module_voltage_high_alarm":"Off","module_voltage_low_alarm":"Off","module_voltage_high_warning":"Off","module_voltage_low_warning":"Off","laser_rx_power_high_alarm":"Off","laser_rx_power_low_alarm":"On","laser_rx_power_high_warning":"Off","laser_rx_power_low_warning":"On","laser_bias_current_ma":51.494,"laser_output_power_mw":0.8183,"laser_output_power_dbm":-0.87,"receiver_signal_average_optical_power_mw":0.0001,"receiver_signal_average_optical_power_dbm":-40.0,"module_temperature_celsius":34.99,"module_temperature_farenheit":94.99,"module_voltage_v":3.2995,"laser_bias_current_high_alarm_threshold_ma":110.0,"laser_bias_current_low_alarm_threshold_ma":1.0,"laser_bias_current_high_warning_threshold_ma":100.0,"laser_bias_current_low_warning_threshold_ma":1.0,"laser_output_power_high_alarm_threshold_mw":3.1623,"laser_output_power_high_alarm_threshold_dbm":5.0,"laser_output_power_low_alarm_threshold_mw":0.1,"laser_output_power_low_alarm_threshold_dbm":-10.0,"laser_output_power_high_warning_threshold_mw":1.5849,"laser_output_power_high_warning_threshold_dbm":2.0,"laser_output_power_low_warning_threshold_mw":0.1995,"laser_output_power_low_warning_threshold_dbm":-7.0,"module_temperature_high_alarm_threshold_celsius":95.0,"module_temperature_high_alarm_threshold_farenheit":203.0,"module_temperature_low_alarm_threshold_celsius":-50.0,"module_temperature_low_alarm_threshold_farenheit":-58.0,"module_temperature_high_warning_threshold_celsius":85.0,"module_temperature_high_warning_threshold_farenheit":185.0,"module_temperature_low_warning_threshold_celsius":-40.0,"module_temperature_low_warning_threshold_farenheit":-40.0,"module_voltage_high_alarm_threshold_v":3.63,"module_voltage_low_alarm_threshold_v":2.97,"module_voltage_high_warning_threshold_v":3.465,"module_voltage_low_warning_threshold_v":3.135,"laser_rx_power_high_alarm_threshold_mw":3.1623,"laser_rx_power_high_alarm_threshold_dbm":5.0,"laser_rx_power_low_alarm_threshold_mw":0.0234,"laser_rx_power_low_alarm_threshold_dbm":-16.31,"laser_rx_power_high_warning_threshold_mw":1.5849,"laser_rx_power_high_warning_threshold_dbm":2.0,"laser_rx_power_low_warning_threshold_mw":0.0468,"laser_rx_power_low_warning_threshold_dbm":-13.3} diff --git a/tests/fixtures/generic/ethtool--module-info-convert-units.out b/tests/fixtures/generic/ethtool--module-info-convert-units.out new file mode 100644 index 00000000..1d67bae1 --- /dev/null +++ b/tests/fixtures/generic/ethtool--module-info-convert-units.out @@ -0,0 +1,79 @@ + Identifier : 0x03 (SFP) + Extended identifier : 0x04 (GBIC/SFP defined by 2-wire interface ID) + Connector : 0x07 (LC) + Transceiver codes : 0x20 0x00 0x00 0x00 0x44 0x40 0x00 0x00 0x03 + Transceiver type : 10G Ethernet: 10G Base-LR + Transceiver type : FC: short distance (S) + Transceiver type : FC: Shortwave laser, linear Rx (SA) + Transceiver type : FC: Shortwave laser w/o OFC (SN) + Transceiver type : Extended: 100G Base-LR4 or 25GBase-LR + Encoding : 0x03 (NRZ) + BR, Nominal : 25750MBd + Rate identifier : 0x00 (unspecified) + Length (SMF,km) : 10km + Length (SMF) : 10000m + Length (50um) : 0m + Length (62.5um) : 0m + Length (Copper) : 0m + Length (OM3) : 0m + Laser wavelength : 1310nm + Vendor name : EDGE + Vendor OUI : 00:1b:21 + Vendor PN : SFP-10/25GLR-31 + Vendor rev : 1A + Option values : 0x08 0x1a + Option : RX_LOS implemented + Option : TX_FAULT implemented + Option : TX_DISABLE implemented + Option : Retimer or CDR implemented + BR margin, max : 0% + BR margin, min : 0% + Vendor SN : EO11111111111 + Date code : 230619 + Optical diagnostics support : Yes + Laser bias current : 51.494 mA + Laser output power : 0.8183 mW / -0.87 dBm + Receiver signal average optical power : 0.0001 mW / -40.00 dBm + Module temperature : 34.99 degrees C / 94.99 degrees F + Module voltage : 3.2995 V + Alarm/warning flags implemented : Yes + Laser bias current high alarm : Off + Laser bias current low alarm : Off + Laser bias current high warning : Off + Laser bias current low warning : Off + Laser output power high alarm : Off + Laser output power low alarm : Off + Laser output power high warning : Off + Laser output power low warning : Off + Module temperature high alarm : Off + Module temperature low alarm : Off + Module temperature high warning : Off + Module temperature low warning : Off + Module voltage high alarm : Off + Module voltage low alarm : Off + Module voltage high warning : Off + Module voltage low warning : Off + Laser rx power high alarm : Off + Laser rx power low alarm : On + Laser rx power high warning : Off + Laser rx power low warning : On + Laser bias current high alarm threshold : 110.000 mA + Laser bias current low alarm threshold : 1.000 mA + Laser bias current high warning threshold : 100.000 mA + Laser bias current low warning threshold : 1.000 mA + Laser output power high alarm threshold : 3.1623 mW / 5.00 dBm + Laser output power low alarm threshold : 0.1000 mW / -10.00 dBm + Laser output power high warning threshold : 1.5849 mW / 2.00 dBm + Laser output power low warning threshold : 0.1995 mW / -7.00 dBm + Module temperature high alarm threshold : 95.00 degrees C / 203.00 degrees F + Module temperature low alarm threshold : -50.00 degrees C / -58.00 degrees F + Module temperature high warning threshold : 85.00 degrees C / 185.00 degrees F + Module temperature low warning threshold : -40.00 degrees C / -40.00 degrees F + Module voltage high alarm threshold : 3.6300 V + Module voltage low alarm threshold : 2.9700 V + Module voltage high warning threshold : 3.4650 V + Module voltage low warning threshold : 3.1350 V + Laser rx power high alarm threshold : 3.1623 mW / 5.00 dBm + Laser rx power low alarm threshold : 0.0234 mW / -16.31 dBm + Laser rx power high warning threshold : 1.5849 mW / 2.00 dBm + Laser rx power low warning threshold : 0.0468 mW / -13.30 dBm