-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalertreceiver.py
executable file
·92 lines (79 loc) · 2.78 KB
/
alertreceiver.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""this is the receiver for alertringer"""
from bottle import route, run, template, get, put, post, request, error, abort, response, auth_basic
import json
import requests
import logging
import redis
import time
debug = True
defaultTTL = 600
heartbeatTTL = 11
@get('/api/v1/alerts')
def get_alerts():
"""
list all alerts by key redis
"""
remote_ip = request.environ.get('REMOTE_ADDR')
data = []
for key in r.scan_iter():
data.append(key)
logger.info('sent len: %i get_alerts to %s' % (len(data), remote_ip))
response.content_type = 'application/json'
return json.dumps(data, indent=4)
@post('/api/v1/alerts')
def post_alerts():
"""
get the alerts from alertmanager and put it in the redis
"""
try:
remote_ip = request.environ.get('REMOTE_ADDR')
redis_data = {}
json_data = request.json
redis_id = json_data['groupKey']
redis_data['team'] = request.query['team']
redis_data['status'] = json_data['status']
except:
if debug:
logger.info('debug except: %s' % (str(sys.exc_info())))
response.status = 400
response.content_type = 'application/json'
logger.info('failed post alert resv %s' % (remote_ip))
return {'result': 'failed', 'message': 'json blob was not readable'}
if debug:
print('team is {}'.format(redis_data['team']))
print(str(redis_id)+':'+redis_data['team'])
if redis_data['team'] not in teams:
logger.info('team: %s not in teams: %s' % (redis_data['team'], str(teams)))
response.status = 400
response.content_type = 'application/json'
return {'result': 'failed', 'message': 'team not valid'}
if redis_data['status'] == "firing":
logger.info('firing: key %s, team: %s from %s' % (redis_id, redis_data['team'], remote_ip))
if redis_data['team'] == 'heartbeat':
TTL = heartbeatTTL
else:
TTL = defaultTTL
r.setex(str(redis_id)+':'+redis_data['team'], TTL, json.dumps(redis_data))
else:
logger.info('resolved: key %s, team: %s from %s' % (redis_id, redis_data['team'], remote_ip))
try:
r.delete(str(redis_id)+':'+redis_data['team'])
if debug:
logger.info('deleted from db')
except:
if debug:
logger.info('except: %s' % (sys.exc_info()))
if __name__ == '__main__':
logger = logging.getLogger("receiver")
logfile = '/var/log/alertringer.log'
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler = logging.FileHandler(logfile)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info('starting up')
teams = ['services','core','access','powerpatrol','wifi','heartbeat']
r = redis.StrictRedis(host='localhost', port=6379, db=0)
run(host='0.0.0.0', port=80, debug=False, reloader=False)