0
0
Fork 0
mirror of https://github.com/alerta/alerta-contrib.git synced 2025-03-18 14:22:52 +00:00
alerta_alerta-contrib/plugins/logstash/alerta_logstash.py
2016-09-15 10:18:03 +01:00

43 lines
1.2 KiB
Python

import os
import socket
import logging
from alerta.app import app
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:
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("%s\r\n" % alert)
except Exception as e:
LOG.exception(e)
raise RuntimeError("logstash exception")
self.sock.close()
def status_change(self, alert, status, text):
return