7
7
#####################################################################
8
8
9
9
import json
10
- import argparse
10
+ import click
11
11
import datetime
12
12
import os .path
13
13
import sys
@@ -102,23 +102,40 @@ def build_json(port, cnstat, voq=False):
102
102
out .update (ports_stats (k ))
103
103
return out
104
104
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
105
110
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 )
108
113
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 )
113
124
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
116
131
self .voq = voq
132
+ self .namespace = namespace
133
+ self .namespace_str = f" for { namespace } " if namespace else ''
117
134
118
135
def get_queue_port (table_id ):
119
136
port_table_id = self .db .get (self .db .COUNTERS_DB , COUNTERS_QUEUE_PORT_MAP , table_id )
120
137
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 )
122
139
sys .exit (1 )
123
140
124
141
return port_table_id
@@ -130,7 +147,7 @@ class Queuestat(object):
130
147
self .counter_port_name_map = self .db .get_all (self .db .COUNTERS_DB , COUNTERS_PORT_NAME_MAP )
131
148
132
149
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 } !" )
134
151
sys .exit (1 )
135
152
136
153
self .port_queues_map = {}
@@ -148,7 +165,7 @@ class Queuestat(object):
148
165
counter_queue_name_map = self .db .get_all (self .db .COUNTERS_DB , COUNTERS_QUEUE_NAME_MAP )
149
166
150
167
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 } !" )
152
169
sys .exit (1 )
153
170
154
171
for queue in counter_queue_name_map :
@@ -166,15 +183,15 @@ class Queuestat(object):
166
183
def get_queue_index (table_id ):
167
184
queue_index = self .db .get (self .db .COUNTERS_DB , COUNTERS_QUEUE_INDEX_MAP , table_id )
168
185
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 )
170
187
sys .exit (1 )
171
188
172
189
return queue_index
173
190
174
191
def get_queue_type (table_id ):
175
192
queue_type = self .db .get (self .db .COUNTERS_DB , COUNTERS_QUEUE_TYPE_MAP , table_id )
176
193
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 )
178
195
sys .exit (1 )
179
196
elif queue_type == SAI_QUEUE_TYPE_MULTICAST :
180
197
return QUEUE_TYPE_MC
@@ -185,7 +202,7 @@ class Queuestat(object):
185
202
elif queue_type == SAI_QUEUE_TYPE_ALL :
186
203
return QUEUE_TYPE_ALL
187
204
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 )
189
206
sys .exit (1 )
190
207
191
208
if self .voq :
@@ -255,6 +272,7 @@ class Queuestat(object):
255
272
else :
256
273
hdr = voq_header if self .voq else header
257
274
if table :
275
+ print (f"For namespace { self .namespace } :" )
258
276
print (tabulate (table , hdr , tablefmt = 'simple' , stralign = 'right' ))
259
277
print ()
260
278
@@ -314,7 +332,7 @@ class Queuestat(object):
314
332
else :
315
333
hdr = voq_header if self .voq else header
316
334
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' )))
318
336
print (tabulate (table , hdr , tablefmt = 'simple' , stralign = 'right' ))
319
337
print ()
320
338
@@ -370,7 +388,7 @@ class Queuestat(object):
370
388
json_output [port ].update ({"cached_time" :cnstat_cached_dict .get ('time' )})
371
389
json_output .update (self .cnstat_diff_print (port , cnstat_dict , cnstat_cached_dict , json_opt , non_zero ))
372
390
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' )))
374
392
self .cnstat_diff_print (port , cnstat_dict , cnstat_cached_dict , json_opt , non_zero )
375
393
except IOError as e :
376
394
print (e .errno , e )
@@ -395,38 +413,33 @@ class Queuestat(object):
395
413
else :
396
414
print ("Clear and update saved counters for " + port )
397
415
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
+
399
436
global cnstat_dir
400
437
global cnstat_fqn_file
401
438
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
430
443
431
444
cache = UserCache ()
432
445
@@ -436,16 +449,8 @@ Examples:
436
449
if delete_stats :
437
450
cache .remove ()
438
451
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 )
449
454
450
455
sys .exit (0 )
451
456
0 commit comments