From 6d00d1413d7e4a8e8ac826ad1add3403402c1a60 Mon Sep 17 00:00:00 2001 From: Wenda Ni Date: Sat, 13 Oct 2018 13:46:12 -0700 Subject: [PATCH] pfcwd cmd check (#342) * require root privilege for all pfcwd configuration commands (#330) Signed-off-by: Guohan Lu * pfcwd start: 1) default restoration time to 2 times detection time if not defined; 2) check if options are prefixed with "--"; Signed-off-by: Wenda * Check if ports list has invalid string element; Correct typo restoration-time Signed-off-by: Wenda --- pfcwd/main.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/pfcwd/main.py b/pfcwd/main.py index 93508c3529cd..d6569a2686af 100644 --- a/pfcwd/main.py +++ b/pfcwd/main.py @@ -2,6 +2,7 @@ import click import swsssdk +import os from tabulate import tabulate from natsort import natsorted @@ -124,18 +125,30 @@ def config(ports): @cli.command() @click.option('--action', '-a', type=click.Choice(['drop', 'forward', 'alert'])) @click.option('--restoration-time', '-r', type=click.IntRange(100, 60000)) -@click.argument('ports', nargs = -1) +@click.argument('ports', nargs=-1) @click.argument('detection-time', type=click.IntRange(100, 5000)) def start(action, restoration_time, ports, detection_time): - """ Start PFC watchdog on port(s). To config all ports, use all as input. """ + """ + Start PFC watchdog on port(s). To config all ports, use all as input. + + Example: + + sudo pfcwd start --action drop ports all detection-time 400 --restoration-time 400 + + """ if os.geteuid() != 0: exit("Root privileges are required for this operation") + allowed_strs = ['ports', 'all', 'detection-time'] configdb = swsssdk.ConfigDBConnector() configdb.connect() countersdb = swsssdk.SonicV2Connector(host='127.0.0.1') countersdb.connect(countersdb.COUNTERS_DB) all_ports = get_all_ports(countersdb) + allowed_strs = allowed_strs + all_ports + for p in ports: + if p not in allowed_strs: + raise click.BadOptionUsage("Bad command line format. Try 'pfcwd start --help' for usage") if len(ports) == 0: ports = all_ports @@ -147,6 +160,9 @@ def start(action, restoration_time, ports, detection_time): pfcwd_info['action'] = action if restoration_time is not None: pfcwd_info['restoration_time'] = restoration_time + else: + pfcwd_info['restoration_time'] = 2 * detection_time + print "restoration time not defined; default to 2 times detection time: %d ms" % (2 * detection_time) for port in ports: if port == "all":