forked from alerta/alerta-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerta_hipchat.py
97 lines (79 loc) · 3.28 KB
/
alerta_hipchat.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
import os
import json
import requests
import logging
from alerta.app import app
from alerta.plugins import PluginBase
LOG = logging.getLogger('alerta.plugins.hipchat')
HIPCHAT_URL = os.environ.get('HIPCHAT_URL') or app.config.get('HIPCHAT_URL', 'https://api.hipchat.com/v2')
HIPCHAT_ROOM = os.environ.get('HIPCHAT_ROOM') or app.config['HIPCHAT_ROOM'] # Room Name or Room API ID
HIPCHAT_API_KEY = os.environ.get('HIPCHAT_API_KEY') or app.config['HIPCHAT_API_KEY'] # Room Notification Token
HIPCHAT_SUMMARY_FMT = os.environ.get('HIPCHAT_SUMMARY_FMT') or app.config.get('HIPCHAT_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('HipChat: ERROR - Jinja template error: %s, template functionality will be unavailable', e)
class SendRoomNotification(PluginBase):
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
if alert.repeat:
return
if HIPCHAT_SUMMARY_FMT:
try:
template = Template(HIPCHAT_SUMMARY_FMT)
except Exception as e:
LOG.error('HipChat: 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('HipChat: ERROR - Template render failed: %s', e)
return
else:
summary = ('<b>[{status}] {environment} {service} {severity} - <i>{event} on {resource}</i></b> <a href="{dashboard}/#/alert/{alert_id}">{short_id}</a>').format(
status=alert.status.capitalize(),
environment=alert.environment.upper(),
service=','.join(alert.service),
severity=alert.severity.capitalize(),
event=alert.event,
resource=alert.resource,
alert_id=alert.id,
short_id=alert.get_id(short=True),
dashboard=DASHBOARD_URL
)
if alert.severity == 'critical':
color = "red"
elif alert.severity == 'major':
color = "purple"
elif alert.severity == 'minor':
color = "yellow"
elif alert.severity == 'warning':
color = "gray"
else:
color = "green"
payload = {
"color": color,
"message": summary,
"notify": True,
"message_format": "html"
}
LOG.debug('HipChat payload: %s', payload)
url = '%s/room/%s/notification' % (HIPCHAT_URL, HIPCHAT_ROOM)
headers = {
'Authorization': 'Bearer ' + HIPCHAT_API_KEY,
'Content-type': 'application/json'
}
LOG.debug('HipChat: Notification sent to %s', url)
try:
r = requests.post(url, data=json.dumps(payload), headers=headers, timeout=2)
except Exception as e:
raise RuntimeError("HipChat: ERROR - %s", e)
LOG.debug('HipChat: %s - %s', r.status_code, r.text)
def status_change(self, alert, status, text):
return