Skip to content

Commit 254cafc

Browse files
authored
Event Counters CLI (#2449)
Add CLI for event_counters
1 parent 2dab0d0 commit 254cafc

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

show/main.py

+26
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,32 @@ def vrf(vrf_name):
336336
body.append(["", intf])
337337
click.echo(tabulate(body, header))
338338

339+
#
340+
# 'events' command ("show event-counters")
341+
#
342+
343+
@cli.command()
344+
def event_counters():
345+
"""Show events counter"""
346+
# dump keys as formatted
347+
counters_db = SonicV2Connector(host='127.0.0.1')
348+
counters_db.connect(counters_db.COUNTERS_DB, retry_on=False)
349+
350+
header = ['name', 'count']
351+
keys = counters_db.keys(counters_db.COUNTERS_DB, 'COUNTERS_EVENTS*')
352+
table = []
353+
354+
for key in natsorted(keys):
355+
key_list = key.split(':')
356+
data_dict = counters_db.get_all(counters_db.COUNTERS_DB, key)
357+
table.append((key_list[1], data_dict["value"]))
358+
359+
if table:
360+
click.echo(tabulate(table, header, tablefmt='simple', stralign='right'))
361+
else:
362+
click.echo('No data available in COUNTERS_EVENTS\n')
363+
364+
339365
#
340366
# 'arp' command ("show arp")
341367
#
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"COUNTERS_EVENTS:latency_in_ms": {
3+
"value": 2
4+
},
5+
"COUNTERS_EVENTS:missed_to_cache": {
6+
"value": 0
7+
},
8+
"COUNTERS_EVENTS:missed_internal": {
9+
"value": 0
10+
},
11+
"COUNTERS_EVENTS:missed_by_slow_receiver": {
12+
"value": 0
13+
},
14+
"COUNTERS_EVENTS:published": {
15+
"value": 40147
16+
}
17+
}
18+

tests/show_event_counters_test.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
import sys
3+
from click.testing import CliRunner
4+
from utilities_common.db import Db
5+
import show.main as show
6+
7+
class TestShowEventCounters(object):
8+
@classmethod
9+
def setup_class(cls):
10+
print("SETUP")
11+
os.environ["UTILITIES_UNIT_TESTING"] = "1"
12+
13+
def test_event_counters_show(self):
14+
from .mock_tables import dbconnector
15+
test_path = os.path.dirname(os.path.abspath(__file__))
16+
mock_db_path = os.path.join(test_path, "event_counters_input")
17+
jsonfile_counters = os.path.join(mock_db_path, "counters_db")
18+
dbconnector.dedicated_dbs['COUNTERS_DB'] = jsonfile_counters
19+
runner = CliRunner()
20+
db = Db()
21+
expected_output = """\
22+
name count
23+
----------------------- -------
24+
latency_in_ms 2
25+
missed_by_slow_receiver 0
26+
missed_internal 0
27+
missed_to_cache 0
28+
published 40147
29+
"""
30+
31+
result = runner.invoke(show.cli.commands['event-counters'], [], obj=db)
32+
print(result.exit_code)
33+
print(result.output)
34+
dbconnector.dedicated_dbs['COUNTERS_DB'] = None
35+
assert result.exit_code == 0
36+
assert result.output == expected_output
37+

0 commit comments

Comments
 (0)