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

Error Handling Framework : CLI support #666

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions clear/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,5 +388,17 @@ def line(linenum):
cmd = "consutil clear " + str(linenum)
run_command(cmd)

# 'error_database' group ("clear error_database ...")
@cli.command('error_database')
@click.argument('tablename', metavar='<tablename>', required=False, type=str)
def error_database_clear(tablename):
"""Clear ERROR DB entries"""

cmd = "errordbclear"
if tablename is not None:
cmd += " -t {}".format(tablename)

run_command(cmd)

if __name__ == '__main__':
cli()
61 changes: 61 additions & 0 deletions scripts/errordbclear
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/python
"""
Script to clear ERROR DB entries

usage: errordbclear [-t tablename]
optional arguments:
-t, --tablename ERROR DB table name: ERROR_ROUTE_TABLE

Example of the output:

root@sonic:# errordbclear -t ERROR_ROUTE_TABLE
('ERROR DB entries are cleared from Table.', 'ERROR_ROUTE_TABLE')

"""

import argparse
import json
import sys

from natsort import natsorted
from swsssdk import SonicV2Connector, port_util
from tabulate import tabulate

class ErrorDbClear(object):

def __init__(self):
super(ErrorDbClear,self).__init__()
self.db = SonicV2Connector(host="127.0.0.1")
self.db.connect(self.db.ERROR_DB)
return

def send_notification(self, op, data):
opdata = [op,data]
msg = json.dumps(opdata,separators=(',',':'))
self.db.publish('ERROR_DB','FLUSH_ERROR_DB', msg)
return

def main():

parser = argparse.ArgumentParser(description='Clear ERROR DB entries', formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-t', '--tablename', type=str, help='Clear failure entries on a specific table: ERROR_ROUTE_TABLE', default=None)
args = parser.parse_args()

try:
errorDb = ErrorDbClear()
if args.tablename is not None:
if (args.tablename.find("ERROR_",0,6) == -1):
print ("Error: Tablename is not in 'ERROR_...' format")
return
else:
errorDb.send_notification("TABLE", args.tablename)
print("ERROR DB entries are cleared from Table.", args.tablename)
else:
errorDb.send_notification("ALL", "ALL")
print("ERROR DB entries are cleared.")
except Exception as e:
print e.message
sys.exit(1)

if __name__ == "__main__":
main()
121 changes: 121 additions & 0 deletions scripts/errordbshow
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/usr/bin/python
"""
Script to show table entries in ERROR DB

usage: errordbshow [-t tablename]
optional arguments:
-t, --tablename Error DB table name: ERROR_ROUTE_TABLE

Example of the output:
admin@sonic# show error_database ERROR_ROUTE_TABLE
Prefix Nexthop Interface Error Code Operation
------------- --------- ----------- -------------- -----------
55.55.55.0/24 2.2.2.2 Ethernet32 SWSS_RC_EXISTS create

"""
import argparse
import json
import sys
import re

from natsort import natsorted
from swsssdk import SonicV2Connector, port_util
from tabulate import tabulate

class ErrorDbShow(object):

def __init__(self):
super(ErrorDbShow,self).__init__()
self.error_db = SonicV2Connector(host="127.0.0.1")
self.error_db.connect(self.error_db.ERROR_DB)
return

def errorDbShow(self, tablename):
if tablename is None or tablename == "ERROR_ROUTE_TABLE":
self.showRouteTable(tablename);

if tablename is None or tablename == "ERROR_NEIGH_TABLE":
self.showNeighTable(tablename);

def getRouteTableInfo(self, key):
route_data = {}
route_data['ifname'] = route_data['prefix'] = route_data['rc'] = ""
route_data['operation'] = route_data['nexthop'] = ""

entry = self.error_db.get_all(self.error_db.ERROR_DB, key)
if not entry:
return

if entry is not None:
route_data['prefix'] = re.split(':', key, maxsplit=1)[-1].strip()
route_data['rc'] = entry['rc']
route_data['operation'] = entry['operation']
if 'nexthop' in entry.keys():
route_data['nexthop'] = entry['nexthop']
if 'ifname' in entry.keys():
route_data['ifname'] = entry['ifname']
return route_data

def showRouteTable(self, tablename):
"""
Fetch Route table entries from ERROR DB.
"""
header = ['Prefix', 'Nexthop', 'Interface', 'Error Code', 'Operation']
table = []
table_keys = self.error_db.keys(self.error_db.ERROR_DB, "ERROR_ROUTE_TABLE:*")
if table_keys == None:
return
for key in natsorted(table_keys, reverse=True):
data = self.getRouteTableInfo(key)
if data:
table.append((data['prefix'], data['nexthop'], data['ifname'], data['rc'], data['operation']))
print tabulate(table, header, tablefmt='simple', stralign='left')

def getNeighTableInfo(self, key):
neigh_data = {}
entry = self.error_db.get_all(self.error_db.ERROR_DB, key)
if not entry:
return

neigh_data['ip'] = neigh_data['ifname'] = neigh_data['neigh'] = "";
neigh_data['rc'] = neigh_data['operation'] = "";

if entry is not None:
neigh_data['ip'] = re.split(':', key, maxsplit=2)[-1].strip()
neigh_data['ifname'] = re.split(':', key, maxsplit=2)[1].strip()
neigh_data['rc'] = entry['rc']
neigh_data['operation'] = entry['operation']
if 'neigh' in entry.keys():
neigh_data['neigh'] = entry['neigh']
return neigh_data

def showNeighTable(self, tablename):
"""
Fetch Neighbor table entries from ERROR DB.
"""
header = ['IP address', 'MAC address', 'Interface', 'Error Code', 'Operation']
table = []
table_keys = self.error_db.keys(self.error_db.ERROR_DB, "ERROR_NEIGH_TABLE:*")
if table_keys == None:
return
for key in natsorted(table_keys, reverse=True):
data = self.getNeighTableInfo(key)
table.append((data['ip'], data['neigh'], data['ifname'], data['rc'], data['operation']))
print tabulate(table, header, tablefmt='simple', stralign='left')

def main():

parser = argparse.ArgumentParser(description='Display ERROR DB entries',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-t', '--tablename', type=str, help='ERROR DB entries on specific table: ERROR_ROUTE_TABLE', default=None)
args = parser.parse_args()

try:
edb = ErrorDbShow()
edb.errorDbShow(args.tablename)
except Exception as e:
print e.message
sys.exit(1)

if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
'scripts/decode-syseeprom',
'scripts/dropcheck',
'scripts/ecnconfig',
'scripts/errordbclear',
'scripts/errordbshow',
'scripts/fast-reboot',
'scripts/fast-reboot-dump.py',
'scripts/fdbclear',
Expand Down
13 changes: 13 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,19 @@ def mac(vlan, port, verbose):

run_command(cmd, display_cmd=verbose)

# 'error_database' command ("show error_database ...")
@cli.command('error_database')
@click.argument('tablename', required=False)
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def error_database_show(tablename, verbose):
"""Show ERROR DB entries"""

cmd = "errordbshow"
if tablename is not None:
cmd += " -t {}".format(tablename)

run_command(cmd, display_cmd=verbose)

#
# 'show route-map' command ("show route-map")
#
Expand Down