homek8s/infrastructure/networking/rosenpass/rosenpass-metrics.sh

100 lines
4.1 KiB
Shell
Raw Normal View History

#!/bin/sh
# rosenpass-metrics.sh — emit Rosenpass health metrics for node-exporter's
# textfile collector.
#
# Installed on rpi and siriussec ONLY (the two ends of Link A: rpi wg0
# 10.10.0.1 <-> siriussec wg0 10.10.0.2), run every 60 s by
# rosenpass-metrics.timer as root. See README.md in this directory.
#
# Why a host script: kube-prometheus-stack-prometheus-node-exporter is
# distroless and ships without --collector.textfile.directory (and without the
# dbus plumbing --collector.systemd needs), so nothing inside the cluster can
# observe a host systemd unit. This writes a textfile that the DaemonSet's
# textfile collector then serves on :9100/metrics.
#
# Signals:
# rosenpass_unit_active{node}
# 1 when `systemctl is-active rosenpass.service`
# says active, else 0.
# rosenpass_psk_rotation_age_seconds{node,interface}
# seconds since the wg0 preshared-key set last
# changed. Rotation runs about every two minutes.
# rosenpass_psk_rotation_last_change_timestamp_seconds{node,interface}
# unix timestamp of that last observed change.
#
# The preshared key is NEVER printed, logged or stored: the key listing is
# piped straight into sha256sum and only the hex digest is kept, in a
# root-only state file. Do not add an `echo` of the listing while debugging.
#
# Deliberately independent of rosenpass.service: this script must keep
# reporting (unit_active=0, age growing) while the thing it watches is dead.
set -eu
NODE=$(hostname -s)
IFACE=wg0
UNIT=rosenpass.service
STATE_DIR=/var/lib/rosenpass-exporter
STATE_FILE=$STATE_DIR/last-hash
OUT_DIR=/var/lib/node-exporter/textfile
OUT_FILE=$OUT_DIR/rosenpass.prom
# 0644 so the node-exporter container (nobody/65534) can read the file and the
# directory stays traversable.
umask 022
mkdir -p "$STATE_DIR" "$OUT_DIR"
now=$(date +%s)
# --- 1. unit state -----------------------------------------------------------
active=0
if [ "$(systemctl is-active "$UNIT" 2>/dev/null || true)" = "active" ]; then
active=1
fi
# --- 2. PSK rotation ---------------------------------------------------------
# `wg show ... preshared-keys` fails (and prints nothing) when wg0 is absent;
# command substitution would otherwise hash an empty string, which looks like a
# change on the first poll after the interface returns. Only hash real output.
psk_list=$(wg show "$IFACE" preshared-keys 2>/dev/null || true)
last_hash=""
last_change=""
if [ -f "$STATE_FILE" ]; then
last_hash=$(sed -n '1p' "$STATE_FILE")
last_change=$(sed -n '2p' "$STATE_FILE")
fi
if [ -n "$psk_list" ]; then
hash_now=$(printf '%s' "$psk_list" | sha256sum | awk '{print $1}')
if [ "$hash_now" != "$last_hash" ]; then
# First observation or an actual rotation: stamp it.
last_change=$now
printf '%s\n%s\n' "$hash_now" "$last_change" > "$STATE_FILE.tmp"
mv "$STATE_FILE.tmp" "$STATE_FILE"
fi
elif [ -z "$last_change" ]; then
# wg unreadable and no history: nothing to compare against yet.
last_change=""
fi
# --- 3. write the textfile atomically ---------------------------------------
tmp="$OUT_FILE.$$"
{
printf '# HELP rosenpass_unit_active rosenpass.service is active (1) or not (0).\n'
printf '# TYPE rosenpass_unit_active gauge\n'
printf 'rosenpass_unit_active{node="%s"} %s\n' "$NODE" "$active"
if [ -n "$last_change" ]; then
age=$((now - last_change))
[ "$age" -ge 0 ] || age=0
printf '# HELP rosenpass_psk_rotation_age_seconds Seconds since the wg0 preshared-key set last changed.\n'
printf '# TYPE rosenpass_psk_rotation_age_seconds gauge\n'
printf 'rosenpass_psk_rotation_age_seconds{node="%s",interface="%s"} %s\n' "$NODE" "$IFACE" "$age"
printf '# HELP rosenpass_psk_rotation_last_change_timestamp_seconds Unix timestamp of the last observed wg0 preshared-key change.\n'
printf '# TYPE rosenpass_psk_rotation_last_change_timestamp_seconds gauge\n'
printf 'rosenpass_psk_rotation_last_change_timestamp_seconds{node="%s",interface="%s"} %s\n' "$NODE" "$IFACE" "$last_change"
fi
} > "$tmp"
chmod 0644 "$tmp"
mv "$tmp" "$OUT_FILE"