SLACK_SEVERITY_FILTER allows pre-filtering of alerts sent to Slack solely by severity ()

* Adds configuration setting SLACK_SEVERITY_FILTER (list) to allow filtering of alerts to be forwarded to Slack based upon their severity.

* Fixes incorrect parsing of list from alertad.conf for SLACK_SEVERITY_FILTER

* Updates comment in README

* Fixes incorrect string formatting.

* Reverses the logic of SLACK_SEVERITY_FILTER to be more permissive, such that alerts with severity matching one listed will NOT be forwarded to Slack (i.e. Alerts with severity not listed in SLACK_SEVERITY_FILTER will pass through the filter).

Co-authored-by: Michael Fischer <michael@marinelabs.io>
This commit is contained in:
mdfischer 2020-06-14 13:26:40 -07:00 committed by GitHub
parent 45134a1033
commit 2d0bf01c98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View file

@ -43,8 +43,8 @@ SLACK_ATTACHMENTS = True # default=False
SLACK_CHANNEL = '' # if empty then uses channel from incoming webhook configuration
SLACK_CHANNEL_ENV_MAP = { 'Production' : '#alert-prod' } # Default=None (optionnal) Allow to specify a channel on a per-environment basis. SLACK_CHANNEL is used a default value
SLACK_CHANNEL_EVENT_MAP = { 'Node offline' : '#critical-alerts' } # Default=None (optionnal) Allow to specify a channel on a per-event basis. SLACK_CHANNEL is used a default value
SLACK_CHANNEL_SEVERITY_MAP = { 'crtical' : '#critical-alerts', 'informational': '#noisy-feed' } # Default=None (optionnal) Allow to specify a channel on a per-severity basis. SLACK_CHANNEL is used a default value
SLACK_CHANNEL_SEVERITY_MAP = { 'critical' : '#critical-alerts', 'informational': '#noisy-feed' } # Default=None (optionnal) Allow to specify a channel on a per-severity basis. SLACK_CHANNEL is used a default value
SLACK_SEVERITY_FILTER = ['warning'] # blocks alerts with severity in this list from being forwarded to Slack
ICON_EMOJI = '' # default :rocket:
ALERTA_USERNAME = '' # default alerta

View file

@ -4,6 +4,7 @@ import logging
import os
import requests
import traceback
import ast
LOG = logging.getLogger('alerta.plugins.slack')
@ -40,7 +41,13 @@ try:
os.environ.get('SLACK_CHANNEL_SEVERITY_MAP'))
except Exception as e:
SLACK_CHANNEL_SEVERITY_MAP = app.config.get('SLACK_CHANNEL_SEVERITY_MAP', dict())
try:
SLACK_SEVERITY_FILTER = ast.literal_eval(
os.environ.get('SLACK_SEVERITY_FILTER'))
except Exception as e:
SLACK_SEVERITY_FILTER = app.config.get('SLACK_SEVERITY_FILTER', list())
SLACK_SEND_ON_ACK = os.environ.get(
'SLACK_SEND_ON_ACK') or app.config.get('SLACK_SEND_ON_ACK', False)
SLACK_SEVERITY_MAP = app.config.get('SLACK_SEVERITY_MAP', {})
@ -183,9 +190,12 @@ class ServiceIntegration(PluginBase):
if alert.repeat:
return
if alert.severity in SLACK_SEVERITY_FILTER:
LOG.debug("Alert severity %s is included in SLACK_SEVERITY_FILTER list, thus it will not be forwarded to Slack." % alert.severity)
return
try:
payload = self._slack_prepare_payload(alert, **kwargs)
LOG.debug('Slack payload: %s', payload)
except Exception as e:
LOG.error('Exception formatting payload: %s\n%s' % (e, traceback.format_exc()))