From 8d478f3b23ee777299d10999f93138f8ad946b49 Mon Sep 17 00:00:00 2001 From: Linus Wallgren Date: Mon, 9 Dec 2019 12:47:13 +0100 Subject: [PATCH] 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 --- examples/updater/pushprox-updater.service | 13 +++++++++++++ examples/updater/pushprox-updater.timer | 9 +++++++++ examples/updater/update.sh | 21 +++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 examples/updater/pushprox-updater.service create mode 100644 examples/updater/pushprox-updater.timer create mode 100755 examples/updater/update.sh 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"