diff --git a/show/main.py b/show/main.py index 5e4bcf30f0..b206b48635 100755 --- a/show/main.py +++ b/show/main.py @@ -336,6 +336,32 @@ def vrf(vrf_name): body.append(["", intf]) click.echo(tabulate(body, header)) +# +# 'events' command ("show event-counters") +# + +@cli.command() +def event_counters(): + """Show events counter""" + # dump keys as formatted + counters_db = SonicV2Connector(host='127.0.0.1') + counters_db.connect(counters_db.COUNTERS_DB, retry_on=False) + + header = ['name', 'count'] + keys = counters_db.keys(counters_db.COUNTERS_DB, 'COUNTERS_EVENTS*') + table = [] + + for key in natsorted(keys): + key_list = key.split(':') + data_dict = counters_db.get_all(counters_db.COUNTERS_DB, key) + table.append((key_list[1], data_dict["value"])) + + if table: + click.echo(tabulate(table, header, tablefmt='simple', stralign='right')) + else: + click.echo('No data available in COUNTERS_EVENTS\n') + + # # 'arp' command ("show arp") # diff --git a/tests/event_counters_input/counters_db.json b/tests/event_counters_input/counters_db.json new file mode 100644 index 0000000000..294e4c0a07 --- /dev/null +++ b/tests/event_counters_input/counters_db.json @@ -0,0 +1,18 @@ +{ + "COUNTERS_EVENTS:latency_in_ms": { + "value": 2 + }, + "COUNTERS_EVENTS:missed_to_cache": { + "value": 0 + }, + "COUNTERS_EVENTS:missed_internal": { + "value": 0 + }, + "COUNTERS_EVENTS:missed_by_slow_receiver": { + "value": 0 + }, + "COUNTERS_EVENTS:published": { + "value": 40147 + } +} + diff --git a/tests/show_event_counters_test.py b/tests/show_event_counters_test.py new file mode 100644 index 0000000000..0cb965a287 --- /dev/null +++ b/tests/show_event_counters_test.py @@ -0,0 +1,37 @@ +import os +import sys +from click.testing import CliRunner +from utilities_common.db import Db +import show.main as show + +class TestShowEventCounters(object): + @classmethod + def setup_class(cls): + print("SETUP") + os.environ["UTILITIES_UNIT_TESTING"] = "1" + + def test_event_counters_show(self): + from .mock_tables import dbconnector + test_path = os.path.dirname(os.path.abspath(__file__)) + mock_db_path = os.path.join(test_path, "event_counters_input") + jsonfile_counters = os.path.join(mock_db_path, "counters_db") + dbconnector.dedicated_dbs['COUNTERS_DB'] = jsonfile_counters + runner = CliRunner() + db = Db() + expected_output = """\ + name count +----------------------- ------- + latency_in_ms 2 +missed_by_slow_receiver 0 + missed_internal 0 + missed_to_cache 0 + published 40147 +""" + + result = runner.invoke(show.cli.commands['event-counters'], [], obj=db) + print(result.exit_code) + print(result.output) + dbconnector.dedicated_dbs['COUNTERS_DB'] = None + assert result.exit_code == 0 + assert result.output == expected_output +