Skip to content
Snippets Groups Projects
Commit c23029e8 authored by jurgenhaas's avatar jurgenhaas
Browse files

Support for alerta alerts created from fluentd

parent 40c6abbc
No related branches found
No related tags found
No related merge requests found
......@@ -69,6 +69,15 @@
tags: 'Config'
notify: "Restart td-agent"
- name: "Copy script to post Alerta"
template:
src='alertalerta.py'
dest='/usr/local/bin/alertalerta.py'
owner='root'
group='root'
mode='755'
tags: 'Config'
- name: "Add td-agent user to adm groups"
user:
name='td-agent'
......
#!/usr/bin/env python
"""
Create an Alerta alert
======================
Alerts are unique with environment-resource-event
"""
import argparse
import json
import os
import time
from subprocess import call
class Alerta:
args = None
extras = None
def __init__(self):
self.parser = argparse.ArgumentParser(description='Alerta CLI')
def run(self):
self.parser.add_argument('host', help='Host')
self.parser.add_argument('environment', help='Environment')
self.parser.add_argument('webhook', help='Alerta webhook')
self.parser.add_argument('apikey', help='Alerta API key')
self.parser.add_argument('payload', help='Json string or filename')
self.args, self.extras = self.parser.parse_known_args()
if os.path.isfile(self.args.payload):
fp = file(self.args.payload)
for line in fp:
self.process(line)
else:
self.process(self.args.payload)
def default_values(self):
values = {
'origin': self.args.host,
'timeout': 86400,
'environment': self.args.environment,
'resource': 'undefined-%s' % time.time(),
'event': 'Exception',
'tags': ['environment:%s' % self.args.environment],
'group': 'Application',
'severity': 'critical',
'service': ['www.example.com'],
'text': '',
'value': '',
'rawData': '',
}
return values
@staticmethod
def custom_keys():
return [
'timeout',
'resource',
'event',
'severity',
'service',
'text',
'value',
'rawData'
]
def process(self, line):
values = self.default_values()
linevalues = json.loads(line)
for key in self.custom_keys():
if key in linevalues.keys():
values.__setitem__(key, linevalues[key])
cmd = [
'/usr/bin/curl',
'-XPOST',
'%s/alert' % self.args.webhook,
'-H',
'Authorization: Key %s' % self.args.apikey,
'-H',
'Content-type: application/json',
'-d',
json.dumps(values)
]
call(cmd)
Alerta().run()
......@@ -27,6 +27,7 @@
format json
port {{ appl.port }}
bind 127.0.0.1
message_length_limit 1MB
</source>
{% endfor %}
......@@ -192,6 +193,19 @@
pattern \/server-status\?auto
</exclude>
</filter>
<match alerta>
@type exec
command /usr/local/bin/alertalerta.py {{ inventory_hostname }} {{ netdata_alerta_environment|default("production") }} {{ netdata_alerta_webhook }} {{ netdata_alerta_apikey }}
<buffer>
@type file
path /var/log/td-agent/buffer/alerta.*.buffer
flush_at_shutdown true
</buffer>
<format>
@type json
</format>
</match>
{% if fluentd_mail_output is defined %}
<match mail>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment