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)