Skip to content

Commit c2d13c6

Browse files
Volodymyr Samotiylguohan
Volodymyr Samotiy
authored andcommitted
[crm]: Fix failures in CLI show commands (sonic-net#221)
* [crm]: Fix failures in CLI show commands Signed-off-by: Volodymyr Samotiy <[email protected]> * [crm]: Fix failures in CLI show commands * Print appropriate messages if got empty entry from DB Signed-off-by: Volodymyr Samotiy <[email protected]>
1 parent c1d0b96 commit c2d13c6

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

crm/main.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ def show_summary(self):
3030

3131
crm_info = configdb.get_entry('CRM', 'Config')
3232

33-
print '\nPolling Interval: ' + crm_info['polling_interval'] + ' second(s)\n'
33+
if crm_info:
34+
print '\nPolling Interval: ' + crm_info['polling_interval'] + ' second(s)\n'
35+
else:
36+
print '\nError! Could not get CRM configuration.\n'
3437

3538
def show_thresholds(self, resource):
3639
"""
@@ -44,13 +47,16 @@ def show_thresholds(self, resource):
4447
header = ("Resource Name", "Threshold Type", "Low Threshold", "High Threshold")
4548
data = []
4649

47-
if resource == 'all':
48-
for res in ["ipv4_route", "ipv6_route", "ipv4_nexthop", "ipv6_nexthop", "ipv4_neighbor", "ipv6_neighbor",
49-
"nexthop_group_member", "nexthop_group", "acl_table", "acl_group", "acl_entry",
50-
"acl_counter", "fdb_entry"]:
51-
data.append([res, crm_info[res + "_threshold_type"], crm_info[res + "_low_threshold"], crm_info[res + "_high_threshold"]])
50+
if crm_info:
51+
if resource == 'all':
52+
for res in ["ipv4_route", "ipv6_route", "ipv4_nexthop", "ipv6_nexthop", "ipv4_neighbor", "ipv6_neighbor",
53+
"nexthop_group_member", "nexthop_group", "acl_table", "acl_group", "acl_entry",
54+
"acl_counter", "fdb_entry"]:
55+
data.append([res, crm_info[res + "_threshold_type"], crm_info[res + "_low_threshold"], crm_info[res + "_high_threshold"]])
56+
else:
57+
data.append([resource, crm_info[resource + "_threshold_type"], crm_info[resource + "_low_threshold"], crm_info[resource + "_high_threshold"]])
5258
else:
53-
data.append([resource, crm_info[resource + "_threshold_type"], crm_info[resource + "_low_threshold"], crm_info[resource + "_high_threshold"]])
59+
print '\nError! Could not get CRM configuration.'
5460

5561
print '\n'
5662
print tabulate(data, headers=header, tablefmt="simple", missingval="")
@@ -68,12 +74,15 @@ def show_resources(self, resource):
6874
header = ("Resource Name", "Used Count", "Available Count")
6975
data = []
7076

71-
if resource == 'all':
72-
for res in ["ipv4_route", "ipv6_route", "ipv4_nexthop", "ipv6_nexthop", "ipv4_neighbor", "ipv6_neighbor",
73-
"nexthop_group_member", "nexthop_group", "fdb_entry"]:
74-
data.append([res, crm_stats['crm_stats_' + res + "_used"], crm_stats['crm_stats_' + res + "_available"]])
77+
if crm_stats:
78+
if resource == 'all':
79+
for res in ["ipv4_route", "ipv6_route", "ipv4_nexthop", "ipv6_nexthop", "ipv4_neighbor", "ipv6_neighbor",
80+
"nexthop_group_member", "nexthop_group", "fdb_entry"]:
81+
data.append([res, crm_stats['crm_stats_' + res + "_used"], crm_stats['crm_stats_' + res + "_available"]])
82+
else:
83+
data.append([resource, crm_stats['crm_stats_' + resource + "_used"], crm_stats['crm_stats_' + resource + "_available"]])
7584
else:
76-
data.append([resource, crm_stats['crm_stats_' + resource + "_used"], crm_stats['crm_stats_' + resource + "_available"]])
85+
print '\nCRM counters are not ready. They would be populated after the polling interval.'
7786

7887
print '\n'
7988
print tabulate(data, headers=header, tablefmt="simple", missingval="")
@@ -118,15 +127,17 @@ def show_acl_table_resources(self):
118127
proc = Popen("docker exec -i database redis-cli --raw -n 2 KEYS *CRM:ACL_TABLE_STATS*", stdout=PIPE, stderr=PIPE, shell=True)
119128
out, err = proc.communicate()
120129

121-
for key in out.splitlines():
130+
for key in out.splitlines() or [None]:
122131
data = []
123-
id = key.replace('CRM:ACL_TABLE_STATS:', '')
124132

125-
crm_stats = countersdb.get_all(countersdb.COUNTERS_DB, key)
133+
if key:
134+
id = key.replace('CRM:ACL_TABLE_STATS:', '')
135+
136+
crm_stats = countersdb.get_all(countersdb.COUNTERS_DB, key)
126137

127-
for res in ['acl_entry', 'acl_counter']:
128-
if ('crm_stats_' + res + '_used' in crm_stats) and ('crm_stats_' + res + '_available' in crm_stats):
129-
data.append([id, res, crm_stats['crm_stats_' + res + '_used'], crm_stats['crm_stats_' + res + '_available']])
138+
for res in ['acl_entry', 'acl_counter']:
139+
if ('crm_stats_' + res + '_used' in crm_stats) and ('crm_stats_' + res + '_available' in crm_stats):
140+
data.append([id, res, crm_stats['crm_stats_' + res + '_used'], crm_stats['crm_stats_' + res + '_available']])
130141

131142
print '\n'
132143
print tabulate(data, headers=header, tablefmt="simple", missingval="")
@@ -459,14 +470,14 @@ def table(ctx):
459470
elif ctx.obj["crm"].cli_mode == 'resources':
460471
ctx.obj["crm"].show_acl_table_resources()
461472

462-
@acl.group()
473+
@acl.command()
463474
@click.pass_context
464475
def group(ctx):
465476
"""Show CRM information for acl group resource"""
466477
if ctx.obj["crm"].cli_mode == 'thresholds':
467478
ctx.obj["crm"].show_thresholds('acl_group')
468479
elif ctx.obj["crm"].cli_mode == 'resources':
469-
ctx.obj["crm"].show_resources('acl_group')
480+
ctx.obj["crm"].show_acl_resources()
470481

471482
@resources.command()
472483
@click.pass_context

0 commit comments

Comments
 (0)