forked from alerta/alerta-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerta_msteams.py
86 lines (70 loc) · 2.81 KB
/
alerta_msteams.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
import json
import logging
import os
import pymsteams
try:
from alerta.plugins import app # alerta >= 5.0
except ImportError:
from alerta.app import app # alerta < 5.0
from alerta.plugins import PluginBase
LOG = logging.getLogger('alerta.plugins.msteams')
MS_TEAMS_WEBHOOK_URL = os.environ.get('MS_TEAMS_WEBHOOK_URL') or app.config.get('MS_TEAMS_WEBHOOK_URL')
MS_TEAMS_SUMMARY_FMT = os.environ.get('MS_TEAMS_SUMMARY_FMT') or app.config.get('MS_TEAMS_SUMMARY_FMT', None) # Message summary format
DASHBOARD_URL = os.environ.get('DASHBOARD_URL') or app.config.get('DASHBOARD_URL', '')
try:
from jinja2 import Template
except Exception as e:
LOG.error('MS Teams: ERROR - Jinja template error: %s, template functionality will be unavailable', e)
class SendConnectorCardMessage(PluginBase):
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
if alert.repeat:
return
if MS_TEAMS_SUMMARY_FMT:
try:
template = Template(MS_TEAMS_SUMMARY_FMT)
except Exception as e:
LOG.error('MS Teams: ERROR - Template init failed: %s', e)
return
try:
template_vars = {
'alert': alert,
'config': app.config
}
summary = template.render(**template_vars)
except Exception as e:
LOG.error('MS Teams: ERROR - Template render failed: %s', e)
return
else:
summary = ('<b>[{status}] {environment} {service} {severity} - <i>{event} on {resource}</i></b>').format(
status=alert.status.capitalize(),
environment=alert.environment.upper(),
service=','.join(alert.service),
severity=alert.severity.capitalize(),
event=alert.event,
resource=alert.resource
)
url = "%s/#/alert/%s" % (DASHBOARD_URL, alert.id)
if alert.severity == 'critical':
color = "D8122A"
elif alert.severity == 'major':
color = "EA680F"
elif alert.severity == 'minor':
color = "FFBE1E"
elif alert.severity == 'warning':
color = "BA2222"
else:
color = "00AA5A"
LOG.debug('MS Teams payload: %s', summary)
try:
msTeamsMessage = pymsteams.connectorcard(MS_TEAMS_WEBHOOK_URL)
msTeamsMessage.title(summary)
msTeamsMessage.text(alert.text)
msTeamsMessage.addLinkButton("Open in Alerta", url)
msTeamsMessage.color(color)
msTeamsMessage.send()
except Exception as e:
raise RuntimeError("MS Teams: ERROR - %s", e)
def status_change(self, alert, status, text):
return