Skip to content

Commit

Permalink
Merge 9acad04 into 8565bf8
Browse files Browse the repository at this point in the history
  • Loading branch information
mohierf committed Apr 22, 2018
2 parents 8565bf8 + 9acad04 commit 2243d74
Show file tree
Hide file tree
Showing 19 changed files with 661 additions and 157 deletions.
106 changes: 85 additions & 21 deletions alignak_backend/app.py
Expand Up @@ -208,20 +208,23 @@ def _validate_schema_version(self, schema_version, field, value):
return


def notify_alignak(notification='reload_configuration'):
def notify_alignak(event=None, parameters=None, notification=None):
"""Send a notification to the Alignak arbiter if configured"""
if not settings['ALIGNAK_URL'] or not notification:
if not settings['ALIGNAK_URL'] or not event:
return

headers = None
try:
current_app.logger.warning("Notifying Alignak for: %s..." % notification)
response = requests.get(settings['ALIGNAK_URL'] + '/' + notification,
headers=headers, timeout=10)
resp = response.json()
current_app.logger.warning("Notified, response: %s..." % resp)
current_app.logger.info("Logging an Alignak notification: %s / %s (%s)"
% (event, parameters, notification))
data = {'event': event}
if parameters:
data['parameters'] = parameters
if notification:
data['notification'] = notification
response, _, _, _, _ = post_internal('alignak_notifications', data, True)
current_app.logger.debug("Notification: %s" % response)
except Exception as exp:
current_app.logger.error("Alignak notification failed: %s..." % str(exp))
current_app.logger.error("Alignak notification log failed: %s" % str(exp))


# Hooks used to check user's rights
Expand Down Expand Up @@ -1254,8 +1257,8 @@ def after_insert_realm(items):
}, False, False, **lookup)
g.updateRealm = False

# Notify Alignak
notify_alignak(notification='reload_configuration')
# Notify Alignak
notify_alignak(event='creation', parameters='realm:%s' % item['name'])


def after_update_realm(updated, original):
Expand Down Expand Up @@ -1333,7 +1336,7 @@ def after_delete_realm(item):
g.updateRealm = False

# Notify Alignak
notify_alignak(notification='reload_configuration')
notify_alignak(event='deletion', parameters='realm:%s' % item['name'])


def after_delete_resource_realm():
Expand Down Expand Up @@ -1381,7 +1384,7 @@ def after_delete_host(item):
current_app.logger.debug("Deleted host: %s", item['name'])

# Notify Alignak
notify_alignak(notification='reload_configuration')
notify_alignak(event='deletion', parameters='host:%s' % item['name'])


# Alignak
Expand Down Expand Up @@ -1559,15 +1562,16 @@ def after_insert_host(items):

# Host overall was computed, update the host overall state
lookup = {"_id": item['_id']}
(_, _, etag, _) = patch_internal('host', {"_overall_state_id": overall_state}, False, False,
**lookup)
(_, _, etag, _) = patch_internal('host', {"_overall_state_id": overall_state},
False, False, **lookup)
etags[item['_etag']] = etag

# Notify Alignak
notify_alignak(event='creation', parameters='host:%s' % item['name'])

if etags:
g.replace_etags = etags

# Notify Alignak
notify_alignak(notification='reload_configuration')


