-
Notifications
You must be signed in to change notification settings - Fork 831
Expand file tree
/
Copy pathteamshow
More file actions
executable file
·176 lines (150 loc) · 6.49 KB
/
teamshow
File metadata and controls
executable file
·176 lines (150 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/python
"""
Script to show LAG and LAG member status in a summary view
Example of the output:
acsadmin@sonic:~$ teamshow
Flags: A - active, I - inactive, Up - up, Dw - down, N/A - Not Available,
S - selected, D - deselected, * - not synced
No. Team Dev Protocol Ports
----- ------------- ---------- ---------------------------
0 PortChannel0 LACP(A)(Up) Ethernet0(D) Ethernet4(S)
8 PortChannel8 LACP(A)(Up) Ethernet8(S) Ethernet12(S)
16 PortChannel16 LACP(A)(Up) Ethernet20(S) Ethernet16(S)
24 PortChannel24 LACP(A)(Dw) Ethernet28(S) Ethernet24(S)
32 PortChannel32 LACP(A)(Up) Ethernet32(S) Ethernet36(S)
40 PortChannel40 LACP(A)(Dw) Ethernet44(S) Ethernet40(S)
48 PortChannel48 LACP(A)(Up) Ethernet52(S) Ethernet48(S)
56 PortChannel56 LACP(A)(Dw) Ethernet60(S) Ethernet56(S)
"""
import json
import os
import re
import subprocess
import sys
from natsort import natsorted
from tabulate import tabulate
from sonic_py_common.multi_asic import get_asic_id_from_name
import utilities_common.multi_asic as multi_asic_util
from utilities_common import constants
PORT_CHANNEL_APPL_TABLE_PREFIX = "LAG_TABLE:"
PORT_CHANNEL_CFG_TABLE_PREFIX = "PORTCHANNEL|"
PORT_CHANNEL_STATUS_FIELD = "oper_status"
PORT_CHANNEL_MEMBER_APPL_TABLE_PREFIX = "LAG_MEMBER_TABLE:"
PORT_CHANNEL_MEMBER_STATUS_FIELD = "status"
class Teamshow(object):
def __init__(self,display_option, namespace_option):
self.teams = []
self.teamsraw = {}
self.summary = {}
self.err = None
self.db = None
self.multi_asic = multi_asic_util.MultiAsic(display_option, namespace_option)
@multi_asic_util.run_on_multi_asic
def get_teams_info(self):
self.get_portchannel_names()
self.get_teamdctl()
self.get_teamshow_result()
def get_portchannel_names(self):
"""
Get the portchannel names from database.
"""
self.teams = []
team_keys = self.db.keys(self.db.CONFIG_DB, PORT_CHANNEL_CFG_TABLE_PREFIX+"*")
if team_keys == None:
return
for key in team_keys:
team_name = key[len(PORT_CHANNEL_CFG_TABLE_PREFIX):]
if self.multi_asic.skip_display(constants.PORT_CHANNEL_OBJ, team_name) is True:
continue
self.teams.append(team_name)
def get_portchannel_status(self, port_channel_name):
"""
Get port channel status from database.
"""
full_table_id = PORT_CHANNEL_APPL_TABLE_PREFIX + port_channel_name
return self.db.get(self.db.APPL_DB, full_table_id, PORT_CHANNEL_STATUS_FIELD)
def get_portchannel_member_status(self, port_channel_name, port_name):
full_table_id = PORT_CHANNEL_MEMBER_APPL_TABLE_PREFIX + port_channel_name + ":" + port_name
return self.db.get(self.db.APPL_DB, full_table_id, PORT_CHANNEL_MEMBER_STATUS_FIELD)
def get_team_id(self, team):
"""
Skip the 'PortChannel' prefix and extract the team id.
"""
return team[11:]
def get_teamdctl(self):
"""
Get teams raw data from teamdctl.
Command: 'teamdctl <teamdevname> state dump'.
"""
for team in self.teams:
teamdctl_cmd = "sudo docker exec -it teamd{} teamdctl {} state dump".format(get_asic_id_from_name(self.multi_asic.current_namespace), team)
p = subprocess.Popen(teamdctl_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
rc = p.wait()
if rc == 0:
self.teamsraw[self.get_team_id(team)] = output
else:
self.err = err
def get_teamshow_result(self):
"""
Get teamshow results by parsing the output of teamdctl and combining port channel status.
"""
for team in self.teams:
info = {}
team_id = self.get_team_id(team)
if team_id not in self.teamsraw:
info['protocol'] = 'N/A'
self.summary[team_id] = info
self.summary[team_id]['ports'] = ''
continue
json_info = json.loads(self.teamsraw[team_id])
info['protocol'] = json_info['setup']['runner_name'].upper()
info['protocol'] += '(A)' if json_info['runner']['active'] else '(I)'
portchannel_status = self.get_portchannel_status(team)
if portchannel_status is None:
info['protocol'] += '(N/A)'
elif portchannel_status.lower() == 'up':
info['protocol'] += '(Up)'
elif portchannel_status.lower() == 'down':
info['protocol'] += '(Dw)'
else:
info['protocol'] += '(N/A)'
info['ports'] = ""
if 'ports' not in json_info:
info['ports'] = 'N/A'
else:
for port in json_info['ports']:
status = self.get_portchannel_member_status(team, port)
selected = json_info["ports"][port]["runner"]["selected"]
info["ports"] += port + "("
info["ports"] += "S" if selected else "D"
if status is None or (status == "enabled" and not selected) or (status == "disabled" and selected):
info["ports"] += "*"
info["ports"] += ") "
self.summary[team_id] = info
def display_summary(self):
"""
Display the portchannel (team) summary.
"""
print("Flags: A - active, I - inactive, Up - up, Dw - Down, N/A - not available,\n"
" S - selected, D - deselected, * - not synced")
header = ['No.', 'Team Dev', 'Protocol', 'Ports']
output = []
for team_id in natsorted(self.summary):
output.append([team_id, 'PortChannel'+team_id, self.summary[team_id]['protocol'], self.summary[team_id]['ports']])
print tabulate(output, header)
def main():
if os.geteuid() != 0:
exit("This utility must be run as root")
parser = multi_asic_util.multi_asic_args()
args = parser.parse_args()
display_option = args.display
namespace_option = args.namespace
try:
team = Teamshow(display_option, namespace_option)
team.get_teams_info()
team.display_summary()
except Exception as e:
sys.exit(e.message)
if __name__ == "__main__":
main()