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

First code-drop dealing with intf_description and intf_status enhancements #158

Merged
merged 2 commits into from
Dec 6, 2017
Merged
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
58 changes: 0 additions & 58 deletions scripts/interface_stat

This file was deleted.

178 changes: 178 additions & 0 deletions scripts/intfutil
Original file line number Diff line number Diff line change
@@ -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:])
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
25 changes: 18 additions & 7 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def alias(interfacename):

click.echo(tabulate(body, header))


# 'summary' subcommand ("show interfaces summary")
@interfaces.command()
@click.argument('interfacename', required=False)
Expand Down Expand Up @@ -239,6 +240,7 @@ def basic(interfacename):

run_command(command)


@transceiver.command()
@click.argument('interfacename', required=False)
def details(interfacename):
Expand All @@ -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)

Expand Down Expand Up @@ -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 ...")
Expand Down