Skip to content

Commit b158a56

Browse files
sihuihan88Shuotian Cheng
authored andcommitted
[portstat]: Provide both admin state and operation state (sonic-net#56)
* return empty table when there is no port * refactor the get_port_status function
1 parent 6224416 commit b158a56

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

scripts/portstat

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ PORT_RATE = 40
2727

2828
NStats = namedtuple("NStats", "rx_ok, rx_err, rx_drop, rx_ovr, tx_ok,\
2929
tx_err, tx_drop, tx_ovr, rx_byt, tx_byt")
30-
header_all = ['Iface', 'STATE', 'RX_OK', 'RX_BPS', 'RX_PPS', 'RX_UTIL', 'RX_ERR', 'RX_DRP', 'RX_OVR',
30+
header_all = ['Iface', 'ADMIN', 'OPER', 'RX_OK', 'RX_BPS', 'RX_PPS', 'RX_UTIL', 'RX_ERR', 'RX_DRP', 'RX_OVR',
3131
'TX_OK', 'TX_BPS', 'Tx_PPS', 'TX_UTIL', 'TX_ERR', 'TX_DRP', 'TX_OVR']
3232

33-
header = ['Iface', 'STATE', 'RX_OK', 'RX_BPS', 'RX_UTIL', 'RX_ERR', 'RX_DRP', 'RX_OVR',
33+
header = ['Iface', 'ADMIN', 'OPER', 'RX_OK', 'RX_BPS', 'RX_UTIL', 'RX_ERR', 'RX_DRP', 'RX_OVR',
3434
'TX_OK', 'TX_BPS', 'TX_UTIL', 'TX_ERR', 'TX_DRP', 'TX_OVR']
3535

3636
counter_bucket_dict = {
@@ -51,7 +51,8 @@ counter_bucket_dict = {
5151
COUNTER_TABLE_PREFIX = "COUNTERS:"
5252
COUNTERS_PORT_NAME_MAP = "COUNTERS_PORT_NAME_MAP"
5353
PORT_STATUS_TABLE_PREFIX = "PORT_TABLE:"
54-
PORT_STATUS_FIELD = "oper_status"
54+
PORT_OPER_STATUS_FIELD = "oper_status"
55+
PORT_ADMIN_STATUS_FIELD = "admin_status"
5556

5657
class Portstat(object):
5758
def __init__(self):
@@ -83,24 +84,22 @@ class Portstat(object):
8384
# Build a dictionary of the stats
8485
cnstat_dict = OrderedDict()
8586
cnstat_dict['time'] = datetime.datetime.now()
87+
if counter_port_name_map is None:
88+
return cnstat_dict
8689
for port in natsorted(counter_port_name_map):
8790
cnstat_dict[port] = get_counters(counter_port_name_map[port])
8891
return cnstat_dict
8992

90-
def get_port_status(self, port_name):
93+
def get_port_status(self, port_name, status_type):
9194
"""
9295
Get the port status
9396
"""
9497
full_table_id = PORT_STATUS_TABLE_PREFIX + port_name
95-
status = self.db.get(self.db.APPL_DB, full_table_id, PORT_STATUS_FIELD)
98+
status = self.db.get(self.db.APPL_DB, full_table_id, status_type)
9699
if status is None:
97100
return "N/A"
98-
elif status.lower() == 'up':
99-
return "U"
100-
elif status.lower() == 'down':
101-
return "D"
102101
else:
103-
return "N/A"
102+
return status
104103

105104
def table_as_json(self, table, print_all):
106105
"""
@@ -128,8 +127,9 @@ class Portstat(object):
128127
header_all[11] : line[11],
129128
header_all[12] : line[12],
130129
header_all[13] : line[13],
131-
header_all[14] : line[14]
132-
}
130+
header_all[14] : line[14],
131+
header_all[15] : line[15]
132+
}
133133
else:
134134
for line in table:
135135
if_name = line[0]
@@ -149,7 +149,8 @@ class Portstat(object):
149149
header[10] : line[10],
150150
header[11] : line[11],
151151
header[12] : line[12],
152-
}
152+
header[13] : line[13]
153+
}
153154

154155
return json.dumps(output, indent=4, sort_keys=True)
155156

@@ -164,13 +165,13 @@ class Portstat(object):
164165
continue
165166

166167
if print_all:
167-
table.append((key, self.get_port_status(key),
168+
table.append((key, self.get_port_status(key, PORT_ADMIN_STATUS_FIELD), self.get_port_status(key, PORT_OPER_STATUS_FIELD),
168169
data.rx_ok, "N/A", "N/A", "N/A", data.rx_err,
169170
data.rx_drop, data.rx_ovr,
170171
data.tx_ok, "N/A", "N/A", "N/A", data.tx_err,
171172
data.tx_drop, data.tx_ovr))
172173
else:
173-
table.append((key, self.get_port_status(key),
174+
table.append((key, self.get_port_status(key, PORT_ADMIN_STATUS_FIELD), self.get_port_status(key, PORT_OPER_STATUS_FIELD),
174175
data.rx_ok, "N/A", "N/A", data.rx_err,
175176
data.rx_drop, data.rx_ovr,
176177
data.tx_ok, "N/A", "N/A", data.tx_err,
@@ -250,7 +251,7 @@ class Portstat(object):
250251

251252
if print_all:
252253
if old_cntr is not None:
253-
table.append((key, self.get_port_status(key),
254+
table.append((key, self.get_port_status(key, PORT_ADMIN_STATUS_FIELD), self.get_port_status(key, PORT_OPER_STATUS_FIELD),
254255
ns_diff(cntr.rx_ok, old_cntr.rx_ok),
255256
ns_brate(cntr.rx_byt, old_cntr.rx_byt, time_gap),
256257
ns_prate(cntr.rx_ok, old_cntr.rx_ok, time_gap),
@@ -266,7 +267,7 @@ class Portstat(object):
266267
ns_diff(cntr.tx_drop, old_cntr.tx_drop),
267268
ns_diff(cntr.tx_ovr, old_cntr.tx_ovr)))
268269
else:
269-
table.append((key, self.get_port_status(key),
270+
table.append((key, self.get_port_status(key, PORT_ADMIN_STATUS_FIELD), self.get_port_oper_status(key, PORT_OPER_STATUS_FIELD),
270271
cntr.rx_ok,
271272
"N/A",
272273
"N/A",
@@ -283,7 +284,7 @@ class Portstat(object):
283284
cntr.tx_err))
284285
else:
285286
if old_cntr is not None:
286-
table.append((key, self.get_port_status(key),
287+
table.append((key, self.get_port_status(key, PORT_ADMIN_STATUS_FIELD), self.get_port_status(key, PORT_OPER_STATUS_FIELD),
287288
ns_diff(cntr.rx_ok, old_cntr.rx_ok),
288289
ns_brate(cntr.rx_byt, old_cntr.rx_byt, time_gap),
289290
ns_util(cntr.rx_byt, old_cntr.rx_byt, time_gap),
@@ -297,7 +298,7 @@ class Portstat(object):
297298
ns_diff(cntr.tx_drop, old_cntr.tx_drop),
298299
ns_diff(cntr.tx_ovr, old_cntr.tx_ovr)))
299300
else:
300-
table.append((key, self.get_port_status(key),
301+
table.append((key, self.get_port_status(key, PORT_ADMIN_STATUS_FIELD), self.get_port_oper_status(key, PORT_OPER_STATUS_FIELD),
301302
cntr.rx_ok,
302303
"N/A",
303304
"N/A",

0 commit comments

Comments
 (0)