-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhume_alertmanager_receiver.py
202 lines (179 loc) · 5.58 KB
/
hume_alertmanager_receiver.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
#!/usr/bin/env python3
#
# An http server that conforms to:
# https://prometheus.io/docs/alerting/latest/configuration/#webhook_config
#
# Enables hume support for Prometheus' AlertManager
#
# Maybe include a nice AlertManager template...
# https://prometheus.io/blog/2016/03/03/custom-alertmanager-templates/
#
# Author: Buanzo
import argparse
from hume import Hume
import json
import time
import datetime
import webhook_listener
from pprint import pprint
__version__ = '0.1'
__version_str = 'Hume AlertManager Receiver v{} by Buanzo'.format(__version__)
def construct_hume(alert):
labels = alert['labels']
print('construct_hume')
# OK, let's handle this
# First, lets extract/construct level, msg, tags and task
try:
status = alert['status'].strip()
except Exception:
status = 'unknown_status'
try:
level = labels['severity']
except Exception:
level = 'warning' # Sensible default? alpha software, people!
try:
#task = '{}[{}]'.format(labels['instance_name'], labels['ip'])
task = labels['name']
except Exception:
if 'instance_name' in labels.keys():
task = labels['instance_name']
else:
task = 'Alertmanager'
tags = []
try:
tags.append(labels['alertname'])
except Exception:
pass
try:
tags.append(labels['job'])
except Exception:
pass
try:
tags.append(labels['type'])
except Exception:
pass
try:
tags.append(labels['region'])
except Exception:
pass
try:
tags.append(labels['flavor'])
except Exception:
pass
try:
startsAt = alert['startsAt']
except Exception:
d = datetime.datetime.utcnow()
startsAt = '{}Z'.format(d.isoformat("T"))
try:
# summary = alert['annotations']['description']
summary = alert['annotations']['summary']
except Exception:
summary = 'n/a'
# TODO: hume.msg = "STATUS: "+status+"\nDETAILS: "+annotations.summary
try:
status = alert['status']
except Exception:
status = 'n/a'
msg = "Alert is {}.\n\n{}".format(status,
summary)
try:
hostname = labels['instance'].split(':')[0]
except Exception:
hostname = 'n/a'
extra = {}
try:
extra['instance_name'] = labels['instance_name']
except Exception:
extra = None
humePkt = {'level': level,
'msg': msg,
'tags': tags,
'task': task}
if extra is not None:
humePkt['extra'] = extra
return(humePkt)
def process_alertmanager_request(request, *args, **kwargs):
method = request.method
cLength = int(request.headers['Content-Length'])
cType = request.headers['Content-Type']
if cType == 'application/json' and cLength > 0 and cLength < 65535:
print('Decoding Body...')
try:
body = request.body.read(cLength)
except Exception as exc:
print(exc)
return
try:
body = body.decode('utf8')
except Exception as exc:
print(exc)
return
try:
jBody = json.loads(body)
except Exception as exc:
print(exc)
return
else:
return
# print('DECODED JSON BODY:')
# pprint(jBody)
#pprint(jBody['alerts'])
for alert in jBody['alerts']:
print('ALERTA:')
pprint(alert)
print('HUME:')
humePkt = construct_hume(alert)
pprint(humePkt)
try:
Hume(humePkt).send()
except Exception as exc:
print('Error sending hume:')
print(exc)
return
def run():
parser = argparse.ArgumentParser()
parser.add_argument('--version',
action='version',
version=__version_str)
parser.add_argument('-l', '--listen',
default='127.0.0.1',
dest='host',
help='Listening IP for receiver. Default: 127.0.0.1.')
parser.add_argument('-p', '--port',
default=8090,
type=int,
nargs=1,
dest='port',
help='Listening port for receiver. Default is 8090.')
parser.add_argument('-d', '--debug',
default=False,
action='store_true',
dest='debug',
help='Enables debugging messages')
parser.add_argument('-t', '--threadpoolsize',
default=10,
type=int,
nargs=1,
dest='threadPool',
help='Receiver threading pool size. Default 10.')
handlers = {"POST": process_alertmanager_request}
args = parser.parse_args()
if args.debug:
print('Running with args:')
print('host={}'.format(args.host))
print('port={}'.format(args.port))
print('threadPoolSize={}'.format(args.threadPool))
webhook_receiver = webhook_listener.Listener(host=args.host,
port=args.port,
debug=args.debug,
threadPool=args.threadPool,
handlers=handlers)
webhook_receiver.start()
try:
while True:
time.sleep(300)
except KeyboardInterrupt:
webhook_receiver.stop()
if __name__ == '__main__':
run()