Skip to content

Commit

Permalink
[portstat] Always check whether uid is root for delete or Clear opera…
Browse files Browse the repository at this point in the history
…tion

Signed-off-by: Sangita Maity <samaity@linkedin.com>

RB=2856421
G=lnos-reviewers
R=pchaudhary,pmao,stexu,samaity,zxu
A=pchaudhary
  • Loading branch information
Sangita Maity committed Oct 6, 2021
1 parent 2691e60 commit 9fde08a
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions scripts/portstat
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -681,3 +680,4 @@ Examples:

if __name__ == "__main__":
main()

0 comments on commit 9fde08a

Please sign in to comment.