From 9fde08a2a5964255789ea3cec91e88f7f557e33c Mon Sep 17 00:00:00 2001 From: Sangita Maity Date: Mon, 27 Sep 2021 22:01:38 +0000 Subject: [PATCH] [portstat] Always check whether uid is root for delete or Clear operation Signed-off-by: Sangita Maity RB=2856421 G=lnos-reviewers R=pchaudhary,pmao,stexu,samaity,zxu A=pchaudhary --- scripts/portstat | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/scripts/portstat b/scripts/portstat index 0b6a6e17f4..d853df542a 100755 --- a/scripts/portstat +++ b/scripts/portstat @@ -37,7 +37,7 @@ try: if os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] == "multi_asic": import mock_tables.mock_multi_asic mock_tables.dbconnector.load_namespace_config() - + except KeyError: pass @@ -148,7 +148,7 @@ class Portstat(object): @multi_asic_util.run_on_multi_asic def collect_stat(self): """ - Collect the statisitics from all the asics present on the + Collect the statisitics from all the asics present on the device and store in a dict """ self.cnstat_dict.update(self.get_cnstat()) @@ -236,7 +236,7 @@ class Portstat(object): self.db = multi_asic.connect_to_all_dbs_for_ns(ns) admin_state = self.db.get(self.db.APPL_DB, full_table_id, PORT_ADMIN_STATUS_FIELD) oper_state = self.db.get(self.db.APPL_DB, full_table_id, PORT_OPER_STATUS_FIELD) - + if admin_state is None or oper_state is None: continue if admin_state.upper() == PORT_STATUS_VALUE_DOWN: @@ -357,7 +357,7 @@ class Portstat(object): print("Time Since Counters Last Cleared............... " + str(cnstat_old_dict.get('time'))) - + def cnstat_diff_print(self, cnstat_new_dict, cnstat_old_dict, intf_list, use_json, print_all, errors_only, rates_only, detail=False): """ Print the difference between two cnstat results. @@ -475,11 +475,6 @@ class Portstat(object): else: print(tabulate(table, header, tablefmt='simple', stralign='right')) - def get_file_path(self, cnstat_file): - - cnstat_fqn_file = self.cnstat_dir + "/" + cnstat_file - return cnstat_fqn_file - def _lock_file(self, f): """ Lock the file using fnctl which manages file descriptors """ try: @@ -512,7 +507,7 @@ Examples: portstat -c -P Ethernet112 portstat -t test portstat -d -t test - portstat -e + portstat -e portstat portstat -r portstat -R @@ -558,13 +553,20 @@ Examples: portstat = Portstat(namespace, display_option) + # [LI specific] Always check whether uid is root or not. + if delete_all_stats or delete_saved_stats or save_fresh_stats: + if uid != '0': + print("Permission Denied, Please run as root/sudo") + sys.exit(ERR_PER_DEN) + cnstat_directory = portstat.cnstat_dir + if tag_name is not None: cnstat_file = portstat.cached_stats_file_hidden + tag_name else: cnstat_file = portstat.cached_stats_file_hidden - cnstat_fqn_file = portstat.get_file_path(cnstat_file) + cnstat_fqn_file = cnstat_directory + "/" + cnstat_file if delete_all_stats: if not os.path.isdir(cnstat_directory): @@ -618,9 +620,6 @@ Examples: sys.exit(1) if save_fresh_stats: - if uid != '0': - print("Permission Denied, Please run as root/sudo") - sys.exit(ERR_PER_DEN) try: if portClear is not None: # At this point, we'll read the contents and keep it in a dictionary. @@ -681,3 +680,4 @@ Examples: if __name__ == "__main__": main() +