forked from alerta/alerta-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consulalerta.py
executable file
·113 lines (93 loc) · 2.71 KB
/
consulalerta.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
#!/usr/bin/env python
import json
import os
import consul
import sys
import time
from alertaclient.api import Client
CONSUL_HOST = os.environ.get('CONSUL_HOST', '127.0.0.1')
CONSUL_PORT = int(os.environ.get('CONSUL_PORT', 8500))
client = consul.Consul(host=CONSUL_HOST, port=CONSUL_PORT, token=None, scheme='http', consistency='default', dc=None, verify=True)
j = json.load(sys.stdin)
print("Request:")
print(j)
try:
url = client.kv.get('alerta/apiurl')[1]['Value']
except Exception:
print("No URL defined, exiting")
sys.exit(1)
try:
key = client.kv.get('alerta/apikey')[1]['Value']
except Exception:
print("No key defined, exiting")
sys.exit(1)
try:
max_retries = int(client.kv.get('alerta/max_retries')[1]['Value'])
except TypeError:
print("No value defined, using default")
max_retries = 3
try:
sleep = int(client.kv.get('alerta/sleep')[1]['Value'])
except TypeError:
print("No value defined, using default")
sleep = 2
try:
timeout = int(client.kv.get('alerta/timeout')[1]['Value'])
except TypeError:
print("No value defined, using default")
timeout = 900
try:
origin = client.kv.get('alerta/origin')[1]['Value']
except TypeError:
print("No value defined, using default")
origin = "consul"
try:
alerttype = client.kv.get('alerta/alerttype')[1]['Value']
except TypeError:
print("No value defined, using default")
alerttype = "ConsulAlert"
api = Client(endpoint=url, key=key)
SEVERITY_MAP = {
'critical': 'critical',
'warning': 'warning',
'passing': 'ok',
}
def createalert( data ):
try:
environment = client.kv.get('alerta/env/{0}'.format(data['Node']))[1]['Value']
except Exception:
try:
environment = client.kv.get('alerta/defaultenv')[1]['Value']
except Exception:
environment = "Production"
for _ in range(max_retries):
try:
print("Response:")
response = api.send_alert(
resource=data['Node'],
event=data['CheckId'],
value=data['Status'],
correlate=SEVERITY_MAP.keys(),
environment=environment,
service=[data['CheckId']],
severity=SEVERITY_MAP[data['Status']],
text=data['Output'],
timeout=timeout,
origin=origin,
type=alerttype
)
print(response)
except Exception as e:
print("HTTP Error: {}".format(e))
time.sleep(sleep)
continue
else:
break
else:
print("api is down")
def main():
for item in enumerate(j):
i=item[0]
createalert(j[i])
if __name__ == "__main__":
main()