Skip to content

Commit c138eaa

Browse files
authored
[consutil] Display remote device name in show command (sonic-net#1120)
- Display remote device name in consutil show result table Co-authored-by: Jing Kan<[email protected]>
1 parent e634d88 commit c138eaa

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

consutil/lib.py

+31-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
ERR_DEV = 2
2121

2222
CONSOLE_PORT_TABLE = "CONSOLE_PORT"
23+
LINE_KEY = "LINE"
2324
BAUD_KEY = "baud_rate"
2425
DEVICE_KEY = "remote_device"
2526
FLOW_KEY = "flow_control"
@@ -46,22 +47,42 @@ def run_command(cmd):
4647
sys.exit(ERR_CMD)
4748
return output
4849

49-
# returns a sorted list of all devices (whose name matches DEVICE_PREFIX)
50+
# returns a sorted list of all devices
5051
def getAllDevices():
52+
config_db = ConfigDBConnector()
53+
config_db.connect()
54+
55+
# Querying CONFIG_DB to get configured console ports
56+
keys = config_db.get_keys(CONSOLE_PORT_TABLE)
57+
devices = []
58+
for k in keys:
59+
device = config_db.get_entry(CONSOLE_PORT_TABLE, k)
60+
device[LINE_KEY] = k
61+
devices.append(device)
62+
63+
# Querying device directory to get all available console ports
5164
cmd = "ls " + DEVICE_PREFIX + "*"
5265
output = run_command(cmd)
5366

54-
devices = output.split('\n')
55-
devices = list(filter(lambda dev: re.match(DEVICE_PREFIX + r"\d+", dev) != None, devices))
56-
devices.sort(key=lambda dev: int(dev[len(DEVICE_PREFIX):]))
57-
67+
availableTtys = output.split('\n')
68+
availableTtys = list(filter(lambda dev: re.match(DEVICE_PREFIX + r"\d+", dev) != None, availableTtys))
69+
for tty in availableTtys:
70+
k = tty[len(DEVICE_PREFIX):]
71+
if k not in keys:
72+
device = { LINE_KEY: k }
73+
devices.append(device)
74+
75+
devices.sort(key=lambda dev: int(dev[LINE_KEY]))
5876
return devices
5977

6078
# exits if inputted line number does not correspond to a device
6179
# input: linenum
6280
def checkDevice(linenum):
63-
devices = getAllDevices()
64-
if DEVICE_PREFIX + str(linenum) not in devices:
81+
config_db = ConfigDBConnector()
82+
config_db.connect()
83+
84+
entry = config_db.get_entry(CONSOLE_PORT_TABLE, str(linenum))
85+
if not entry:
6586
click.echo("Line number {} does not exist".format(linenum))
6687
sys.exit(ERR_DEV)
6788

@@ -120,12 +141,9 @@ def getLineNumber(target, deviceBool):
120141
config_db.connect()
121142

122143
devices = getAllDevices()
123-
linenums = list(map(lambda dev: dev[len(DEVICE_PREFIX):], devices))
124-
125-
for linenum in linenums:
126-
entry = config_db.get_entry(CONSOLE_PORT_TABLE, linenum)
127-
if DEVICE_KEY in entry and entry[DEVICE_KEY] == target:
128-
return linenum
144+
for device in devices:
145+
if DEVICE_KEY in device and device[DEVICE_KEY] == target:
146+
return device[LINE_KEY]
129147

130148
click.echo("Device {} does not exist".format(target))
131149
sys.exit(ERR_DEV)

consutil/main.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,21 @@ def show():
3030
devices = getAllDevices()
3131
busyDevices = getBusyDevices()
3232

33-
header = ["Line", "Actual/Configured Baud", "PID", "Start Time"]
33+
header = ["Line", "Actual/Configured Baud", "PID", "Start Time", "Device"]
3434
body = []
3535
for device in devices:
36-
lineNum = device[11:]
36+
lineNum = device[LINE_KEY]
3737
busy = " "
3838
pid = ""
3939
date = ""
40+
remoteDevice = '-' if DEVICE_KEY not in device else device[DEVICE_KEY]
4041
if lineNum in busyDevices:
4142
pid, date = busyDevices[lineNum]
4243
busy = "*"
4344
actBaud, confBaud, _ = getConnectionInfo(lineNum)
4445
# repeated "~" will be replaced by spaces - hacky way to align the "/"s
4546
baud = "{}/{}{}".format(actBaud, confBaud, "~"*(15-len(confBaud)))
46-
body.append([busy+lineNum, baud, pid, date])
47+
body.append([busy+lineNum, baud, pid, date, remoteDevice])
4748

4849
# replace repeated "~" with spaces - hacky way to align the "/"s
4950
click.echo(tabulate(body, header, stralign="right").replace('~', ' '))

0 commit comments

Comments
 (0)