forked from alerta/alerta-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerta_cachet.py
54 lines (36 loc) · 1.49 KB
/
alerta_cachet.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
import os
import logging
import json
from alerta.app import app
from alerta.plugins import PluginBase
import cachetclient.cachet as cachet
LOG = logging.getLogger('alerta.plugins.cachet')
CACHET_API_URL = os.environ.get('CACHET_API_URL') or app.config['CACHET_API_URL']
CACHET_API_TOKEN = os.environ.get('CACHET_API_TOKEN') or app.config['CACHET_API_TOKEN']
CACHET_SSL_VERIFY = True if (os.environ.get('CACHET_SSL_VERIFY') == 'True' or app.config.get('CACHET_SSL_VERIFY', False)) else False
STATUS_MAP = {
'open': 1, # Investigating
'ack': 2, # Identified
'assigned': 3, # Watching
'closed': 4 # Fixed
}
class CachetIncident(PluginBase):
def __init__(self, name=None):
self.incidents = cachet.Incidents(endpoint=CACHET_API_URL, api_token=CACHET_API_TOKEN, verify=CACHET_SSL_VERIFY)
super(CachetIncident, self).__init__(name)
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
name = alert.event
status = STATUS_MAP[alert.status]
message = alert.text
r = json.loads(self.incidents.get(name=name, message=message, status=status))
if r['meta']['pagination']['count']:
return
try:
r = json.loads(self.incidents.post(name=name, message=message, status=status, visible=True))
except Exception as e:
raise RuntimeError("Cachet: ERROR - %s", e)
LOG.debug('Cachet: %s', r)
def status_change(self, alert, status, text):
return