diff --git a/examples/updater/pushprox-updater.service b/examples/updater/pushprox-updater.service new file mode 100644 index 0000000..2febb8e --- /dev/null +++ b/examples/updater/pushprox-updater.service @@ -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 diff --git a/examples/updater/pushprox-updater.timer b/examples/updater/pushprox-updater.timer new file mode 100644 index 0000000..bfef313 --- /dev/null +++ b/examples/updater/pushprox-updater.timer @@ -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 diff --git a/examples/updater/update.sh b/examples/updater/update.sh new file mode 100755 index 0000000..bf672ac --- /dev/null +++ b/examples/updater/update.sh @@ -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"