diff options
author | Christian Pointner <equinox@spreadspace.org> | 2024-06-19 13:57:27 +0200 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2024-06-19 13:57:27 +0200 |
commit | ab97569f03c44e563237886fbcf2b81fb945f009 (patch) | |
tree | f71208d593d453924b9fee655448211ebe8dcb4b /roles/monitoring/prometheus | |
parent | bind/zones: add DMARC policies (diff) |
prometheus: revamp labels for metrcis from blackbox exporter
Diffstat (limited to 'roles/monitoring/prometheus')
3 files changed, 19 insertions, 7 deletions
diff --git a/roles/monitoring/prometheus/exporter/register/filter_plugins/prometheus.py b/roles/monitoring/prometheus/exporter/register/filter_plugins/prometheus.py index e8067a72..27ba29e6 100644 --- a/roles/monitoring/prometheus/exporter/register/filter_plugins/prometheus.py +++ b/roles/monitoring/prometheus/exporter/register/filter_plugins/prometheus.py @@ -6,6 +6,11 @@ from functools import partial from ansible import errors +_multitarget_id_map_ = { + "blackbox/probe": (lambda vars, config: (vars['inventory_hostname'], "%s-%s" % (config['svc_kind'], config['svc_name']))), +} + + def prometheus_job_targets(jobs, vars): try: result = [] @@ -14,15 +19,20 @@ def prometheus_job_targets(jobs, vars): if multitarget_config_varname in vars: for exporter_hostname, configs in vars[multitarget_config_varname].items(): for config in configs: - result.append({'job': job, 'instance': config['instance'], 'enabled': True, + if job in _multitarget_id_map_: + instance, id = _multitarget_id_map_[job](vars, config) + else: + id = config['instance'] + instance = config['instance'] + result.append({'_id': id, 'job': job, 'instance': instance, 'enabled': True, 'exporter_hostname': exporter_hostname, 'config': config}) else: enabled = job in vars['prometheus_exporters_default'] or job in vars['prometheus_exporters_extra'] - result.append({'job': job, 'instance': vars['inventory_hostname'], 'enabled': enabled}) + result.append({'_id': vars['inventory_hostname'], 'job': job, 'instance': vars['inventory_hostname'], 'enabled': enabled}) return result except Exception as e: - raise errors.AnsibleFilterError("prometheus_job_targets(): %s" % str(e)) + raise errors.AnsibleFilterError("prometheus_job_targets(): %s" % repr(e)) class FilterModule(object): diff --git a/roles/monitoring/prometheus/exporter/register/tasks/main.yml b/roles/monitoring/prometheus/exporter/register/tasks/main.yml index 519975d6..1d8585e9 100644 --- a/roles/monitoring/prometheus/exporter/register/tasks/main.yml +++ b/roles/monitoring/prometheus/exporter/register/tasks/main.yml @@ -8,20 +8,20 @@ loop: "{{ job_targets | selectattr('enabled') }}" loop_control: loop_var: target - label: "{{ target.job }} -> {{ target.instance }}" + label: "{{ target.job }} -> {{ target._id }}" delegate_to: "{{ prometheus_server }}" template: src: "{{ lookup('first_found', {'paths': ['templates'], 'files': [target.job + '.yml.j2', 'generic.yml.j2']}) }}" - dest: "/etc/prometheus/targets/{{ target.job }}/{{ target.instance }}.yml" + dest: "/etc/prometheus/targets/{{ target.job }}/{{ target._id }}.yml" notify: reload prometheus - name: remove files for disabled targets loop: "{{ job_targets | rejectattr('enabled') }}" loop_control: loop_var: target - label: "{{ target.job }} -> {{ target.instance }}" + label: "{{ target.job }} -> {{ target._id }}" delegate_to: "{{ prometheus_server }}" file: - path: "/etc/prometheus/targets/{{ target.job }}/{{ target.instance }}.yml" + path: "/etc/prometheus/targets/{{ target.job }}/{{ target._id }}.yml" state: absent notify: reload prometheus diff --git a/roles/monitoring/prometheus/exporter/register/templates/blackbox/probe.yml.j2 b/roles/monitoring/prometheus/exporter/register/templates/blackbox/probe.yml.j2 index 5f98e3a8..6319c1c5 100644 --- a/roles/monitoring/prometheus/exporter/register/templates/blackbox/probe.yml.j2 +++ b/roles/monitoring/prometheus/exporter/register/templates/blackbox/probe.yml.j2 @@ -1,6 +1,8 @@ - targets: [ '{{ (target.exporter_hostname == prometheus_server) | ternary('127.0.0.1:9999', hostvars[target.exporter_hostname].prometheus_scrape_endpoint) }}' ] labels: instance: '{{ target.instance }}' + svc_kind: '{{ target.config.svc_kind }}' + svc_name: '{{ target.config.svc_name }}' __param_target: '{{ target.config.target }}' __param_module: '{{ target.config.module }}' {% if 'hostname' in target.config %} |