mirror of
https://github.com/alerta/alerta-contrib.git
synced 2025-03-17 22:12:38 +00:00
41 lines
935 B
Python
41 lines
935 B
Python
|
|
import json
|
|
import requests
|
|
|
|
from alerta.app import app
|
|
from alerta.plugins import PluginBase
|
|
|
|
LOG = app.logger
|
|
|
|
INFLUXDB_URL = 'http://localhost:8086'
|
|
INFLUXDB_USER = 'alerta'
|
|
INFLUXDB_PASSWORD = 'alerta'
|
|
|
|
|
|
class InfluxDBWrite(PluginBase):
|
|
|
|
def pre_receive(self, alert):
|
|
|
|
return alert
|
|
|
|
def post_receive(self, alert):
|
|
|
|
url = INFLUXDB_URL + '/db/alerta/series'
|
|
|
|
data = [{
|
|
"name": alert.event,
|
|
"columns": ["value", "environment", "resource"],
|
|
"points": [
|
|
[alert.value, alert.environment, alert.resource]
|
|
]
|
|
}]
|
|
|
|
LOG.debug('InfluxDB data: %s', data)
|
|
|
|
try:
|
|
response = requests.post(url=url, data=json.dumps(data), auth=(INFLUXDB_USER, INFLUXDB_PASSWORD))
|
|
except Exception as e:
|
|
raise RuntimeError("InfluxDB connection error: %s", e)
|
|
|
|
LOG.debug('InfluxDB response: %s', response)
|
|
|