forked from extremenetworks/ExtremeScripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshowlag.py
More file actions
494 lines (441 loc) · 19.9 KB
/
Copy pathshowlag.py
File metadata and controls
494 lines (441 loc) · 19.9 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# 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.
'''
After downloading to an EXOS switch, the help command shows the different options available
# run script showlag.py -h
usage: showlag [-h] [-d] [cmd [cmd ...]]
positional arguments:
cmd statistics, rxerrors, txerrors, utilization, all
Use the same format as the "show port" EXOS commands
optional arguments:
-h, --help show this help message and exit
-d, --debug Show debug information
The 'all' command displays the output for:
statistics
rxerrors
txerrors
utilization
# run script showlag.py statistics
LAG Tx Pkt Tx Byte Rx Pkt Rx Byte Rx Pkt Rx Pkt Tx Pkt Tx Pkt
Count Count Count Count Bcast Mcast Bcast Mcast
------- ------- ------- ------- ------- ------- ------- ------- -------
lag1 0 0 0 0 0 0 0 0
lag10 0 0 0 0 0 0 0 0
lag20 0 0 0 0 0 0 0 0
------- ------- ------- ------- ------- ------- ------- ------- -------
(Private) X460G2-24t-10G4.48 # run script showlag.py rxerrors
LAG Rx Rx Rx Rx Rx Rx Rx
Crc Over Under Frag Jabber Align Lost
------ ------ ------ ------ ------ ------ ------ ------
lag1 0 0 0 0 0 0 0
lag10 0 0 0 0 0 0 0
lag20 0 0 0 0 0 0 0
------ ------ ------ ------ ------ ------ ------ ------
(Private) X460G2-24t-10G4.49 # run script showlag.py -h
usage: showlag [-h] [-d] [cmd [cmd ...]]
positional arguments:
cmd statistics, rxerrors, txerrors, utilization, all
Use the same format as the "show port" EXOS commands
optional arguments:
-h, --help show this help message and exit
-d, --debug Show debug information
(Private) X460G2-24t-10G4.50 # run script showlag.py all
LAG Tx Tx Tx Tx Tx Tx
Coll Late coll Deferred Errors Lost Parity
--------- --------- --------- --------- --------- --------- ---------
lag1 0 0 0 0 0 0
lag10 0 0 0 0 0 0
lag20 0 0 0 0 0 0
--------- --------- --------- --------- --------- --------- ---------
LAG Link Rx Peak Rx Tx Peak Tx
Speed % bandwidth % bandwidth % bandwidth % bandwidth
----------- ----------- ----------- ----------- ----------- -----------
lag1 None 0.00000 0.00000 0.00000 0.00000
lag10 None 0.00000 0.00000 0.00000 0.00000
lag20 None 0.00000 0.00000 0.00000 0.00000
----------- ----------- ----------- ----------- ----------- -----------
LAG Tx Pkt Tx Byte Rx Pkt Rx Byte Rx Pkt Rx Pkt Tx Pkt Tx Pkt
Count Count Count Count Bcast Mcast Bcast Mcast
------- ------- ------- ------- ------- ------- ------- ------- -------
lag1 0 0 0 0 0 0 0 0
lag10 0 0 0 0 0 0 0 0
lag20 0 0 0 0 0 0 0 0
------- ------- ------- ------- ------- ------- ------- ------- -------
LAG Rx Rx Rx Rx Rx Rx Rx
Crc Over Under Frag Jabber Align Lost
------ ------ ------ ------ ------ ------ ------ ------
lag1 0 0 0 0 0 0 0
lag10 0 0 0 0 0 0 0
lag20 0 0 0 0 0 0 0
------ ------ ------ ------ ------ ------ ------ ------
'''
import sys
import exsh
import argparse
import json
import logging
from argparse import (
ArgumentParser
)
from os.path import (
basename,
splitext,
)
__version__ = '1.0.0.3'
PROCESS_NAME = splitext(basename(__file__))[0]
class ExosDb(object):
def __init__(self):
self.proc_name = splitext(basename(__file__))[0]
self.port_lag = {}
self.cmdb = {}
self.lagdb = {}
self.lagnamedb = {}
def json_clicmd(self, cmd):
# issue debug cfgmgr CLI command to EXOS and return the JSON data
self.log.debug(cmd)
json_result = exsh.clicmd(cmd, capture=True)
try:
json_dict = json.loads(json_result)
except Exception:
self.log.debug('JSON format error')
return None, None
# extract and return the data list
return (json_dict.get("class"), json_dict.get("data"))
def build_db(self):
# extract the LAGs and create a port list of all lag memberships
cm_class, cm_list = self.json_clicmd('debug cfgmgr show next vlan.ls_ports_show')
# store the command results in the cmdb
db_list = self.cmdb.setdefault(cm_class, [])
db_list += cm_list
# for the lags, build a list of member ports per lag
for row in cm_list:
if row.get("status") not in ["MORE", "SUCCESS"]:
continue
loadShareMaster = row.get("loadShareMaster")
port = row.get("port")
# Collect a list of member ports by lag master
lag_list = self.lagdb.setdefault(loadShareMaster, [])
lag_list.append(port)
# build cross reference of individual ports pointing to the lag masters
self.port_lag[port] = loadShareMaster
# build up CM data for LAGs and ports
for cmd in [
'debug cfgmgr show one vlan.show_ports_info portList={plist} port=None',
'debug cfgmgr show one vlan.show_ports_config portList={plist} port=None',
'debug cfgmgr show one vlan.show_ports_utilization portList={plist} port=None',
'debug cfgmgr show one vlan.show_ports_stats portList={plist} port=None',
'debug cfgmgr show one vlan.show_ports_rxerrors portList={plist} port=None',
'debug cfgmgr show one vlan.show_ports_txerrors portList={plist} port=None',
]:
for port in self.port_lag.keys():
cm_class, cm_list = self.json_clicmd(cmd.format(plist=port))
db_list = self.cmdb.setdefault(cm_class, [])
row = cm_list[0]
if row.get("status") not in ["MORE", "SUCCESS"]:
continue
db_list.append(row)
self.log.debug(json.dumps(self.cmdb, indent=2))
# build lag name to port dict
# lag name is the master port display string
# when no display string is present, 'lagnn' where nn is the port number
for cm_row in self.cmdb.get('show_ports_info', []):
if cm_row.get("status") not in ["MORE", "SUCCESS"]:
continue
port = cm_row.get('port')
if port in self.lagdb:
lag_name = cm_row.get("displayString")
if lag_name is None:
lag_name = 'lag{}'.format(port)
self.lagnamedb[lag_name] = port
self.log.debug(json.dumps(self.lagnamedb, indent=2))
class ShowLag(ExosDb):
def __init__(self):
super(ShowLag, self).__init__()
self.args = None
self.handler = logging.StreamHandler(sys.stderr)
self.handler.setLevel(logging.INFO)
self.handler.setFormatter(logging.Formatter(
'%(levelname)s:%(threadName)s:%(name)s:%(funcName)s:%(lineno)s:: '
'%(message)s'))
self.log = logging.getLogger(self.__class__.__name__)
self.log.setLevel(logging.INFO)
if not len(self.log.handlers):
self.log.addHandler(self.handler)
self.REPORT_STATS = 'statistics'
self.REPORT_RXERR = 'rxerrors'
self.REPORT_TXERR = 'txerrors'
self.REPORT_UTIL = 'utilization'
self.REPORT_ALL = 'all'
self.REPORT_TYPES = [
self.REPORT_STATS,
self.REPORT_RXERR,
self.REPORT_TXERR,
self.REPORT_UTIL,
self.REPORT_ALL
]
self.report_func_dict = {
self.REPORT_STATS: self.lag_statistics,
self.REPORT_RXERR: self.lag_rxerrors,
self.REPORT_TXERR: self.lag_txerrors,
self.REPORT_UTIL: self.lag_utilization,
self.REPORT_ALL: self.lag_all,
}
self.report_type = None
self.report_lagnames = []
def __call__(self, args, **kwargs):
self.args = args
if args.debug is True:
self.handler.setLevel(logging.DEBUG)
self.log.setLevel(logging.DEBUG)
self.log.debug(args)
self.build_db()
self.get_params()
if self.report_type is None:
print 'No report type specified'
print 'Report types are: {}'.format(', '.join(self.REPORT_TYPES))
return
func = self.report_func_dict.get(self.report_type[0])
if func:
func()
else:
print 'Program error. No support for {} report'.format(self.report_type)
def get_params(self):
for word in self.args.cmd:
match_word = [s for s in self.REPORT_TYPES + self.lagnamedb.keys() if s.startswith(word)]
if not match_word:
print 'Unknown input {}'.format(word)
return
# look for report type
for word in self.args.cmd:
self.report_type = [s for s in self.REPORT_TYPES if s.startswith(word)]
if self.report_type:
self.args.cmd.remove(word)
break
# look for lag names
for word in self.args.cmd:
self.report_lagnames += [s for s in self.lagnamedb.keys() if s.startswith(word)]
self.log.debug('word={}, report_type={}, report_lagnames={}'.format(
word, self.report_type, self.report_lagnames))
# report all lags
if not self.report_lagnames:
self.report_lagnames = sorted(self.lagnamedb.keys())
self.log.debug('lag list {}'.format(self.report_lagnames))
self.log.debug('report type {}'.format(self.report_type))
def compute_total(self, data_type, port_field_name, port_list):
sum_dict = {}
cm_data_list = self.cmdb.get(data_type)
if cm_data_list is None:
print 'Program error. Unknown data structure {}. Contact Extreme'.format(data_type)
return None
for cm_row in cm_data_list:
port = cm_row.get(port_field_name)
self.log.debug(cm_row)
if str(port) not in port_list:
self.log.debug('Continue')
continue
for k, v in cm_row.items():
try:
num = int(v)
ttl = sum_dict.setdefault(k, 0)
ttl += num
sum_dict[k] = ttl
continue
except Exception:
pass
try:
num = float(v)
ttl = sum_dict.setdefault(k, 0.0)
ttl += num
sum_dict[k] = ttl
continue
except Exception:
pass
return sum_dict
def summarize_lag_data(self, data_type, port_field_name):
self.log.debug('Called {} {}'.format(data_type, port_field_name))
sum_list = []
for lagname in self.report_lagnames:
master_port = self.lagnamedb.get(lagname)
port_list = self.lagdb.get(master_port)
sum_dict = self.compute_total(data_type, port_field_name, port_list)
sum_list.append({lagname: sum_dict})
self.log.debug(json.dumps(sum_list, indent=2))
return sum_list
def create_report(self, lag_summary_list, report_spec, report_footer):
# go through all of the data fields to determine the longest value
# set a minimum width
max_width = 6
for hdg1, hdg2, data_field_name in report_spec:
for hdg in [hdg1, hdg2]:
if len(hdg) > max_width:
max_width = len(hdg)
for lag_summary_row in lag_summary_list:
for lagname, sum_dict in lag_summary_row.items():
if len(lagname) > max_width:
max_width = len(lagname)
for field_name, field_value in sum_dict.items():
if len(str(field_value)) > max_width:
max_width = len(str(field_value))
# max_width is now set to the widest field value
# print field headings
line = ''
for hdg1, hdg2, data_field_name in report_spec:
line += '{x:>{width}} '.format(x=hdg1, width=max_width)
print line
line = ''
for hdg1, hdg2, data_field_name in report_spec:
line += '{x:>{width}} '.format(x=hdg2, width=max_width)
print line
line = ''
for hdg1, hdg2, data_field_name in report_spec:
line += '{x:>{width}} '.format(x='-' * max_width, width=max_width)
print line
for lag_summary_row in lag_summary_list:
line = ''
for k, v in lag_summary_row.items():
for hdg1, hdg2, data_field_name in report_spec:
if data_field_name == '<key>':
line += '{x:>{width}} '.format(x=k, width=max_width)
else:
line += '{x:>{width}} '.format(x=v.get(data_field_name), width=max_width)
print line
line = ''
for hdg1, hdg2, data_field_name in report_spec:
line += '{x:>{width}} '.format(x='-' * max_width, width=max_width)
print line
line = ''
for line in report_footer:
print line
def lag_all(self):
for report_type, func in self.report_func_dict.items():
if report_type == self.REPORT_ALL:
continue
func()
print '\n'
def lag_statistics(self):
self.log.debug('Called')
lag_summary_list = self.summarize_lag_data('show_ports_stats', 'port')
# report spec is (hdg1, hdg2, data field name)
report_spec = [
('LAG', '', '<key>'), # use the data structure key
('Tx Pkt', 'Count', 'txPktCnt'),
('Tx Byte', 'Count', 'txByteCnt'),
('Rx Pkt', 'Count', 'rxPktCnt'),
('Rx Byte', 'Count', 'rxByteCnt'),
('Rx Pkt', 'Bcast', 'rxBcast'),
('Rx Pkt', 'Mcast', 'rxMcast'),
('Tx Pkt', 'Bcast', 'txBcast'),
('Tx Pkt', 'Mcast', 'txMcast'),
]
report_footer = []
self.create_report(lag_summary_list, report_spec, report_footer)
return
def lag_rxerrors(self):
self.log.debug('Called')
lag_summary_list = self.summarize_lag_data('show_ports_rxerrors', 'port')
# report spec is (hdg1, hdg2, data field name)
report_spec = [
('LAG', '', '<key>'), # use the data structure key
('Rx', 'Crc', 'rxCrc'),
('Rx', 'Over', 'rxOver'),
('Rx', 'Under', 'rxUnder'),
('Rx', 'Frag', 'rxFrag'),
('Rx', 'Jabber', 'rxJabber'),
('Rx', 'Align', 'rxAlign'),
('Rx', 'Lost', 'rxLost'),
]
report_footer = []
self.create_report(lag_summary_list, report_spec, report_footer)
return
def lag_txerrors(self):
self.log.debug('Called')
lag_summary_list = self.summarize_lag_data('show_ports_txerrors', 'port')
# report spec is (hdg1, hdg2, data field name)
report_spec = [
('LAG', '', '<key>'), # use the data structure key
('Tx', 'Coll', 'txCollisions'),
('Tx', 'Late coll', 'txLateCollisions'),
('Tx', 'Deferred', 'txDeferred'),
('Tx', 'Errors', 'txErrors'),
('Tx', 'Lost', 'txLost'),
('Tx', 'Parity', 'txParity'),
]
report_footer = []
self.create_report(lag_summary_list, report_spec, report_footer)
return
def percent(self, bytes_per_sec, link_speed):
try:
return '{:>.5f}'.format(((float(bytes_per_sec)/float(link_speed))*100)/125000)
except Exception:
return '{:>.5f}'.format(0.0)
def lag_utilization(self):
self.log.debug('Called')
port_config_summary_list = self.summarize_lag_data('show_ports_config', 'port')
lag_summary_list = self.summarize_lag_data('show_ports_utilization', 'port')
for lag_summary_row in lag_summary_list:
for lagname, lag_dict in lag_summary_row.items():
# find the matching lag in port_config_summary_list
for port_row in port_config_summary_list:
for port_lagname, port_dict in port_row.items():
if lagname == port_lagname:
lag_dict['linkSpeed'] = port_dict.get('speedActual')
break
lag_dict['rxBwPercent'] = self.percent(
lag_dict.get('rxBytesPerSec'),
lag_dict.get('linkSpeed'),
)
lag_dict['rxBwPeakPercent'] = self.percent(
lag_dict.get('rxPeakBytesPerSec'),
lag_dict.get('linkSpeed'),
)
lag_dict['txBwPercent'] = self.percent(
lag_dict.get('rxBytesPerSec'),
lag_dict.get('linkSpeed'),
)
lag_dict['txBwPeakPercent'] = self.percent(
lag_dict.get('txPeakBytesPerSec'),
lag_dict.get('linkSpeed'),
)
report_spec = [
('LAG', '', '<key>'), # use the data structure key
('Link', 'Speed', 'linkSpeed'),
('Rx', '% bandwidth', 'rxBwPercent'),
('Peak Rx', '% bandwidth', 'rxBwPeakPercent'),
('Tx', '% bandwidth', 'txBwPercent'),
('Peak Tx', '% bandwidth', 'txBwPeakPercent'),
]
report_footer = []
self.create_report(lag_summary_list, report_spec, report_footer)
return
def get_params():
# These are the command line options for clone
# Both master and client options are evalated here
parser = ArgumentParser(
prog=PROCESS_NAME,
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-d', '--debug',
help='Show debug information',
action='store_true',
default=False)
parser.add_argument('cmd',
help='statistics, rxerrors, txerrors, utilization, all\n'
'Use the same format as the "show port" EXOS commands',
nargs='*',
default=None)
return parser.parse_args()
if __name__ == '__main__':
args = get_params()
showlag = ShowLag()
try:
showlag(args)
except SystemExit, KeyboardInterrupt:
pass