From 9a986bebf70726dc586b5252d4d3501bc9630ca0 Mon Sep 17 00:00:00 2001 From: Bartosz Kupidura Date: Mon, 26 Jun 2017 15:34:08 +0200 Subject: [PATCH] Merge grains with pillars for static targets Change-Id: I2f85fe2f9cc67d2d03d5e8dad5ac0b74e82b0fcc --- prometheus/files/prometheus.yml | 16 +++++++++------- prometheus/map.jinja | 4 ++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/prometheus/files/prometheus.yml b/prometheus/files/prometheus.yml index 69bc0f6..c8dd17b 100644 --- a/prometheus/files/prometheus.yml +++ b/prometheus/files/prometheus.yml @@ -37,24 +37,26 @@ remote_write: rule_files: - alerts.yml -{%- set static_target = {} %} +{%- set static_target = server.target.static %} {%- for node_name, node_grains in salt['mine.get']('*', 'grains.items').iteritems() %} {%- set static_grain = node_grains.get('prometheus', {}).get('server', {}).get('target', {}).get('static', {}) %} {%- for job_name, job in static_grain.iteritems() %} {%- if static_target[job_name] is not defined %} - {%- do static_target.update({job_name: []}) %} + {%- do static_target.update({job_name: {}}) %} {%- endif %} - {%- for target in job.get('endpoint') %} - {%- do static_target[job_name].append(target) %} + {%- if static_target[job_name]['endpoint'] is not defined %} + {%- do static_target[job_name].update({'endpoint': []}) %} + {%- endif %} + {%- for target in job.get('endpoint', []) %} + {%- do static_target[job_name]['endpoint'].append(target) %} {%- endfor %} {%- endfor %} {%- endfor %} scrape_configs: -{%- for job_name, targets in static_target.iteritems() %} - {%- set job = server.get('target', {}).get('static', {}).get(job_name, {}) %} +{%- for job_name, job in static_target.iteritems() %} {%- set nodes = [] %} - {%- for target in targets + job.get('endpoint', []) %} + {%- for target in job.get('endpoint', []) %} {%- set address = "'%s:%d'" | format(target.address, target.port) %} {%- do nodes.append(address) %} {%- endfor %} diff --git a/prometheus/map.jinja b/prometheus/map.jinja index 6d62276..7b6887a 100644 --- a/prometheus/map.jinja +++ b/prometheus/map.jinja @@ -1,5 +1,9 @@ {% set server = salt['grains.filter_by']({ 'default': { + 'target': { + 'static': { + }, + }, }, }, merge=salt['pillar.get']('prometheus:server')) %}