forked from alerta/alerta-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerta_logstash.py
53 lines (39 loc) · 1.57 KB
/
alerta_logstash.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
import json
import logging
import os
import socket
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.logstash')
DEFAULT_LOGSTASH_HOST = 'localhost'
DEFAULT_LOGSTASH_PORT = 6379
LOGSTASH_HOST = os.environ.get('LOGSTASH_HOST') or app.config.get('LOGSTASH_HOST', DEFAULT_LOGSTASH_HOST)
LOGSTASH_PORT = os.environ.get('LOGSTASH_PORT') or app.config.get('LOGSTASH_PORT', DEFAULT_LOGSTASH_PORT)
class LogStashOutput(PluginBase):
def __init__(self, name=None):
self.sock = None
super(LogStashOutput, self).__init__(name)
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
try:
logstash_port = int(LOGSTASH_PORT)
except Exception as e:
LOG.error("Alerta_logstash: Could not parse 'LOGSTASH_PORT': %s", e)
raise RuntimeError("Could not parse 'LOGSTASH_PORT': %s" % e)
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((LOGSTASH_HOST, logstash_port))
except Exception as e:
raise RuntimeError("Logstash TCP connection error: %s" % str(e))
try:
self.sock.send(b"%s\r\n" % json.dumps(alert.get_body(history=False)).encode('utf-8'))
except Exception as e:
LOG.exception(e)
raise RuntimeError("logstash exception")
self.sock.close()
def status_change(self, alert, status, text):
return