alerta-contrib/integrations/consul/consulalerta.py

119 lines
2.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python
import json
import os
import sys
import time
2023-03-20 23:39:34 +01:00
import consul
from alertaclient.api import Client
CONSUL_HOST = os.environ.get('CONSUL_HOST', '127.0.0.1')
CONSUL_PORT = int(os.environ.get('CONSUL_PORT', 8500))
2023-03-20 23:39:34 +01:00
client = consul.Consul(host=CONSUL_HOST, port=CONSUL_PORT, token=None,
scheme='http', consistency='default', dc=None, verify=True)
j = json.load(sys.stdin)
2023-03-20 23:39:34 +01:00
print('Request:')
2017-11-15 23:48:46 +00:00
print(j)
2017-02-19 10:33:24 +02:00
try:
url = client.kv.get('alerta/apiurl')[1]['Value']
except Exception:
2023-03-20 23:39:34 +01:00
print('No URL defined, exiting')
2017-02-19 10:33:24 +02:00
sys.exit(1)
2017-02-19 10:33:24 +02:00
try:
key = client.kv.get('alerta/apikey')[1]['Value']
except Exception:
2023-03-20 23:39:34 +01:00
print('No key defined, exiting')
2017-02-19 10:33:24 +02:00
sys.exit(1)
try:
max_retries = int(client.kv.get('alerta/max_retries')[1]['Value'])
except TypeError:
2023-03-20 23:39:34 +01:00
print('No value defined, using default')
2017-02-19 10:33:24 +02:00
max_retries = 3
try:
sleep = int(client.kv.get('alerta/sleep')[1]['Value'])
except TypeError:
2023-03-20 23:39:34 +01:00
print('No value defined, using default')
2017-02-19 10:33:24 +02:00
sleep = 2
try:
timeout = int(client.kv.get('alerta/timeout')[1]['Value'])
except TypeError:
2023-03-20 23:39:34 +01:00
print('No value defined, using default')
2017-02-19 10:33:24 +02:00
timeout = 900
try:
origin = client.kv.get('alerta/origin')[1]['Value']
except TypeError:
2023-03-20 23:39:34 +01:00
print('No value defined, using default')
origin = 'consul'
2017-02-19 10:33:24 +02:00
try:
alerttype = client.kv.get('alerta/alerttype')[1]['Value']
except TypeError:
2023-03-20 23:39:34 +01:00
print('No value defined, using default')
alerttype = 'ConsulAlert'
api = Client(endpoint=url, key=key)
SEVERITY_MAP = {
2023-03-20 23:39:34 +01:00
'critical': 'critical',
'warning': 'warning',
'passing': 'ok',
}
2023-03-20 23:39:34 +01:00
def createalert(data):
try:
2023-03-20 23:39:34 +01:00
environment = client.kv.get(
'alerta/env/{}'.format(data['Node']))[1]['Value']
except Exception:
2017-02-19 10:50:30 +02:00
try:
environment = client.kv.get('alerta/defaultenv')[1]['Value']
except Exception:
2023-03-20 23:39:34 +01:00
environment = 'Production'
2017-02-19 10:50:30 +02:00
for _ in range(max_retries):
try:
2023-03-20 23:39:34 +01:00
print('Response:')
response = api.send_alert(
2023-03-20 23:39:34 +01:00
resource=data['Node'],
event=data['CheckId'],
value=data['Status'],
correlate=SEVERITY_MAP.keys(),
environment=environment,
service=[data['CheckId']],
severity=SEVERITY_MAP[data['Status']],
text=data['Output'],
timeout=timeout,
origin=origin,
type=alerttype
)
print(response)
except Exception as e:
2023-03-20 23:39:34 +01:00
print('HTTP Error: {}'.format(e))
time.sleep(sleep)
2017-02-19 10:33:24 +02:00
continue
else:
break
else:
2023-03-20 23:39:34 +01:00
print('api is down')
def main():
for item in enumerate(j):
2023-03-20 23:39:34 +01:00
i = item[0]
createalert(j[i])
2023-03-20 23:39:34 +01:00
if __name__ == '__main__':
main()