def pre_service_patch(updates, original):
"""
Expand Down Expand Up @@ -2051,8 +2055,12 @@ def get_settings(prev_settings):
settings['SERVER_NAME'] = None
settings['DEBUG'] = False

settings['SCHEDULER_ALIGNAK_ACTIVE'] = True
settings['SCHEDULER_ALIGNAK_PERIOD'] = 300
settings['SCHEDULER_TIMESERIES_ACTIVE'] = False
settings['SCHEDULER_TIMESERIES_PERIOD'] = 10
settings['SCHEDULER_GRAFANA_ACTIVE'] = False
settings['SCHEDULER_GRAFANA_PERIOD'] = 120
settings['SCHEDULER_LIVESYNTHESIS_HISTORY'] = 0
settings['SCHEDULER_TIMEZONE'] = 'Etc/GMT'
settings['JOBS'] = []
Expand All @@ -2079,7 +2087,7 @@ def get_settings(prev_settings):
'func': 'alignak_backend.scheduler:cron_cache',
'args': (),
'trigger': 'interval',
'seconds': 10
'seconds': settings['SCHEDULER_TIMESERIES_PERIOD']
}
)
if settings['SCHEDULER_GRAFANA_ACTIVE']:
Expand All @@ -2089,7 +2097,7 @@ def get_settings(prev_settings):
'func': 'alignak_backend.scheduler:cron_grafana',
'args': (),
'trigger': 'interval',
'seconds': 120
'seconds': settings['SCHEDULER_GRAFANA_PERIOD']
}
)
if settings['SCHEDULER_LIVESYNTHESIS_HISTORY'] > 0:
Expand All @@ -2099,7 +2107,17 @@ def get_settings(prev_settings):
'func': 'alignak_backend.scheduler:cron_livesynthesis_history',
'args': (),
'trigger': 'interval',
'seconds': 60
'seconds': settings['SCHEDULER_LIVESYNTHESIS_HISTORY']
}
)
if settings['SCHEDULER_ALIGNAK_ACTIVE']:
jobs.append(
{
'id': 'cron_alignak',
'func': 'alignak_backend.scheduler:cron_alignak',
'args': (),
'trigger': 'interval',
'seconds': settings['SCHEDULER_ALIGNAK_PERIOD']
}
)

Expand Down Expand Up @@ -2536,6 +2554,52 @@ def backend_version():
return jsonify(my_version)


@app.route("/cron_alignak")
def cron_alignak():
"""
Cron used to notify Alignak about some events: configuration reload, ...
:return: None
"""
with app.test_request_context():
alignak_notifications_db = app.data.driver.db['alignak_notifications']
current_app.logger.warning("[cron_alignak]: %d notifications"
% alignak_notifications_db.count())
if not alignak_notifications_db.count():
return

sent = set()
notifications = alignak_notifications_db.find()
for data in notifications:
current_app.logger.warning("[cron_alignak]: %s" % data)
headers = {'Content-Type': 'application/json'}
params = {
"event": data['event'],
"parameters": data['parameters']
}
url = "%s/%s" % (settings['ALIGNAK_URL'], data['notification'])

try:
if data['notification'] not in sent:
current_app.logger.warning("[cron_alignak]: Notifying Alignak: %s / %s"
% (url, params))
response = requests.post(url=url, headers=headers, json=params, timeout=10)
current_app.logger.warning("[cron_alignak]: Notified, response: %s"
% response.json())
# except NewConnectionError:
# current_app.logger.warning("Alignak is not available for notification")
except Exception as exp:
current_app.logger.warning("[cron_alignak]: Alignak notification failed: %s..."
% str(exp))
finally:
# Delete
lookup = {"_id": data['_id']}
deleteitem_internal('alignak_notifications', False, False, **lookup)
current_app.logger.warning("[cron_alignak]: Deleted notification: %s" % data['_id'])
# Sent!
sent.add(data['notification'])


@app.route("/cron_timeseries")
def cron_timeseries():
"""
Expand Down
66 changes: 66 additions & 0 deletions alignak_backend/models/alignak_notifications.py
@@ -0,0 +1,66 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Resource information for Alignak notifications (eg. reload configuration)
"""


def get_name(friendly=False):
"""Get name of this resource
:return: name of this resource
:rtype: str
"""
if friendly: # pragma: no cover
return "Alignak notifications"
return 'alignak_notifications'


def get_doc(): # pragma: no cover
"""Get documentation of this resource
:return: rst string
:rtype: str
"""
return """
The ``alignak_notifications`` model is a cache used internally by the backend to store the
notifications that must be sent out to the Alignak arbiter.
"""


def get_schema():
"""Schema structure of this resource
:return: schema dictionary
:rtype: dict
"""
return {
'internal_resource': True,
'schema': {
'schema_version': {
'type': 'integer',
'default': 1,
},
'event': {
'schema_version': 1,
'title': 'Notification event (creation, deletion,...)',
'type': 'string',
'required': True,
},
'parameters': {
'schema_version': 1,
'title': 'Notification parameters',
'type': 'string',
'default': '',
'required': False
},
'notification': {
'schema_version': 1,
'title': 'Notification url',
'type': 'string',
'default': 'backend_notification',
'required': False
}
},
'schema_deleted': {}
}
2 changes: 1 addition & 1 deletion alignak_backend/models/userrestrictrole.py
Expand Up @@ -85,7 +85,7 @@ def get_schema():
'host', 'hostgroup', 'hostdependency', 'hostescalation',
'service', 'servicegroup', 'servicedependency', 'serviceescalation',
'grafana', 'graphite', 'influxdb', 'statsd',
'timeseriesretention',
'timeseriesretention', 'aligank_notifications',
'livesynthesis', 'livesynthesisretention',
'logcheckresult', 'history'
],
Expand Down
9 changes: 9 additions & 0 deletions alignak_backend/scheduler.py
Expand Up @@ -9,6 +9,15 @@
import alignak_backend.app


def cron_alignak():
"""
It's the scheduler used to notify Alignak
:return: None
"""
alignak_backend.app.cron_alignak()


def cron_cache():
"""
It's the scheduler used to send to graphite / influxdb retention perfdata if previously
Expand Down
6 changes: 0 additions & 6 deletions alignak_backend/timeseries.py
Expand Up @@ -215,15 +215,9 @@ def get_realms_prefix(realm_id):
:return: realms name separed by .
:rtype: str
"""
# print("******Realm All: %s" % realm_id)
prefix_realm = ''
realm_db = current_app.data.driver.db['realm']
# print("******Realm id: %s" % realm_id)
# realms = realm_db.find()
# for realm in realms:
# print("******Realm: %s (%s)" % (realm['name'], realm['_id']))
realm_info = realm_db.find_one({'_id': realm_id})
# print("******Realm info: %s" % realm_info)
if realm_info['_tree_parents']:
realms = realm_db.find({'_id': {"$in": realm_info['_tree_parents']}}).sort("_level")
for realm in realms:
Expand Down
Binary file added docs/_static/alignakretention.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_static/configalignak_notifications.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/_static/configalignakretention.png
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/resources/userrestrictrole.rst
Expand Up @@ -38,7 +38,7 @@ User role restriction (userrestrictrole)

``resource``: Resource concerned with the right

Allowed values: [, ', *, ', ,, , ', a, c, t, i, o, n, a, c, k, n, o, w, l, e, d, g, e, ', ,, , ', a, c, t, i, o, n, d, o, w, n, t, i, m, e, ', ,, , ', a, c, t, i, o, n, f, o, r, c, e, c, h, e, c, k, ', ,, , ', a, l, i, g, n, a, k, ', ,, , ', a, l, i, g, n, a, k, d, a, e, m, o, n, ', ,, , ', r, e, a, l, m, ', ,, , ', c, o, m, m, a, n, d, ', ,, , ', t, i, m, e, p, e, r, i, o, d, ', ,, , ', u, s, e, r, ', ,, , ', u, s, e, r, g, r, o, u, p, ', ,, , ', u, s, e, r, r, e, s, t, r, i, c, t, r, o, l, e, ', ,, , ', h, o, s, t, ', ,, , ', h, o, s, t, g, r, o, u, p, ', ,, , ', h, o, s, t, d, e, p, e, n, d, e, n, c, y, ', ,, , ', h, o, s, t, e, s, c, a, l, a, t, i, o, n, ', ,, , ', s, e, r, v, i, c, e, ', ,, , ', s, e, r, v, i, c, e, g, r, o, u, p, ', ,, , ', s, e, r, v, i, c, e, d, e, p, e, n, d, e, n, c, y, ', ,, , ', s, e, r, v, i, c, e, e, s, c, a, l, a, t, i, o, n, ', ,, , ', g, r, a, f, a, n, a, ', ,, , ', g, r, a, p, h, i, t, e, ', ,, , ', i, n, f, l, u, x, d, b, ', ,, , ', s, t, a, t, s, d, ', ,, , ', t, i, m, e, s, e, r, i, e, s, r, e, t, e, n, t, i, o, n, ', ,, , ', l, i, v, e, s, y, n, t, h, e, s, i, s, ', ,, , ', l, i, v, e, s, y, n, t, h, e, s, i, s, r, e, t, e, n, t, i, o, n, ', ,, , ', l, o, g, c, h, e, c, k, r, e, s, u, l, t, ', ,, , ', h, i, s, t, o, r, y, ', ]
Allowed values: [, ', *, ', ,, , ', a, c, t, i, o, n, a, c, k, n, o, w, l, e, d, g, e, ', ,, , ', a, c, t, i, o, n, d, o, w, n, t, i, m, e, ', ,, , ', a, c, t, i, o, n, f, o, r, c, e, c, h, e, c, k, ', ,, , ', a, l, i, g, n, a, k, ', ,, , ', a, l, i, g, n, a, k, d, a, e, m, o, n, ', ,, , ', r, e, a, l, m, ', ,, , ', c, o, m, m, a, n, d, ', ,, , ', t, i, m, e, p, e, r, i, o, d, ', ,, , ', u, s, e, r, ', ,, , ', u, s, e, r, g, r, o, u, p, ', ,, , ', u, s, e, r, r, e, s, t, r, i, c, t, r, o, l, e, ', ,, , ', h, o, s, t, ', ,, , ', h, o, s, t, g, r, o, u, p, ', ,, , ', h, o, s, t, d, e, p, e, n, d, e, n, c, y, ', ,, , ', h, o, s, t, e, s, c, a, l, a, t, i, o, n, ', ,, , ', s, e, r, v, i, c, e, ', ,, , ', s, e, r, v, i, c, e, g, r, o, u, p, ', ,, , ', s, e, r, v, i, c, e, d, e, p, e, n, d, e, n, c, y, ', ,, , ', s, e, r, v, i, c, e, e, s, c, a, l, a, t, i, o, n, ', ,, , ', g, r, a, f, a, n, a, ', ,, , ', g, r, a, p, h, i, t, e, ', ,, , ', i, n, f, l, u, x, d, b, ', ,, , ', s, t, a, t, s, d, ', ,, , ', t, i, m, e, s, e, r, i, e, s, r, e, t, e, n, t, i, o, n, ', ,, , ', a, l, i, g, a, n, k, _, n, o, t, i, f, i, c, a, t, i, o, n, s, ', ,, , ', l, i, v, e, s, y, n, t, h, e, s, i, s, ', ,, , ', l, i, v, e, s, y, n, t, h, e, s, i, s, r, e, t, e, n, t, i, o, n, ', ,, , ', l, o, g, c, h, e, c, k, r, e, s, u, l, t, ', ,, , ', h, i, s, t, o, r, y, ', ]

.. _userrestrictrole-sub_realm:

Expand Down
14 changes: 13 additions & 1 deletion etc/settings.json
Expand Up @@ -28,23 +28,35 @@
"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
The Alignak backend will use this adress to notify Alignak about backend newly created
or deleted items
Set to an empty value to disable this feature
Notes:
- / characters must be \ escaped!
*/
"ALIGNAK_URL": "http:\/\/127.0.0.1:7770",

/* Alignak event reporting scheduler
Every SCHEDULER_ALIGNAK_PERIOD, an event is raised to the ALIGNAK_URL if an host/realm/user
was created or deleted

Only raise notifications every 10 minutes
*/
"SCHEDULER_ALIGNAK_ACTIVE": true,
"SCHEDULER_ALIGNAK_PERIOD": 600,

/* 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.
The timeseries scheduler will check periodially if some some metrics are existing in the
retention and will send them to the configured TSDB.
BE CAREFULL, ACTIVATE THIS ON ONE BACKEND ONLY! */
"SCHEDULER_TIMESERIES_ACTIVE": false,
"SCHEDULER_TIMESERIES_PERIOD": 10,
/* This scheduler will create / update dashboards in grafana.
BE CAREFULL, ACTIVATE IT ONLY ON ONE BACKEND */
"SCHEDULER_GRAFANA_ACTIVE": false,
"SCHEDULER_GRAFANA_PERIOD": 120,
/* Enable/disable this backend instance as a Grafana datasource */
"GRAFANA_DATASOURCE": true,
/* Name of the file that contains the list of proposed queries in a Grafana table panel */
Expand Down
2 changes: 1 addition & 1 deletion test/cfg/default/reactionners/reactionner-master.cfg
Expand Up @@ -26,7 +26,7 @@ define reactionner {
modules

# Reactionner tags are the tag that the reactionner will manage. Use None as tag name to manage
# untaggued notification/event handlers
# untagged notification/event handlers
#reactionner_tags None

# Enable https or not
Expand Down

0 comments on commit 2243d74

Please sign in to comment.