/
vpcconcheck.py
executable file
·174 lines (128 loc) · 5.89 KB
/
vpcconcheck.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
'''
Cisco Nexus 9000 - vPC Consistency Checker Script
Author: Brantley Richbourg
email: brichbourg@gmail.com
Version: 1.1
'''
from device import Device
import xmltodict
import json
import sys
import os.path
def get_hostname(show_hw_dict):
hostname = show_hw_dict['ins_api']['outputs']['output']['body']['host_name']
return hostname
#This takes the dictionary of vpc information and returns justs the vpc ids as an integer.
def list_vpc_nums(vpcdata):
list_vpc_ids = []
for each in vpcdata:
vpc_id_int = int(each['vpc-id'])
list_vpc_ids.append(vpc_id_int)
return list_vpc_ids
#This functions displays generic vPC information and then calls vpc_con_check function to look for problems.
def vpc_check(vpcinput,vpcconinput):
vpcdata = vpcinput['ins_api']['outputs']['output']['body']
#This is used as a check to see if mismatches are found.
vpc_error_exist = False
domainid = vpcdata['vpc-domain-id']
numofvpcs = vpcdata['num-of-vpcs']
vpcrole = vpcdata['vpc-role']
print 'The vPC domain ID is %s' % domainid
print 'Total vPCs: %s' % numofvpcs
print 'Role: %s' % vpcrole
#Calls the vPC Consistency Checker function (vpc_con_check) to check the global vPC settings
vpc_con_check(None,vpcconinput)
vpclist = vpcinput['ins_api']['outputs']['output']['body']['TABLE_vpc']['ROW_vpc']
return vpclist
#This function is the meat of the script. This is where the values are compared to find mismatches with vPC settings.
def vpc_con_check (vpcid,vpcconinput):
#This is used later as a check to see if mismatches are found. Used for controlling print output later
vpc_error_exist = False
#This piece check to see if the data is from the global setting or not. It is used to control print output later.
vpcglobal_checker = vpcconinput['ins_api']['outputs']['output']['input']
if vpcglobal_checker == 'show vpc consistency-parameters global':
is_vpc_data_global = True
else:
is_vpc_data_global = False
#This puts the data we need to check in a dictonary
vpcdata = vpcconinput['ins_api']['outputs']['output']['body']['TABLE_vpc_consistency']['ROW_vpc_consistency']
#For loop to run over each dictionary of vPCs that are configured and to look for mismatches
for each in vpcdata:
param_name = each ['vpc-param-name']
localvalue = each ['vpc-param-local-val']
peervalue = each ['vpc-param-peer-val']
if localvalue != peervalue:
print '*' * 30
print '*MISMATCH DETECTED in vPC', vpcid
print '*' * 30
print 'Value Name:',param_name
print 'Local Value: ', localvalue
print 'Peer Value:', peervalue
print '*' * 30, '\n'
#This will set a variable to show there was a vPC mismatch.
vpc_error_exist = True
#This checks to see if certain conditions are present so the correct message is printed when no issues are detected and returns a Boolean value to the main function.
if vpc_error_exist == False:
if is_vpc_data_global == True:
print '\n===No global consistency issues===\n'
if vpc_error_exist == False:
if is_vpc_data_global == False:
return False #This will return True will be used to tell the main script that at least one mismatch was found for a vPC.
else:
return True #this will return False to show mismatches were found.
def main():
args = sys.argv
if len(args) == 4:
#This section will check to see if the file pass to Python in a argument actually exists
if os.path.exists(args[1]):
switch_ips = open(args[1]).read().splitlines()
else:
print 'File ', os.path.realpath(args[1]), 'does not exist. Please try again'
sys.exit(1)
#For look to open each switch in the list of IP addresses passed via CLI arguments
for switch_ip in switch_ips:
switch = Device(ip=switch_ip,username=args[2],password=args[3])
switch.open()
#These lines gat the XML output from the XML API on the N9K
getswitchhardwaredata = switch.show('show hardware')
getswitchvpcdata = switch.show('show vpc')
getvpccondata = switch.show('show vpc consistency-parameters global')
#These lines parse the XML into JSON format
getswitchvpcdatajson = xmltodict.parse(getswitchvpcdata[1])
getswitchhardwaredatajson = xmltodict.parse(getswitchhardwaredata[1])
getvpccondatajson = xmltodict.parse(getvpccondata[1])
#This grabs the name on the switch
switchname = get_hostname(getswitchhardwaredatajson)
#<<<This is the start of outputting information regarding a switch>>>
print '=' * 20, switchname, '=' * 20
#This sends the show vpc command and show vpc con global command JSON data to the function to check for problems.
#This will also return a list of the indivdual vPCs that are configured on the switch.
vpcinfo = vpc_check(getswitchvpcdatajson,getvpccondatajson)
#Calls a function to return the vPC IDs from the switch.
vpc_list = list_vpc_nums(vpcinfo)
#Now we take the list of vPC IDs and call the NXAPI with the command show vpc con vpc X
vpc_error_count = 0
for each in vpc_list:
stringeach = str(each)
string_cmd = 'show vpc consistency-parameters vpc '
string_cmd += stringeach
#This line takes the string_cmd variable and uses that to run the commmand with the customer VPC ID
getvpccondetail = switch.show(string_cmd)
#Parse the XML to JSON
getvpccondetailjson = xmltodict.parse(getvpccondetail[1])
#This calls a function to check each indivdual vPC for inconsistencies
is_vpc_error = vpc_con_check(each,getvpccondetailjson)
if is_vpc_error == True:
vpc_error_count +=1
#This section will monitor if any vPCs returned mismatches, if none did, then it prints there were no errors.
if is_vpc_error == False:#
if vpc_error_count == 0:
print '===No vPC consistency issues===\n'
#<<<This is the end of outputting information regarding a switch>>>
print '=' * 20, switchname, '=' * 20 , '\n'
else:
print 'ERROR: Invalid syntax\n'\
'Example: \"python vPCconcheck.py file.txt username password\"'
sys.exit(1)
if __name__ == "__main__":
main()