Skip to content

Commit 16b3a9b

Browse files
committed
add group label (login,control,compute) to prom targets
1 parent 4b437aa commit 16b3a9b

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

ansible/filter_plugins/utils.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,26 @@
1111
import os.path
1212
import re
1313

14-
def prometheus_node_exporter_targets(hosts, hostvars, env_key):
14+
def prometheus_node_exporter_targets(hosts, hostvars, env_key, group):
15+
""" Return a mapping in cloudalchemy.nodeexporter prometheus_targets
16+
format.
17+
18+
hosts: list of inventory_hostnames
19+
hostvars: Ansible hostvars variable
20+
env_key: key to lookup in each host's hostvars to add as label 'env' (default: 'ungrouped')
21+
group: string to add as label 'group'
22+
"""
1523
result = []
1624
per_env = defaultdict(list)
1725
for host in hosts:
1826
host_env = hostvars[host].get(env_key, 'ungrouped')
1927
per_env[host_env].append(host)
2028
for env, hosts in per_env.items():
2129
target = {
22-
"targets": ["{target}:9100".format(target=target) for target in hosts],
30+
"targets": [f"{target}:9100" for target in hosts],
2331
"labels": {
24-
"env": env
32+
'env': env,
33+
'group': group
2534
}
2635
}
2736
result.append(target)

environments/common/inventory/group_vars/all/prometheus.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ prometheus_alert_rules: []
2424

2525
# Can set a hostvar 'prometheus_env' to an arbitrary string to group prometheus targets, e.g. by rack.
2626
prometheus_targets:
27-
node: "{{ groups.get('node_exporter', []) | prometheus_node_exporter_targets(hostvars, 'prometheus_env') }}"
27+
control: "{{ groups.get('node_exporter', []) | intersect(groups['control']) | prometheus_node_exporter_targets(hostvars, 'prometheus_env', 'control') }}"
28+
login: "{{ groups.get('node_exporter', []) | intersect(groups['login']) | prometheus_node_exporter_targets(hostvars, 'prometheus_env', 'login') }}"
29+
compute: "{{ groups.get('node_exporter', []) | intersect(groups['compute']) | prometheus_node_exporter_targets(hostvars, 'prometheus_env', 'compute') }}"
2830

2931
prometheus_scrape_configs_default:
3032
- job_name: "prometheus"
@@ -39,7 +41,9 @@ prometheus_scrape_configs_default:
3941
- job_name: "node"
4042
file_sd_configs:
4143
- files:
42-
- /etc/prometheus/file_sd/node.yml
44+
- /etc/prometheus/file_sd/control.yml
45+
- /etc/prometheus/file_sd/login.yml
46+
- /etc/prometheus/file_sd/compute.yml
4347
relabel_configs:
4448
# strip off port
4549
- source_labels: ['__address__']

0 commit comments

Comments
 (0)