-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
443 lines (363 loc) · 16.4 KB
/
parser.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
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
import argparse
from tabulate import tabulate
from cumulus_parser import CumulusDevice
from inventory.management import *
from utils import bcolors, logger
from exceptions import *
from multiprocessing .dummy import Pool as Threadpool
def get_credentials_by_key(key):
if os.path.isfile(CREDENTIALS_FILENAME):
credentials = read_yaml_file(CREDENTIALS_FILENAME)
else:
logger.error(f"Cannot find specified credentials file: {CREDENTIALS_FILENAME}")
raise CredentialsFileNotFound
credentials = credentials[key]
return {
'username': credentials['username'],
'password': credentials['password']
}
def multithread_command(command, hosts):
pool = Threadpool(4)
host_ips = [INVENTORY_HOSTS[host] for host in hosts]
def _send_multithread_command(host):
try:
device = CumulusDevice(
hostname=host,
username=creds['username'],
password=creds['password']
)
result = device.send_command([command])[0]
result['host'] = host
result['hostname'] = find_hostname_from_ip_address(host)
except ConnectionError:
result = None
return result
results = pool.map(_send_multithread_command, host_ips)
pool.close()
pool.join()
results = [result for result in results if result]
return results
def search_interface_configuration(iface, results):
iface = iface.lower()
interface_search_results = []
for result in results:
found = False
iface_config = {}
hostname = find_hostname_from_ip_address(result['host'])
for interface in result['output']:
if interface['name'].lower() == iface:
found = True
iface_config.update(interface)
interface_search_results.append(dict(
hostname=hostname,
ip=result['host'],
configured=found,
configuration=iface_config
))
return interface_search_results
def search_mac_address(mac, hosts):
mac = mac.lower()
mac_search_results = []
results = multithread_command('show bridge macs', hosts)
for result in results:
found = False
hostname = find_hostname_from_ip_address(result['host'])
for entry in result['output']:
if entry['mac'].lower() == mac:
found = True
mac_search_results.append(dict(
hostname=hostname,
ip=result['host'],
found=found,
))
return mac_search_results
def check_bgp_neighbors(results):
required_peers = []
for result in results:
hostname = get_hostname_by_ip(result['host'])
required_peers_dict = {'hostname': hostname,'ipv4 unicast': [], 'l2vpn evpn': []}
bgp_validators = get_validation_by_hostname(hostname, 'bgp')
# get validators
for family in ADDRESS_FAMILIES:
if bgp_validators:
if bgp_validators.get('bgp_neighbors'):
if bgp_validators['bgp_neighbors'].get(family):
if bgp_validators['bgp_neighbors'][family].get('peers'):
for i, h in bgp_validators['bgp_neighbors'][family]['peers'].items():
required_peers_dict[family].append({
'interface': i,
'peer_hostname': h,
'found': False,
'established': False
})
# get current state
for family in ADDRESS_FAMILIES:
if result.get('output'):
if result['output'][family]:
if result['output'][family].get('peers'):
for interface, details in result['output'][family]['peers'].items():
for required_peer in required_peers_dict[family]:
if details.get('hostname'):
if interface.upper() == required_peer['interface'].upper() and \
details['hostname'].upper() == required_peer['peer_hostname'].upper():
required_peer['found'] = True
if details['state'].upper() == 'ESTABLISHED':
required_peer['established'] = True
break
required_peers.append(required_peers_dict)
return required_peers
def check_clag(results):
#hosts = ['SFD-C319-SPN-SN2700-01', 'SFD-C320-BLF-S4048-01']
down_peers = []
tr = {
'hostname': [],
'status': [],
'role': [],
'vxlan anycast ip': [],
'backup active': [],
'backup IP': []
}
cit = {
'hostname': [],
'interface': [],
'peerIf': [],
'clagId': [],
'operstate': [],
'status': []
}
for entry in results:
hostname = get_hostname_by_ip(entry['host'])
tr['hostname'].append(hostname)
ce = {
'hostname': '',
'alive': '',
'role': '',
'vxlan anycast ip': '',
'backup active': '',
'backup IP': ''
}
if entry['output']:
#print(json.dumps(entry['output']['clagIntfs'], indent=4))
alive = entry['output']['status']['peerAlive']
role = entry['output']['status']['ourRole']
vxlan_anycast_ip = entry['output']['status'].get('vxlanAnycast', 'not configured')
backup_active = entry['output']['status'].get('backupActive', 'not configured')
backup_ip = entry['output']['status'].get('backupIp', 'not configured')
clag_interfaces = entry['output']['clagIntfs']
ce['hostname'] = hostname
ce['alive'] = alive
ce['role'] = role
ce['vxlan anycast ip'] = vxlan_anycast_ip
ce['backup active'] = backup_active
ce['backup IP'] = backup_ip
tr['role'].append(role)
tr['vxlan anycast ip'].append(vxlan_anycast_ip)
tr['backup active'].append(backup_active)
tr['backup IP'].append(backup_ip)
if alive:
tr['status'].append('up')
else:
tr['status'].append(bcolors.FAIL + 'down' + bcolors.ENDC)
down_peers.append(ce)
# get individual clag interfaces
for k, v in clag_interfaces.items():
cit['hostname'].append(hostname)
cit['interface'].append(k),
cit['clagId'].append(v.get('clagId'))
cit['operstate'].append(v.get('operstate'))
cit['peerIf'].append(v.get('peerIf'))
cit['status'].append(v.get('status'))
else:
ce['hostname'] = hostname
ce['alive'] = 'not configured'
ce['role'] = '-'
ce['vxlan anycast ip'] = 'not configured'
ce['backup active'] = '-'
ce['backup IP'] = '-'
down_peers.append(ce)
tr['status'].append('not configured')
tr['role'].append('-')
tr['vxlan anycast ip'].append('not configured')
tr['backup active'].append('-')
tr['backup IP'].append('-')
overall_mlag_status_table = tabulate(tr,
headers="keys", tablefmt="simple")
overall_clag_interfaces_table = tabulate(cit,
headers="keys", tablefmt="simple")
return dict(
down_peers=down_peers,
#overal_mlag_status=tr,
#overall_clag_interfaces_status=cit,
overall_mlag_status_table=overall_mlag_status_table,
overall_clag_interfaces_table=overall_clag_interfaces_table
)
def cumulus_interface_to_ansible_vars(interfaces):
""" This takes in json/dict data and converts it to yaml
For use with Ansible vars """
ansible_structure = {
'switchports': {},
'bridges': {},
'bonds': {},
'vlans': {},
'vnis': {}
}
# classify interfaces into their respective types
# then create yaml structure
for interface in interfaces:
if interface.get('bridge_ports'):
name = interface.pop('name')
ansible_structure['bridges'][name] = interface
for port in interface['bridge_ports']:
if port.lower().startswith('vni-'):
vni = int(port.split('vni-')[1])
break
# find vlan iterating through ports for <interface>.<vlan>
for port in interface['bridge_ports']:
if '.' in port:
vlan = int(port.split('.')[1])
ansible_structure['bridges'][name]['vlan_id'] = vlan
ansible_structure['vlans'].update({vlan: {'vni': vni}})
break
interface['bridge_ports'] = \
[port.split('.')[0] for port in interface['bridge_ports']]
elif interface.get('bond_mode') or interface.get('bond_slaves'):
name = interface.pop('name')
ansible_structure['bonds'][name] = interface
elif interface.get('vxlan_id'):
name = interface.pop('name')
ansible_structure['vnis'][name] = interface
else:
name = interface.pop('name')
ansible_structure['switchports'][name] = interface
return ansible_structure
def gen_args():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='Arguments', dest='cumulus_crawler')
subparser_search = subparsers.add_parser('search', help='Search')
subparser_check = subparsers.add_parser('check', help='Check Status, such as BGP')
mgroup_search = subparser_search.add_mutually_exclusive_group(required=True)
#mgroup_check = subparser_check.add_mutually_exclusive_group(required=True)
mgroup_search.add_argument("-m", "--mac", help="Search MAC address", dest='mac')
mgroup_search.add_argument("-i", "--iface", help="Search Interface", dest='iface')
subparser_check.add_argument("-b", "--bgp", help="Check BGP", dest='check_bgp', action='store_true')
subparser_check.add_argument("-c", "--clag", help="Check CLAG", dest='check_clag', action='store_true')
subparser_check.add_argument("-v", "--verbose", help="Verbose output", dest='verbose', action='store_true')
return parser.parse_args()
if __name__ == '__main__':
# get host inventory
creds = get_credentials_by_key(VENDOR)
hosts = [host for host in INVENTORY_HOSTS]
# device = CumulusDevice(
# hostname=INVENTORY_HOSTS['CSS1A-106-LEF-01'],
# #hostname=INVENTORY_HOSTS['CSS1A-106-LEF-01'],
# username=creds['username'],
# password=creds['password']
# )
# interfaces = device.show_interfaces_configuration()
# print(yaml.dump(cumulus_interface_to_ansible_vars(interfaces), indent=2))
#device.show_interfaces_status()
# r = search_mac_address('5c:f9:dd:ef:ab:82', hosts)
# print(r)
#
args = gen_args()
if not args.cumulus_crawler:
print("type --help for help with this command.")
else:
if args.cumulus_crawler == 'search':
if args.mac:
results = search_mac_address(args.mac, hosts)
print('\n')
tabulated_results = {}
for entry in results:
for k, v in entry.items():
k = k.upper()
if not tabulated_results.get(k):
tabulated_results[k] = []
tabulated_results[k].append(v)
if tabulated_results:
if tabulated_results.get('FOUND'):
found_with_bcolor = []
for i in tabulated_results['FOUND']:
if i:
found_with_bcolor.append(bcolors.OKBLUE +
"Yes" + bcolors.ENDC)
else:
found_with_bcolor.append(bcolors.FAIL +
"No" + bcolors.ENDC)
tabulated_results['FOUND'] = found_with_bcolor
tabulated_results = tabulate(tabulated_results,
headers="keys", tablefmt="fancy_grid")
print(tabulated_results)
elif args.iface:
results = search_interface_configuration(
args.iface,
multithread_command('show interfaces configuration',
hosts))
print(results)
elif args.cumulus_crawler == 'check':
if args.check_bgp:
#results = check_bgp_neighbors(['CSS1A-109-LEF-03', 'CSS1A-109-LEF-04'])
#results = check_bgp_neighbors(INVENTORY_HOSTS)
results = check_bgp_neighbors(
results=multithread_command('show bgp summary', hosts))
down_peers = []
tabulated_results = None
tr = {
'hostname': [],
'address family': [],
'interface': [],
'peer hostname': [],
'configured': [],
'status': []
}
for entry in results:
hostname = entry['hostname']
for family in ADDRESS_FAMILIES:
for peer in entry[family]:
peer['hostname'] = hostname
tr['hostname'].append(hostname)
tr['address family'].append(family)
tr['interface'].append(peer['interface'])
tr['peer hostname'].append(peer['peer_hostname'])
if peer['found']:
tr['configured'].append('yes')
else:
down_peers.append(peer)
tr['configured'].append(bcolors.FAIL + 'no' + bcolors.ENDC)
if peer['established']:
tr['status'].append('Established')
else:
down_peers.append(peer)
tr['status'].append(bcolors.FAIL + 'Not Established' + bcolors.ENDC)
tabulated_table = tabulate(tr,
headers="keys", tablefmt="simple")
if args.verbose:
print('\n', tabulated_table)
print('\n')
else:
if not down_peers:
print("\n", f"{bcolors.OKBLUE} --> All BGP neighbors established! {bcolors.ENDC} \n")
else:
for peer in down_peers:
if not peer['found']:
reason = "is not configured"
elif not peer['established']:
reason = "peering is not established"
print("\n", f"{bcolors.FAIL} --> {peer['hostname']}'s required peer "
f"on interface {peer['interface']} "
f"to {peer['peer_hostname']} failed check because \n {' ' * 4} {reason}. {bcolors.ENDC}", "\n")
elif args.check_clag:
clag_status = check_clag(
results=multithread_command('show clag', INVENTORY_HOSTS))
if args.verbose:
print(f"\n{clag_status['overall_clag_interfaces_table']}")
print("\nSwitch CLAG Status: ")
print(f"\n{clag_status['overall_mlag_status_table']}", "\n")
else:
if clag_status['down_peers']:
for dp in clag_status['down_peers']:
print("\n", f"{bcolors.FAIL} --> {dp['hostname']} failed check because system clag (mlag) is down. "
f"The VXLAN Anycast IP is: {dp['vxlan anycast ip']} {bcolors.ENDC}", "\n")
print("\n")
else:
print("Please enter argument. Use -h for help.")