summaryrefslogtreecommitdiff
path: root/roles/monitoring/prometheus/server/filter_plugins/prometheus.py
blob: d91ef619d95885c3faa2013c9c91ce6e35a99396 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from functools import partial

from ansible import errors


def prometheus_job_targets(hostvars, jobs, targets):
    try:
        result = []
        for job in jobs:
            for target in targets:
                multitarget_config_varname = 'prometheus_job_multitarget_' + job.replace('-', '_').replace('/', '__')
                if multitarget_config_varname in hostvars[target]:
                    for exporter_hostname, configs in hostvars[target][multitarget_config_varname].items():
                        for config in configs:
                            result.append({'job': job, 'instance': config['instance'], 'enabled': True,
                                           'exporter_hostname': exporter_hostname, 'config': config})

                else:
                    enabled = job in hostvars[target]['prometheus_exporters_default'] or job in hostvars[target]['prometheus_exporters_extra']
                    result.append({'job': job, 'instance': target, 'enabled': enabled})
        return result
    except Exception as e:
        raise errors.AnsibleFilterError("prometheus_job_targets(): %s" % str(e))


class FilterModule(object):

    ''' prometheus filters '''
    filter_map = {
        'prometheus_job_targets': prometheus_job_targets,
    }

    def filters(self):
        return self.filter_map