Skip to content

Commit

Permalink
Enhance cron_grafana (add argument to force recreate dashboard and li…
Browse files Browse the repository at this point in the history
…mit this endpoint to localhost)
  • Loading branch information
David Durieux committed Jan 17, 2017
1 parent d512a61 commit 5440bf5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
35 changes: 28 additions & 7 deletions alignak_backend/app.py
Expand Up @@ -1728,7 +1728,7 @@ def cron_timeseries():
deleteitem_internal('timeseriesretention', False, False, **lookup)


@app.route("/cron_grafana")
@app.route("/cron_grafana", methods=['GET'])
def cron_grafana(engine='jsonify'):
"""
Cron used to add / update grafana dashboards
Expand All @@ -1737,6 +1737,15 @@ def cron_grafana(engine='jsonify'):
:rtype: dict
"""
# pylint: disable=too-many-nested-blocks
# pylint: disable=too-many-locals
try:
forcegenerate = request.args.get('forcegenerate')
if request.remote_addr != '127.0.0.1':
print('Access denied for %s' % request.remote_addr)
return make_response("Access denied from remote host", 412)
except Exception:
forcegenerate = None

with app.test_request_context():
resp = {}
hosts_db = current_app.data.driver.db['host']
Expand All @@ -1753,28 +1762,40 @@ def cron_grafana(engine='jsonify'):
if graf.connection:
# get the realms of the grafana
realm = realm_db.find_one({'_id': grafana['_realm']})
search = {'_realm': realm['_id'], '_is_template': False}
if forcegenerate is not None:
print("[cron_grafana] Force generation of grafana dashboard")
else:
search['ls_grafana'] = False
search['ls_perf_data'] = {"$ne": ""}
search['ls_last_check'] = {"$ne": 0}

if grafana['_sub_realm']:
children = realm['_all_children']
children.append(realm['_id'])
items = hosts_db.find({'ls_grafana': False, '_realm': {"$in": children},
'ls_perf_data': {"$ne": ""}, '_is_template': False})
search['_realm'] = {"$in": children}
items = hosts_db.find(search)
else:
items = hosts_db.find({'ls_grafana': False, '_realm': realm['_id'],
'ls_perf_data': {"$ne": ""}, '_is_template': False})
search['_realm'] = realm['_id']
items = hosts_db.find(search)
for item in items:
created = graf.create_dashboard(item['_id'])
if created:
resp[grafana['name']]['create_dashboard'].append(item['name'])
if forcegenerate is not None:
continue
# manage the cases hosts have new services or hosts not have ls_perf_data
hosts_dashboards = {}
if grafana['_sub_realm']:
children = realm['_all_children']
children.append(realm['_id'])
items = services_db.find({'ls_grafana': False, '_realm': {"$in": children},
'ls_perf_data': {"$ne": ""}, '_is_template': False})
'ls_perf_data': {"$ne": ""}, '_is_template': False,
'ls_last_check': {"$ne": 0}})
else:
items = services_db.find({'ls_grafana': False, '_realm': realm['_id'],
'ls_perf_data': {"$ne": ""}, '_is_template': False})
'ls_perf_data': {"$ne": ""}, '_is_template': False,
'ls_last_check': {"$ne": 0}})
for item in items:
if item['host'] not in hosts_dashboards:
host = hosts_db.find_one({'_id': item['host']})
Expand Down
16 changes: 8 additions & 8 deletions alignak_backend/grafana.py
Expand Up @@ -120,14 +120,14 @@ def create_dashboard(self, host_id): # pylint: disable=too-many-locals

if len(targets) > 0:
rows.append(self.generate_row(command_name, targets, ObjectId(host['_realm'])))
if host['ls_last_check'] > 0:
# Update host live state
data = {
"ls_grafana": True,
"ls_grafana_panelid": self.panel_id
}
lookup = {"_id": host['_id']}
patch_internal('host', data, False, False, **lookup)
if host['ls_last_check'] > 0:
# Update host live state
data = {
"ls_grafana": True,
"ls_grafana_panelid": self.panel_id
}
lookup = {"_id": host['_id']}
patch_internal('host', data, False, False, **lookup)

# now get services
services = service_db.find({'host': host_id})
Expand Down
5 changes: 5 additions & 0 deletions docs/api.rst
Expand Up @@ -523,6 +523,11 @@ possibility can be used to give different access to grafana to different users (
grafana server or with same grafana server but with different organizations and so different
API KEYS).

It's possible to force to regenerate all dashboards in grafana (works only on localhost)::

curl "http://127.0.0.1:5000/cron_grafana?forcegenerate=1"


Special parameters for livesynthesis
------------------------------------

Expand Down

0 comments on commit 5440bf5

Please sign in to comment.