Skip to content

Commit afb6e3f

Browse files
authored
Add Prometheus datasource provisioning for Grafana (#432)
Query all Prometheus servers across domains and provision them as named datasources (prometheus_<domain>) via Grafana's file-based provisioning.
1 parent 7a8634e commit afb6e3f

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

modules/grafana.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@
66

77

88
def generate(host, *args):
9+
prometheus_servers = {}
10+
for server in sorted(lib.get_nodes_with_package('prometheus').keys()):
11+
try:
12+
domain = lib.get_domain(server).lower()
13+
except lib.NoDomainError:
14+
domain = 'unknown'
15+
prometheus_servers[server] = domain
16+
917
info = {
1018
'grafana': {
11-
'current_event': lib.get_current_event()
19+
'current_event': lib.get_current_event(),
20+
'prometheus_servers': prometheus_servers,
1221
}
1322
}
1423

modules/grafana/manifests/init.pp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# for package details such as default paths etc.
1212
#
1313

14-
class grafana($current_event) {
14+
class grafana($current_event, $prometheus_servers = []) {
1515

1616
# Adding the apt repository
1717
package { 'apt-transport-https':
@@ -110,6 +110,15 @@
110110
}
111111
}
112112

113+
# Prometheus datasource provisioning
114+
file { 'grafana-prometheus-datasources':
115+
path => '/etc/grafana/provisioning/datasources/prometheus.yaml',
116+
content => template('grafana/prometheus-datasource.yaml.erb'),
117+
mode => '0644',
118+
require => Package['grafana'],
119+
notify => Service['grafana-server'],
120+
}
121+
113122
# Setting up the Apache proxy
114123
apache::proxy { 'grafana-backend':
115124
url => '/',
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Managed by Puppet - do not edit manually
2+
apiVersion: 1
3+
4+
datasources:
5+
<% @prometheus_servers.each do |server, domain| -%>
6+
- name: prometheus_<%= domain %>
7+
type: prometheus
8+
url: http://<%= server %>:9090
9+
access: proxy
10+
isDefault: false
11+
editable: false
12+
<% end -%>

0 commit comments

Comments
 (0)