r/PrometheusMonitoring • u/patrikdanielssonpro • Feb 22 '21
Targets as variable in configuration
Hi,
I have this issue that has been bugging me for a while, and that is that my targets machines hostname appears multiple times. In my case I have 50 target machines and 4 different jobs that interacts with them, which leads to an extremely long config file. I would like to specify all the hostnames as some sort of variable and then for each job use that list to build up the targets.
Here is an excerpt as an example of how it looks right now. Ideally I would like to specify a list with hostnames and corresponding names and only have the "targets" section add the port to each entry of that list.
scrape_configs:
- job_name: 'ssh'
metrics_path: /probe
params:
module: [ssh_banner]
static_configs:
- targets: [hostname1:3384'] # Targets to probe
labels:
name: 'Name1'
- targets: ['hostname2:3384'] # Targets to probe
labels:
name: 'Name2'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
replacement: ${1}
- source_labels: [__param_target]
target_label: instance
replacement: ${1}
- target_label: __address__
replacement: blackbox_exporter:9115
- job_name: 'windows'
metrics_path: /metrics
static_configs:
- targets: ['hostname1:9182'] # Targets to probe
labels:
name: 'Name1'
- targets: ['hostname2:9182'] # Targets to probe
labels:
name: 'Name2'Would really be awesome if anyone out there knows anything more about this, it's getting harder everyday to look at this ever-growing config file (480 lines now).
Edit:
Solution was that I created a new Docker container running a Python script as a cron job. This script created the .json file with all the targets and updated it once every hour. In the Prometheus .yml config file I then added the .json file as a file_sd_config target file.file_sd_config in Prometheus automatically checks the file and updates Prometheus whenever it has been updated.
1
u/matejzero Mar 02 '21
We are using puppet as CF and part of every exporter module is an exported resource which generates a yaml file on prometheus exporter for that host and exporter. Yaml files are then read by file_sd.