Skip to content

Commit 8d478f3

Browse files
author
Linus Wallgren
committed
Add example on how to update prometheus targets
Updates a target list in the format expected by file_sd_config[0] every 20 seconds. [0]: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#file_sd_config
1 parent 8f4561f commit 8d478f3

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

Diff for: examples/updater/pushprox-updater.service

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[Unit]
2+
Description=Update the prometheus static file config based off of PushProx
3+
4+
[Service]
5+
User=pushprox-updater
6+
Group=pushprox-updater
7+
8+
EnvironmentFile=-/etc/default/pushprox-updater
9+
ExecStart=/usr/bin/pushprox-updater
10+
11+
NoNewPrivileges=true
12+
PrivateTmp=yes
13+
PrivateDevices=true

Diff for: examples/updater/pushprox-updater.timer

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Unit]
2+
Description=Update the prometheus static file config based off of PushProx
3+
4+
[Timer]
5+
OnStartupSec=20s
6+
OnUnitInactiveSec=20s
7+
8+
[Install]
9+
WantedBy=timers.target

Diff for: examples/updater/update.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
host=${PUSHPROX_HOST:-localhost:8080}
6+
ports=${PUSHPROXY_PORTS:-9100}
7+
sd_file=${PUSHPROXY_FILE:-/etc/prometheus/file_sd/clients.json}
8+
9+
tmpfile=$(mktemp --suffix "pushprox-target-updater")
10+
trap 'rm -f "$tmpfile"' EXIT
11+
12+
if ! curl -s "$host/clients" | jq --argjson ports "[$ports]" '
13+
. | map(.targets[0] as $target | {
14+
targets: $ports | map(tostring) | map($target + ":" + .)
15+
})' > "$tmpfile"; then
16+
echo >&2 "Failed to update inventory"
17+
exit 2
18+
fi
19+
20+
chmod a+r "$tmpfile"
21+
mv "$tmpfile" "$sd_file"

0 commit comments

Comments
 (0)