-
Notifications
You must be signed in to change notification settings - Fork 171
/
alerta_prometheus.py
195 lines (164 loc) · 8.01 KB
/
alerta_prometheus.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
import datetime
import json
import logging
import os
from typing import Any
import requests
from alerta.models.alert import Alert
from alerta.plugins import PluginBase
try:
from alerta.plugins import app # alerta >= 5.0
except ImportError:
from alerta.app import app # alerta < 5.0
LOG = logging.getLogger('alerta.plugins.prometheus')
DEFAULT_ALERTMANAGER_API_URL = 'http://localhost:9093'
ALERTMANAGER_API_URL = os.environ.get(
'ALERTMANAGER_API_URL') or app.config.get('ALERTMANAGER_API_URL', None)
ALERTMANAGER_USERNAME = os.environ.get(
'ALERTMANAGER_USERNAME') or app.config.get('ALERTMANAGER_USERNAME', None)
ALERTMANAGER_PASSWORD = os.environ.get(
'ALERTMANAGER_PASSWORD') or app.config.get('ALERTMANAGER_PASSWORD', None)
ALERTMANAGER_SILENCE_DAYS = os.environ.get(
'ALERTMANAGER_SILENCE_DAYS') or app.config.get('ALERTMANAGER_SILENCE_DAYS', 1)
ALERTMANAGER_SILENCE_FROM_ACK = os.environ.get(
'ALERTMANAGER_SILENCE_FROM_ACK') or app.config.get('ALERTMANAGER_SILENCE_FROM_ACK', False)
ALERTMANAGER_USE_EXTERNALURL_FOR_SILENCES = os.environ.get(
'ALERTMANAGER_USE_EXTERNALURL_FOR_SILENCES') or app.config.get('ALERTMANAGER_USE_EXTERNALURL_FOR_SILENCES', False)
class AlertmanagerSilence(PluginBase):
def __init__(self, name=None):
self.auth = (ALERTMANAGER_USERNAME,
ALERTMANAGER_PASSWORD) if ALERTMANAGER_USERNAME else None
super().__init__(name)
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
return
def status_change(self, alert, status, text):
'''
If a silence exists for an open or closed alert we probably want to remove it
'''
if status in ('open', 'closed'):
silenceId = alert.attributes.get('silenceId', None)
if silenceId:
LOG.debug('Alertmanager: Remove silence for alertname=%s instance=%s',
alert.event, alert.resource)
base_url = ALERTMANAGER_API_URL or alert.attributes.get(
'externalUrl', DEFAULT_ALERTMANAGER_API_URL)
url = base_url + '/api/v1/silence/%s' % silenceId
try:
r = requests.delete(url, auth=self.auth, timeout=2)
except Exception as e:
raise RuntimeError('Alertmanager: ERROR - %s' % e)
LOG.debug('Alertmanager: %s - %s', r.status_code, r.text)
try:
alert.attributes['silenceId'] = None
except Exception as e:
raise RuntimeError('Alertmanager: ERROR - %s' % e)
LOG.debug(
'Alertmanager: Removed silenceId %s from attributes', silenceId)
if status == 'closed':
LOG.warning('Status is now closed')
return alert
def take_action(self, alert: Alert, action: str, text: str, **kwargs) -> Any:
'''
Set silence in alertmanager.
'''
if alert.event_type != 'prometheusAlert':
return alert
base_url = ALERTMANAGER_API_URL or alert.attributes.get(
'externalUrl', DEFAULT_ALERTMANAGER_API_URL)
if action == 'close':
LOG.warning(
'Got a close action so trying to close this in alertmanager too')
url = base_url + '/api/v1/alerts'
raw_data_string = alert.raw_data
raw_data = json.loads(raw_data_string)
# set the endsAt to now so alertmanager will consider it expired or whatever
raw_data['endsAt'] = (datetime.datetime.utcnow(
) - datetime.timedelta(minutes=5)).replace(microsecond=0).isoformat() + '.000Z'
LOG.debug('Raw data type: {}, Raw data contents: {}'.format(
type(raw_data), raw_data))
data = [raw_data]
try:
r = requests.post(url, json=data, auth=self.auth, timeout=2)
except Exception as e:
raise RuntimeError('Alertmanager: ERROR - %s' % e)
LOG.debug('Alertmanager response was: %s - %s',
r.status_code, r.text)
elif action == 'ack' and ALERTMANAGER_SILENCE_FROM_ACK:
if not ALERTMANAGER_SILENCE_DAYS:
silence_seconds = kwargs.get('timeout', alert.timeout)
else:
try:
silence_days = int(ALERTMANAGER_SILENCE_DAYS)
except Exception as e:
LOG.error(
"Alertmanager: Could not parse 'ALERTMANAGER_SILENCE_DAYS': %s", e)
raise RuntimeError(
"Could not parse 'ALERTMANAGER_SILENCE_DAYS': %s" % e)
silence_seconds = silence_days * 86400
LOG.debug('Alertmanager: Add silence for alertname=%s instance=%s timeout=%s',
alert.event, alert.resource, str(silence_seconds))
data = {
'matchers': [
{
'name': 'alertname',
'value': alert.event
},
{
'name': 'instance',
'value': alert.resource
}
],
'startsAt': datetime.datetime.utcnow().replace(microsecond=0).isoformat() + '.000Z',
'endsAt': (datetime.datetime.utcnow() + datetime.timedelta(seconds=silence_seconds))
.replace(microsecond=0).isoformat() + '.000Z',
'createdBy': 'alerta',
'comment': text if text != '' else 'silenced by alerta'
}
# if alertmanager is clustered behind a load balancer that mirrors requests we should prefer to create one silence
# rather than many
if ALERTMANAGER_USE_EXTERNALURL_FOR_SILENCES:
base_url = alert.attributes.get(
'externalUrl', DEFAULT_ALERTMANAGER_API_URL) or ALERTMANAGER_API_URL
else:
base_url = ALERTMANAGER_API_URL or alert.attributes.get(
'externalUrl', DEFAULT_ALERTMANAGER_API_URL)
url = base_url + '/api/v1/silences'
try:
r = requests.post(url, json=data, auth=self.auth, timeout=2)
except Exception as e:
raise RuntimeError('Alertmanager: ERROR - %s' % e)
LOG.debug('Alertmanager: %s - %s', r.status_code, r.text)
# example r={"status":"success","data":{"silenceId":8}}
try:
data = r.json().get('data', [])
if data:
silenceId = data['silenceId']
alert.attributes['silenceId'] = silenceId
else:
silenceId = alert.attributes.get('silenceId', 'unknown')
text = text + ' (silenced in Alertmanager)'
except Exception as e:
raise RuntimeError('Alertmanager: ERROR - %s' % e)
LOG.debug('Alertmanager: Added silenceId %s to attributes', silenceId)
elif action == 'unack':
LOG.debug('Alertmanager: Remove silence for alertname=%s instance=%s',
alert.event, alert.resource)
silenceId = alert.attributes.get('silenceId', None)
if silenceId:
base_url = ALERTMANAGER_API_URL or alert.attributes.get(
'externalUrl', DEFAULT_ALERTMANAGER_API_URL)
url = base_url + '/api/v1/silence/%s' % silenceId
try:
r = requests.delete(url, auth=self.auth, timeout=2)
except Exception as e:
raise RuntimeError('Alertmanager: ERROR - %s' % e)
LOG.debug('Alertmanager: %s - %s', r.status_code, r.text)
try:
alert.attributes['silenceId'] = None
except Exception as e:
raise RuntimeError('Alertmanager: ERROR - %s' % e)
LOG.debug(
'Alertmanager: Removed silenceId %s from attributes', silenceId)
return alert