From 4175cb904e3e934829ff89f1f980bd8727881158 Mon Sep 17 00:00:00 2001 From: Andriy Yurkiv <70649192+ayurkiv-nvda@users.noreply.github.com> Date: Wed, 11 Aug 2021 18:37:30 +0300 Subject: [PATCH] [show priority-group drop counters] Add user info output when user want to check PG counters and polling are disabled (#1678) Signed-off-by: Andriy Yurkiv What I did Added additional output info for user when trying to show priority group counters and pg drop counters are disabled How I did it modify pg-drop script, add additional print during executing "show priority-group drop counters" if PG polling disabled How to verify it counterpoll pg-drop disable show priority-group drop counters Expect: Warning: PG counters are disabled. Current stats may be outdated. Use 'counterpoll pg-drop enable' to enable' Previous command output (if the output of a command-line utility has changed) admin@r-tigon-04:/usr/local/bin$ show priority-group drop counters Ingress PG dropped packets: Port PG0 PG1 PG2 PG3 PG4 PG5 PG6 PG7 ----------- ----- ----- ----- ----- ----- ----- ----- ----- Ethernet0 0 0 0 0 0 0 0 0 Ethernet2 0 0 0 0 0 0 0 0 Ethernet8 0 0 0 0 0 0 0 0 Ethernet10 0 0 0 0 0 0 0 0 Ethernet16 0 0 0 0 0 0 0 0 New command output (if the output of a command-line utility has changed) admin@r-tigon-04:/usr/local/bin$ show priority-group drop counters Warning: PG counters are disabled. Use 'counterpoll pg-drop enable' to enable polling --- scripts/pg-drop | 14 +++++++++++++- tests/pgdrop_input/config_db.json | 26 ++++++++++++++++++++++++++ tests/pgdropstat_test.py | 25 +++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/pgdrop_input/config_db.json diff --git a/scripts/pg-drop b/scripts/pg-drop index ae891c50b59c..b437e53bbab7 100755 --- a/scripts/pg-drop +++ b/scripts/pg-drop @@ -26,7 +26,7 @@ try: except KeyError: pass -from swsscommon.swsscommon import SonicV2Connector +from swsscommon.swsscommon import ConfigDBConnector, SonicV2Connector STATUS_NA = 'N/A' @@ -47,6 +47,9 @@ class PgDropStat(object): self.counters_db = SonicV2Connector(host='127.0.0.1') self.counters_db.connect(self.counters_db.COUNTERS_DB) + self.configdb = ConfigDBConnector() + self.configdb.connect() + dropstat_dir = get_dropstat_dir() self.port_drop_stats_file = os.path.join(dropstat_dir, 'pg_drop_stats') @@ -212,6 +215,14 @@ class PgDropStat(object): sys.exit(e.errno) print("Cleared PG drop counter") + def check_if_stats_enabled(self): + pg_drop_info = self.configdb.get_entry('FLEX_COUNTER_TABLE', 'PG_DROP') + if pg_drop_info: + status = pg_drop_info.get("FLEX_COUNTER_STATUS", 'disable') + if status == "disable": + print("Warning: PG counters are disabled. Use 'counterpoll pg-drop enable' to enable polling") + sys.exit(0) + def main(): parser = argparse.ArgumentParser(description='Display PG drop counter', formatter_class=argparse.RawTextHelpFormatter, @@ -240,6 +251,7 @@ pg-drop -c clear if command == 'clear': pgdropstat.clear_drop_counts() elif command == 'show': + pgdropstat.check_if_stats_enabled() pgdropstat.print_all_stat(COUNTER_TABLE_PREFIX, "pg_drop" ) else: print("Command not recognized") diff --git a/tests/pgdrop_input/config_db.json b/tests/pgdrop_input/config_db.json new file mode 100644 index 000000000000..73691a4bea2e --- /dev/null +++ b/tests/pgdrop_input/config_db.json @@ -0,0 +1,26 @@ +{ + "FLEX_COUNTER_TABLE|QUEUE": { + "POLL_INTERVAL": "10000", + "FLEX_COUNTER_STATUS": "enable" + }, + "FLEX_COUNTER_TABLE|PORT": { + "POLL_INTERVAL": "1000", + "FLEX_COUNTER_STATUS": "enable" + }, + "FLEX_COUNTER_TABLE|PORT_BUFFER_DROP": { + "POLL_INTERVAL": "60000", + "FLEX_COUNTER_STATUS": "enable" + }, + "FLEX_COUNTER_TABLE|QUEUE_WATERMARK": { + "POLL_INTERVAL": "10000", + "FLEX_COUNTER_STATUS": "enable" + }, + "FLEX_COUNTER_TABLE|PG_WATERMARK": { + "POLL_INTERVAL": "10000", + "FLEX_COUNTER_STATUS": "enable" + }, + "FLEX_COUNTER_TABLE|PG_DROP": { + "POLL_INTERVAL": "10000", + "FLEX_COUNTER_STATUS": "disable" + } +} \ No newline at end of file diff --git a/tests/pgdropstat_test.py b/tests/pgdropstat_test.py index ee156b7588e7..3aea0f2959f9 100644 --- a/tests/pgdropstat_test.py +++ b/tests/pgdropstat_test.py @@ -1,11 +1,13 @@ import os import sys +import pytest import show.main as show import clear.main as clear import config.main as config from click.testing import CliRunner +from shutil import copyfile test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) @@ -39,6 +41,29 @@ def setup_class(cls): os.environ['UTILITIES_UNIT_TESTING'] = "2" print("SETUP") + @pytest.fixture(scope='function') + def replace_config_db_file(self): + sample_config_db_file = os.path.join(test_path, "pgdrop_input", "config_db.json") + mock_config_db_file = os.path.join(test_path, "mock_tables", "config_db.json") + + #Backup origin config_db and replace it with config_db file with disabled PG_DROP counters + copyfile(mock_config_db_file, "/tmp/config_db.json") + copyfile(sample_config_db_file, mock_config_db_file) + + yield + + copyfile("/tmp/config_db.json", mock_config_db_file) + + def test_show_pg_drop_disabled(self, replace_config_db_file): + runner = CliRunner() + + result = runner.invoke(show.cli.commands["priority-group"].commands["drop"].commands["counters"]) + assert result.exit_code == 0 + print(result.exit_code) + + assert result.output == "Warning: PG counters are disabled. Use 'counterpoll pg-drop enable' to enable polling\n" + print(result.output) + def test_show_pg_drop_show(self): self.executor(clear_before_show = False)