Skip to content

Commit

Permalink
[crm]: Fix failures in CLI show commands (sonic-net#221)
Browse files Browse the repository at this point in the history
* [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]>
  • Loading branch information
Volodymyr Samotiy authored and lguohan committed Mar 29, 2018
1 parent c1d0b96 commit c2d13c6
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions crm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ def show_summary(self):

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

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

def show_thresholds(self, resource):
"""
Expand All @@ -44,13 +47,16 @@ def show_thresholds(self, resource):
header = ("Resource Name", "Threshold Type", "Low Threshold", "High Threshold")
data = []

if resource == 'all':
for res in ["ipv4_route", "ipv6_route", "ipv4_nexthop", "ipv6_nexthop", "ipv4_neighbor", "ipv6_neighbor",
"nexthop_group_member", "nexthop_group", "acl_table", "acl_group", "acl_entry",
"acl_counter", "fdb_entry"]:
data.append([res, crm_info[res + "_threshold_type"], crm_info[res + "_low_threshold"], crm_info[res + "_high_threshold"]])
if crm_info:
if resource == 'all':
for res in ["ipv4_route", "ipv6_route", "ipv4_nexthop", "ipv6_nexthop", "ipv4_neighbor", "ipv6_neighbor",
"nexthop_group_member", "nexthop_group", "acl_table", "acl_group", "acl_entry",
"acl_counter", "fdb_entry"]:
data.append([res, crm_info[res + "_threshold_type"], crm_info[res + "_low_threshold"], crm_info[res + "_high_threshold"]])
else:
data.append([resource, crm_info[resource + "_threshold_type"], crm_info[resource + "_low_threshold"], crm_info[resource + "_high_threshold"]])
else:
data.append([resource, crm_info[resource + "_threshold_type"], crm_info[resource + "_low_threshold"], crm_info[resource + "_high_threshold"]])
print '\nError! Could not get CRM configuration.'

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

if resource == 'all':
for res in ["ipv4_route", "ipv6_route", "ipv4_nexthop", "ipv6_nexthop", "ipv4_neighbor", "ipv6_neighbor",
"nexthop_group_member", "nexthop_group", "fdb_entry"]:
data.append([res, crm_stats['crm_stats_' + res + "_used"], crm_stats['crm_stats_' + res + "_available"]])
if crm_stats:
if resource == 'all':
for res in ["ipv4_route", "ipv6_route", "ipv4_nexthop", "ipv6_nexthop", "ipv4_neighbor", "ipv6_neighbor",
"nexthop_group_member", "nexthop_group", "fdb_entry"]:
data.append([res, crm_stats['crm_stats_' + res + "_used"], crm_stats['crm_stats_' + res + "_available"]])
else:
data.append([resource, crm_stats['crm_stats_' + resource + "_used"], crm_stats['crm_stats_' + resource + "_available"]])
else:
data.append([resource, crm_stats['crm_stats_' + resource + "_used"], crm_stats['crm_stats_' + resource + "_available"]])
print '\nCRM counters are not ready. They would be populated after the polling interval.'

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

for key in out.splitlines():
for key in out.splitlines() or [None]:
data = []
id = key.replace('CRM:ACL_TABLE_STATS:', '')

crm_stats = countersdb.get_all(countersdb.COUNTERS_DB, key)
if key:
id = key.replace('CRM:ACL_TABLE_STATS:', '')

crm_stats = countersdb.get_all(countersdb.COUNTERS_DB, key)

for res in ['acl_entry', 'acl_counter']:
if ('crm_stats_' + res + '_used' in crm_stats) and ('crm_stats_' + res + '_available' in crm_stats):
data.append([id, res, crm_stats['crm_stats_' + res + '_used'], crm_stats['crm_stats_' + res + '_available']])
for res in ['acl_entry', 'acl_counter']:
if ('crm_stats_' + res + '_used' in crm_stats) and ('crm_stats_' + res + '_available' in crm_stats):
data.append([id, res, crm_stats['crm_stats_' + res + '_used'], crm_stats['crm_stats_' + res + '_available']])

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

@acl.group()
@acl.command()
@click.pass_context
def group(ctx):
"""Show CRM information for acl group resource"""
if ctx.obj["crm"].cli_mode == 'thresholds':
ctx.obj["crm"].show_thresholds('acl_group')
elif ctx.obj["crm"].cli_mode == 'resources':
ctx.obj["crm"].show_resources('acl_group')
ctx.obj["crm"].show_acl_resources()

@resources.command()
@click.pass_context
Expand Down

0 comments on commit c2d13c6

Please sign in to comment.