-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitoring_rollback.py
310 lines (284 loc) · 10.4 KB
/
monitoring_rollback.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
from pprint import pprint
import requests
import credentials
import demodbdata
import mongodb_auth
import thousandEyesAppHealth
data =[]
db = []
def compare_alarm():
""" Compares the alarms from vManage and DNAC and returns the summary and details of the comparison
parameters: None
returns: [alarm_summary, alarm_details]
"""
removed = []
changed = []
added = []
same=[]
vManageAlarms_data = data["vManageAlarms"]
DnacAlarms_data = data["DnacAlarms"]
new_alarms = vManageAlarms_data.copy() + DnacAlarms_data.copy()
collection = db['Alarms']
old_data = collection.find()
for item in old_data:
count = 0
for item2 in new_alarms:
if(item["name"]==item2["name"] and item["summary"]==item2["summary"]):
if(item["severity"]==item2["severity"]):
del item["_id"]
same.append(item.copy())
del item
new_alarms.remove(item2)
break
else:
del item["_id"]
changed.append([item.copy(),item2.copy()])
del item
new_alarms.remove(item2)
break
elif(item["name"]!=item2["name"] or item["summary"]!=item2["summary"] ):
count+=1
if(count==len(new_alarms)):
del item["_id"]
removed.append(item.copy())
del item
break
added = new_alarms
alarm_summary = {
"changed":len(changed),
"added":len(added),
"removed":len(removed),
"same":len(same),
"total":len(changed)+len(added)+len(same)
}
alarm_details = {
"changed":changed,
"added":added,
"removed":removed,
"same":same,
"total":changed + added + same
}
return [alarm_summary, alarm_details]
def compare_networkhealth():
""" Compares the network health from vManage and DNAC and returns the summary and details of the comparison
parameters: None
returns: [networkhealth_summary, networkhealth_details]
"""
removed = []
changed = []
added = []
same=[]
vManageHealth_data = data["vManageHealth"]
DnacHealth_data = data["DnacHealth"]
tmp = {}
result = int(vManageHealth_data[0]["networkHealth"]) * int(DnacHealth_data[0]["networkHealth"])
if(result == 0):
tmp["NetworkHealth"] = "critical"
elif(result == -1):
tmp["NetworkHealth"] = "moderate"
else:
tmp["NetworkHealth"] = "positive"
newdata = [tmp].copy()
collection = db['NetworkHealth']
olddata = collection.find()
olddata = list(olddata)
if(olddata==[]):
olddata = [{"NetworkHealth":"No Data", "_id":"nodata"}]
for item in olddata:
for item2 in newdata:
if(item["NetworkHealth"]==item2["NetworkHealth"]):
del item["_id"]
same.append(item.copy())
del item
newdata.remove(item2)
break
else:
del item["_id"]
changed.append([item.copy(),item2.copy()])
del item
newdata.remove(item2)
break
networkhealth_summary = {
"changed":len(changed),
"added":0,
"removed":0,
"same":len(same),
"total":len(changed)+len(added)+len(same)
}
networkhealth_details = {
"changed":changed,
"added":[],
"removed":[],
"same":same,
"total":changed + added + same
}
return [networkhealth_summary, networkhealth_details]
def compare_devicehealth():
""" Compares the device health from vManage and DNAC and returns the summary and details of the comparison
parameters: None
returns: [devicehealth_summary, devicehealth_details]
"""
removed = []
changed = []
added = []
same=[]
vManageHealth_data = data["vManageHealth"][1]
DnacHealth_data = data["DnacHealth"][1]
newdata = vManageHealth_data.copy() + DnacHealth_data.copy()
collection = db['DeviceHealth']
olddata = collection.find()
for item in olddata:
count = 0
for item2 in newdata:
if(item["url"]==item2["url"]):
if(item["deviceHealth"]==item2["deviceHealth"]):
del item["_id"]
same.append(item.copy())
del item
newdata.remove(item2)
break
else:
del item["_id"]
changed.append([item.copy(),item2.copy()])
del item
newdata.remove(item2)
break
elif(item["url"]!=item2["url"]):
count+=1
if(count==len(newdata)):
del item["_id"]
removed.append(item.copy())
del item
break
added = newdata
devicehealth_summary = {
"changed":len(changed),
"added":len(added),
"removed":len(removed),
"same":len(same),
"total":len(changed)+len(added)+len(same)
}
devicehealth_details = {
"changed":changed,
"added":added,
"removed":removed,
"same":same,
"total":changed + added + same
}
return [devicehealth_summary, devicehealth_details]
def compare_applicationhealth():
""" Compares the application health from vManage, DNAC and ThousandEyes and returns the summary and details of the comparison
parameters: None
returns: [applicationhealth_summary, applicationhealth_details]
"""
removed = []
changed = []
added = []
same=[]
vManageNWPI_readTrace_sloDetails = data["vManageNWPI_readTrace"][0]
vManageNWPIAppHealth_data = data["vManageNWPI_readTrace"][1]
DnacAppHealth_data = data["DnacAppHealth"]
thousandEyesAppHealth_data = thousandEyesAppHealth.get_data()
if(vManageNWPIAppHealth_data==[] and DnacAppHealth_data==[] and vManageNWPI_readTrace_sloDetails==[]):
vManageNWPIAppHealth_data = [{"health":"green","name":"all","events":["positive"]}]
newdata = vManageNWPI_readTrace_sloDetails.copy()+vManageNWPIAppHealth_data.copy()+DnacAppHealth_data.copy()+thousandEyesAppHealth_data.copy()
collection = db['ApplicationHealth']
olddata = collection.find()
for item in olddata:
count = 0
for item2 in newdata:
if(item["name"]==item2["name"] and item["events"]==item2["events"] and item["url"]==item2["url"]):
if(item["health"]==item2["health"]):
del item["_id"]
same.append(item.copy())
del item
newdata.remove(item2)
break
else:
del item["_id"]
changed.append([item.copy(),item2.copy()])
del item
newdata.remove(item2)
break
elif(item["name"]!=item2["name"] or item["events"]!=item2["events"] and item["url"]!=item2["url"]):
count+=1
if(count==len(newdata)):
del item["_id"]
removed.append(item.copy())
del item
break
added = newdata
applicationhealth_summary = {
"changed":len(changed),
"added":len(added),
"removed":len(removed),
"same":len(same),
"total":len(changed)+len(added)+len(same)
}
applicationhealth_details = {
"changed":changed,
"added":added,
"removed":removed,
"same":same,
"total":changed + added + same
}
return [applicationhealth_summary, applicationhealth_details]
def messageme(text):
""" Sends a message to the user on webex teams
parameters: text
returns: None
"""
header = {"Content-Type":"application/json", "Authorization":"Bearer "+credentials.webex_token}
roomId="Y2lzY29zcGFyazovL3VzL1JPT00vYjc3ZjFhYTAtZDQ4Ni0xMWVkLThjOTgtMGIyNDQ4YjZmYzU4"
body = {"roomId": roomId,"markdown":text}
status = requests.post(url="https://webexapis.com/v1/messages",headers=header,json=body)
if(status.status_code==200):
pprint(status.json()["text"])
else:
print("some error trying to send webex message..",status)
def notify(intro,id,summary):
""" Sends a message to the user on webex teams
parameters: intro,id,summary
returns: None
"""
text = intro+" id : " + str(id)
messageme(text=text)
text = "**"+intro+"**"+" summary : "+str(summary)
messageme(text=text)
def runme(dbparam,dataparam):
""" Runs the script and sends the summary and details to the user on webex teams
parameters: dbparam,dataparam
returns: None
"""
if(dbparam==None):
dbparam = mongodb_auth.authenticatedb(dbname='storedb')
if(dataparam==None):
dataparam = demodbdata.data.copy()
global data, db
db = dbparam
data = dataparam
text = "**Cross-domain NOC for MSPs** : https://charts.mongodb.com/charts-global-msp-noc-vktwd/public/dashboards/643d02a2-33ac-4db0-82cc-1e76be904285"
messageme(text=text)
collection = db['store']
summary, details = compare_alarm()
print(mongodb_auth.purge_collection(collection))
mdata = {"alarm_summary":summary.copy(),"alarm_details":details.copy()}
id = collection.insert_one(mdata).inserted_id
notify(intro="Alarm",id=id,summary=summary)
summary, details = compare_networkhealth()
print(mongodb_auth.purge_collection(collection))
mdata = {"network_health_summary":summary.copy(),"network_health_details":details.copy()}
id = collection.insert_one(mdata).inserted_id
notify(intro="Network Health",id=id,summary=summary)
summary, details = compare_devicehealth()
print(mongodb_auth.purge_collection(collection))
mdata = {"device_health_summary":summary.copy(),"device_health_details":details.copy()}
id = collection.insert_one(mdata).inserted_id
notify(intro="Devices Health",id=id,summary=summary)
summary, details = compare_applicationhealth()
print(mongodb_auth.purge_collection(collection))
mdata = {"application_health_summary":summary.copy(),"application_health_details":details.copy()}
id = collection.insert_one(mdata).inserted_id
notify(intro="Application Health",id=id,summary=summary)
if __name__ == "__main__":
runme()