Skip to content

Commit b1b4545

Browse files
authored
Merge pull request #4680 from l0crian1/show-kernel-stats
T7742: Add 'show interfaces kernel statistics' command
2 parents 8dc61c3 + 13a37b8 commit b1b4545

File tree

2 files changed

+63
-36
lines changed

2 files changed

+63
-36
lines changed

op-mode-definitions/show-interfaces.xml.in

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<children>
4343
<leafNode name="detail">
4444
<properties>
45-
<help>Show system interface in JSON format</help>
45+
<help>Show system interface details</help>
4646
</properties>
4747
<command>${vyos_op_scripts_dir}/interfaces.py show_kernel --intf-name=$4 --detail</command>
4848
</leafNode>
@@ -52,11 +52,17 @@
5252
</properties>
5353
<command>${vyos_op_scripts_dir}/interfaces.py show_kernel --intf-name=$4 --raw</command>
5454
</leafNode>
55+
<leafNode name="statistics">
56+
<properties>
57+
<help>Show system interface statistics</help>
58+
</properties>
59+
<command>${vyos_op_scripts_dir}/interfaces.py show_kernel --intf-name=$4 --statistics</command>
60+
</leafNode>
5561
</children>
5662
</virtualTagNode>
5763
<leafNode name="detail">
5864
<properties>
59-
<help>Show system interface in JSON format</help>
65+
<help>Show system interface details</help>
6066
</properties>
6167
<command>${vyos_op_scripts_dir}/interfaces.py show_kernel --detail</command>
6268
</leafNode>
@@ -66,6 +72,12 @@
6672
</properties>
6773
<command>${vyos_op_scripts_dir}/interfaces.py show_kernel --raw</command>
6874
</leafNode>
75+
<leafNode name="statistics">
76+
<properties>
77+
<help>Show system interface statistics</help>
78+
</properties>
79+
<command>${vyos_op_scripts_dir}/interfaces.py show_kernel --statistics</command>
80+
</leafNode>
6981
</children>
7082
</node>
7183
</children>

src/op_mode/interfaces.py

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ def _get_counter_data(ifname: typing.Optional[str],
309309

310310
return ret
311311

312-
def _get_kernel_data(raw, ifname = None, detail = False):
312+
def _get_kernel_data(raw, ifname = None, detail = False,
313+
statistics = False):
313314
if ifname:
314315
# Check if the interface exists
315316
if not interface_exists(ifname):
@@ -325,11 +326,11 @@ def _get_kernel_data(raw, ifname = None, detail = False):
325326
return kernel_interface, None
326327

327328
# Format the kernel data
328-
kernel_interface_out = _format_kernel_data(kernel_interface, detail)
329+
kernel_interface_out = _format_kernel_data(kernel_interface, detail, statistics)
329330

330331
return kernel_interface, kernel_interface_out
331332

332-
def _format_kernel_data(data, detail):
333+
def _format_kernel_data(data, detail, statistics):
333334
output_list = []
334335
podman_vrf = {}
335336
tmpInfo = {}
@@ -378,6 +379,9 @@ def _format_kernel_data(data, detail):
378379
if master.startswith('pod-'):
379380
vrf = podman_vrf.get(master).get('vrf', 'default')
380381

382+
rx_stats = interface.get('stats64', {}).get('rx')
383+
tx_stats = interface.get('stats64', {}).get('tx')
384+
381385
sl_status = ('A' if not 'UP' in interface['flags'] else 'u') + '/' + ('D' if interface['operstate'] == 'DOWN' else 'u')
382386

383387
# Generate temporary dict to hold data
@@ -392,8 +396,6 @@ def _format_kernel_data(data, detail):
392396
tmpInfo['alternate_names'] = interface.get('altnames', '')
393397
tmpInfo['minimum_mtu'] = interface.get('min_mtu', '')
394398
tmpInfo['maximum_mtu'] = interface.get('max_mtu', '')
395-
rx_stats = interface.get('stats64', {}).get('rx')
396-
tx_stats = interface.get('stats64', {}).get('tx')
397399
tmpInfo['rx_packets'] = rx_stats.get('packets', "")
398400
tmpInfo['rx_bytes'] = rx_stats.get('bytes', "")
399401
tmpInfo['rx_errors'] = rx_stats.get('errors', "")
@@ -407,30 +409,37 @@ def _format_kernel_data(data, detail):
407409
tmpInfo['tx_carrier_errors'] = tx_stats.get('carrier_errors', "")
408410
tmpInfo['tx_collisions'] = tx_stats.get('collisions', "")
409411

412+
# Order the stats based on 'detail' or 'statistics'
413+
if detail:
414+
stat_keys = [
415+
"rx_packets", "rx_bytes", "rx_errors", "rx_dropped",
416+
"rx_over_errors", "multicast",
417+
"tx_packets", "tx_bytes", "tx_errors", "tx_dropped",
418+
"tx_carrier_errors", "tx_collisions",
419+
]
420+
elif statistics:
421+
stat_keys = [
422+
"rx_packets", "rx_bytes", "tx_packets", "tx_bytes",
423+
"rx_dropped", "tx_dropped", "rx_errors", "tx_errors",
424+
]
425+
else:
426+
stat_keys = []
427+
428+
stat_list = [tmpInfo.get(k, "") for k in stat_keys]
429+
410430
# Generate output list; detail adds more fields
411431
output_list.append([tmpInfo['ifname'],
412-
'\n'.join(tmpInfo['ip']),
413-
tmpInfo['mac'],
414-
tmpInfo['vrf'],
415-
tmpInfo['mtu'],
416-
tmpInfo['status'],
417-
tmpInfo['description'],
432+
*(['\n'.join(tmpInfo['ip'])] if not statistics else []),
433+
*([tmpInfo['mac']] if not statistics else []),
434+
*([tmpInfo['vrf']] if not statistics else []),
435+
*([tmpInfo['mtu']] if not statistics else []),
436+
*([tmpInfo['status']] if not statistics else []),
437+
*([tmpInfo['description']] if not statistics else []),
418438
*([tmpInfo['device']] if detail else []),
419439
*(['\n'.join(tmpInfo['alternate_names'])] if detail else []),
420440
*([tmpInfo['minimum_mtu']] if detail else []),
421441
*([tmpInfo['maximum_mtu']] if detail else []),
422-
*([tmpInfo['rx_packets']] if detail else []),
423-
*([tmpInfo['rx_bytes']] if detail else []),
424-
*([tmpInfo['rx_errors']] if detail else []),
425-
*([tmpInfo['rx_dropped']] if detail else []),
426-
*([tmpInfo['rx_over_errors']] if detail else []),
427-
*([tmpInfo['multicast']] if detail else []),
428-
*([tmpInfo['tx_packets']] if detail else []),
429-
*([tmpInfo['tx_bytes']] if detail else []),
430-
*([tmpInfo['tx_errors']] if detail else []),
431-
*([tmpInfo['tx_dropped']] if detail else []),
432-
*([tmpInfo['tx_carrier_errors']] if detail else []),
433-
*([tmpInfo['tx_collisions']] if detail else [])])
442+
*(stat_list if any([detail, statistics]) else [])])
434443

