Skip to content

Commit fa2894b

Browse files
tsvanduynjleveque
authored andcommitted
[show] adding optics info to show int status command (sonic-net#407)
1 parent 5d99b78 commit fa2894b

File tree

1 file changed

+64
-38
lines changed

1 file changed

+64
-38
lines changed

scripts/intfutil

+64-38
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,46 @@ from natsort import natsorted
1212

1313

1414
PORT_STATUS_TABLE_PREFIX = "PORT_TABLE:"
15+
PORT_TRANSCEIVER_TABLE_PREFIX = "TRANSCEIVER_INFO|"
1516
PORT_LANES_STATUS = "lanes"
1617
PORT_ALIAS = "alias"
1718
PORT_OPER_STATUS = "oper_status"
1819
PORT_ADMIN_STATUS = "admin_status"
1920
PORT_SPEED = "speed"
2021
PORT_MTU_STATUS = "mtu"
2122
PORT_DESCRIPTION = "description"
23+
PORT_OPTICS_TYPE = "type"
2224

2325

24-
def db_connect():
25-
db = swsssdk.SonicV2Connector(host='127.0.0.1')
26-
if db == None:
26+
def db_connect_appl():
27+
appl_db = swsssdk.SonicV2Connector(host='127.0.0.1')
28+
if appl_db is None:
2729
return None
2830

29-
db.connect(db.APPL_DB)
31+
appl_db.connect(appl_db.APPL_DB)
3032

31-
return db
33+
return appl_db
3234

3335

34-
def db_keys_get(db, intf_name):
36+
def appl_db_keys_get(appl_db, intf_name):
3537

36-
if intf_name == None:
37-
db_keys = db.keys(db.APPL_DB, "PORT_TABLE:*")
38+
if intf_name is None:
39+
appl_db_keys = appl_db.keys(appl_db.APPL_DB, "PORT_TABLE:*")
3840
elif intf_name.startswith('Ethernet'):
39-
db_keys = db.keys(db.APPL_DB, "PORT_TABLE:%s" % intf_name)
41+
appl_db_keys = db.keys(appl_db.APPL_DB, "PORT_TABLE:%s" % intf_name)
4042
else:
4143
return None
4244

43-
return db_keys
45+
return appl_db_keys
4446

4547

46-
def db_port_status_get(db, intf_name, status_type):
48+
def appl_db_port_status_get(appl_db, intf_name, status_type):
4749
"""
4850
Get the port status
4951
"""
5052

5153
full_table_id = PORT_STATUS_TABLE_PREFIX + intf_name
52-
status = db.get(db.APPL_DB, full_table_id, status_type)
54+
status = appl_db.get(appl_db.APPL_DB, full_table_id, status_type)
5355
if status is None:
5456
return "N/A"
5557

@@ -59,13 +61,34 @@ def db_port_status_get(db, intf_name, status_type):
5961
return status
6062

6163

64+
def db_connect_state():
65+
"""
66+
Connect to REDIS STATE DB and get optics info
67+
"""
68+
state_db = swsssdk.SonicV2Connector(host='127.0.0.1')
69+
if state_db is None:
70+
return None
71+
state_db.connect(state_db.STATE_DB, False) # Make one attempt only
72+
return state_db
73+
74+
75+
def state_db_port_optics_get(state_db, intf_name, type):
76+
"""
77+
Get optic type info for port
78+
"""
79+
full_table_id = PORT_TRANSCEIVER_TABLE_PREFIX + intf_name
80+
optics_type = state_db.get(state_db.STATE_DB, full_table_id, type)
81+
if optics_type is None:
82+
return "N/A"
83+
return optics_type
84+
6285
# ========================== interface-status logic ==========================
6386

64-
header_stat = ['Interface', 'Lanes', 'Speed', 'MTU', 'Alias', 'Oper', 'Admin']
87+
header_stat = ['Interface', 'Lanes', 'Speed', 'MTU', 'Alias', 'Oper', 'Admin', 'Type']
6588

6689
class IntfStatus(object):
6790

68-
def display_intf_status(self, db_keys):
91+
def display_intf_status(self, appl_db_keys):
6992
"""
7093
Generate interface-status output
7194
"""
@@ -78,16 +101,17 @@ class IntfStatus(object):
78101
# Iterate through all the keys and append port's associated state to
79102
# the result table.
80103
#
81-
for i in db_keys:
104+
for i in appl_db_keys:
82105
key = re.split(':', i, maxsplit=1)[-1].strip()
83106
if key and key.startswith('Ethernet'):
84107
table.append((key,
85-
db_port_status_get(self.db, key, PORT_LANES_STATUS),
86-
db_port_status_get(self.db, key, PORT_SPEED),
87-
db_port_status_get(self.db, key, PORT_MTU_STATUS),
88-
db_port_status_get(self.db, key, PORT_ALIAS),
89-
db_port_status_get(self.db, key, PORT_OPER_STATUS),
90-
db_port_status_get(self.db, key, PORT_ADMIN_STATUS)))
108+
appl_db_port_status_get(self.appl_db, key, PORT_LANES_STATUS),
109+
appl_db_port_status_get(self.appl_db, key, PORT_SPEED),
110+
appl_db_port_status_get(self.appl_db, key, PORT_MTU_STATUS),
111+
appl_db_port_status_get(self.appl_db, key, PORT_ALIAS),
112+
appl_db_port_status_get(self.appl_db, key, PORT_OPER_STATUS),
113+
appl_db_port_status_get(self.appl_db, key, PORT_ADMIN_STATUS),
114+
state_db_port_optics_get(self.state_db, key, PORT_OPTICS_TYPE)))
91115

