forked from alerta/alerta-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerta_pushover.py
78 lines (58 loc) · 2.15 KB
/
alerta_pushover.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
import logging
import os
import requests
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.pushover')
PUSHOVER_URL = 'https://api.pushover.net/1/messages.json'
PUSHOVER_TOKEN = os.environ.get('PUSHOVER_TOKEN') or app.config['PUSHOVER_TOKEN']
PUSHOVER_USER = os.environ.get('PUSHOVER_USER') or app.config['PUSHOVER_USER']
DASHBOARD_URL = os.environ.get('DASHBOARD_URL') or app.config.get('DASHBOARD_URL', '')
PUSHOVER_EMERG = 2 # requires user ack
PUSHOVER_HIGH = 1
PUSHOVER_NORMAL = 0
PUSHOVER_LOW = -1
PUSHOVER_BADGE = -2 # no notification
# See https://pushover.net/api#priority
PRIORITY_MAP = {
'critical': PUSHOVER_EMERG,
'major': PUSHOVER_HIGH,
'minor': PUSHOVER_NORMAL,
'warning': PUSHOVER_LOW
}
class PushMessage(PluginBase):
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
if alert.repeat:
return
title = "%s: %s alert for %s - %s is %s" % (
alert.environment, alert.severity.capitalize(),
','.join(alert.service), alert.resource, alert.event
)
priority = PRIORITY_MAP.get(alert.severity, PUSHOVER_BADGE)
payload = {
"token": PUSHOVER_TOKEN,
"user": PUSHOVER_USER,
"title": title,
"message": alert.text,
"url": '%s/#/alert/%s' % (DASHBOARD_URL, alert.id),
"url_title": "View alert",
"priority": priority,
"timestamp": alert.create_time,
"sound": "tugboat"
}
if priority == PUSHOVER_EMERG:
payload['retry'] = 299 # retry every seconds
payload['expire'] = 900 # stop after seconds
LOG.debug('Pushover.net: %s', payload)
try:
r = requests.post(PUSHOVER_URL, data=payload, timeout=2)
except Exception as e:
raise RuntimeError("Pushover.net: ERROR - %s" % e)
LOG.debug('Pushover.net: %s - %s', r.status_code, r.text)
def status_change(self, alert, status, text):
return