@@ -309,7 +309,8 @@ def _get_counter_data(ifname: typing.Optional[str],
309
309
310
310
return ret
311
311
312
- def _get_kernel_data (raw , ifname = None , detail = False ):
312
+ def _get_kernel_data (raw , ifname = None , detail = False ,
313
+ statistics = False ):
313
314
if ifname :
314
315
# Check if the interface exists
315
316
if not interface_exists (ifname ):
@@ -325,11 +326,11 @@ def _get_kernel_data(raw, ifname = None, detail = False):
325
326
return kernel_interface , None
326
327
327
328
# 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 )
329
330
330
331
return kernel_interface , kernel_interface_out
331
332
332
- def _format_kernel_data (data , detail ):
333
+ def _format_kernel_data (data , detail , statistics ):
333
334
output_list = []
334
335
podman_vrf = {}
335
336
tmpInfo = {}
@@ -378,6 +379,9 @@ def _format_kernel_data(data, detail):
378
379
if master .startswith ('pod-' ):
379
380
vrf = podman_vrf .get (master ).get ('vrf' , 'default' )
380
381
382
+ rx_stats = interface .get ('stats64' , {}).get ('rx' )
383
+ tx_stats = interface .get ('stats64' , {}).get ('tx' )
384
+
381
385
sl_status = ('A' if not 'UP' in interface ['flags' ] else 'u' ) + '/' + ('D' if interface ['operstate' ] == 'DOWN' else 'u' )
382
386
383
387
# Generate temporary dict to hold data
@@ -392,8 +396,6 @@ def _format_kernel_data(data, detail):
392
396
tmpInfo ['alternate_names' ] = interface .get ('altnames' , '' )
393
397
tmpInfo ['minimum_mtu' ] = interface .get ('min_mtu' , '' )
394
398
tmpInfo ['maximum_mtu' ] = interface .get ('max_mtu' , '' )
395
- rx_stats = interface .get ('stats64' , {}).get ('rx' )
396
- tx_stats = interface .get ('stats64' , {}).get ('tx' )
397
399
tmpInfo ['rx_packets' ] = rx_stats .get ('packets' , "" )
398
400
tmpInfo ['rx_bytes' ] = rx_stats .get ('bytes' , "" )
399
401
tmpInfo ['rx_errors' ] = rx_stats .get ('errors' , "" )
@@ -407,30 +409,37 @@ def _format_kernel_data(data, detail):
407
409
tmpInfo ['tx_carrier_errors' ] = tx_stats .get ('carrier_errors' , "" )
408
410
tmpInfo ['tx_collisions' ] = tx_stats .get ('collisions' , "" )
409
411
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
+
410
430
# Generate output list; detail adds more fields
411
431
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 []) ,
418
438
* ([tmpInfo ['device' ]] if detail else []),
419
439
* (['\n ' .join (tmpInfo ['alternate_names' ])] if detail else []),
420
440
* ([tmpInfo ['minimum_mtu' ]] if detail else []),
421
441
* ([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 [])])
434
443
435
444
return output_list
436
445
@@ -583,27 +592,33 @@ def _format_show_counters(data: list):
583
592
print (output )
584
593
return output
585
594
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 )
588
598
589
599
# Return early if raw
590
600
if raw :
591
601
return raw_data
592
602
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' ]
595
617
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' ]
602
618
603
619
if detail :
604
620
detailed_output (data , detail_header )
605
621
else :
606
- print ('Codes: S - State, L - Link, u - Up, D - Down, A - Admin Down' )
607
622
print (tabulate (data , headers ))
608
623
609
624
def _show_raw (data : list , intf_name : str ):
0 commit comments