diff options
author | Christian Pointner <equinox@spreadspace.org> | 2024-06-30 00:32:34 +0200 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2024-06-30 00:32:34 +0200 |
commit | 8ef71cb6c33a134c1565a034a39a85b0a0a6324f (patch) | |
tree | 11f660aa745ebed88e2deaa7eeb9ea6f12885da0 /roles/monitoring/prometheus/exporter/register | |
parent | bind/zones: add DMARC policies (diff) | |
parent | prometheus: incooporate multitarget labelding to elevate hosts as well (diff) |
Merge branch 'topic/promethues-multitarget-exporter-labels'
Diffstat (limited to 'roles/monitoring/prometheus/exporter/register')
6 files changed, 29 insertions, 8 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..50e34865 100644 --- a/roles/monitoring/prometheus/exporter/register/filter_plugins/prometheus.py +++ b/roles/monitoring/prometheus/exporter/register/filter_plugins/prometheus.py @@ -6,6 +6,15 @@ 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_instance']))), + "ssl/probe": (lambda vars, config: (vars['inventory_hostname'], "%s-%s-%s" % (config['module'], config['sslcert_instance'], vars['inventory_hostname']))), + "ipmi/remote": (lambda vars, config: (vars['inventory_hostname'], vars['inventory_hostname'])), + "nut/ups": (lambda vars, config: (vars['inventory_hostname'], config['ups'])), + "whawty-nginx-sso": (lambda vars, config: (vars['inventory_hostname'], "%s-%s" % (config['app_instance'], vars['inventory_hostname']))), +} + + def prometheus_job_targets(jobs, vars): try: result = [] @@ -14,15 +23,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..fbbce7df 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_instance: '{{ target.config.svc_instance }}' __param_target: '{{ target.config.target }}' __param_module: '{{ target.config.module }}' {% if 'hostname' in target.config %} diff --git a/roles/monitoring/prometheus/exporter/register/templates/nut/ups.yml.j2 b/roles/monitoring/prometheus/exporter/register/templates/nut/ups.yml.j2 index 5181f4e4..6c549e1e 100644 --- a/roles/monitoring/prometheus/exporter/register/templates/nut/ups.yml.j2 +++ b/roles/monitoring/prometheus/exporter/register/templates/nut/ups.yml.j2 @@ -1,6 +1,7 @@ - targets: [ '{{ (target.exporter_hostname == prometheus_server) | ternary('127.0.0.1:9999', hostvars[target.exporter_hostname].prometheus_scrape_endpoint) }}' ] labels: instance: '{{ target.instance }}' + ups_instance: '{{ target.config.ups }}' __param_ups: '{{ target.config.ups }}' __param_server: '{{ target.config.server | default('127.0.0.1') }}' {% if 'username' in target.config %} diff --git a/roles/monitoring/prometheus/exporter/register/templates/ssl/probe.yml.j2 b/roles/monitoring/prometheus/exporter/register/templates/ssl/probe.yml.j2 index 3ecf129a..6d26e779 100644 --- a/roles/monitoring/prometheus/exporter/register/templates/ssl/probe.yml.j2 +++ b/roles/monitoring/prometheus/exporter/register/templates/ssl/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 }}' + sslcert_kind: '{{ target.config.module }}' + sslcert_instance: '{{ target.config.sslcert_instance }}' __param_target: '{{ target.config.target }}' __param_module: '{{ target.config.module }}' {% for name, value in prometheus_target_labels.items() %} diff --git a/roles/monitoring/prometheus/exporter/register/templates/whawty-nginx-sso.yml.j2 b/roles/monitoring/prometheus/exporter/register/templates/whawty-nginx-sso.yml.j2 index 74ada64e..8675a143 100644 --- a/roles/monitoring/prometheus/exporter/register/templates/whawty-nginx-sso.yml.j2 +++ b/roles/monitoring/prometheus/exporter/register/templates/whawty-nginx-sso.yml.j2 @@ -1,7 +1,9 @@ - targets: [ '{{ (target.exporter_hostname == prometheus_server) | ternary('127.0.0.1:9999', hostvars[target.exporter_hostname].prometheus_scrape_endpoint) }}' ] labels: instance: '{{ target.instance }}' - __metrics_path__: '/whawty-nginx-sso/{{ target.config.instance_name }}' + app_kind: 'whawty-nginx-sso' + app_instance: '{{ target.config.app_instance }}' + __metrics_path__: '/whawty-nginx-sso/{{ target.config.app_instance }}' {% for name, value in prometheus_target_labels.items() %} {{ name }}: '{{ value }}' {% endfor %} |