Allow raw Grafana links

This commit introduces two configuration changes:

* The 'raw' attribute for links in GRAFANA_LINKS which allows
  the user to point the dashboard URLs specified through
  GRAFANA_LINKS at arbitrary paths or URLs without further
  modification. This is False by default, which means the
  'fileName' and 'path' attributes are considered to be
  database or JSON file names to be inserted into a URL.
* The SHOW_GRAFANA_HOME setting which controls whether the
  "Grafana Home" button is displayed or not. By default this
  is True and the button is shown.

Change-Id: I55660f91fd56eb22dd7ad4674af422aeb2ba2ea3
This commit is contained in:
Johannes Grassler 2017-11-06 16:14:58 +01:00
parent dd6e09b01d
commit 8aa1cccf29
3 changed files with 51 additions and 4 deletions

View File

@ -61,18 +61,49 @@ DASHBOARDS = getattr(settings, 'GRAFANA_LINKS', GRAFANA_LINKS)
# For any Grafana version additional links to specific dashboards can be
# created in two formats.
# Flat:
# GRAFANA_LINKS = [ {'title': _('Dashboard'), 'path': 'openstack'} ]
# GRAFANA_LINKS = [ {'title': _('Dashboard'), 'path': 'openstack', 'raw': False} ]
#
# Per project: '*' will be applied to all projects not explicitly listed.
# GRAFANA_LINKS = [
# {'admin': [
# {'title': _('Dashboard'), 'path': 'openstack'}]},
# {'title': _('Dashboard'), 'path': 'openstack', 'raw': False}]},
# {'*': [
# {'title': _('OpenStack Dashboard'), 'path': 'project'}]}
# {'title': _('OpenStack Dashboard'), 'path': 'project', 'raw': False}]}
# ]
#
# If GRAFANA_URL is specified, the dashboard file name/raw URL must be
# specified through the 'path' attribute as shown above.
#
# Flat:
# GRAFANA_LINKS = [ {'title': _('Dashboard'), 'fileName': 'openstack.json', 'raw': False} ]
#
# GRAFANA_LINKS = [
# {'admin': [
# {'fileName': _('Dashboard'), 'fileName': 'openstack.json', 'raw': False}]},
# {'*': [
# {'title': _('OpenStack Dashboard'), 'fileName': 'project.json': False}]}
# ]
#
# If GRAFANA_URL is unspecified the dashboard file name must be specified
# through the fileName attribute.
#
# Both with and without GRAFANA_URL, the links have an optional 'raw' attribute
# which defaults to False if unspecified. If it is False, the value of 'path'
# (or 'fileName', respectively) is interpreted as a dashboard name and a link
# to the dashboard based on the dashboard's name will be generated. If it is
# True, the value of 'path' or 'fileName' will be treated as a URL to be used
# verbatim.
GRAFANA_URL = getattr(settings, 'GRAFANA_URL', None)
# If GRAFANA_URL is specified, an additional link will be shown that points to
# Grafana's list of dashboards. If you do not wish this, set SHOW_GRAFANA_HOME
# to False (by default this setting is True and the link will thus be shown).
SHOW_GRAFANA_HOME = getattr(settings, 'SHOW_GRAFANA_HOME', True)
ENABLE_KIBANA_BUTTON = getattr(settings, 'ENABLE_KIBANA_BUTTON', False)
KIBANA_POLICY_RULE = getattr(settings, 'KIBANA_POLICY_RULE',
'monitoring:kibana_access')

View File

@ -8,19 +8,29 @@
{% block main %}
<div style="padding: 3px;">
{% if grafana_url %}
{% if show_grafana_home %}
<a target="Grafana Home" href="{{grafana_url}}" class="btn btn-primary">
<span class="fa fa-bar-chart"></span>
Grafana Home
</a>
{% endif %}
{% for dashboard in dashboards %}
{% if dashboard.raw %}
<a target={{ dashboard.title }} href="{{ dashboard.path }}" class="btn btn-default">
{% else %}
<a target={{ dashboard.title }} href="{{grafana_url}}/dashboard/db/{{ dashboard.path }}" class="btn btn-default">
{% endif %}
<span class="fa fa-bar-chart"></span>
{% trans dashboard.title %}
</a>
{% endfor %}
{% else %}
{% for dashboard in dashboards %}
{% if dashboard.raw %}
<a target={{ dashboard.title }} href="{{ dashboard.fileName }}" class="btn btn-default">
{% else %}
<a target={{ dashboard.title }} href="/grafana/index.html#/dashboard/file/{{ dashboard.fileName }}?api={{api}}" class="btn btn-default">
{% endif %}
<span class="fa fa-bar-chart"></span>
{% trans dashboard.title %}
</a>

View File

@ -240,10 +240,14 @@ class IndexView(TemplateView):
api_root = self.request.build_absolute_uri(proxy_url_path)
context["api"] = api_root
context["dashboards"] = get_dashboard_links(self.request)
# Ensure all links have a 'raw' attribute
for link in context["dashboards"]:
link['raw'] = link.get('raw', False)
context['can_access_logs'] = policy.check(
((getattr(settings, 'KIBANA_POLICY_SCOPE'), getattr(settings, 'KIBANA_POLICY_RULE')), ), self.request
)
context['enable_kibana_button'] = settings.ENABLE_KIBANA_BUTTON
context['show_grafana_home'] = settings.SHOW_GRAFANA_HOME
return context
@ -261,7 +265,9 @@ class MonascaProxyView(TemplateView):
dimensions_str = req_kwargs['dimensions'][0]
dimensions_str_array = dimensions_str.split(',')
for dimension in dimensions_str_array:
dimension_name_value = dimension.split(':')
# limit splitting since value may contain a ':' such as in
# the `url` dimension of the service_status check.
dimension_name_value = dimension.split(':', 1)
if len(dimension_name_value) == 2:
name = dimension_name_value[0].encode('utf8')
value = dimension_name_value[1].encode('utf8')