mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-15 01:24:29 +02:00
add lane info lines
This commit is contained in:
@ -37,6 +37,7 @@ Schema:
|
||||
"ipv6_addr": string, # [0]
|
||||
"ipv6_mask": integer, # [0]
|
||||
"ipv6_scope": string, # [0]
|
||||
"ipv6_scope_id": string, # [0]
|
||||
"ipv6_type": string, # [0]
|
||||
"rx_packets": integer,
|
||||
"rx_bytes": integer,
|
||||
@ -82,10 +83,19 @@ Schema:
|
||||
"ipv6: [
|
||||
{
|
||||
"address": string,
|
||||
"scope_id": string,
|
||||
"mask": integer,
|
||||
"scope": string,
|
||||
"type": string
|
||||
}
|
||||
],
|
||||
"lanes": [
|
||||
{
|
||||
"lane": integer,
|
||||
"rx_power_mw": float,
|
||||
"rx_power_dbm": float,
|
||||
"tx_bias_ma": float
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
@ -142,6 +152,7 @@ Examples:
|
||||
"ipv6": [
|
||||
{
|
||||
"address": "fe80::c1cb:715d:bc3e:b8a0",
|
||||
"scope_id": null,
|
||||
"mask": 64,
|
||||
"scope": "0x20",
|
||||
"type": "link"
|
||||
@ -190,6 +201,7 @@ Examples:
|
||||
"ipv6": [
|
||||
{
|
||||
"address": "fe80::c1cb:715d:bc3e:b8a0",
|
||||
"scope_id": null,
|
||||
"mask": "64",
|
||||
"scope": "0x20",
|
||||
"type": "link"
|
||||
@ -207,7 +219,7 @@ import jc.utils
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '2.0'
|
||||
version = '2.1'
|
||||
description = '`ifconfig` command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -237,8 +249,9 @@ def _process(proc_data: List[JSONDictType]) -> List[JSONDictType]:
|
||||
int_list = {
|
||||
'flags', 'mtu', 'ipv6_mask', 'rx_packets', 'rx_bytes', 'rx_errors', 'rx_dropped',
|
||||
'rx_overruns', 'rx_frame', 'tx_packets', 'tx_bytes', 'tx_errors', 'tx_dropped',
|
||||
'tx_overruns', 'tx_carrier', 'tx_collisions', 'metric', 'nd6_options'
|
||||
'tx_overruns', 'tx_carrier', 'tx_collisions', 'metric', 'nd6_options', 'lane'
|
||||
}
|
||||
float_list = {'rx_power_mw', 'rx_power_dbm', 'tx_bias_ma'}
|
||||
|
||||
for entry in proc_data:
|
||||
for key in entry:
|
||||
@ -291,6 +304,15 @@ def _process(proc_data: List[JSONDictType]) -> List[JSONDictType]:
|
||||
if 'mask' in ip_address:
|
||||
ip_address['mask'] = jc.utils.convert_to_int(ip_address['mask']) # type: ignore
|
||||
|
||||
# conversions for list of lanes
|
||||
if 'lanes' in entry:
|
||||
for lane_item in entry['lanes']: # type: ignore
|
||||
for key in lane_item:
|
||||
if key in int_list:
|
||||
lane_item[key] = jc.utils.convert_to_int(lane_item[key]) # type: ignore
|
||||
if key in float_list:
|
||||
lane_item[key] = jc.utils.convert_to_float(lane_item[key]) # type: ignore
|
||||
|
||||
# final conversions
|
||||
if entry.get('media_flags', None):
|
||||
entry['media_flags'] = entry['media_flags'].split(',') # type: ignore
|
||||
@ -371,6 +393,7 @@ def parse(
|
||||
interface_item: Dict = interface_obj.copy()
|
||||
ipv4_info: List = []
|
||||
ipv6_info: List = []
|
||||
lane_info: List = []
|
||||
|
||||
# Below regular expression patterns are based off of the work of:
|
||||
# https://github.com/KnightWhoSayNi/ifconfig-parser
|
||||
@ -527,7 +550,7 @@ def parse(
|
||||
re_freebsd_ipv6 = re.compile(r'''
|
||||
\s?inet6\s(?P<address>.*?)
|
||||
(?:\%(?P<scope_id>\w+\d+))?\s
|
||||
prefixlen\s(?P<mask>\d+).*?(?=scopeid|$)
|
||||
prefixlen\s(?P<mask>\d+).*?(?=scopeid|$) # positive lookahead for scopeid or end of line
|
||||
(?:scopeid\s(?P<scope>0x\w+))?
|
||||
''', re.IGNORECASE | re.VERBOSE
|
||||
)
|
||||
@ -582,6 +605,15 @@ def parse(
|
||||
''', re.IGNORECASE | re.VERBOSE
|
||||
)
|
||||
|
||||
# this pattern is special since it is used to build the lane_info list
|
||||
re_freebsd_lane = re.compile(r'''
|
||||
lane\s(?P<lane>\d+):\s
|
||||
RX\spower:\s(?P<rx_power_mw>\S+)\smW\s
|
||||
\((?P<rx_power_dbm>\S+)\sdBm\)\s
|
||||
TX\sbias:\s(?P<tx_bias_ma>\S+)
|
||||
''', re.IGNORECASE | re.VERBOSE
|
||||
)
|
||||
|
||||
re_linux = [
|
||||
re_linux_interface, re_linux_ipv4, re_linux_ipv6, re_linux_state, re_linux_rx, re_linux_tx,
|
||||
re_linux_bytes, re_linux_tx_stats
|
||||
@ -612,10 +644,13 @@ def parse(
|
||||
interface_item['ipv4'] = ipv4_info
|
||||
if ipv6_info:
|
||||
interface_item['ipv6'] = ipv6_info
|
||||
if lane_info:
|
||||
interface_item['lanes'] = lane_info
|
||||
raw_output.append(interface_item)
|
||||
interface_item = interface_obj.copy()
|
||||
ipv4_info = []
|
||||
ipv6_info = []
|
||||
lane_info = []
|
||||
|
||||
interface_item.update(interface_match.groupdict())
|
||||
continue
|
||||
@ -668,6 +703,12 @@ def parse(
|
||||
ipv6_info.append(ipv6_match.groupdict())
|
||||
continue
|
||||
|
||||
# lane information lines
|
||||
lane_match = re.search(re_freebsd_lane, line)
|
||||
if lane_match:
|
||||
lane_info.append(lane_match.groupdict())
|
||||
continue
|
||||
|
||||
# All other lines
|
||||
other_match = _bundle_match(re_linux + re_openbsd + re_freebsd, line)
|
||||
if other_match:
|
||||
@ -679,6 +720,8 @@ def parse(
|
||||
interface_item['ipv4'] = ipv4_info
|
||||
if ipv6_info:
|
||||
interface_item['ipv6'] = ipv6_info
|
||||
if lane_info:
|
||||
interface_item['lanes'] = lane_info
|
||||
raw_output.append(interface_item)
|
||||
|
||||
return raw_output if raw else _process(raw_output)
|
||||
|
@ -1 +1 @@
|
||||
[{"name":"cxl3","flags":8843,"state":["UP","BROADCAST","RUNNING","SIMPLEX","MULTICAST"],"mtu":1500,"type":null,"mac_addr":"00:07:43:3d:b7:70","ipv4_addr":null,"ipv4_mask":null,"ipv4_bcast":null,"ipv6_addr":null,"ipv6_mask":null,"ipv6_scope":null,"ipv6_type":null,"metric":0,"rx_packets":null,"rx_errors":null,"rx_dropped":null,"rx_overruns":null,"rx_frame":null,"tx_packets":null,"tx_errors":null,"tx_dropped":null,"tx_overruns":null,"tx_carrier":null,"tx_collisions":null,"rx_bytes":null,"tx_bytes":null,"options":"6ec07bb","options_flags":["RXCSUM","TXCSUM","VLAN_MTU","VLAN_HWTAGGING","JUMBO_MTU","VLAN_HWCSUM","TSO4","TSO6","LRO","VLAN_HWTSO","LINKSTATE","RXCSUM_IPV6","TXCSUM_IPV6","HWRXTSTMP","NOMAP"],"hw_address":"00:07:43:3d:b7:88","media":"Ethernet 10Gbase-LR","media_flags":["full-duplex","rxpause","txpause"],"status":"active","nd6_options":29,"nd6_flags":["PERFORMNUD","IFDISABLED","AUTO_LINKLOCAL"],"plugged":"SFP/SFP+/SFP28 10G Base-LR (LC)","vendor":"INNOLIGHT","vendor_pn":"TR-PX13L-N00","vendor_sn":"INJBL0431986","vendor_date":"2020-01-04","module_temperature":"21.20 C","module_voltage":"3.16 Volts"}]
|
||||
[{"name":"cxl3","flags":8843,"state":["UP","BROADCAST","RUNNING","SIMPLEX","MULTICAST"],"mtu":1500,"type":null,"mac_addr":"00:07:43:3d:b7:70","ipv4_addr":null,"ipv4_mask":null,"ipv4_bcast":null,"ipv6_addr":null,"ipv6_mask":null,"ipv6_scope":null,"ipv6_type":null,"metric":0,"rx_packets":null,"rx_errors":null,"rx_dropped":null,"rx_overruns":null,"rx_frame":null,"tx_packets":null,"tx_errors":null,"tx_dropped":null,"tx_overruns":null,"tx_carrier":null,"tx_collisions":null,"rx_bytes":null,"tx_bytes":null,"options":"6ec07bb","options_flags":["RXCSUM","TXCSUM","VLAN_MTU","VLAN_HWTAGGING","JUMBO_MTU","VLAN_HWCSUM","TSO4","TSO6","LRO","VLAN_HWTSO","LINKSTATE","RXCSUM_IPV6","TXCSUM_IPV6","HWRXTSTMP","NOMAP"],"hw_address":"00:07:43:3d:b7:88","media":"Ethernet 10Gbase-LR","media_flags":["full-duplex","rxpause","txpause"],"status":"active","nd6_options":29,"nd6_flags":["PERFORMNUD","IFDISABLED","AUTO_LINKLOCAL"],"plugged":"SFP/SFP+/SFP28 10G Base-LR (LC)","vendor":"INNOLIGHT","vendor_pn":"TR-PX13L-N00","vendor_sn":"INJBL0431986","vendor_date":"2020-01-04","module_temperature":"21.20 C","module_voltage":"3.16 Volts","lanes":[{"lane":1,"rx_power_mw":0.49,"rx_power_dbm":-3.1,"tx_bias_ma":23.85}]}]
|
||||
|
@ -1 +1 @@
|
||||
[{"name":"cxl3","flags":8843,"state":["UP","BROADCAST","RUNNING","SIMPLEX","MULTICAST"],"mtu":1500,"type":null,"mac_addr":"00:07:43:3d:b7:70","ipv4_addr":null,"ipv4_mask":null,"ipv4_bcast":null,"ipv6_addr":null,"ipv6_mask":null,"ipv6_scope":null,"ipv6_type":null,"metric":0,"rx_packets":null,"rx_errors":null,"rx_dropped":null,"rx_overruns":null,"rx_frame":null,"tx_packets":null,"tx_errors":null,"tx_dropped":null,"tx_overruns":null,"tx_carrier":null,"tx_collisions":null,"rx_bytes":null,"tx_bytes":null,"options":"6ec07bb","options_flags":["RXCSUM","TXCSUM","VLAN_MTU","VLAN_HWTAGGING","JUMBO_MTU","VLAN_HWCSUM","TSO4","TSO6","LRO","VLAN_HWTSO","LINKSTATE","RXCSUM_IPV6","TXCSUM_IPV6","HWRXTSTMP","NOMAP"],"hw_address":"00:07:43:3d:b7:88","media":null,"media_flags":null,"status":"active","nd6_options":29,"nd6_flags":["PERFORMNUD","IFDISABLED","AUTO_LINKLOCAL"],"plugged":"SFP/SFP+/SFP28 10G Base-LR (LC)","vendor":"INNOLIGHT","vendor_pn":"TR-PX13L-N00","vendor_sn":"INJBL0431986","vendor_date":"2020-01-04","module_temperature":"21.20 C","module_voltage":"3.16 Volts"}]
|
||||
[{"name":"cxl3","flags":8843,"state":["UP","BROADCAST","RUNNING","SIMPLEX","MULTICAST"],"mtu":1500,"type":null,"mac_addr":"00:07:43:3d:b7:70","ipv4_addr":null,"ipv4_mask":null,"ipv4_bcast":null,"ipv6_addr":null,"ipv6_mask":null,"ipv6_scope":null,"ipv6_type":null,"metric":0,"rx_packets":null,"rx_errors":null,"rx_dropped":null,"rx_overruns":null,"rx_frame":null,"tx_packets":null,"tx_errors":null,"tx_dropped":null,"tx_overruns":null,"tx_carrier":null,"tx_collisions":null,"rx_bytes":null,"tx_bytes":null,"options":"6ec07bb","options_flags":["RXCSUM","TXCSUM","VLAN_MTU","VLAN_HWTAGGING","JUMBO_MTU","VLAN_HWCSUM","TSO4","TSO6","LRO","VLAN_HWTSO","LINKSTATE","RXCSUM_IPV6","TXCSUM_IPV6","HWRXTSTMP","NOMAP"],"hw_address":"00:07:43:3d:b7:88","media":null,"media_flags":null,"status":"active","nd6_options":29,"nd6_flags":["PERFORMNUD","IFDISABLED","AUTO_LINKLOCAL"],"plugged":"SFP/SFP+/SFP28 10G Base-LR (LC)","vendor":"INNOLIGHT","vendor_pn":"TR-PX13L-N00","vendor_sn":"INJBL0431986","vendor_date":"2020-01-04","module_temperature":"21.20 C","module_voltage":"3.16 Volts","lanes":[{"lane":1,"rx_power_mw":0.49,"rx_power_dbm":-3.1,"tx_bias_ma":23.85}]}]
|
||||
|
1
tests/fixtures/freebsd12/ifconfig-extra-fields4.json
vendored
Normal file
1
tests/fixtures/freebsd12/ifconfig-extra-fields4.json
vendored
Normal file
@ -0,0 +1 @@
|
||||
[{"name":"mce0","flags":8863,"state":["UP","BROADCAST","RUNNING","SIMPLEX","MULTICAST"],"mtu":1500,"type":null,"mac_addr":"0c:42:a1:2b:03:74","ipv4_addr":"23.246.26.130","ipv4_mask":"255.255.255.128","ipv4_bcast":"23.246.26.255","ipv6_addr":"2a00:86c0:1026:1026::130","ipv6_mask":64,"ipv6_scope":null,"ipv6_type":null,"metric":0,"rx_packets":null,"rx_errors":null,"rx_dropped":null,"rx_overruns":null,"rx_frame":null,"tx_packets":null,"tx_errors":null,"tx_dropped":null,"tx_overruns":null,"tx_carrier":null,"tx_collisions":null,"rx_bytes":null,"tx_bytes":null,"ipv6_scope_id":null,"media":"Ethernet 100GBase-SR4","media_flags":["full-duplex","rxpause","txpause"],"status":"active","nd6_options":21,"nd6_flags":["PERFORMNUD","AUTO_LINKLOCAL"],"plugged":"QSFP28 Unknown (MPO 1x12 Parallel Optic)","vendor":"INNOLIGHT","vendor_pn":"TR-FC85S-N00","vendor_sn":"INKAT2930248","vendor_date":"2020-05-20","module_temperature":"49.41 C","module_voltage":"3.29 Volts","ipv4":[{"address":"23.246.26.130","mask":"255.255.255.128","broadcast":"23.246.26.255"}],"ipv6":[{"address":"fe80::e42:a1ff:fe2b:374","scope_id":"mce0","mask":64,"scope":"0x4"},{"address":"2a00:86c0:1026:1026::130","scope_id":null,"mask":64,"scope":null}],"lanes":[{"lane":1,"rx_power_mw":0.98,"rx_power_dbm":-0.11,"tx_bias_ma":7.6},{"lane":2,"rx_power_mw":0.98,"rx_power_dbm":-0.07,"tx_bias_ma":7.6},{"lane":3,"rx_power_mw":1.01,"rx_power_dbm":0.03,"tx_bias_ma":7.6},{"lane":4,"rx_power_mw":0.92,"rx_power_dbm":-0.34,"tx_bias_ma":7.6}]}]
|
16
tests/fixtures/freebsd12/ifconfig-extra-fields4.out
vendored
Normal file
16
tests/fixtures/freebsd12/ifconfig-extra-fields4.out
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
mce0: flags=8863<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
|
||||
options RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,TSO6,LRO,VLAN_HWFILTER,VLAN_HWTSO,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6,HWSTATS,TXRTLMT,HWRXTSTMP,MEXTPG,TXTLS4,TXTLS6,VXLAN_HWCSUM,VXLAN_HWTSO,TXTLS_RTLMT,RXTLS4,RXTLS6
|
||||
ether 0c:42:a1:2b:03:74
|
||||
inet 23.246.26.130 netmask 0xffffff80 broadcast 23.246.26.255
|
||||
inet6 fe80::e42:a1ff:fe2b:374%mce0 prefixlen 64 scopeid 0x4
|
||||
inet6 2a00:86c0:1026:1026::130 prefixlen 64
|
||||
media: Ethernet 100GBase-SR4 <full-duplex,rxpause,txpause>
|
||||
status: active
|
||||
nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
|
||||
plugged: QSFP28 Unknown (MPO 1x12 Parallel Optic)
|
||||
vendor: INNOLIGHT PN: TR-FC85S-N00 SN: INKAT2930248 DATE: 2020-05-20
|
||||
module temperature: 49.41 C voltage: 3.29 Volts
|
||||
lane 1: RX power: 0.98 mW (-0.11 dBm) TX bias: 7.60 mA
|
||||
lane 2: RX power: 0.98 mW (-0.07 dBm) TX bias: 7.60 mA
|
||||
lane 3: RX power: 1.01 mW (0.03 dBm) TX bias: 7.60 mA
|
||||
lane 4: RX power: 0.92 mW (-0.34 dBm) TX bias: 7.60 mA
|
@ -36,6 +36,9 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/ifconfig-extra-fields3.out'), 'r', encoding='utf-8') as f:
|
||||
osx_freebsd12_ifconfig_extra_fields3 = f.read()
|
||||
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/ifconfig-extra-fields4.out'), 'r', encoding='utf-8') as f:
|
||||
osx_freebsd12_ifconfig_extra_fields4 = f.read()
|
||||
|
||||
# output
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ifconfig.json'), 'r', encoding='utf-8') as f:
|
||||
centos_7_7_ifconfig_json = json.loads(f.read())
|
||||
@ -64,6 +67,9 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/ifconfig-extra-fields3.json'), 'r', encoding='utf-8') as f:
|
||||
freebsd12_ifconfig_extra_fields3_json = json.loads(f.read())
|
||||
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/ifconfig-extra-fields4.json'), 'r', encoding='utf-8') as f:
|
||||
freebsd12_ifconfig_extra_fields4_json = json.loads(f.read())
|
||||
|
||||
def test_ifconfig_nodata(self):
|
||||
"""
|
||||
Test 'ifconfig' with no data
|
||||
@ -124,5 +130,11 @@ class MyTests(unittest.TestCase):
|
||||
"""
|
||||
self.assertEqual(jc.parsers.ifconfig.parse(self.osx_freebsd12_ifconfig_extra_fields3, quiet=True), self.freebsd12_ifconfig_extra_fields3_json)
|
||||
|
||||
def test_ifconfig_freebsd_extra_fields4(self):
|
||||
"""
|
||||
Test 'ifconfig' on freebsd12 with lane fields
|
||||
"""
|
||||
self.assertEqual(jc.parsers.ifconfig.parse(self.osx_freebsd12_ifconfig_extra_fields4, quiet=True), self.freebsd12_ifconfig_extra_fields4_json)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
Reference in New Issue
Block a user