77#####################################################################
88
99import json
10- import argparse
10+ import click
1111import datetime
1212import os .path
1313import sys
@@ -102,23 +102,40 @@ def build_json(port, cnstat, voq=False):
102102 out .update (ports_stats (k ))
103103 return out
104104
105+ class QueuestatWrapper (object ):
106+ """A wrapper to execute queuestat cmd over the correct namespaces"""
107+ def __init__ (self , namespace , voq ):
108+ self .namespace = namespace
109+ self .voq = voq
105110
106- class Queuestat ( object ):
107- def __init__ ( self , namespace , voq = False ):
111+ # Initialize the multi-asic namespace
112+ self . multi_asic = multi_asic_util . MultiAsic ( constants . DISPLAY_ALL , namespace_option = namespace )
108113 self .db = None
109- self .multi_asic = multi_asic_util .MultiAsic (constants .DISPLAY_ALL , namespace )
110- if namespace is not None :
111- for ns in self .multi_asic .get_ns_list_based_on_options ():
112- self .db = multi_asic .connect_to_all_dbs_for_ns (ns )
114+
115+ @multi_asic_util .run_on_multi_asic
116+ def run (self , save_fresh_stats , port_to_show_stats , json_opt , non_zero ):
117+ queuestat = Queuestat (self .multi_asic .current_namespace , self .db , self .voq )
118+ if save_fresh_stats :
119+ queuestat .save_fresh_stats ()
120+ return
121+
122+ if port_to_show_stats != None :
123+ queuestat .get_print_port_stat (port_to_show_stats , json_opt , non_zero )
113124 else :
114- self .db = SonicV2Connector (use_unix_socket_path = False )
115- self .db .connect (self .db .COUNTERS_DB )
125+ queuestat .get_print_all_stat (json_opt , non_zero )
126+
127+
128+ class Queuestat (object ):
129+ def __init__ (self , namespace , db , voq = False ):
130+ self .db = db
116131 self .voq = voq
132+ self .namespace = namespace
133+ self .namespace_str = f" for { namespace } " if namespace else ''
117134
118135 def get_queue_port (table_id ):
119136 port_table_id = self .db .get (self .db .COUNTERS_DB , COUNTERS_QUEUE_PORT_MAP , table_id )
120137 if port_table_id is None :
121- print ("Port is not available!" , table_id )
138+ print (f "Port is not available{ self . namespace_str } !" , table_id )
122139 sys .exit (1 )
123140
124141 return port_table_id
@@ -130,7 +147,7 @@ class Queuestat(object):
130147 self .counter_port_name_map = self .db .get_all (self .db .COUNTERS_DB , COUNTERS_PORT_NAME_MAP )
131148
132149 if self .counter_port_name_map is None :
133- print ("COUNTERS_PORT_NAME_MAP is empty!" )
150+ print (f "COUNTERS_PORT_NAME_MAP is empty{ self . namespace_str } !" )
134151 sys .exit (1 )
135152
136153 self .port_queues_map = {}
@@ -148,7 +165,7 @@ class Queuestat(object):
148165 counter_queue_name_map = self .db .get_all (self .db .COUNTERS_DB , COUNTERS_QUEUE_NAME_MAP )
149166
150167 if counter_queue_name_map is None :
151- print ("COUNTERS_QUEUE_NAME_MAP is empty!" )
168+ print (f "COUNTERS_QUEUE_NAME_MAP is empty{ self . namespace_str } !" )
152169 sys .exit (1 )
153170
154171 for queue in counter_queue_name_map :
@@ -166,15 +183,15 @@ class Queuestat(object):
166183 def get_queue_index (table_id ):
167184 queue_index = self .db .get (self .db .COUNTERS_DB , COUNTERS_QUEUE_INDEX_MAP , table_id )
168185 if queue_index is None :
169- print ("Queue index is not available!" , table_id )
186+ print (f "Queue index is not available{ self . namespace_str } !" , table_id )
170187 sys .exit (1 )
171188
172189 return queue_index
173190
174191 def get_queue_type (table_id ):
175192 queue_type = self .db .get (self .db .COUNTERS_DB , COUNTERS_QUEUE_TYPE_MAP , table_id )
176193 if queue_type is None :
177- print ("Queue Type is not available!" , table_id )
194+ print (f "Queue Type is not available{ self . namespace_str } !" , table_id )
178195 sys .exit (1 )
179196 elif queue_type == SAI_QUEUE_TYPE_MULTICAST :
180197 return QUEUE_TYPE_MC
@@ -185,7 +202,7 @@ class Queuestat(object):
185202 elif queue_type == SAI_QUEUE_TYPE_ALL :
186203 return QUEUE_TYPE_ALL
187204 else :
188- print ("Queue Type is invalid:" , table_id , queue_type )
205+ print (f "Queue Type is invalid{ self . namespace_str } :" , table_id , queue_type )
189206 sys .exit (1 )
190207
191208 if self .voq :
@@ -255,6 +272,7 @@ class Queuestat(object):
255272 else :
256273 hdr = voq_header if self .voq else header
257274 if table :
275+ print (f"For namespace { self .namespace } :" )
258276 print (tabulate (table , hdr , tablefmt = 'simple' , stralign = 'right' ))
259277 print ()
260278
@@ -314,7 +332,7 @@ class Queuestat(object):
314332 else :
315333 hdr = voq_header if self .voq else header
316334 if table :
317- print (port + " Last cached time was " + str (cnstat_old_dict .get ('time' )))
335+ print (port + f " Last cached time{ self . namespace_str } was " + str (cnstat_old_dict .get ('time' )))
318336 print (tabulate (table , hdr , tablefmt = 'simple' , stralign = 'right' ))
319337 print ()
320338
@@ -370,7 +388,7 @@ class Queuestat(object):
370388 json_output [port ].update ({"cached_time" :cnstat_cached_dict .get ('time' )})
371389 json_output .update (self .cnstat_diff_print (port , cnstat_dict , cnstat_cached_dict , json_opt , non_zero ))
372390 else :
373- print ("Last cached time was " + str (cnstat_cached_dict .get ('time' )))
391+ print (f "Last cached time{ self . namespace_str } was " + str (cnstat_cached_dict .get ('time' )))
374392 self .cnstat_diff_print (port , cnstat_dict , cnstat_cached_dict , json_opt , non_zero )
375393 except IOError as e :
376394 print (e .errno , e )
@@ -395,38 +413,33 @@ class Queuestat(object):
395413 else :
396414 print ("Clear and update saved counters for " + port )
397415
398- def main ():
416+
417+ @click .command ()
418+ @click .option ('-p' , '--port' , type = str , help = 'Show the queue conters for just one port' , default = None )
419+ @click .option ('-c' , '--clear' , is_flag = True , default = False , help = 'Clear previous stats and save new ones' )
420+ @click .option ('-d' , '--delete' , is_flag = True , default = False , help = 'Delete saved stats' )
421+ @click .option ('-j' , '--json_opt' , is_flag = True , default = False , help = 'Print in JSON format' )
422+ @click .option ('-V' , '--voq' , is_flag = True , default = False , help = 'display voq stats' )
423+ @click .option ('-nz' ,'--non_zero' , is_flag = True , default = False , help = 'Display non-zero queue counters' )
424+ @click .option ('-n' , '--namespace' , type = click .Choice (multi_asic .get_namespace_list ()), help = 'Display queuecounters for a specific namespace name or skip for all' , default = None )
425+ @click .version_option (version = '1.0' )
426+ def main (port , clear , delete , json_opt , voq , non_zero , namespace ):
427+ """
428+ Examples:
429+ queuestat
430+ queuestat -p Ethernet0
431+ queuestat -c
432+ queuestat -d
433+ queuestat -p Ethernet0 -n asic0
434+ """
435+
399436 global cnstat_dir
400437 global cnstat_fqn_file
401438
402- parser = argparse .ArgumentParser (description = 'Display the queue state and counters' ,
403- formatter_class = argparse .RawTextHelpFormatter ,
404- epilog = """
405- Examples:
406- queuestat
407- queuestat -p Ethernet0
408- queuestat -c
409- queuestat -d
410- """ )
411-
412- parser .add_argument ('-p' , '--port' , type = str , help = 'Show the queue conters for just one port' , default = None )
413- parser .add_argument ('-c' , '--clear' , action = 'store_true' , help = 'Clear previous stats and save new ones' )
414- parser .add_argument ('-d' , '--delete' , action = 'store_true' , help = 'Delete saved stats' )
415- parser .add_argument ('-v' , '--version' , action = 'version' , version = '%(prog)s 1.0' )
416- parser .add_argument ('-j' , '--json_opt' , action = 'store_true' , help = 'Print in JSON format' )
417- parser .add_argument ('-V' , '--voq' , action = 'store_true' , help = 'display voq stats' )
418- parser .add_argument ('-n' ,'--namespace' , default = None , help = 'Display queue counters for specific namespace' )
419- parser .add_argument ('-nz' ,'--non_zero' , action = 'store_true' , help = 'Display non-zero queue counters' )
420- args = parser .parse_args ()
421-
422- save_fresh_stats = args .clear
423- delete_stats = args .delete
424- voq = args .voq
425- json_opt = args .json_opt
426- namespace = args .namespace
427- non_zero = args .non_zero
428-
429- port_to_show_stats = args .port
439+ save_fresh_stats = clear
440+ delete_stats = delete
441+
442+ port_to_show_stats = port
430443
431444 cache = UserCache ()
432445
@@ -436,16 +449,8 @@ Examples:
436449 if delete_stats :
437450 cache .remove ()
438451
439- queuestat = Queuestat ( namespace , voq )
440-
441- if save_fresh_stats :
442- queuestat .save_fresh_stats ()
443- sys .exit (0 )
444-
445- if port_to_show_stats != None :
446- queuestat .get_print_port_stat (port_to_show_stats , json_opt , non_zero )
447- else :
448- queuestat .get_print_all_stat (json_opt , non_zero )
452+ queuestat_wrapper = QueuestatWrapper (namespace , voq )
453+ queuestat_wrapper .run (save_fresh_stats , port_to_show_stats , json_opt , non_zero )
449454
450455 sys .exit (0 )
451456
0 commit comments