forked from alerta/alerta-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerta_influxdb.py
67 lines (48 loc) · 1.91 KB
/
alerta_influxdb.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
import os
import logging
from alerta.app import app
from alerta.plugins import PluginBase
from influxdb import InfluxDBClient
LOG = logging.getLogger('alerta.plugins.influxdb')
DEFAULT_INFLUXDB_DSN = 'influxdb://user:pass@localhost:8086/alerta' # 'influxdb://username:password@localhost:8086/databasename'
INFLUXDB_DSN = os.environ.get('INFLUXDB_DSN') or app.config.get('INFLUXDB_DSN', DEFAULT_INFLUXDB_DSN)
INFLUXDB_DATABASE = os.environ.get('INFLUXDB_DATABASE') or app.config.get('INFLUXDB_DATABASE', None)
class InfluxDBWrite(PluginBase):
def __init__(self, name=None):
self.client = InfluxDBClient.from_DSN(INFLUXDB_DSN, timeout=2)
dbname = INFLUXDB_DATABASE or self.client._database
try:
if dbname:
self.client.switch_database(dbname)
self.client.create_database(dbname)
except Exception as e:
LOG.error('InfluxDB: ERROR - %s' % e)
super(InfluxDBWrite, self).__init__(name)
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
# event data
points = [
{
"measurement": alert.event,
"tags": {
"resource": alert.resource,
"environment": alert.environment
},
"time": alert.last_receive_time,
"fields": {
"value": alert.value
}
}
]
# common tags
tags = {"service": ','.join(alert.service)}
if alert.customer:
tags.update(customer=alert.customer)
LOG.debug('InfluxDB: points=%s, tags=%s', points, tags)
try:
self.client.write_points(points, time_precision='ms', tags=tags)
except Exception as e:
raise RuntimeError("InfluxDB: ERROR - %s" % e)
def status_change(self, alert, status, text):
return