From c44a67cf54e18c203d8b0888f678a4e148caa0de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20MOHIER?= Date: Fri, 23 Feb 2018 13:29:28 +0100 Subject: [PATCH] Closes #488: notify Alignak arbiter for some configuration modifications (new host, new realm) --- alignak_backend/app.py | 28 +++++++++++++++++++++++++++- etc/settings.json | 6 ++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/alignak_backend/app.py b/alignak_backend/app.py index 6e075cca..da282b66 100755 --- a/alignak_backend/app.py +++ b/alignak_backend/app.py @@ -25,6 +25,9 @@ import logging from logging.config import dictConfig as logger_dictConfig + +import requests + from dateutil import parser from future.utils import iteritems @@ -205,6 +208,21 @@ def _validate_schema_version(self, schema_version, field, value): return +def notify_alignak(notification='reload_configuration'): + """Send a notification to the Alignak arbiter if configured""" + if not settings['ALIGNAK_URL'] or not notification: + return + + headers = None + try: + current_app.logger.info("Notifying Alignak for: %s..." % notification) + response = requests.get(settings['ALIGNAK_URL'] + notification, headers=headers, timeout=10) + resp = response.json() + current_app.logger.info("Notified, response: %s..." % resp) + except Exception as exp: + current_app.logger.error("Alignak notification exception: %s..." % str(exp)) + + # Hooks used to check user's rights def pre_get(resource, user_request, lookup): """Hook before get data. Add filter depend on roles of user @@ -1235,6 +1253,9 @@ def after_insert_realm(items): }, False, False, **lookup) g.updateRealm = False + # Notify Alignak + notify_alignak(notification='reload_configuration') + def after_update_realm(updated, original): """ @@ -1536,6 +1557,9 @@ def after_insert_host(items): if etags: g.replace_etags = etags + # Notify Alignak + notify_alignak(notification='reload_configuration') + def pre_service_patch(updates, original): """ @@ -2027,6 +2051,8 @@ def get_settings(prev_settings): settings['SCHEDULER_TIMEZONE'] = 'Etc/GMT' settings['JOBS'] = [] +settings['ALIGNAK_URL'] = '' + # Read configuration file to update/complete the configuration configuration_file = get_settings(settings) print("Application configuration file: %s" % configuration_file) @@ -2079,7 +2105,7 @@ def get_settings(prev_settings): settings['DOMAIN'] = register_models() base_path = os.path.dirname(os.path.abspath(alignak_backend.__file__)) -print("Application base path: %s" % base_path) +# print("Application base path: %s" % base_path) app = Eve( settings=settings, diff --git a/etc/settings.json b/etc/settings.json index b577832c..bb4c8af8 100644 --- a/etc/settings.json +++ b/etc/settings.json @@ -27,6 +27,12 @@ "LOGGER": "alignak-backend-logger.json", /* Python logger configuration file */ + /* Address of Alignak arbiter + The Alignak backend will use this adress to notify Alignak about backend newly created items + Set to an empty value to disable this feature + */ + "ALIGNAK_URL": "http://127.0.0.1:7770", + /* As soon as a Graphite or Influx is existing in the backend, the received metrics are sent to the corresponding TSDB. If the TSDB is not available, metrics are stored internally in the backend.