Skip to content

Commit a1eb2df

Browse files
author
Linus Wallgren
committed
Add example on how to update prometheus targets
This will build a deb packge (does not follow the debian guidelines) that can be installed which 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 a1eb2df

File tree

10 files changed

+209
-0
lines changed

10 files changed

+209
-0
lines changed

examples/updater/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./build/

examples/updater/Makefile

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
.POSIX:
2+
.SUFFIXES:
3+
4+
NAME=pushprox-updater
5+
# the 0- is prepended until a version of this repo is tagged
6+
VERSION=0-$(shell git describe --always --match v[0-9]* HEAD | cut -c2-)
7+
OUT_DIR=build
8+
PACKAGE_DIR=$(OUT_DIR)/$(NAME)-$(VERSION)
9+
10+
$(PACKAGE_DIR)/: \
11+
$(PACKAGE_DIR)/DEBIAN/ \
12+
$(PACKAGE_DIR)/lib/systemd/system/$(NAME).service \
13+
$(PACKAGE_DIR)/lib/systemd/system/$(NAME).timer \
14+
$(PACKAGE_DIR)/usr/bin/pushprox-updater \
15+
16+
@touch "$@"
17+
18+
$(PACKAGE_DIR)/DEBIAN/: \
19+
$(PACKAGE_DIR)/DEBIAN/conffile \
20+
$(PACKAGE_DIR)/DEBIAN/control \
21+
$(PACKAGE_DIR)/DEBIAN/postinst \
22+
$(PACKAGE_DIR)/DEBIAN/postrm \
23+
$(PACKAGE_DIR)/DEBIAN/prerm \
24+
25+
@touch "$@"
26+
27+
$(PACKAGE_DIR)/DEBIAN/control: debian/control
28+
(cat debian/control && echo -n 'Version: ' && echo "${VERSION}") > "$@"
29+
30+
$(PACKAGE_DIR)/DEBIAN/%: debian/%
31+
@mkdir -p "$(dir $@)"
32+
cp -p "debian/$*" "$@"
33+
34+
$(PACKAGE_DIR)/lib/systemd/system/%: %
35+
@mkdir -p $(PACKAGE_DIR)/lib/systemd/system
36+
cp -r "$<" "$@"
37+
38+
$(PACKAGE_DIR)/usr/bin/pushprox-updater: update.sh
39+
@mkdir -p "$(dir $@)"
40+
cp -p "$<" "$@"
41+
42+
.PHONY: deb
43+
deb: $(PACKAGE_DIR).deb
44+
45+
$(PACKAGE_DIR).deb: $(PACKAGE_DIR)/
46+
chmod 755 $(PACKAGE_DIR)/DEBIAN/postinst
47+
chmod 755 $(PACKAGE_DIR)/DEBIAN/postrm
48+
chmod 755 $(PACKAGE_DIR)/DEBIAN/prerm
49+
fakeroot dpkg-deb --build "${PACKAGE_DIR}"
50+
51+
.PHONY: release
52+
release: clean $(PACKAGE_DIR).deb
53+
hub release create --attach="$(PACKAGE_DIR).deb" "$(VERSION)"
54+
55+
.PHONY: clean
56+
clean:
57+
rm -rf "$(OUT_DIR)"

examples/updater/debian/conffile

Whitespace-only changes.

examples/updater/debian/control

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Package: pushprox-updater
2+
Architecture: all
3+
Maintainer: linus <[email protected]>
4+
Depends: bash, jq, curl, coreutils
5+
Description: Update a prometheus static scrape config based on what is found by
6+
PushProx

examples/updater/debian/postinst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
# summary of how this script can be called:
6+
# * <postinst> `configure' <most-recently-configured-version>
7+
# * <old-postinst> `abort-upgrade' <new version>
8+
# * <conflictor's-postinst> `abort-remove' `in-favour' <package>
9+
# <new-version>
10+
# * <postinst> `abort-remove'
11+
# * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
12+
# <failed-install-package> <version> `removing'
13+
# <conflicting-package> <version>
14+
# for details, see https://www.debian.org/doc/debian-policy/ or
15+
# the debian-policy package
16+
17+
case "$1" in
18+
configure)
19+
if ! getent group pushprox-updater >/dev/null; then
20+
addgroup --quiet --system pushprox-updater
21+
fi
22+
23+
if ! getent passwd pushprox-updater >/dev/null; then
24+
adduser --quiet --system --ingroup pushprox-updater --shell /usr/sbin/nologin pushprox-updater
25+
fi
26+
27+
systemctl daemon-reload
28+
systemctl enable --now pushprox-updater.timer
29+
;;
30+
31+
abort-upgrade|abort-remove|abort-deconfigure)
32+
;;
33+
34+
*)
35+
echo "postinst called with unknown argument \`$1'" >&2
36+
exit 1
37+
;;
38+
esac
39+
40+
exit 0

examples/updater/debian/postrm

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
# summary of how this script can be called:
6+
# * <postrm> `remove'
7+
# * <postrm> `purge'
8+
# * <old-postrm> `upgrade' <new-version>
9+
# * <new-postrm> `failed-upgrade' <old-version>
10+
# * <new-postrm> `abort-install'
11+
# * <new-postrm> `abort-install' <old-version>
12+
# * <new-postrm> `abort-upgrade' <old-version>
13+
# * <disappearer's-postrm> `disappear' <overwriter>
14+
# <overwriter-version>
15+
# for details, see https://www.debian.org/doc/debian-policy/ or
16+
# the debian-policy package
17+
18+
19+
case "$1" in
20+
purge|remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
21+
systemctl daemon-reload
22+
;;
23+
*)
24+
echo "postrm called with unknown argument \`$1'" >&2
25+
exit 1
26+
;;
27+
esac
28+
29+
exit 0

examples/updater/debian/prerm

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
# summary of how this script can be called:
6+
# * <prerm> `remove'
7+
# * <old-prerm> `upgrade' <new-version>
8+
# * <new-prerm> `failed-upgrade' <old-version>
9+
# * <conflictor's-prerm> `remove' `in-favour' <package> <new-version>
10+
# * <deconfigured's-prerm> `deconfigure' `in-favour'
11+
# <package-being-installed> <version> `removing'
12+
# <conflicting-package> <version>
13+
# for details, see https://www.debian.org/doc/debian-policy/ or
14+
# the debian-policy package
15+
16+
17+
case "$1" in
18+
remove)
19+
systemctl disable --now pushprox-updater.timer
20+
;;
21+
upgrade|deconfigure)
22+
;;
23+
24+
failed-upgrade)
25+
;;
26+
27+
*)
28+
echo "prerm called with unknown argument \`$1'" >&2
29+
exit 1
30+
;;
31+
esac
32+
33+
exit 0
Lines changed: 13 additions & 0 deletions
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
Lines changed: 9 additions & 0 deletions
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

examples/updater/update.sh

Lines changed: 21 additions & 0 deletions
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)