mirror of
https://github.com/alerta/alerta-contrib.git
synced 2025-03-18 06:12:51 +00:00
Add plugin for logging alerts to syslog
This commit is contained in:
parent
e2f7a127a1
commit
3246722d10
3 changed files with 134 additions and 0 deletions
52
plugins/syslog/README.md
Normal file
52
plugins/syslog/README.md
Normal file
|
@ -0,0 +1,52 @@
|
|||
alerta-logger
|
||||
=============
|
||||
|
||||
Log alerts to syslog.
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
$ python setup.py install
|
||||
|
||||
|
||||
Configuration
|
||||
-------------
|
||||
|
||||
Add `syslog` to the list of enabled `PLUGINS`.
|
||||
|
||||
```
|
||||
PLUGINS = ['reject', 'syslog']
|
||||
```
|
||||
|
||||
The defaults are enough for most use-cases or override them in the `alertad.conf` configuration file:
|
||||
|
||||
```
|
||||
SYSLOG_FORMAT = '%(name)s[%(process)d]: %(levelname)s - %(message)s'
|
||||
SYSLOG_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
|
||||
SYSLOG_FACILITY = 'local7'
|
||||
```
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
```
|
||||
Aug 2 15:49:54 macbookpro alerta[47471]: ERROR - {"origin": "alerta/macbookpro.home", "text": "Host is down", "lastReceiveTime": "2016-08-02T14:49:54.353Z", "receiveTime": "2016-08-02T14:49:54.353Z", "trendIndication": "moreSevere", "rawData": "", "previousSeverity": "unknown", "event": "node_down", "group": "Network", "severity": "major", "service": ["Test"], "id": "66deab25-a02e-4233-84d7-0573a326447c", "environment": "Development", "type": "exceptionAlert", "status": "open", "repeat": false, "tags": [], "createTime": "2016-08-02T14:49:54.306Z", "lastReceiveId": "66deab25-a02e-4233-84d7-0573a326447c", "customer": null, "resource": "web04", "duplicateCount": 0, "correlate": [], "value": "n/a", "timeout": 86400, "attributes": {"ip": "127.0.0.1"}, "history": []}
|
||||
```
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
Alerta monitoring system and console
|
||||
Copyright 2016 Nick Satterly
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
51
plugins/syslog/logger.py
Normal file
51
plugins/syslog/logger.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
|
||||
from logging.handlers import SysLogHandler
|
||||
|
||||
from alerta.app import app
|
||||
from alerta.plugins import PluginBase
|
||||
|
||||
LOGGER_SYSLOG_FORMAT = os.environ.get('LOGGER_SYSLOG_FORMAT') or app.config.get('SYSLOG_FORMAT','%(name)s[%(process)d]: %(levelname)s - %(message)s')
|
||||
LOGGER_SYSLOG_DATE_FORMAT = os.environ.get('LOGGER_SYSLOG_DATE_FORMAT') or app.config.get('SYSLOG_DATE_FORMAT', '%Y-%m-%d %H:%M:%S')
|
||||
LOGGER_SYSLOG_FACILITY = os.environ.get('LOGGER_SYSLOG_FACILITY') or app.config.get('SYSLOG_FACILITY', 'local7')
|
||||
|
||||
class Syslog(PluginBase):
|
||||
|
||||
def __init__(self, name=None):
|
||||
|
||||
self.logger = logging.getLogger('alerta')
|
||||
|
||||
if sys.platform == "darwin":
|
||||
socket = '/var/run/syslog'
|
||||
else:
|
||||
socket = '/dev/log'
|
||||
facility = LOGGER_SYSLOG_FACILITY
|
||||
|
||||
syslog = SysLogHandler(address=socket, facility=facility)
|
||||
formatter = logging.Formatter(fmt=LOGGER_SYSLOG_FORMAT, datefmt=LOGGER_SYSLOG_DATE_FORMAT)
|
||||
syslog.setFormatter(formatter)
|
||||
self.logger.addHandler(syslog)
|
||||
|
||||
super(Syslog, self).__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
|
31
plugins/syslog/setup.py
Normal file
31
plugins/syslog/setup.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import os
|
||||
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
version = '0.1.0'
|
||||
|
||||
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
|
||||
|
||||
setup(
|
||||
name="alerta-logger",
|
||||
version=version,
|
||||
description='Alerta plugin for syslog logging',
|
||||
url='https://github.com/alerta/alerta-contrib',
|
||||
license='Apache License 2.0',
|
||||
author='Nick Satterly',
|
||||
author_email='nick.satterly@theguardian.com',
|
||||
packages=find_packages(),
|
||||
py_modules=['logger'],
|
||||
install_requires=[
|
||||
'alerta-server'
|
||||
],
|
||||
include_package_data=True,
|
||||
zip_safe=False,
|
||||
entry_points={
|
||||
'alerta.plugins': [
|
||||
'syslog = logger:Syslog'
|
||||
]
|
||||
}
|
||||
)
|
Loading…
Reference in a new issue