92116
# Sorting and tabulating the result table.
93117
sorted_table = natsorted(table)
@@ -96,15 +120,17 @@ class IntfStatus(object):
96120

97121
def __init__(self, intf_name):
98122

99-
self.db = db_connect()
100-
if self.db == None:
123+
self.appl_db = db_connect_appl()
124+
self.state_db = db_connect_state()
125+
if self.appl_db is None:
101126
return
102-
103-
db_keys = db_keys_get(self.db, intf_name)
104-
if db_keys == None:
127+
if self.state_db is None:
128+
return
129+
appl_db_keys = appl_db_keys_get(self.appl_db, intf_name)
130+
if appl_db_keys is None:
105131
return
132+
self.display_intf_status(appl_db_keys)
106133

107-
self.display_intf_status(db_keys)
108134

109135

110136
# ========================== interface-description logic ==========================
@@ -115,7 +141,7 @@ header_desc = ['Interface', 'Oper', 'Admin', 'Alias', 'Description']
115141

116142
class IntfDescription(object):
117143

118-
def display_intf_description(self, db_keys):
144+
def display_intf_description(self, appl_db_keys):
119145
"""
120146
Generate interface-description output
121147
"""
@@ -128,30 +154,30 @@ class IntfDescription(object):
128154
# Iterate through all the keys and append port's associated state to
129155
# the result table.
130156
#
131-
for i in db_keys:
157+
for i in appl_db_keys:
132158
key = re.split(':', i, maxsplit=1)[-1].strip()
133159
if key and key.startswith('Ethernet'):
134160
table.append((key,
135-
db_port_status_get(self.db, key, PORT_OPER_STATUS),
136-
db_port_status_get(self.db, key, PORT_ADMIN_STATUS),
137-
db_port_status_get(self.db, key, PORT_ALIAS),
138-
db_port_status_get(self.db, key, PORT_DESCRIPTION)))
161+
appl_db_port_status_get(self.appl_db, key, PORT_OPER_STATUS),
162+
appl_db_port_status_get(self.appl_db, key, PORT_ADMIN_STATUS),
163+
appl_db_port_status_get(self.appl_db, key, PORT_ALIAS),
164+
appl_db_port_status_get(self.appl_db, key, PORT_DESCRIPTION)))
139165

140166
# Sorting and tabulating the result table.
141167
sorted_table = natsorted(table)
142168
print tabulate(sorted_table, header_desc, tablefmt="simple", stralign='right')
143169

144170
def __init__(self, intf_name):
145171

146-
self.db = db_connect()
147-
if self.db == None:
172+
self.appl_db = db_connect_appl()
173+
if self.appl_db is None:
148174
return
149175

150-
db_keys = db_keys_get(self.db, intf_name)
151-
if db_keys == None:
176+
appl_db_keys = appl_db_keys_get(self.appl_db, intf_name)
177+
if appl_db_keys is None:
152178
return
153179

154-
self.display_intf_description(db_keys)
180+
self.display_intf_description(appl_db_keys)
155181

156182

157183

0 commit comments

Comments
 (0)