forked from extremenetworks/ExtremeScripting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportsum.py
331 lines (286 loc) · 10.2 KB
/
portsum.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# Python Scripts provided by Extreme Networks.
# This script is provided free of charge by Extreme. We hope such scripts are
# helpful when used in conjunction with Extreme products and technology;
# however, scripts are provided simply as an accommodation and are not
# supported nor maintained by Extreme. ANY SCRIPTS PROVIDED BY EXTREME ARE
# HEREBY PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL EXTREME OR ITS
# THIRD PARTY LICENSORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
# IN CONNECTION WITH THE USE OR DISTRIBUTION OF SUCH SCRIPTS.
# This script produces a port summary output for selected ports
import argparse
import json
from collections import OrderedDict
from os.path import (
basename,
splitext,
)
import exsh
_version_ = '1.0.0.2'
class ExosDb(object):
def __init__(self):
self.port_stats = OrderedDict()
self.port_rxerr = {}
self.port_txerr = {}
self.port_congestion = {}
self.port_qos = {}
self.proc_name = splitext(basename(__file__))[0]
def get_exos_json_data(self, cmd):
# run the command that returns JSON results
result = exsh.clicmd(cmd, True)
# convert the results to JSON
try:
json_result = json.loads(result)
except Exception as e:
print e
return []
# return the list of dictionaries
return json_result.get('data', [])
def get_port_statistics(self, portlist):
# did the first command succeed?
if self.show_ports_stats(portlist) is False:
return False
self.show_ports_txerrors()
self.show_ports_rxerrors()
self.show_ports_congestion()
self.show_ports_qos_monitor()
return True
def show_ports_stats(self, portlist):
cmd = 'debug cfgmgr show next vlan.show_ports_stats port=None portList={plist}'.format(
plist=portlist)
port_list = self.get_exos_json_data(cmd)
for row in port_list:
msg = row.get('message')
if msg:
print msg
return False
port = row.get('port')
if port:
self.port_stats[port] = row
return True
def show_ports_rxerrors(self):
# for each port, collect the rxerror coungers
for portlist in self.port_stats.keys():
cmd = 'debug cfgmgr show next vlan.show_ports_rxerrors port=None portList={plist}'.format(
plist=portlist)
port_list = self.get_exos_json_data(cmd)
for row in port_list:
port = row.get('port')
if port:
self.port_rxerr[port] = row
def show_ports_txerrors(self):
# for each port, collect the txerror coungers
for portlist in self.port_stats.keys():
cmd = 'debug cfgmgr show one vlan.show_ports_txerrors port=None portList={plist}'.format(
plist=portlist)
port_list = self.get_exos_json_data(cmd)
for row in port_list:
port = row.get('port')
if port:
self.port_txerr[port] = row
def show_ports_congestion(self):
# for each port, collect the txerror coungers
for portlist in self.port_stats.keys():
cmd = 'debug cfgmgr show one vlan.show_ports_congestion port=None portList={plist}'.format(
plist=portlist)
port_list = self.get_exos_json_data(cmd)
for row in port_list:
port = row.get('port')
if port:
self.port_congestion[port] = row
def show_ports_qos_monitor(self):
# for each port, collect the txerror coungers
for portlist in self.port_stats.keys():
cmd = 'debug cfgmgr show next vlan.show_ports_qos_monitor port=None countType=1 ingress=1 portList={plist}'.format(
plist=portlist)
port_list = self.get_exos_json_data(cmd)
for row in port_list:
port = row.get('port')
if port:
self.port_qos[port] = row
class PortSummary(ExosDb):
def __init__(self):
super(PortSummary, self).__init__()
def __call__(self):
self.get_params()
if self.args.version is True:
print self.proc_name, "Version:", _version_
return
if self.get_port_statistics(self.args.ports) is False:
return
self.build_report()
def get_params(self):
parser = argparse.ArgumentParser(prog=self.proc_name)
parser.add_argument(
'ports',
help='Ports to display. Default is all ports',
nargs='?',
default='*')
parser.add_argument(
'-v', '--version',
help='Display version',
action='store_true',
default=False)
self.args = parser.parse_args()
return self.args
def build_report(self):
hdg = [
[
'',
'In',
'Out',
'In',
'Out',
'In',
'Out',
'In',
'Out',
'In',
'Out',
'In',
'Out',
'In'
],
[
'Port',
'Octets',
'Octets',
'Ucasts',
'Ucasts',
'Mcasts',
'Mcasts',
'Bcasts',
'Bcasts',
'Discards',
'Discards',
'Errors',
'Errors',
'Unknowns'
]
]
try:
exsh.clicmd('show time')
print exsh.clicmd('show time', True),
except RuntimeError:
pass
rows = []
for port, port_stat in self.port_stats.items():
port_rxerr = self.port_rxerr.get(port)
port_txerr = self.port_txerr.get(port)
port_congestion = self.port_congestion.get(port)
port_qos = self.port_qos.get(port)
cols = []
# Port
cols.append(port)
# In Octets
cols.append(port_stat.get('rxByteCnt'))
# Out Octets
cols.append(port_stat.get('txByteCnt'))
# In Ucasts
cols.append(port_stat.get('rxPktCnt'))
# Out Ucasts
cols.append(port_stat.get('txPktCnt'))
# In Mcasts
cols.append(port_stat.get('rxMcast'))
# Out Mcasts
cols.append(port_stat.get('txMcast'))
# In Bcasts
cols.append(port_stat.get('rxBcast'))
# Out Bcasts
cols.append(port_stat.get('txBcast'))
# In Discards
cnt = 0
for fld in [
"q0congPkts",
"q1congPkts",
"q2congPkts",
"q3congPkts",
"q4congPkts",
"q5congPkts",
"q6congPkts",
"q7congPkts"]:
cnt += int(port_qos.get(fld), 0)
cols.append(cnt)
# OutDiscards
cols.append(port_congestion.get('dropPkts'))
# In Errors
cnt = 0
for fld in [
"rxAlign",
"rxCrc",
"rxFrag",
"rxJabber",
"rxLost",
"rxOver",
"rxUnder"]:
cnt += int(port_rxerr.get(fld), 0)
cols.append(cnt)
# Out Errors
cnt = 0
for fld in [
"txCollisions",
"txDeferred",
"txErrors",
"txLateCollisions",
"txLost",
"txParity"]:
cnt += int(port_txerr.get(fld), 0)
cols.append(cnt)
# In Unknowns
# TODO
cols.append(0)
rows.append(cols)
# determine the max width of data fields
max_width = 0
for row in hdg:
for entry in row:
if len(str(entry)) > max_width:
max_width = len(str(entry))
for row_entry in rows:
for entry in row_entry:
if len(str(entry)) > max_width:
max_width = len(str(entry))
# leave room for a space
max_width += 1
# print headings
row = hdg[0]
print_line = ''
for idx in range(len(row)):
if idx:
print_line += '{:^{}}'.format(row[idx], max_width)
else:
print_line += '{:<{}}'.format(row[idx], max_width)
print print_line
row = hdg[1]
print_line = ''
for idx in range(len(row)):
if idx:
print_line += '{:>{}}'.format(row[idx], max_width)
else:
print_line += '{:<{}}'.format(row[idx], max_width)
print print_line
# print heading separator
print_line = ''
for idx in range(len(hdg[0])):
if idx:
print_line += ' {}'.format('-' * (max_width - 1))
else:
print_line += '{} '.format('-' * (max_width - 1))
print print_line
# print ports stats
for row in rows:
cnt = 0
print_line = ''
for idx in range(len(row)):
if idx:
print_line += '{:>{}}'.format(row[idx], max_width)
else:
print_line += '{:<{}}'.format(row[idx], max_width)
print print_line
try:
a = PortSummary()
a()
except (KeyboardInterrupt, SystemExit, TypeError):
pass