5
5
import subprocess
6
6
7
7
import utilities_common .cli as clicommon
8
+ import utilities_common .multi_asic as multi_asic_util
8
9
from sonic_py_common import logger
10
+ from sonic_py_common import multi_asic
9
11
from syslog_util import common as syslog_common
10
12
11
13
@@ -457,20 +459,46 @@ def delete(db, server_ip_address):
457
459
def rate_limit_host (db , interval , burst ):
458
460
""" Configure syslog rate limit for host """
459
461
syslog_common .rate_limit_validator (interval , burst )
460
- syslog_common .save_rate_limit_to_db (db , None , interval , burst , log )
462
+ syslog_common .save_rate_limit_to_db (db . cfgdb , None , interval , burst , log )
461
463
462
464
463
465
@syslog .command ("rate-limit-container" )
464
466
@click .argument ("service_name" , required = True )
465
467
@click .option ("-i" , "--interval" , help = "Configures syslog rate limit interval in seconds for specified containers" , type = click .IntRange (0 , 2147483647 ))
466
468
@click .option ("-b" , "--burst" , help = "Configures syslog rate limit burst in number of messages for specified containers" , type = click .IntRange (0 , 2147483647 ))
469
+ @click .option ('--namespace' , '-n' , 'namespace' , default = None ,
470
+ type = click .Choice (multi_asic_util .multi_asic_ns_choices () + ['default' ]),
471
+ show_default = True , help = 'Namespace name or all' )
467
472
@clicommon .pass_db
468
- def rate_limit_container (db , service_name , interval , burst ):
473
+ def rate_limit_container (db , service_name , interval , burst , namespace ):
469
474
""" Configure syslog rate limit for containers """
470
475
syslog_common .rate_limit_validator (interval , burst )
471
- feature_data = db .cfgdb .get_table (syslog_common .FEATURE_TABLE )
476
+ features = db .cfgdb .get_table (syslog_common .FEATURE_TABLE )
477
+ syslog_common .service_validator (features , service_name )
478
+
479
+ global_feature_data , per_ns_feature_data = syslog_common .extract_feature_data (features )
480
+ if not namespace :
481
+ # for all namespaces
482
+ for namespace , cfg_db in db .cfgdb_clients .items ():
483
+ if namespace == multi_asic .DEFAULT_NAMESPACE :
484
+ feature_data = global_feature_data
485
+ else :
486
+ feature_data = per_ns_feature_data
487
+ if service_name and service_name not in feature_data :
488
+ continue
489
+ syslog_common .service_validator (feature_data , service_name )
490
+ syslog_common .save_rate_limit_to_db (cfg_db , service_name , interval , burst , log )
491
+ return
492
+ elif namespace == 'default' :
493
+ # for default/global namespace only
494
+ namespace = multi_asic .DEFAULT_NAMESPACE
495
+ feature_data = global_feature_data
496
+ else :
497
+ # for a specific namespace
498
+ feature_data = per_ns_feature_data
499
+
472
500
syslog_common .service_validator (feature_data , service_name )
473
- syslog_common .save_rate_limit_to_db (db , service_name , interval , burst , log )
501
+ syslog_common .save_rate_limit_to_db (db . cfgdb_clients [ namespace ] , service_name , interval , burst , log )
474
502
475
503
476
504
@syslog .group (
@@ -482,14 +510,70 @@ def rate_limit_feature():
482
510
pass
483
511
484
512
513
+ def get_feature_names_to_proceed (db , service_name , namespace ):
514
+ """Get feature name list to be proceed by "config syslog rate-limit-feature enable" and
515
+ "config syslog rate-limit-feature disable" CLIs
516
+
517
+ Args:
518
+ db (object): Db object
519
+ service_name (str): Nullable service name to be enable/disable
520
+ namespace (str): Namespace provided by user
521
+
522
+ Returns:
523
+ list: A list of feature name
524
+ """
525
+ features = db .cfgdb .get_table (syslog_common .FEATURE_TABLE )
526
+ if service_name :
527
+ syslog_common .service_validator (features , service_name )
528
+
529
+ global_feature_data , per_ns_feature_data = syslog_common .extract_feature_data (features )
530
+ if not namespace :
531
+ if not service_name :
532
+ feature_list = [feature_name for feature_name in global_feature_data .keys ()]
533
+ if multi_asic .is_multi_asic ():
534
+ asic_count = multi_asic .get_num_asics ()
535
+ for i in range (asic_count ):
536
+ feature_list .extend ([f'{ feature_name } { i } ' for feature_name in per_ns_feature_data .keys ()])
537
+ else :
538
+ feature_config = features [service_name ]
539
+ feature_list = []
540
+ if feature_config [syslog_common .FEATURE_HAS_GLOBAL_SCOPE ].lower () == 'true' :
541
+ feature_list .append (service_name )
542
+
543
+ if multi_asic .is_multi_asic ():
544
+ if feature_config [syslog_common .FEATURE_HAS_PER_ASIC_SCOPE ].lower () == 'true' :
545
+ asic_count = multi_asic .get_num_asics ()
546
+ for i in range (asic_count ):
547
+ feature_list .append (multi_asic .get_container_name_from_asic_id (service_name , i ))
548
+ elif namespace == 'default' :
549
+ if not service_name :
550
+ feature_list = [feature_name for feature_name in global_feature_data .keys ()]
551
+ else :
552
+ syslog_common .service_validator (global_feature_data , service_name )
553
+ feature_list = [service_name ]
554
+ else :
555
+ asic_num = multi_asic .get_asic_id_from_name (namespace )
556
+ if not service_name :
557
+ feature_list = [multi_asic .get_container_name_from_asic_id (feature_name , asic_num ) for feature_name in per_ns_feature_data .keys ()]
558
+ else :
559
+ syslog_common .service_validator (per_ns_feature_data , service_name )
560
+ feature_list = [multi_asic .get_container_name_from_asic_id (service_name , asic_num )]
561
+ return feature_list
562
+
563
+
485
564
@rate_limit_feature .command ("enable" )
565
+ @click .argument ("service_name" , required = False )
566
+ @click .option ('--namespace' , '-n' , 'namespace' , default = None ,
567
+ type = click .Choice (multi_asic_util .multi_asic_ns_choices () + ['default' ]),
568
+ show_default = True , help = 'Namespace name or all' )
486
569
@clicommon .pass_db
487
- def enable_rate_limit_feature (db ):
570
+ def enable_rate_limit_feature (db , service_name , namespace ):
488
571
""" Enable syslog rate limit feature """
489
- feature_data = db . cfgdb . get_table ( syslog_common . FEATURE_TABLE )
490
- for feature_name in feature_data . keys () :
572
+ feature_list = get_feature_names_to_proceed ( db , service_name , namespace )
573
+ for feature_name in feature_list :
491
574
click .echo (f'Enabling syslog rate limit feature for { feature_name } ' )
492
- output , _ = clicommon .run_command (['docker' , 'ps' , '-q' , '-f' , 'status=running' , '-f' , f'name={ feature_name } ' ], return_cmd = True )
575
+ shell_cmd = f'docker ps -f status=running --format "{{{{.Names}}}}" | grep -E "^{ feature_name } $"'
576
+ output , _ = clicommon .run_command (shell_cmd , return_cmd = True , shell = True )
493
577
if not output :
494
578
click .echo (f'{ feature_name } is not running, ignoring...' )
495
579
continue
@@ -517,16 +601,21 @@ def enable_rate_limit_feature(db):
517
601
518
602
if not failed :
519
603
click .echo (f'Enabled syslog rate limit feature for { feature_name } ' )
520
-
521
-
604
+
605
+
522
606
@rate_limit_feature .command ("disable" )
607
+ @click .argument ("service_name" , required = False )
608
+ @click .option ('--namespace' , '-n' , 'namespace' , default = None ,
609
+ type = click .Choice (multi_asic_util .multi_asic_ns_choices () + ['default' ]),
610
+ show_default = True , help = 'Namespace name or all' )
523
611
@clicommon .pass_db
524
- def disable_rate_limit_feature (db ):
612
+ def disable_rate_limit_feature (db , service_name , namespace ):
525
613
""" Disable syslog rate limit feature """
526
- feature_data = db . cfgdb . get_table ( syslog_common . FEATURE_TABLE )
527
- for feature_name in feature_data . keys () :
614
+ feature_list = get_feature_names_to_proceed ( db , service_name , namespace )
615
+ for feature_name in feature_list :
528
616
click .echo (f'Disabling syslog rate limit feature for { feature_name } ' )
529
- output , _ = clicommon .run_command (['docker' , 'ps' , '-q' , '-f' , 'status=running' , '-f' , f'name={ feature_name } ' ], return_cmd = True )
617
+ shell_cmd = f'docker ps -f status=running --format "{{{{.Names}}}}" | grep -E "^{ feature_name } $"'
618
+ output , _ = clicommon .run_command (shell_cmd , return_cmd = True , shell = True )
530
619
if not output :
531
620
click .echo (f'{ feature_name } is not running, ignoring...' )
532
621
continue
@@ -553,4 +642,3 @@ def disable_rate_limit_feature(db):
553
642
554
643
if not failed :
555
644
click .echo (f'Disabled syslog rate limit feature for { feature_name } ' )
556
-
0 commit comments