435444
return output_list
436445

@@ -583,27 +592,33 @@ def _format_show_counters(data: list):
583592
print (output)
584593
return output
585594

586-
def show_kernel(raw: bool, intf_name: typing.Optional[str], detail: bool):
587-
raw_data, data = _get_kernel_data(raw, intf_name, detail)
595+
def show_kernel(raw: bool, intf_name: typing.Optional[str],
596+
detail: bool, statistics: bool):
597+
raw_data, data = _get_kernel_data(raw, intf_name, detail, statistics)
588598

589599
# Return early if raw
590600
if raw:
591601
return raw_data
592602

593-
# Normal headers; show interfaces kernel
594-
headers = ['Interface', 'IP Address', 'MAC', 'VRF', 'MTU', 'S/L', 'Description']
603+
if detail:
604+
# Detail headers; ex. show interfaces kernel detail; show interfaces kernel eth0 detail
605+
detail_header = ['Interface', 'IP Address', 'MAC', 'VRF', 'MTU', 'S/L', 'Description',
606+
'Device', 'Alternate Names','Minimum MTU', 'Maximum MTU', 'RX_Packets',
607+
'RX_Bytes', 'RX_Errors', 'RX_Dropped', 'Receive Overrun Errors', 'Received Multicast',
608+
'TX_Packets', 'TX_Bytes', 'TX_Errors', 'TX_Dropped', 'Transmit Carrier Errors',
609+
'Transmit Collisions']
610+
elif statistics:
611+
# Statistics headers; ex. show interfaces kernel statistics; show interfaces kernel eth0 statistics
612+
headers = ['Interface', 'Rx Packets', 'Rx Bytes', 'Tx Packets', 'Tx Bytes', 'Rx Dropped', 'Tx Dropped', 'Rx Errors', 'Tx Errors']
613+
else:
614+
# Normal headers; ex. show interfaces kernel; show interfaces kernel eth0
615+
print('Codes: S - State, L - Link, u - Up, D - Down, A - Admin Down')
616+
headers = ['Interface', 'IP Address', 'MAC', 'VRF', 'MTU', 'S/L', 'Description']
595617

596-
# Detail headers; show interfaces kernel detail
597-
detail_header = ['Interface', 'IP Address', 'MAC', 'VRF', 'MTU', 'S/L', 'Description',
598-
'Device', 'Alternate Names','Minimum MTU', 'Maximum MTU', 'RX_Packets',
599-
'RX_Bytes', 'RX_Errors', 'RX_Dropped', 'Receive Overrun Errors', 'Received Multicast',
600-
'TX_Packets', 'TX_Bytes', 'TX_Errors', 'TX_Dropped', 'Transmit Carrier Errors',
601-
'Transmit Collisions']
602618

603619
if detail:
604620
detailed_output(data, detail_header)
605621
else:
606-
print('Codes: S - State, L - Link, u - Up, D - Down, A - Admin Down')
607622
print(tabulate(data, headers))
608623

609624
def _show_raw(data: list, intf_name: str):

0 commit comments

Comments
 (0)