-
Notifications
You must be signed in to change notification settings - Fork 171
/
alerta_logger.py
63 lines (46 loc) · 1.84 KB
/
alerta_logger.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
import logging
import os
import sys
from logging.handlers import SysLogHandler
from alerta.plugins import PluginBase
try:
from alerta.plugins import app # alerta >= 5.0
except ImportError:
from alerta.app import app # alerta < 5.0
LOG = logging.getLogger('alerta.plugins.logger')
DEFAULT_SYSLOG_FORMAT = '%(name)s[%(process)d]: %(levelname)s - %(message)s'
DEFAULT_SYSLOG_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
DEFAULT_SYSLOG_FACILITY = 'local7'
LOGGER_SYSLOG_FORMAT = os.environ.get('LOGGER_SYSLOG_FORMAT') or app.config.get(
'SYSLOG_FORMAT', DEFAULT_SYSLOG_FORMAT)
LOGGER_SYSLOG_DATE_FORMAT = os.environ.get('LOGGER_SYSLOG_DATE_FORMAT') or app.config.get(
'SYSLOG_DATE_FORMAT', DEFAULT_SYSLOG_DATE_FORMAT)
LOGGER_SYSLOG_FACILITY = os.environ.get('LOGGER_SYSLOG_FACILITY') or app.config.get(
'SYSLOG_FACILITY', DEFAULT_SYSLOG_FACILITY)
class Syslog(PluginBase):
def __init__(self, name=None):
self.logger = logging.getLogger(name)
if sys.platform == 'darwin':
socket = '/var/run/syslog'
else:
socket = '/dev/log'
facility = LOGGER_SYSLOG_FACILITY
syslog = SysLogHandler(address=socket, facility=facility)
syslog.setFormatter(logging.Formatter(
fmt=LOGGER_SYSLOG_FORMAT, datefmt=LOGGER_SYSLOG_DATE_FORMAT))
self.logger.addHandler(syslog)
super().__init__(name)
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
if alert.severity == 'critical':
level = logging.CRITICAL
elif alert.severity in ['major', 'minor']:
level = logging.ERROR
elif alert.severity == 'warning':
level = logging.WARNING
else:
level = logging.INFO
self.logger.log(level=level, msg=alert)
def status_change(self, alert, status, text):
pass