Skip to content

Commit

Permalink
Add example on how to update prometheus targets
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Linus Wallgren committed Dec 9, 2019
1 parent 8f4561f commit 8d478f3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/updater/pushprox-updater.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=Update the prometheus static file config based off of PushProx

[Service]
User=pushprox-updater
Group=pushprox-updater

EnvironmentFile=-/etc/default/pushprox-updater
ExecStart=/usr/bin/pushprox-updater

NoNewPrivileges=true
PrivateTmp=yes
PrivateDevices=true
9 changes: 9 additions & 0 deletions examples/updater/pushprox-updater.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Update the prometheus static file config based off of PushProx

[Timer]
OnStartupSec=20s
OnUnitInactiveSec=20s

[Install]
WantedBy=timers.target
21 changes: 21 additions & 0 deletions examples/updater/update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

set -euo pipefail

host=${PUSHPROX_HOST:-localhost:8080}
ports=${PUSHPROXY_PORTS:-9100}
sd_file=${PUSHPROXY_FILE:-/etc/prometheus/file_sd/clients.json}

tmpfile=$(mktemp --suffix "pushprox-target-updater")
trap 'rm -f "$tmpfile"' EXIT

if ! curl -s "$host/clients" | jq --argjson ports "[$ports]" '
. | map(.targets[0] as $target | {
targets: $ports | map(tostring) | map($target + ":" + .)
})' > "$tmpfile"; then
echo >&2 "Failed to update inventory"
exit 2
fi

chmod a+r "$tmpfile"
mv "$tmpfile" "$sd_file"

0 comments on commit 8d478f3

Please sign in to comment.