-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclear_alerts.py
More file actions
77 lines (55 loc) · 1.93 KB
/
Copy pathclear_alerts.py
File metadata and controls
77 lines (55 loc) · 1.93 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
#!/usr/bin/python3
"""This ControlScript should be triggered to run on the VS_UP EVENT and will
automatically clear any previous alerts generated for the VS_DOWN EVENT
for that VS"""
import os
import json
import sys
from avi.sdk.avi_api import ApiSession
import urllib3
import requests
if hasattr(requests.packages.urllib3, 'disable_warnings'):
requests.packages.urllib3.disable_warnings()
if hasattr(urllib3, 'disable_warnings'):
urllib3.disable_warnings()
def parse_avi_params(argv):
if len(argv) != 2:
return
alert_params = json.loads(argv[1])
print(str(alert_params))
return alert_params
def get_api_token():
return os.environ.get('API_TOKEN')
def get_api_user():
return os.environ.get('USER')
def get_api_endpoint():
return os.environ.get('DOCKER_GATEWAY') or 'localhost'
def get_tenant():
return os.environ.get('TENANT')
def clear_vs_down_alerts(session, vs_uuid):
print(vs_uuid)
rsp = session.get(f'alert/?obj_ref={vs_uuid}&search=(event_id,VS_DOWN)')
if rsp.status_code != 200:
print('Could not find any alerts to clear')
return
vs_down_alerts = json.loads(rsp.content)
for alert in vs_down_alerts['results']:
alert_uuid = alert['uuid']
print(f'Clearing alert {alert_uuid}')
session.delete(f'alert/{alert_uuid}')
if __name__ == '__main__':
alert_params = parse_avi_params(sys.argv)
for event in alert_params.get('events', []):
if event['event_id'] == 'VS_UP':
vs_uuid = event['obj_uuid']
break
if vs_uuid:
token = get_api_token()
user = get_api_user()
api_endpoint = get_api_endpoint()
tenant = get_tenant()
with ApiSession(api_endpoint, user, token=token,
tenant=tenant if tenant != 'admin' else '*') as session:
clear_vs_down_alerts(session, vs_uuid)
else:
print('No VS_UP Event found in alert data')