From 6777c903a013d3d8142575625a705dae58934a06 Mon Sep 17 00:00:00 2001 From: Mark Giles Date: Wed, 18 Apr 2018 15:52:09 -0400 Subject: [PATCH] Automatically add Prometheus as a Grafana datasource Assuming both Prometheus and Grafana are enabled, this change configures Grafana such that it can use data collected by the Prometheus server. Partially-Implements: blueprint prometheus Change-Id: I0290806d0c622b1de33cf7047f099795b4c780b9 --- ansible/roles/grafana/handlers/main.yml | 1 + ansible/roles/grafana/tasks/config.yml | 20 +++++++++++++++++++ .../roles/grafana/templates/grafana.ini.j2 | 1 + .../roles/grafana/templates/grafana.json.j2 | 7 +++++++ .../grafana/templates/prometheus.yaml.j2 | 9 +++++++++ 5 files changed, 38 insertions(+) create mode 100644 ansible/roles/grafana/templates/prometheus.yaml.j2 diff --git a/ansible/roles/grafana/handlers/main.yml b/ansible/roles/grafana/handlers/main.yml index 58918dd996..170e86b701 100644 --- a/ansible/roles/grafana/handlers/main.yml +++ b/ansible/roles/grafana/handlers/main.yml @@ -20,3 +20,4 @@ or grafana_conf.changed | bool or grafana_container.changed | bool or grafana_home_dashboard.changed | bool + or grafana_prometheus_data_source.changed | bool diff --git a/ansible/roles/grafana/tasks/config.yml b/ansible/roles/grafana/tasks/config.yml index c1ab6aacdd..1c69d94b7f 100644 --- a/ansible/roles/grafana/tasks/config.yml +++ b/ansible/roles/grafana/tasks/config.yml @@ -80,6 +80,26 @@ notify: - Restart grafana container +- name: Configuring Prometheus as data source for Grafana + vars: + service: "{{ grafana_services['grafana'] }}" + template: + src: "{{ item }}" + dest: "{{ node_config_directory }}/grafana/prometheus.yaml" + mode: "0660" + become: true + register: grafana_prometheus_data_source + when: + - inventory_hostname in groups[service.group] + - service.enabled | bool + - enable_prometheus | bool + with_first_found: + - "{{ node_custom_config }}/grafana/{{ inventory_hostname }}/prometheus.yaml" + - "{{ node_custom_config }}/grafana/prometheus.yaml" + - "prometheus.yaml.j2" + notify: + - Restart grafana container + - name: Check grafana containers kolla_docker: action: "compare_container" diff --git a/ansible/roles/grafana/templates/grafana.ini.j2 b/ansible/roles/grafana/templates/grafana.ini.j2 index 4052a2cb83..e9908ca8c0 100644 --- a/ansible/roles/grafana/templates/grafana.ini.j2 +++ b/ansible/roles/grafana/templates/grafana.ini.j2 @@ -2,6 +2,7 @@ data = /var/lib/grafana logs = /var/log/kolla/grafana plugins = /var/lib/grafana/plugins +provisioning = /etc/grafana/provisioning [server] protocol = http diff --git a/ansible/roles/grafana/templates/grafana.json.j2 b/ansible/roles/grafana/templates/grafana.json.j2 index 142e5815cf..940159b18f 100644 --- a/ansible/roles/grafana/templates/grafana.json.j2 +++ b/ansible/roles/grafana/templates/grafana.json.j2 @@ -21,6 +21,13 @@ "owner": "root", "perm": "0644", "optional": true + }, + { + "source": "{{ container_config_directory }}/prometheus.yaml", + "dest": "/etc/grafana/provisioning/datasources/prometheus.yaml", + "owner": "root", + "perm": "0644", + "optional": true } ], "permissions": [ diff --git a/ansible/roles/grafana/templates/prometheus.yaml.j2 b/ansible/roles/grafana/templates/prometheus.yaml.j2 new file mode 100644 index 0000000000..ce9b96748a --- /dev/null +++ b/ansible/roles/grafana/templates/prometheus.yaml.j2 @@ -0,0 +1,9 @@ +apiVersion: 1 + +datasources: +- name: Prometheus + type: prometheus + access: proxy + orgId: 1 + url: http://{{ kolla_internal_vip_address }}:{{ prometheus_port }} + version: 1