diff --git a/scripts/interface_stat b/scripts/interface_stat deleted file mode 100755 index 434db777d1..0000000000 --- a/scripts/interface_stat +++ /dev/null @@ -1,58 +0,0 @@ -#! /usr/bin/python - -import swsssdk -import sys -import re -from tabulate import tabulate -from natsort import natsorted - -header = ['Iface', 'Lanes', 'Alias', 'Oper', 'Admin', 'MTU'] - - -PORT_STATUS_TABLE_PREFIX = "PORT_TABLE:" -PORT_LANES_STATUS_FIELD = "lanes" -PORT_ALIAS_STATUS_FIELD = "alias" -PORT_OPER_STATUS_FIELD = "oper_status" -PORT_ADMIN_STATUS_FIELD = "admin_status" -PORT_MTU_STATUS_FIELD = "mtu" - -class Intstat(object): - table = [] - - def get_port_status(self, port_name, status_type): - """ - Get the port status - """ - full_table_id = PORT_STATUS_TABLE_PREFIX + port_name - status = self.db.get(self.db.APPL_DB, full_table_id, status_type) - if status is None: - return "N/A" - else: - return status - - def __init__(self): - self.db = swsssdk.SonicV2Connector(host='127.0.0.1') - self.db.connect(self.db.APPL_DB) - a = self.db.keys(self.db.APPL_DB) - - tables = self.db.keys(self.db.APPL_DB, "PORT_TABLE:*") - i = {} - table = [] - key = [] - - for i in tables: - key = re.split(':', i, maxsplit=1)[-1].strip() - if key and key.startswith('Ethernet'): - table.append((key, self.get_port_status(key, PORT_LANES_STATUS_FIELD), self.get_port_status(key, PORT_ALIAS_STATUS_FIELD), self.get_port_status(key, PORT_OPER_STATUS_FIELD), self.get_port_status(key, PORT_ADMIN_STATUS_FIELD), self.get_port_status(key, PORT_MTU_STATUS_FIELD))) - - sorted_table = natsorted(table) - print tabulate(sorted_table, header, tablefmt="simple", stralign='right') - - -def main(): - interface_stat = Intstat() - - sys.exit(0) - -if __name__ == "__main__": - main() diff --git a/scripts/intfutil b/scripts/intfutil new file mode 100755 index 0000000000..33d418437f --- /dev/null +++ b/scripts/intfutil @@ -0,0 +1,178 @@ +#! /usr/bin/python + +import swsssdk +import sys +import re +from tabulate import tabulate +from natsort import natsorted + + + +# ========================== Common interface-utils logic ========================== + + +PORT_STATUS_TABLE_PREFIX = "PORT_TABLE:" +PORT_LANES_STATUS = "lanes" +PORT_ALIAS = "alias" +PORT_OPER_STATUS = "oper_status" +PORT_ADMIN_STATUS = "admin_status" +PORT_SPEED = "speed" +PORT_MTU_STATUS = "mtu" +PORT_DESCRIPTION = "description" + + +def db_connect(): + db = swsssdk.SonicV2Connector(host='127.0.0.1') + if db == None: + return None + + db.connect(db.APPL_DB) + + return db + + +def db_keys_get(db, intf_name): + + if intf_name == None: + db_keys = db.keys(db.APPL_DB, "PORT_TABLE:*") + elif intf_name.startswith('Ethernet'): + db_keys = db.keys(db.APPL_DB, "PORT_TABLE:%s" % intf_name) + else: + return None + + return db_keys + + +def db_port_status_get(db, intf_name, status_type): + """ + Get the port status + """ + + full_table_id = PORT_STATUS_TABLE_PREFIX + intf_name + status = db.get(db.APPL_DB, full_table_id, status_type) + if status is None: + return "N/A" + + if status_type == PORT_SPEED and status != "N/A": + status = '{}G'.format(status[:2] if int(status) < 100000 else status[:3]) + + return status + + +# ========================== interface-status logic ========================== + +header_stat = ['Interface', 'Lanes', 'Speed', 'MTU', 'Alias', 'Oper', 'Admin'] + +class IntfStatus(object): + + def display_intf_status(self, db_keys): + """ + Generate interface-status output + """ + + i = {} + table = [] + key = [] + + # + # Iterate through all the keys and append port's associated state to + # the result table. + # + for i in db_keys: + key = re.split(':', i, maxsplit=1)[-1].strip() + if key and key.startswith('Ethernet'): + table.append((key, + db_port_status_get(self.db, key, PORT_LANES_STATUS), + db_port_status_get(self.db, key, PORT_SPEED), + db_port_status_get(self.db, key, PORT_MTU_STATUS), + db_port_status_get(self.db, key, PORT_ALIAS), + db_port_status_get(self.db, key, PORT_OPER_STATUS), + db_port_status_get(self.db, key, PORT_ADMIN_STATUS))) + + # Sorting and tabulating the result table. + sorted_table = natsorted(table) + print tabulate(sorted_table, header_stat, tablefmt="simple", stralign='right') + + + def __init__(self, intf_name): + + self.db = db_connect() + if self.db == None: + return + + db_keys = db_keys_get(self.db, intf_name) + if db_keys == None: + return + + self.display_intf_status(db_keys) + + +# ========================== interface-description logic ========================== + + +header_desc = ['Interface', 'Oper', 'Admin', 'Alias', 'Description'] + + +class IntfDescription(object): + + def display_intf_description(self, db_keys): + """ + Generate interface-description output + """ + + i = {} + table = [] + key = [] + + # + # Iterate through all the keys and append port's associated state to + # the result table. + # + for i in db_keys: + key = re.split(':', i, maxsplit=1)[-1].strip() + if key and key.startswith('Ethernet'): + table.append((key, + db_port_status_get(self.db, key, PORT_OPER_STATUS), + db_port_status_get(self.db, key, PORT_ADMIN_STATUS), + db_port_status_get(self.db, key, PORT_ALIAS), + db_port_status_get(self.db, key, PORT_DESCRIPTION))) + + # Sorting and tabulating the result table. + sorted_table = natsorted(table) + print tabulate(sorted_table, header_desc, tablefmt="simple", stralign='right') + + def __init__(self, intf_name): + + self.db = db_connect() + if self.db == None: + return + + db_keys = db_keys_get(self.db, intf_name) + if db_keys == None: + return + + self.display_intf_description(db_keys) + + + +def main(args): + if len(args) == 0: + print "No valid arguments provided" + return + + command = args[0] + if command != "status" and command != "description": + print "No valid command provided" + return + + intf_name = args[1] if len(args) == 2 else None + + if command == "status": + interface_stat = IntfStatus(intf_name) + elif command == "description": + interface_desc = IntfDescription(intf_name) + + sys.exit(0) + +if __name__ == "__main__": + main(sys.argv[1:]) diff --git a/setup.py b/setup.py index 2a52164879..153cbcde06 100644 --- a/setup.py +++ b/setup.py @@ -46,7 +46,7 @@ def get_test_suite(): 'scripts/fast-reboot-dump.py', 'scripts/fdbshow', 'scripts/generate_dump', - 'scripts/interface_stat', + 'scripts/intfutil', 'scripts/lldpshow', 'scripts/port2alias', 'scripts/portstat', diff --git a/show/main.py b/show/main.py index 3e735474d8..cd432b6eb1 100755 --- a/show/main.py +++ b/show/main.py @@ -205,6 +205,7 @@ def alias(interfacename): click.echo(tabulate(body, header)) + # 'summary' subcommand ("show interfaces summary") @interfaces.command() @click.argument('interfacename', required=False) @@ -239,6 +240,7 @@ def basic(interfacename): run_command(command) + @transceiver.command() @click.argument('interfacename', required=False) def details(interfacename): @@ -251,15 +253,29 @@ def details(interfacename): run_command(command) + @interfaces.command() @click.argument('interfacename', required=False) def description(interfacename): """Show interface status, protocol and description""" if interfacename is not None: - command = "sudo vtysh -c 'show interface {}'".format(interfacename) + command = "intfutil description {}".format(interfacename) + else: + command = "intfutil description" + + run_command(command) + + +@interfaces.command() +@click.argument('interfacename', required=False) +def status(interfacename): + """Show Interface status information""" + + if interfacename is not None: + command = "intfutil status {}".format(interfacename) else: - command = "sudo vtysh -c 'show interface description'" + command = "intfutil status" run_command(command) @@ -290,11 +306,6 @@ def portchannel(): """Show PortChannel information""" run_command("teamshow") -@interfaces.command() -def status(): - """Show Interface status information""" - run_command("interface_stat") - # # 'mac' command ("show mac ...")