Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event Counters CLI #2449

Merged
merged 6 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
#
Expand Down
18 changes: 18 additions & 0 deletions tests/event_counters_input/counters_db.json
Original file line number Diff line number Diff line change
@@ -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
}
}

37 changes: 37 additions & 0 deletions tests/show_event_counters_test.py
Original file line number Diff line number Diff line change
@@ -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