-
Notifications
You must be signed in to change notification settings - Fork 4
/
blackout_regex.py
231 lines (213 loc) · 9.31 KB
/
blackout_regex.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# -*- coding: utf-8 -*-
"""
Alerta Blackout Regex
=====================
Alerta plugin to enhance the blackout system.
"""
import re
import logging
from alerta.models.blackout import Blackout
from alerta.plugins import PluginBase
from alerta.exceptions import BlackoutPeriod
log = logging.getLogger("alerta.plugins.blackout_regex")
def parse_tags(tag_list):
return {k: v for k, v in (i.split("=", 1) for i in tag_list if "=" in i)}
class BlackoutRegex(PluginBase):
def _fetch_blackouts(self):
try:
# retrieve all blackouts from the DB.
# use the alerta blackout model to retrieve the blackouts.
# The model standardizes the data returned from mongodb and postgres db.
count = Blackout.count()
log.debug(f"There are {count} Blackouts currently open")
blackouts = Blackout.find_all(page=1, page_size=count)
log.debug("Retrieved Blackouts from the DB:")
log.debug(blackouts)
except Exception:
log.debug("Unable to retrieve the Blackouts from the DB", exc_info=True)
blackouts = []
return blackouts
def _apply_blackout(self, alert):
"""
The regex blackouts are evaluated in the ``post_receive`` in order to
have the alert already correlated, therefore provide us with the real
Alert ID (after being correlated, and not just a random fresh ID), the
tags from the previous evaluation, as well as a pre-filtering by the
native blackout mechanisms (i.e., if there's a blackout matching the
alert it won't get to this point - if it gets here we therefore evaluate
the alert and we're sure it didn't match the literal blackout attributes
which is ideal to preserve backwards compatibility).
"""
if not alert:
# It actually does happen sometimes that the Alert can be None (and
# perhaps something else too?) - for whatever reason.
return alert
if alert.status == "closed":
log.debug("Alert %s status is closed, ignoring", alert.id)
return alert
blackouts = self._fetch_blackouts()
NOTIFICATION_BLACKOUT = self.get_config(
"NOTIFICATION_BLACKOUT", default=False, type=bool
)
alert_tags = parse_tags(alert.tags)
# When an alert matches a blackout, this plugin adds a special tag
# ``regex_blackout`` that points to the blackout ID matched.
# This facilitates the blackout matching, by simply checking if the
# blackout is still open.
if "regex_blackout" in alert_tags:
log.debug(
"Checking blackout %s which used to match this alert",
alert_tags["regex_blackout"],
)
for blackout in blackouts:
if blackout.id == alert_tags["regex_blackout"]:
if blackout.status == "active":
log.debug(
"Blackout %s is still active, setting alert %s "
"status as blackout",
blackout.id,
alert.id,
)
if alert.status != "blackout":
alert.status = "blackout"
return alert
# If the blackout is no longer active, simply return
# the alert as-is, without changing the status, but
# removing the regex_blackout tag, so when the alert is
# fired again, we'll know that it does no longer match
# an active blackout.
log.debug(
"Blackout %s does no longer exist, or is not active, removing "
"tag and leaving status unchanged",
alert_tags["regex_blackout"],
)
alert.tags = [tag for tag in alert.tags if "regex_blackout=" not in tag]
return alert
# No previous regex blackout match, let's evaluate.
# The idea is that if a blackout has a number of attributes configured,
# in order to match, the alert must match all of these attributes.
for blackout in blackouts:
# The general assumption is that a blackout has at least one of
# these attributes set, therefore once we try to match only when an
# attribute is configured, and skip to the next blackout when the
# matching fails.
log.debug("Evaluating blackout")
log.debug(blackout)
match = False
if blackout.environment:
if not re.search(blackout.environment, alert.environment):
log.debug(
"%s doesn't match the blackout environment %s",
alert.environment,
blackout.environment,
)
continue
match = True
log.debug(
"%s matched %s",
blackout.environment,
alert.environment,
)
if blackout.customer:
if not re.search(blackout.customer, alert.customer):
log.debug(
"%s doesn't match the blackout customer %s",
alert.customer,
blackout.customer,
)
continue
match = True
log.debug(
"%s matched %s",
blackout.customer,
alert.customer,
)
if blackout.group:
if not re.search(blackout.group, alert.group):
log.debug(
"%s doesn't match the blackout group %s",
alert.group,
blackout.group,
)
continue
match = True
log.debug("%s matched %s", blackout.group, alert.group)
if blackout.event:
if not re.search(blackout.event, alert.event):
log.debug(
"%s doesn't match the blackout event %s",
alert.event,
blackout.event,
)
continue
match = True
log.debug("%s matched %s", blackout.event, alert.event)
if blackout.resource:
if not re.search(blackout.resource, alert.resource):
log.debug(
"%s doesn't match the blackout resource %s",
alert.resource,
blackout.resource,
)
continue
match = True
log.debug("%s matched %s", blackout.resource, alert.resource)
if blackout.service and alert.service:
if len(blackout.service) != len(alert.service):
continue
if not all(
[
re.search(blackout.service[index], alert.service[index])
for index in range(len(alert.service))
]
):
log.debug(
"%s don't seem to match the blackout service(s) %s",
str(alert.service),
str(blackout.service),
)
continue
match = True
log.debug("%s matched %s", blackout.service[0], alert.service[0])
if blackout.tags and alert.tags:
blackout_tags = parse_tags(blackout.tags)
if not set(blackout_tags.keys()).issubset(set(alert_tags.keys())):
# The blackout must have at least as many tags as the alert
# in order to match.
continue
if not all(
[
re.search(blackout_tags[blackout_tag], alert_tags[blackout_tag])
for blackout_tag in blackout_tags
if blackout_tag in alert_tags
]
):
log.debug(
"%s don't seem to match the blackout tag(s) %s",
str(alert_tags),
str(blackout_tags),
)
continue
match = True
if match:
if not NOTIFICATION_BLACKOUT:
log.debug(
f"Suppressed alert during blackout period (id={alert.id})"
)
raise BlackoutPeriod("Suppressed alert during blackout period")
log.debug(
"Alert %s seems to match (regex) blackout %s. "
"Adding regex_blackout and status",
alert.id,
blackout.id,
)
alert.tags.extend(["regex_blackout={}".format(blackout.id)])
alert.status = "blackout"
return alert
return alert
def pre_receive(self, alert):
return self._apply_blackout(alert)
def post_receive(self, alert):
return alert
def status_change(self, alert, status, text):
return alert, status, text