0
0
Fork 0
mirror of https://github.com/alerta/alerta.git synced 2025-03-13 13:03:17 +00:00
alerta_alerta/alerta/plugins/reject.py

50 lines
2 KiB
Python
Raw Permalink Normal View History

2016-09-14 23:52:59 +00:00
import logging
2018-09-15 22:07:50 +00:00
import re
from alerta.exceptions import RejectException
from alerta.plugins import PluginBase
LOG = logging.getLogger('alerta.plugins')
2017-09-01 22:22:59 +00:00
class RejectPolicy(PluginBase):
"""
Default reject policy will block alerts that do not have the following
required attributes:
1) environment - must match an allowed environment. By default it should
be either "Production" or "Development". Config setting is `ALLOWED_ENVIRONMENTS`.
2) service - must supply a value for service. Any value is acceptable.
"""
def pre_receive(self, alert, **kwargs):
ORIGIN_BLACKLIST = self.get_config('ORIGIN_BLACKLIST', default=[], type=list, **kwargs)
ALLOWED_ENVIRONMENTS = self.get_config('ALLOWED_ENVIRONMENTS', default=[], type=list, **kwargs)
ORIGIN_BLACKLIST_REGEX = [re.compile(x) for x in ORIGIN_BLACKLIST]
ALLOWED_ENVIRONMENT_REGEX = [re.compile(x) for x in ALLOWED_ENVIRONMENTS]
if any(regex.match(alert.origin) for regex in ORIGIN_BLACKLIST_REGEX):
LOG.warning("[POLICY] Alert origin '%s' has been blacklisted", alert.origin)
raise RejectException("[POLICY] Alert origin '%s' has been blacklisted" % alert.origin)
if not any(regex.match(alert.environment) for regex in ALLOWED_ENVIRONMENT_REGEX):
LOG.warning('[POLICY] Alert environment does not match one of %s', ', '.join(ALLOWED_ENVIRONMENTS))
raise RejectException('[POLICY] Alert environment does not match one of %s' %
2018-09-15 22:07:50 +00:00
', '.join(ALLOWED_ENVIRONMENTS))
if not alert.service:
LOG.warning('[POLICY] Alert must define a service')
raise RejectException('[POLICY] Alert must define a service')
return alert
def post_receive(self, alert, **kwargs):
2016-03-07 05:08:13 +00:00
return
def status_change(self, alert, status, text, **kwargs):
2014-12-15 11:32:23 +00:00
return
def take_action(self, alert, action, text, **kwargs):
raise NotImplementedError