diff --git a/docs/MONITORING.md b/docs/MONITORING.md
index 0f240bb..dbed15f 100644
--- a/docs/MONITORING.md
+++ b/docs/MONITORING.md
@@ -107,6 +107,50 @@ read -rsp 'Grafana admin password: ' GF_PASS && export GF_PASS
sudo --preserve-env=GF_ADMIN,GF_PASS ./ops/prometheus/install-grafana.sh
```
+## Hub probe
+
+Prometheus asks the Hub's `/healthz` through a blackbox exporter on
+`127.0.0.1:9115`. `/healthz` answers HTTP 200 with `alive` while the Hub process
+serves requests.
+
+```bash
+CLAWCTL_HUB_URL=https://hub.example.com ./ops/prometheus/install-hub-probe.sh --check
+sudo CLAWCTL_HUB_URL=https://hub.example.com ./ops/prometheus/install-hub-probe.sh
+sudo ./ops/prometheus/install-hub-probe.sh --remove
+```
+
+Set `CLAWCTL_HUB_URL` to an `http://` or `https://` address with a host and
+optional port, without a path, credentials, query or fragment. One trailing
+slash is accepted. `--check` prints the plan without changing configuration.
+Install requires root and a Hub that answers HTTP 200 with exactly `alive`
+or `alive` followed by one newline.
+
+| Alert | Fires when | Severity |
+|---|---|---|
+| `HubNotAnswering` | The Hub probe gets no answer for 5 minutes | critical |
+| `HubProbeNotRunning` | Prometheus gets no probe result for 10 minutes | warning |
+
+For a first installation, the exporter stays masked until its loopback
+listener and module file are configured. A failed installation can be retried
+or cleaned up with `--remove`, including failures before the scrape job exists.
+An exporter masked by the operator remains masked.
+
+The installer installs `prometheus-blackbox-exporter` and these files:
+
+| File | Effect |
+|---|---|
+| `/etc/prometheus/clawctl-blackbox.yml` | Defines the `/healthz` probe |
+| `/etc/default/prometheus-blackbox-exporter` | Sets the module file and listener |
+| `/etc/prometheus/scrape.d/clawctl-hub-healthz.yml` | Adds the Hub scrape job |
+| `/etc/prometheus/rules/clawctl-hub-healthz.yml` | Adds both alerts |
+| `/etc/prometheus/prometheus.yml` | Adds the scrape directory when absent |
+
+Telegram delivery uses the Alertmanager of step 2 and its 5 minute grouping
+delay. `--remove` deletes the scrape job and alert rules, reloads Prometheus,
+and stops and disables the exporter. The package, module file, exporter
+settings and `prometheus.yml` stay installed. If removal fails, repeat the
+`--remove` command to complete the reload and exporter shutdown.
+
## Grafana without root
`ops/prometheus/install-grafana-docker.sh` runs Grafana in Docker
@@ -129,6 +173,7 @@ Grafana data, including the admin password, stays in the Docker volume
|---|---|
| Prometheus | `127.0.0.1:9090` |
| Alertmanager | `127.0.0.1:9093`, cluster listener off |
+| Hub blackbox exporter | `127.0.0.1:9115` |
| node_exporter | `
:9100` |
| Grafana (APT, `install-grafana.sh`) | `:3000` |
| Grafana (Docker) | `$CLAWCTL_GRAFANA_ADDR:$CLAWCTL_GRAFANA_PORT` |
@@ -160,3 +205,8 @@ if the whole metric is empty, the query or the scrape is wrong.
`CLAWCTL_REQUIRE_PROMTOOL=1` to fail when `promtool` is missing), that
`install.sh` calls existing stage scripts, the installers' refusals and
Grafana login checks, and the functions of `ops/prometheus/listen.sh`.
+
+```bash
+bash ops/prometheus/test-install-hub-probe.sh
+promtool test rules ops/prometheus/hub-probe/rules_test.yml
+```
diff --git a/ops/prometheus/hub-probe/blackbox.yml b/ops/prometheus/hub-probe/blackbox.yml
new file mode 100644
index 0000000..1821ffd
--- /dev/null
+++ b/ops/prometheus/hub-probe/blackbox.yml
@@ -0,0 +1,13 @@
+# Blackbox exporter modules of ops/prometheus/install-hub-probe.sh.
+modules:
+ # The Hub's GET /healthz answers 200 with the body "alive".
+ clawctl_hub_healthz:
+ prober: http
+ timeout: 10s
+ http:
+ method: GET
+ valid_status_codes: [200]
+ follow_redirects: false
+ preferred_ip_protocol: ip4
+ fail_if_body_not_matches_regexp:
+ - '^alive\n?$'
diff --git a/ops/prometheus/hub-probe/rules.yml b/ops/prometheus/hub-probe/rules.yml
new file mode 100644
index 0000000..4807bdc
--- /dev/null
+++ b/ops/prometheus/hub-probe/rules.yml
@@ -0,0 +1,35 @@
+# Alerts of the Hub probe. ops/prometheus/install-hub-probe.sh installs this
+# file as /etc/prometheus/rules/clawctl-hub-healthz.yml.
+#
+# Both alerts carry machine="hub": Alertmanager groups by machine and the
+# message title names it.
+
+groups:
+ - name: clawctl-hub-healthz
+ interval: 60s
+ rules:
+ - alert: HubNotAnswering
+ expr: probe_success{job="clawctl-hub-healthz"} == 0
+ for: 5m
+ labels:
+ severity: critical
+ source: prometheus
+ machine: hub
+ annotations:
+ summary: 'Hub 沒有回應'
+ description: >-
+ {{ $labels.instance }} 連續 5 分鐘沒有回 200。主控台打不開,
+ 經這個網址報到的機器也報不到。下一步:到 Hub 主機查 Hub 服務的狀態。
+
+ - alert: HubProbeNotRunning
+ expr: up{job="clawctl-hub-healthz"} == 0 or absent(up{job="clawctl-hub-healthz"})
+ for: 10m
+ labels:
+ severity: warning
+ source: prometheus
+ machine: hub
+ annotations:
+ summary: 'Hub 探測沒有在跑'
+ description: >-
+ Prometheus 連續 10 分鐘拿不到 Hub 探測的結果,這段時間沒有人在看 Hub 有沒有回應。
+ 下一步:在觀測主機上跑 systemctl status prometheus-blackbox-exporter。
diff --git a/ops/prometheus/hub-probe/rules_test.yml b/ops/prometheus/hub-probe/rules_test.yml
new file mode 100644
index 0000000..7f433d5
--- /dev/null
+++ b/ops/prometheus/hub-probe/rules_test.yml
@@ -0,0 +1,127 @@
+# promtool test rules ops/prometheus/hub-probe/rules_test.yml
+rule_files:
+ - rules.yml
+evaluation_interval: 1m
+
+tests:
+ # The Hub answers: neither alert fires.
+ - interval: 1m
+ input_series:
+ - series: 'probe_success{job="clawctl-hub-healthz", instance="https://hub.example.com/healthz"}'
+ values: '1x20'
+ - series: 'up{job="clawctl-hub-healthz", instance="https://hub.example.com/healthz"}'
+ values: '1x20'
+ alert_rule_test:
+ - eval_time: 20m
+ alertname: HubNotAnswering
+ exp_alerts: []
+ - eval_time: 20m
+ alertname: HubProbeNotRunning
+ exp_alerts: []
+
+ # Four failed probes, then an answer: HubNotAnswering does not fire.
+ - interval: 1m
+ input_series:
+ - series: 'probe_success{job="clawctl-hub-healthz", instance="https://hub.example.com/healthz"}'
+ values: '1 0 0 0 0 1 1 1 1 1 1 1'
+ - series: 'up{job="clawctl-hub-healthz", instance="https://hub.example.com/healthz"}'
+ values: '1x11'
+ alert_rule_test:
+ - eval_time: 5m
+ alertname: HubNotAnswering
+ exp_alerts: []
+ - eval_time: 11m
+ alertname: HubNotAnswering
+ exp_alerts: []
+
+ # No answer from minute 1: HubNotAnswering fires at minute 6, stays, and
+ # ends with the first answer.
+ - interval: 1m
+ input_series:
+ - series: 'probe_success{job="clawctl-hub-healthz", instance="https://hub.example.com/healthz"}'
+ values: '1 0x14 1 1'
+ - series: 'up{job="clawctl-hub-healthz", instance="https://hub.example.com/healthz"}'
+ values: '1x17'
+ alert_rule_test:
+ - eval_time: 5m
+ alertname: HubNotAnswering
+ exp_alerts: []
+ - eval_time: 6m
+ alertname: HubNotAnswering
+ exp_alerts:
+ - exp_labels:
+ severity: critical
+ source: prometheus
+ machine: hub
+ job: clawctl-hub-healthz
+ instance: https://hub.example.com/healthz
+ exp_annotations:
+ summary: 'Hub 沒有回應'
+ description: 'https://hub.example.com/healthz 連續 5 分鐘沒有回 200。主控台打不開, 經這個網址報到的機器也報不到。下一步:到 Hub 主機查 Hub 服務的狀態。'
+ - eval_time: 15m
+ alertname: HubNotAnswering
+ exp_alerts:
+ - exp_labels:
+ severity: critical
+ source: prometheus
+ machine: hub
+ job: clawctl-hub-healthz
+ instance: https://hub.example.com/healthz
+ exp_annotations:
+ summary: 'Hub 沒有回應'
+ description: 'https://hub.example.com/healthz 連續 5 分鐘沒有回 200。主控台打不開, 經這個網址報到的機器也報不到。下一步:到 Hub 主機查 Hub 服務的狀態。'
+ - eval_time: 16m
+ alertname: HubNotAnswering
+ exp_alerts: []
+ - eval_time: 16m
+ alertname: HubProbeNotRunning
+ exp_alerts: []
+
+ # The exporter stops answering from minute 1: probe_success has no samples,
+ # so HubNotAnswering stays quiet and HubProbeNotRunning fires at minute 11.
+ - interval: 1m
+ input_series:
+ - series: 'probe_success{job="clawctl-hub-healthz", instance="https://hub.example.com/healthz"}'
+ values: '1 _x20'
+ - series: 'up{job="clawctl-hub-healthz", instance="https://hub.example.com/healthz"}'
+ values: '1 0x20'
+ alert_rule_test:
+ - eval_time: 10m
+ alertname: HubProbeNotRunning
+ exp_alerts: []
+ - eval_time: 11m
+ alertname: HubProbeNotRunning
+ exp_alerts:
+ - exp_labels:
+ severity: warning
+ source: prometheus
+ machine: hub
+ job: clawctl-hub-healthz
+ instance: https://hub.example.com/healthz
+ exp_annotations:
+ summary: 'Hub 探測沒有在跑'
+ description: 'Prometheus 連續 10 分鐘拿不到 Hub 探測的結果,這段時間沒有人在看 Hub 有沒有回應。 下一步:在觀測主機上跑 systemctl status prometheus-blackbox-exporter。'
+ - eval_time: 20m
+ alertname: HubNotAnswering
+ exp_alerts: []
+
+ # No scrape job at all: HubProbeNotRunning fires at minute 10.
+ - interval: 1m
+ input_series:
+ - series: 'up{job="node", instance="192.0.2.1:9100"}'
+ values: '1x20'
+ alert_rule_test:
+ - eval_time: 9m
+ alertname: HubProbeNotRunning
+ exp_alerts: []
+ - eval_time: 10m
+ alertname: HubProbeNotRunning
+ exp_alerts:
+ - exp_labels:
+ severity: warning
+ source: prometheus
+ machine: hub
+ job: clawctl-hub-healthz
+ exp_annotations:
+ summary: 'Hub 探測沒有在跑'
+ description: 'Prometheus 連續 10 分鐘拿不到 Hub 探測的結果,這段時間沒有人在看 Hub 有沒有回應。 下一步:在觀測主機上跑 systemctl status prometheus-blackbox-exporter。'
diff --git a/ops/prometheus/install-hub-probe.sh b/ops/prometheus/install-hub-probe.sh
new file mode 100755
index 0000000..3deae2b
--- /dev/null
+++ b/ops/prometheus/install-hub-probe.sh
@@ -0,0 +1,326 @@
+#!/usr/bin/env bash
+# Tests set:
+# CLAWCTL_HUB_PROBE_ETC: configuration root; CLAWCTL_HUB_PROBE_SKIP_ROOT=1: skip root.
+# CLAWCTL_HUB_PROBE_PROMETHEUS: API URL; CLAWCTL_HUB_PROBE_EXPORTER: listener.
+# CLAWCTL_HUB_PROBE_TRIES: API polls; CLAWCTL_HUB_PROBE_WAIT: poll delay in seconds.
+set -euo pipefail
+
+HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
+SCRIPT_PATH=$HERE/$(basename "${BASH_SOURCE[0]}")
+# shellcheck source=listen.sh
+. "$HERE/listen.sh"
+ETC=${CLAWCTL_HUB_PROBE_ETC:-/etc}
+DEFAULTS=$ETC/default/prometheus-blackbox-exporter
+PROM_YML=$ETC/prometheus/prometheus.yml
+MODULES=$ETC/prometheus/clawctl-blackbox.yml
+SCRAPE_DIR=$ETC/prometheus/scrape.d
+SCRAPE=$SCRAPE_DIR/clawctl-hub-healthz.yml
+RULE=$ETC/prometheus/rules/clawctl-hub-healthz.yml
+REMOVE_PENDING=$ETC/prometheus/.clawctl-hub-probe-remove-pending
+MANAGED=$ETC/prometheus/.clawctl-hub-probe-managed
+MASK_PENDING=$ETC/prometheus/.clawctl-hub-probe-mask-pending
+PROM_API=${CLAWCTL_HUB_PROBE_PROMETHEUS:-http://127.0.0.1:9090}
+EXPORTER=${CLAWCTL_HUB_PROBE_EXPORTER:-127.0.0.1:9115}
+TRIES=${CLAWCTL_HUB_PROBE_TRIES:-36}
+WAIT=${CLAWCTL_HUB_PROBE_WAIT:-5}
+STAMP=$(date +%Y%m%d-%H%M%S)
+
+usage() {
+ cat >&2 <<'USAGE'
+Usage:
+CLAWCTL_HUB_URL=https://hub.example.com ./ops/prometheus/install-hub-probe.sh --check
+sudo CLAWCTL_HUB_URL=https://hub.example.com ./ops/prometheus/install-hub-probe.sh
+sudo ./ops/prometheus/install-hub-probe.sh --remove
+USAGE
+ exit 2
+}
+if [ "$#" -gt 1 ]; then usage; fi
+MODE=${1:-install}
+case $MODE in
+--check|--remove) ;;
+install) if [ "$#" -ne 0 ]; then usage; fi ;;
+*) usage ;;
+esac
+
+root_required() {
+ if [ "${CLAWCTL_HUB_PROBE_SKIP_ROOT:-}" != 1 ] && [ "$(id -u)" -ne 0 ]; then
+ if [ "$MODE" = --remove ]; then
+ printf 'Run as root: sudo %s --remove\n' "$SCRIPT_PATH" >&2
+ else
+ printf 'Run as root: sudo CLAWCTL_HUB_URL=%s %s\n' "$URL" "$SCRIPT_PATH" >&2
+ fi
+ exit 1
+ fi
+}
+
+unmask_owned() {
+ if [ -e "$MASK_PENDING" ]; then
+ systemctl unmask prometheus-blackbox-exporter
+ rm -f -- "$MASK_PENDING"
+ fi
+}
+
+if [ "$MODE" = --remove ]; then
+ root_required
+ if [ ! -e "$SCRAPE" ] && [ ! -e "$RULE" ] && [ ! -e "$REMOVE_PENDING" ] && [ ! -e "$MANAGED" ] && [ ! -e "$MASK_PENDING" ]; then
+ echo 'The Hub probe is not installed.'
+ exit 0
+ fi
+ : >"$REMOVE_PENDING"
+ rm -f -- "$SCRAPE" "$RULE"
+ if ! systemctl reload prometheus; then
+ printf 'Prometheus reload failed. Retry removal: sudo %s --remove\n' "$SCRIPT_PATH" >&2
+ exit 1
+ fi
+ unmask_owned
+ LOAD_STATE=$(systemctl show prometheus-blackbox-exporter -p LoadState --value)
+ if [ "$LOAD_STATE" != not-found ] && ! systemctl disable --now prometheus-blackbox-exporter; then
+ echo 'prometheus-blackbox-exporter was not stopped. Run: systemctl disable --now prometheus-blackbox-exporter' >&2
+ exit 1
+ fi
+ rm -f -- "$REMOVE_PENDING" "$MANAGED"
+ echo 'Removed the Hub probe. Prometheus is reloaded; prometheus-blackbox-exporter is stopped and disabled.'
+ exit 0
+fi
+
+URL=${CLAWCTL_HUB_URL:-}
+URL=${URL%/}
+url_re='^https?://[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?(:[0-9]{1,5})?$'
+if [[ ! $URL =~ $url_re ]]; then
+ echo "CLAWCTL_HUB_URL must be the Hub's address: scheme, host and optional port, without a path. Example: https://hub.example.com" >&2
+ exit 2
+fi
+TARGET=$URL/healthz
+if [ ! -f "$PROM_YML" ]; then
+ printf '%s not found. Install Prometheus first: sudo CLAWCTL_FLEET_JSON=/path/to/fleet.json %s/install-prometheus.sh\n' "$PROM_YML" "$HERE" >&2
+ exit 1
+fi
+if ! command -v promtool >/dev/null 2>&1; then
+ echo 'promtool is not on PATH. The prometheus package provides it.' >&2
+ exit 1
+fi
+ADD_SCRAPE=1
+if grep -q '^scrape_config_files:' "$PROM_YML"; then
+ ADD_SCRAPE=0
+ if ! grep -Fq -- "$SCRAPE_DIR/*.yml" "$PROM_YML"; then
+ printf '%s sets scrape_config_files without %s/*.yml. Add that path to the list, then run this again.\n' "$PROM_YML" "$SCRAPE_DIR" >&2
+ exit 1
+ fi
+fi
+WORK=$(mktemp -d)
+trap 'rm -rf -- "$WORK"' EXIT
+if CODE=$(curl -s -m 10 -o "$WORK/body" -w '%{http_code}' "$TARGET"); then :; else CODE=000; fi
+if [ "$CODE" != 200 ]; then
+ printf '%s answered HTTP %s. Expected 200 with the body "alive". Nothing was changed.\n' "$TARGET" "$CODE" >&2
+ exit 1
+fi
+printf alive >"$WORK/alive"
+printf 'alive\n' >"$WORK/alive-newline"
+if ! cmp -s "$WORK/body" "$WORK/alive" && ! cmp -s "$WORK/body" "$WORK/alive-newline"; then
+ printf '%s answered 200 with another body. Expected "alive". Nothing was changed.\n' "$TARGET" >&2
+ exit 1
+fi
+if [ "$MODE" = --check ]; then
+ PACKAGE='to install'
+ if STATUS=$(dpkg-query -W -f='${Status}' prometheus-blackbox-exporter 2>/dev/null) && [ "$STATUS" = 'install ok installed' ]; then PACKAGE=installed; fi
+ CHANGE='no change'
+ if [ "$ADD_SCRAPE" -eq 1 ]; then CHANGE='adds scrape_config_files'; fi
+ printf 'Hub: %s answers 200.\nPackage: prometheus-blackbox-exporter, %s\nExporter: %s, modules %s\nScrape job: %s\nAlert rules: %s\nprometheus.yml: %s\nPrometheus: reload\nInstall: sudo CLAWCTL_HUB_URL=%s %s\n' \
+ "$TARGET" "$PACKAGE" "$EXPORTER" "$MODULES" "$SCRAPE" "$RULE" "$CHANGE" "$URL" "$SCRIPT_PATH"
+ exit 0
+fi
+root_required
+
+echo '== 1/4 package'
+# Block the package's automatic service start until the loopback flags exist.
+if STATUS=$(dpkg-query -W -f='${Status}' prometheus-blackbox-exporter 2>/dev/null) && [ "$STATUS" = 'install ok installed' ]; then
+ PACKAGE_INSTALLED=1
+else
+ PACKAGE_INSTALLED=0
+fi
+if [ ! -e "$MASK_PENDING" ]; then
+ UNIT_STATE=$(systemctl is-enabled prometheus-blackbox-exporter 2>/dev/null || :)
+ case $UNIT_STATE in
+ masked*) echo 'prometheus-blackbox-exporter is masked. Unmask it before installing the Hub probe.' >&2; exit 1 ;;
+ esac
+fi
+if [ "$PACKAGE_INSTALLED" -eq 0 ] && [ ! -e "$MASK_PENDING" ]; then
+ if [ ! -e "$MANAGED" ]; then install -m 0644 /dev/null "$MANAGED"; fi
+ : >"$MASK_PENDING"
+ systemctl mask prometheus-blackbox-exporter
+fi
+if [ ! -e "$MANAGED" ]; then install -m 0644 /dev/null "$MANAGED"; fi
+if ! DEBIAN_FRONTEND=noninteractive apt-get install -y prometheus-blackbox-exporter; then
+ printf 'Package installation failed. Retry this install, or remove the probe: sudo %s --remove\n' "$SCRIPT_PATH" >&2
+ exit 1
+fi
+printf '== 2/4 exporter on %s\n' "$EXPORTER"
+if ! cmp -s "$HERE/hub-probe/blackbox.yml" "$MODULES"; then
+ install -m 0644 "$HERE/hub-probe/blackbox.yml" "$MODULES"
+fi
+flag_is_set() {
+ python3 -c 'import pathlib,sys
+p=pathlib.Path(sys.argv[1])
+if not p.is_file(): sys.exit(1)
+lines=[s for s in p.read_text().splitlines() if s.startswith("ARGS=")]
+if len(lines)!=1 or not lines[0].startswith("ARGS=\"") or not lines[0].endswith("\""): sys.exit(1)
+w=lines[0][6:-1].split()
+f=sys.argv[2]
+v=[s for s in w if s==f or s.startswith(f+"=")]
+sys.exit(v != [f+"="+sys.argv[3]])' "$DEFAULTS" "$1" "$2"
+}
+if ! flag_is_set --config.file "$MODULES"; then
+ set_args_flag "$DEFAULTS" --config.file "$MODULES"
+fi
+if ! flag_is_set --web.listen-address "$EXPORTER"; then
+ set_args_flag "$DEFAULTS" --web.listen-address "$EXPORTER"
+fi
+unmask_owned
+systemctl enable prometheus-blackbox-exporter
+systemctl restart prometheus-blackbox-exporter
+HEALTHY=0
+for ((i=0; i<10; i++)); do
+ if CODE=$(curl -s -m 5 -o /dev/null -w '%{http_code}' "http://$EXPORTER/-/healthy") && [ "$CODE" = 200 ]; then
+ HEALTHY=1
+ break
+ fi
+ if [ "$i" -lt 9 ]; then sleep "${CLAWCTL_HUB_PROBE_WAIT-1}"; fi
+done
+if [ "$HEALTHY" -ne 1 ]; then
+ printf 'The exporter does not answer on %s. See: systemctl status prometheus-blackbox-exporter\n' "$EXPORTER" >&2
+ exit 1
+fi
+SOCKETS=$(ss -ltnH)
+ADDRESSES=$(awk -v port="${EXPORTER##*:}" '$4 ~ (":" port "$") {print $4}' <<<"$SOCKETS" | paste -sd ' ')
+BAD_ADDRESS=0
+if [ -z "$ADDRESSES" ]; then BAD_ADDRESS=1; ADDRESSES=none; fi
+while IFS= read -r address; do
+ if [ -n "$address" ] && [ "$address" != "$EXPORTER" ]; then BAD_ADDRESS=1; fi
+done < <(printf '%s\n' "$ADDRESSES" | tr ' ' '\n')
+if [ "$BAD_ADDRESS" -eq 1 ]; then
+ systemctl stop prometheus-blackbox-exporter
+ printf 'The exporter listens on %s. Expected %s only. Stopped it.\n' "$ADDRESSES" "$EXPORTER" >&2
+ exit 1
+fi
+if PROBE=$(curl -s -m 15 -G --data-urlencode 'module=clawctl_hub_healthz' --data-urlencode "target=$TARGET" "http://$EXPORTER/probe") && grep -qx 'probe_success 1' <<<"$PROBE"; then :; else
+ printf 'The exporter got no answer from %s. Prometheus was not changed.\n' "$TARGET" >&2
+ exit 1
+fi
+
+CHANGED=()
+EXISTED=()
+RESTORE_ERRORS=()
+replace_file() {
+ local source=$1 destination=$2 existed=0
+ if cmp -s "$source" "$destination"; then return; fi
+ if [ -e "$destination" ]; then
+ cp -a -- "$destination" "$destination.bak-$STAMP"
+ existed=1
+ fi
+ CHANGED+=("$destination")
+ EXISTED+=("$existed")
+ if [ "$destination" = "$PROM_YML" ]; then
+ cat "$source" >"$destination"
+ else
+ install -m 0644 "$source" "$destination"
+ fi
+}
+restore_files() {
+ local n file
+ for n in "${!CHANGED[@]}"; do
+ file=${CHANGED[$n]}
+ if [ "${EXISTED[$n]}" -eq 1 ]; then
+ if ! cp -a -- "$file.bak-$STAMP" "$file"; then
+ RESTORE_ERRORS+=("$file: copy $file.bak-$STAMP over it")
+ fi
+ else
+ if ! rm -f -- "$file"; then RESTORE_ERRORS+=("$file: remove it"); fi
+ fi
+ done
+}
+rollback() {
+ local reload=$1 message=$2
+ restore_files
+ if [ "$reload" -eq 1 ]; then
+ if ! systemctl reload prometheus; then RESTORE_ERRORS+=('Prometheus: run systemctl reload prometheus'); fi
+ fi
+ printf '%s\n' "$message" >&2
+ if [ "${#RESTORE_ERRORS[@]}" -gt 0 ]; then
+ printf 'Not restored:\n' >&2
+ printf ' %s\n' "${RESTORE_ERRORS[@]}" >&2
+ fi
+ exit 1
+}
+
+# A failed backup or write must restore every earlier file in this install.
+set -E
+trap 'trap - ERR; rollback 0 "Could not write the probe configuration. Restored the previous files. Prometheus was not reloaded."' ERR
+
+echo '== 3/4 scrape job and alert rules'
+install -d -m 0755 "$SCRAPE_DIR" "$ETC/prometheus/rules"
+cat >"$WORK/scrape" <"$WORK/prometheus"
+ printf '\n# Scrape jobs added by other installers, one file each.\nscrape_config_files:\n - %s/*.yml\n' "$SCRAPE_DIR" >>"$WORK/prometheus"
+ replace_file "$WORK/prometheus" "$PROM_YML"
+fi
+trap - ERR
+if promtool check config "$PROM_YML" >"$WORK/promtool" 2>&1; then :; else
+ cat "$WORK/promtool" >&2
+ rollback 0 'promtool rejected the configuration. Restored the previous files. Prometheus was not reloaded.'
+fi
+
+echo '== 4/4 reload Prometheus and check'
+if ! systemctl reload prometheus; then
+ rollback 0 'systemctl reload prometheus failed. Restored the previous files.'
+fi
+PROBE_OK=0
+ALERTS_OK=0
+for ((i=0; i&2
+rule_files=("$RULES"/*.yml)
+if [ ! -f "${rule_files[0]}" ]; then
+ printf 'rule file not installed: %s\n' "${rule_files[0]##*/}" >&2
exit 1
fi
-echo " $got rule files"
+install -m 0644 "${rule_files[@]}" /etc/prometheus/rules/
+
+# Each repository rule file must be installed with identical content.
+count=0
+for rule in "$RULES"/*.yml; do
+ if [ ! -f "$rule" ] || ! cmp -s "$rule" "/etc/prometheus/rules/${rule##*/}"; then
+ printf 'rule file not installed: %s\n' "${rule##*/}" >&2
+ exit 1
+ fi
+ count=$((count + 1))
+done
+echo " $count rule files"
echo "== 3/5 listen addresses and node_exporter textfile collector"
# node_exporter exports the metrics of the *.prom files in this directory.
diff --git a/ops/prometheus/prometheus.yml b/ops/prometheus/prometheus.yml
index 0d262c5..bf441b1 100644
--- a/ops/prometheus/prometheus.yml
+++ b/ops/prometheus/prometheus.yml
@@ -22,6 +22,11 @@ global:
rule_files:
- /etc/prometheus/rules/*.yml
+# Scrape jobs added by other installers, one file each
+# (ops/prometheus/install-hub-probe.sh).
+scrape_config_files:
+ - /etc/prometheus/scrape.d/*.yml
+
# The Alertmanager of ops/prometheus/install-alertmanager.sh. install-prometheus.sh
# installs this file as is, so a rerun keeps alert delivery only if it is here.
alerting:
diff --git a/ops/prometheus/test-install-hub-probe.sh b/ops/prometheus/test-install-hub-probe.sh
new file mode 100755
index 0000000..c7313bb
--- /dev/null
+++ b/ops/prometheus/test-install-hub-probe.sh
@@ -0,0 +1,764 @@
+#!/usr/bin/env bash
+# Run the Hub probe installer against a fake configuration tree.
+set -euo pipefail
+HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
+SCRIPT=$HERE/install-hub-probe.sh
+ORIG_PATH=$PATH
+REAL_PROMTOOL=$(command -v promtool || :)
+SCRATCH=$(mktemp -d "${TMPDIR:-/tmp}/hub-probe-test.XXXXXX")
+passed=0
+failed=0
+number=0
+CASE=
+rc=0
+
+run_case() {
+ local name=$1 status
+ shift
+ number=$((number + 1))
+ CASE=$SCRATCH/$number
+ set +e
+ ( set -euo pipefail; "$@" ) >"$SCRATCH/case-$number.log" 2>&1
+ status=$?
+ set -e
+ if [ "$status" -eq 0 ]; then
+ printf 'ok - %s\n' "$name"
+ passed=$((passed + 1))
+ else
+ printf 'not ok - %s\n' "$name"
+ cat "$SCRATCH/case-$number.log"
+ if [ -f "$CASE/state/stderr" ]; then cat "$CASE/state/stderr"; fi
+ failed=$((failed + 1))
+ fi
+}
+
+setup() {
+ mkdir -p "$CASE/bin" "$CASE/state" "$CASE/etc/default" "$CASE/etc/prometheus"
+ ETC=$CASE/etc
+ PROM=$ETC/prometheus/prometheus.yml
+ SCRAPE=$ETC/prometheus/scrape.d/clawctl-hub-healthz.yml
+ RULE=$ETC/prometheus/rules/clawctl-hub-healthz.yml
+ DEFAULTS=$ETC/default/prometheus-blackbox-exporter
+ MODULES=$ETC/prometheus/clawctl-blackbox.yml
+ TEST_URL=https://hub.example.com
+ SKIP_ROOT=1
+ printf 'global:\n scrape_interval: 15s\nrule_files:\n - %s/prometheus/rules/*.yml\nscrape_configs: []\n' "$ETC" >"$PROM"
+ printf 'ARGS=""\n' >"$DEFAULTS"
+ cp -a "$PROM" "$CASE/state/original-prometheus"
+ cp -a "$DEFAULTS" "$CASE/state/original-defaults"
+ printf 200 >"$CASE/state/hub-code"
+ printf 'alive\n' >"$CASE/state/hub-body"
+ printf 200 >"$CASE/state/healthy"
+ printf 'probe_success 1\n' >"$CASE/state/probe"
+ printf '%s\n' 'LISTEN 0 128 127.0.0.1:9115 0.0.0.0:*' >"$CASE/state/ss"
+ printf '%s\n' '{"data":{"result":[{"metric":{"instance":"https://hub.example.com/healthz"},"value":[0,"1"]}]}}' >"$CASE/state/query"
+ printf '%s\n' '{"data":{"groups":[{"rules":[{"name":"HubNotAnswering"},{"name":"HubProbeNotRunning"}]}]}}' >"$CASE/state/rules"
+ : >"$CASE/state/calls"
+ cat >"$CASE/bin/stub" <<'STUB'
+#!/usr/bin/env bash
+set -euo pipefail
+STATE=$(cd "$(dirname "${BASH_SOURCE[0]}")/../state" && pwd)
+CMD=${0##*/}
+printf '%s %s\n' "$CMD" "$*" >>"$STATE/calls"
+case $CMD in
+apt-get)
+ # A first install starts the service from postinst unless it is masked.
+ if [ ! -e "$STATE/installed" ] && [ ! -e "$STATE/masked" ]; then touch "$STATE/wildcard-start"; fi
+ [ ! -e "$STATE/fail-apt" ]
+ touch "$STATE/installed"
+ ;;
+systemctl)
+ case $1 in
+ is-enabled) if [ -e "$STATE/masked" ]; then echo masked; else echo disabled; fi ;;
+ mask) touch "$STATE/masked" ;;
+ unmask)
+ if [ -e "$STATE/installed" ] && [ ! -e "$STATE/../etc/prometheus/.clawctl-hub-probe-remove-pending" ] &&
+ ! grep -q -- '--web.listen-address=127.0.0.1:9115' "$STATE/../etc/default/prometheus-blackbox-exporter"; then
+ touch "$STATE/premature-unmask"
+ fi
+ rm -f "$STATE/masked"
+ ;;
+ show) if [ -e "$STATE/unit-absent" ]; then echo not-found; else echo loaded; fi ;;
+ esac
+ if [ "$1" = reload ] && [ -e "$STATE/fail-reload" ]; then exit 1; fi
+ if [ "$1" = disable ] && [ -e "$STATE/fail-disable" ]; then exit 1; fi
+ if [ "$1" = reload ] && [ -e "$STATE/fail-second-reload" ] &&
+ [ "$(grep -c '^systemctl reload prometheus$' "$STATE/calls")" -eq 2 ]; then exit 1; fi
+ ;;
+promtool)
+ if [ -e "$STATE/fail-restore" ]; then
+ rm -- "$3".bak-*
+ echo 'configuration rejected'
+ exit 1
+ fi
+ if [ -e "$STATE/fail-remove" ]; then
+ file=${3%/*}/scrape.d/clawctl-hub-healthz.yml
+ rm -- "$file"
+ mkdir "$file"
+ echo 'configuration rejected'
+ exit 1
+ fi
+ if [ -e "$STATE/fail-promtool" ]; then echo 'configuration rejected'; exit 1; fi
+ echo SUCCESS
+ ;;
+dpkg-query)
+ if [ -e "$STATE/installed" ]; then printf 'install ok installed'; else exit 1; fi
+ ;;
+ss) cat "$STATE/ss" ;;
+sleep) ;;
+curl)
+ URL=${!#}
+ BODY=
+ while [ "$#" -gt 0 ]; do
+ if [ "$1" = -o ]; then BODY=$2; shift; fi
+ shift
+ done
+ case $URL in
+ */-/healthy) cat "$STATE/healthy" ;;
+ */probe) cat "$STATE/probe" ;;
+ */api/v1/query) cat "$STATE/query" ;;
+ */api/v1/rules) cat "$STATE/rules" ;;
+ */healthz)
+ if [ -e "$STATE/fail-curl" ]; then printf 503; exit 7; fi
+ cp "$STATE/hub-body" "$BODY"
+ cat "$STATE/hub-code"
+ ;;
+ *) exit 2 ;;
+ esac
+ ;;
+*) exit 2 ;;
+esac
+STUB
+ chmod 0755 "$CASE/bin/stub"
+ for cmd in apt-get systemctl promtool curl ss dpkg-query sleep; do
+ ln -s stub "$CASE/bin/$cmd"
+ done
+}
+
+installer() {
+ set +e
+ env "PATH=$CASE/bin:$ORIG_PATH" "CLAWCTL_HUB_PROBE_ETC=$ETC" \
+ "CLAWCTL_HUB_PROBE_SKIP_ROOT=$SKIP_ROOT" "CLAWCTL_HUB_URL=$TEST_URL" \
+ CLAWCTL_HUB_PROBE_WAIT=0 CLAWCTL_HUB_PROBE_TRIES=2 \
+ bash "$SCRIPT" "$@" >"$CASE/state/stdout" 2>"$CASE/state/stderr"
+ rc=$?
+ set -e
+}
+need_rc() { [ "$rc" -eq "$1" ]; }
+need_out() { grep -Fq -- "$1" "$CASE/state/stdout"; }
+need_err() { grep -Fq -- "$1" "$CASE/state/stderr"; }
+need_call() { grep -Fxq -- "$1" "$CASE/state/calls"; }
+no_call() { if grep -Fq -- "$1" "$CASE/state/calls"; then return 1; fi; }
+no_backups() { [ -z "$(find "$ETC" -name '*.bak-*' -print -quit)" ]; }
+no_prom_changes() {
+ cmp -s "$PROM" "$CASE/state/original-prometheus"
+ [ ! -e "$SCRAPE" ]
+ [ ! -e "$RULE" ]
+ no_call 'reload'
+}
+no_changes() {
+ no_prom_changes
+ cmp -s "$DEFAULTS" "$CASE/state/original-defaults"
+ [ ! -e "$MODULES" ]
+ no_backups
+ no_call 'apt-get'
+ no_call 'systemctl'
+}
+signature() {
+ find "$ETC" -printf '%y %p %m %u %g %T@\n' | LC_ALL=C sort
+ find "$ETC" -type f -print0 | LC_ALL=C sort -z | xargs -0 -r sha256sum
+}
+expected_scrape() {
+ cat <"$CASE/state/before"
+ installer --check
+ need_rc 0
+ cat >"$CASE/state/expected" <"$CASE/state/after"
+ cmp -s "$CASE/state/before" "$CASE/state/after"
+ [ "$(wc -l <"$CASE/state/calls")" -eq 2 ]
+}
+check_installed() {
+ setup
+ touch "$CASE/state/installed"
+ installer --check
+ need_rc 0
+ need_out 'Package: prometheus-blackbox-exporter, installed'
+ no_changes
+}
+add_directory() { printf 'scrape_config_files:\n - %s/prometheus/scrape.d/*.yml\n' "$ETC" >>"$PROM"; }
+check_directory() {
+ setup
+ add_directory
+ cp -a "$PROM" "$CASE/state/original-prometheus"
+ installer --check
+ need_rc 0
+ need_out 'prometheus.yml: no change'
+ no_changes
+}
+hub_failure() {
+ setup
+ case $1 in
+ 500) printf 500 >"$CASE/state/hub-code" ;;
+ body) printf 'alive\n\n' >"$CASE/state/hub-body" ;;
+ curl) touch "$CASE/state/fail-curl" ;;
+ esac
+ installer
+ need_rc 1
+ need_err "$2"
+ no_changes
+ [ "$(wc -l <"$CASE/state/calls")" -eq 1 ]
+}
+bad_urls() {
+ setup
+ for TEST_URL in https://hub.example.com/path hub.example.com https://user@hub.example.com 'https://hub.example.com space' ''; do
+ installer --check
+ need_rc 2
+ need_err "CLAWCTL_HUB_URL must be the Hub's address: scheme, host and optional port, without a path. Example: https://hub.example.com"
+ no_changes
+ done
+ [ ! -s "$CASE/state/calls" ]
+}
+trailing_slash() {
+ setup
+ TEST_URL=https://hub.example.com/
+ printf alive >"$CASE/state/hub-body"
+ installer --check
+ need_rc 0
+ need_out 'Hub: https://hub.example.com/healthz answers 200.'
+ no_changes
+}
+missing_prometheus() {
+ setup
+ rm "$PROM"
+ installer
+ need_rc 1
+ need_err "$PROM not found. Install Prometheus first: sudo CLAWCTL_FLEET_JSON=/path/to/fleet.json $HERE/install-prometheus.sh"
+ [ ! -e "$PROM" ]
+ [ ! -e "$MODULES" ]
+ [ ! -s "$CASE/state/calls" ]
+}
+wrong_directory() {
+ setup
+ printf 'scrape_config_files:\n - elsewhere/*.yml\n' >>"$PROM"
+ cp -a "$PROM" "$CASE/state/original-prometheus"
+ installer
+ need_rc 1
+ need_err "$PROM sets scrape_config_files without $ETC/prometheus/scrape.d/*.yml. Add that path to the list, then run this again."
+ no_changes
+ [ ! -s "$CASE/state/calls" ]
+}
+install_files() {
+ setup
+ chmod 0640 "$PROM"
+ installer
+ need_rc 0
+ need_out 'Prometheus probes https://hub.example.com/healthz through 127.0.0.1:9115.'
+ need_out 'Alerts: HubNotAnswering after 5 minutes without an answer, HubProbeNotRunning after 10 minutes without a probe result.'
+ need_out "Remove: sudo $SCRIPT --remove"
+ expected_scrape >"$CASE/state/expected"
+ cmp -s "$CASE/state/expected" "$SCRAPE"
+ printf 'ARGS="--config.file=%s --web.listen-address=127.0.0.1:9115"\n' "$MODULES" >"$CASE/state/expected"
+ cmp -s "$CASE/state/expected" "$DEFAULTS"
+ cmp -s "$HERE/hub-probe/rules.yml" "$RULE"
+ cmp -s "$HERE/hub-probe/blackbox.yml" "$MODULES"
+ cat "$CASE/state/original-prometheus" >"$CASE/state/expected"
+ printf '\n# Scrape jobs added by other installers, one file each.\nscrape_config_files:\n - %s/prometheus/scrape.d/*.yml\n' "$ETC" >>"$CASE/state/expected"
+ cmp -s "$CASE/state/expected" "$PROM"
+ for file in "$SCRAPE" "$RULE" "$MODULES"; do [ "$(stat -c %a "$file")" = 644 ]; done
+ [ "$(stat -c %a "$PROM")" = 640 ]
+ [ "$(stat -c %a "${SCRAPE%/*}")" = 755 ]
+ [ "$(stat -c %a "${RULE%/*}")" = 755 ]
+}
+install_order() {
+ setup
+ installer
+ need_rc 0
+ need_out '== 4/4 reload Prometheus and check'
+ grep -E '^(apt-get|systemctl|promtool) ' "$CASE/state/calls" >"$CASE/state/order"
+ printf '%s\n' 'systemctl is-enabled prometheus-blackbox-exporter' 'systemctl mask prometheus-blackbox-exporter' \
+ 'apt-get install -y prometheus-blackbox-exporter' 'systemctl unmask prometheus-blackbox-exporter' \
+ 'systemctl enable prometheus-blackbox-exporter' 'systemctl restart prometheus-blackbox-exporter' \
+ "promtool check config $PROM" 'systemctl reload prometheus' >"$CASE/state/expected"
+ cmp -s "$CASE/state/expected" "$CASE/state/order"
+ [ -f "$SCRAPE" ]
+}
+second_install() {
+ setup
+ installer
+ need_rc 0
+ find "$ETC" -name '*.bak-*' -delete
+ signature >"$CASE/state/before"
+ installer
+ need_rc 0
+ need_out 'Prometheus probes https://hub.example.com/healthz'
+ signature >"$CASE/state/after"
+ cmp -s "$CASE/state/before" "$CASE/state/after"
+ no_backups
+ [ "$(grep -c '^systemctl reload prometheus$' "$CASE/state/calls")" -eq 2 ]
+}
+new_url() {
+ setup
+ installer
+ need_rc 0
+ cp "$SCRAPE" "$CASE/state/old-scrape"
+ TEST_URL=https://hub.example.com:8443
+ printf '%s\n' '{"data":{"result":[{"metric":{"instance":"https://hub.example.com:8443/healthz"},"value":[0,"1"]}]}}' >"$CASE/state/query"
+ installer
+ need_rc 0
+ need_out 'Prometheus probes https://hub.example.com:8443/healthz'
+ expected_scrape >"$CASE/state/expected"
+ cmp -s "$CASE/state/expected" "$SCRAPE"
+ cmp -s "$CASE/state/old-scrape" "$SCRAPE".bak-*
+ need_call 'systemctl reload prometheus'
+}
+install_directory() {
+ setup
+ add_directory
+ cp -a "$PROM" "$CASE/state/original-prometheus"
+ installer
+ need_rc 0
+ need_out 'Prometheus probes https://hub.example.com/healthz'
+ cmp -s "$PROM" "$CASE/state/original-prometheus"
+ [ -z "$(find "${PROM%/*}" -maxdepth 1 -name 'prometheus.yml.bak-*' -print -quit)" ]
+ need_call "promtool check config $PROM"
+}
+apt_failure() {
+ setup
+ touch "$CASE/state/fail-apt"
+ installer
+ need_rc 1
+ need_err 'Package installation failed. Retry this install, or remove the probe:'
+ no_prom_changes
+ [ ! -e "$MODULES" ]
+ cmp -s "$DEFAULTS" "$CASE/state/original-defaults"
+ need_call 'systemctl mask prometheus-blackbox-exporter'
+ no_call 'systemctl unmask'
+ no_call 'systemctl enable'
+ no_call 'systemctl restart'
+ [ -e "$ETC/prometheus/.clawctl-hub-probe-mask-pending" ]
+ no_call promtool
+}
+exporter_failure() {
+ setup
+ printf 503 >"$CASE/state/healthy"
+ installer
+ need_rc 1
+ need_err 'The exporter does not answer on 127.0.0.1:9115. See: systemctl status prometheus-blackbox-exporter'
+ no_prom_changes
+ [ "$(grep -c '/-/healthy$' "$CASE/state/calls")" -eq 10 ]
+ no_call promtool
+}
+wrong_listener() {
+ setup
+ printf '%s\n' 'LISTEN 0 128 127.0.0.1:9115 0.0.0.0:*' 'LISTEN 0 128 0.0.0.0:9115 0.0.0.0:*' >"$CASE/state/ss"
+ installer
+ need_rc 1
+ need_err 'The exporter listens on 127.0.0.1:9115 0.0.0.0:9115. Expected 127.0.0.1:9115 only. Stopped it.'
+ need_call 'systemctl stop prometheus-blackbox-exporter'
+ no_prom_changes
+ no_call promtool
+}
+probe_failure() {
+ setup
+ printf 'probe_success 0\n' >"$CASE/state/probe"
+ installer
+ need_rc 1
+ need_err 'The exporter got no answer from https://hub.example.com/healthz. Prometheus was not changed.'
+ no_prom_changes
+ no_call promtool
+}
+promtool_failure() {
+ setup
+ touch "$CASE/state/fail-promtool"
+ installer
+ need_rc 1
+ need_err 'configuration rejected'
+ need_err 'promtool rejected the configuration. Restored the previous files. Prometheus was not reloaded.'
+ no_prom_changes
+ need_call "promtool check config $PROM"
+}
+reload_failure() {
+ setup
+ touch "$CASE/state/fail-reload"
+ installer
+ need_rc 1
+ need_err 'systemctl reload prometheus failed. Restored the previous files.'
+ cmp -s "$PROM" "$CASE/state/original-prometheus"
+ [ ! -e "$SCRAPE" ]
+ [ ! -e "$RULE" ]
+ [ "$(grep -c '^systemctl reload prometheus$' "$CASE/state/calls")" -eq 1 ]
+}
+query_failure() {
+ setup
+ printf '%s\n' '{"data":{"result":[{"metric":{"instance":"https://hub.example.com/other"},"value":[0,"1"]},{"metric":{"instance":"https://hub.example.com/healthz"},"value":[0,"0"]}]}}' >"$CASE/state/query"
+ installer
+ need_rc 1
+ need_err 'Prometheus got no answer from https://hub.example.com/healthz in 0 seconds. Restored the previous files and reloaded Prometheus.'
+ cmp -s "$PROM" "$CASE/state/original-prometheus"
+ [ ! -e "$SCRAPE" ]
+ [ ! -e "$RULE" ]
+ [ "$(grep -c '^systemctl reload prometheus$' "$CASE/state/calls")" -eq 2 ]
+}
+alerts_failure() {
+ setup
+ printf '%s\n' '{"data":{"result":[]}}' >"$CASE/state/query"
+ printf '%s\n' '{"data":{"groups":[{"rules":[{"name":"HubNotAnswering"}]}]}}' >"$CASE/state/rules"
+ installer
+ need_rc 1
+ need_err 'Prometheus did not load the alerts HubNotAnswering and HubProbeNotRunning. Restored the previous files and reloaded Prometheus.'
+ cmp -s "$PROM" "$CASE/state/original-prometheus"
+ [ ! -e "$SCRAPE" ]
+ [ ! -e "$RULE" ]
+ [ "$(grep -c '^systemctl reload prometheus$' "$CASE/state/calls")" -eq 2 ]
+}
+existing_failure() {
+ setup
+ installer
+ need_rc 0
+ cp "$SCRAPE" "$CASE/state/old-scrape"
+ cp "$RULE" "$CASE/state/old-rule"
+ printf '# site rule\n' >>"$RULE"
+ cp "$RULE" "$CASE/state/old-rule"
+ TEST_URL=https://hub.example.com:8443
+ touch "$CASE/state/fail-promtool"
+ : >"$CASE/state/calls"
+ installer
+ need_rc 1
+ need_err 'promtool rejected the configuration. Restored the previous files. Prometheus was not reloaded.'
+ cmp -s "$SCRAPE" "$CASE/state/old-scrape"
+ cmp -s "$RULE" "$CASE/state/old-rule"
+ no_call reload
+}
+restore_failure() {
+ local base=$CASE kind
+ for kind in copy remove reload; do
+ CASE=$base/$kind
+ setup
+ case $kind in
+ copy) touch "$CASE/state/fail-restore" ;;
+ remove) touch "$CASE/state/fail-remove" ;;
+ reload)
+ touch "$CASE/state/fail-second-reload"
+ printf '%s\n' '{"data":{"result":[]}}' >"$CASE/state/query"
+ ;;
+ esac
+ installer
+ need_rc 1
+ need_err 'Not restored:'
+ case $kind in
+ copy)
+ grep -Eq "^ $PROM: copy $PROM.bak-[0-9-]+ over it$" "$CASE/state/stderr"
+ [ ! -e "$SCRAPE" ]
+ no_call reload
+ ;;
+ remove)
+ need_err " $SCRAPE: remove it"
+ cmp -s "$PROM" "$CASE/state/original-prometheus"
+ no_call reload
+ ;;
+ reload)
+ need_err ' Prometheus: run systemctl reload prometheus'
+ cmp -s "$PROM" "$CASE/state/original-prometheus"
+ [ ! -e "$SCRAPE" ]
+ [ "$(grep -c '^systemctl reload prometheus$' "$CASE/state/calls")" -eq 2 ]
+ ;;
+ esac
+ [ ! -e "$RULE" ]
+ done
+}
+
+remove_probe() {
+ setup
+ installer
+ need_rc 0
+ signature >"$CASE/state/before"
+ cp "$PROM" "$CASE/state/installed-prometheus"
+ cp "$DEFAULTS" "$CASE/state/installed-defaults"
+ : >"$CASE/state/calls"
+ TEST_URL=
+ installer --remove
+ need_rc 0
+ need_out 'Removed the Hub probe. Prometheus is reloaded; prometheus-blackbox-exporter is stopped and disabled.'
+ [ ! -e "$SCRAPE" ]
+ [ ! -e "$RULE" ]
+ cmp -s "$PROM" "$CASE/state/installed-prometheus"
+ cmp -s "$DEFAULTS" "$CASE/state/installed-defaults"
+ cmp -s "$MODULES" "$HERE/hub-probe/blackbox.yml"
+ printf '%s\n' 'systemctl reload prometheus' 'systemctl show prometheus-blackbox-exporter -p LoadState --value' 'systemctl disable --now prometheus-blackbox-exporter' >"$CASE/state/expected"
+ cmp -s "$CASE/state/expected" "$CASE/state/calls"
+}
+remove_absent() {
+ setup
+ installer --remove
+ need_rc 0
+ need_out 'The Hub probe is not installed.'
+ no_changes
+ [ ! -s "$CASE/state/calls" ]
+}
+package_startup() {
+ setup
+ installer
+ need_rc 0
+ [ ! -e "$CASE/state/wildcard-start" ]
+ [ ! -e "$CASE/state/premature-unmask" ]
+ [ ! -e "$CASE/state/masked" ]
+ [ ! -e "$ETC/prometheus/.clawctl-hub-probe-mask-pending" ]
+ [ -e "$ETC/prometheus/.clawctl-hub-probe-managed" ]
+}
+foreign_mask() {
+ local base=$CASE state
+ for state in absent installed; do
+ CASE=$base/$state
+ setup
+ if [ "$state" = installed ]; then touch "$CASE/state/installed"; fi
+ touch "$CASE/state/masked"
+ installer
+ need_rc 1
+ need_err 'prometheus-blackbox-exporter is masked.'
+ [ -e "$CASE/state/masked" ]
+ no_call 'apt-get'
+ no_call 'systemctl unmask'
+ no_prom_changes
+ done
+}
+installed_package() {
+ setup
+ touch "$CASE/state/installed"
+ installer
+ need_rc 0
+ no_call 'systemctl mask'
+ no_call 'systemctl unmask'
+ need_call 'systemctl restart prometheus-blackbox-exporter'
+}
+
+package_retry() {
+ setup
+ touch "$CASE/state/fail-apt"
+ installer
+ need_rc 1
+ [ -e "$CASE/state/masked" ]
+ rm "$CASE/state/fail-apt"
+ installer
+ need_rc 0
+ [ ! -e "$CASE/state/masked" ]
+ [ ! -e "$CASE/state/wildcard-start" ]
+}
+remove_package_failure() {
+ setup
+ touch "$CASE/state/fail-apt" "$CASE/state/unit-absent"
+ installer
+ need_rc 1
+ installer --remove
+ need_rc 0
+ need_call 'systemctl unmask prometheus-blackbox-exporter'
+ [ ! -e "$CASE/state/masked" ]
+ [ ! -e "$ETC/prometheus/.clawctl-hub-probe-managed" ]
+ no_call 'systemctl disable'
+}
+remove_failed_install() {
+ local base=$CASE stage
+ for stage in exporter probe config; do
+ CASE=$base/$stage
+ setup
+ case $stage in
+ exporter) printf 503 >"$CASE/state/healthy" ;;
+ probe) printf 'probe_success 0\n' >"$CASE/state/probe" ;;
+ config) touch "$CASE/state/fail-promtool" ;;
+ esac
+ installer
+ need_rc 1
+ [ ! -e "$SCRAPE" ]
+ [ ! -e "$RULE" ]
+ : >"$CASE/state/calls"
+ installer --remove
+ need_rc 0
+ need_call 'systemctl disable --now prometheus-blackbox-exporter'
+ need_out 'Removed the Hub probe.'
+ : >"$CASE/state/calls"
+ installer --remove
+ need_rc 0
+ need_out 'The Hub probe is not installed.'
+ [ ! -s "$CASE/state/calls" ]
+ done
+}
+remove_retry() {
+ local kind base=$CASE
+ for kind in reload disable; do
+ CASE=$base/$kind
+ setup
+ installer
+ need_rc 0
+ touch "$CASE/state/fail-$kind"
+ installer --remove
+ need_rc 1
+ [ -f "$ETC/prometheus/.clawctl-hub-probe-remove-pending" ]
+ rm "$CASE/state/fail-$kind"
+ : >"$CASE/state/calls"
+ installer --remove
+ need_rc 0
+ need_call 'systemctl reload prometheus'
+ need_call 'systemctl disable --now prometheus-blackbox-exporter'
+ [ ! -e "$ETC/prometheus/.clawctl-hub-probe-remove-pending" ]
+ done
+}
+missing_listener() {
+ setup
+ : >"$CASE/state/ss"
+ installer
+ need_rc 1
+ need_err 'The exporter listens on none.'
+ need_call 'systemctl stop prometheus-blackbox-exporter'
+ no_prom_changes
+}
+write_failure() {
+ setup
+ # Fail the rule write after the scrape job was already installed.
+ local real_install
+ real_install=$(command -v install)
+ printf '#!/usr/bin/env bash\ncase "${!#}" in */rules/clawctl-hub-healthz.yml) exit 1;; esac\nexec %q "$@"\n' "$real_install" >"$CASE/bin/install"
+ chmod 0755 "$CASE/bin/install"
+ installer
+ need_rc 1
+ need_err 'Could not write the probe configuration.'
+ no_prom_changes
+}
+remove_reload_failure() {
+ setup
+ mkdir -p "${SCRAPE%/*}" "${RULE%/*}"
+ touch "$SCRAPE" "$RULE" "$CASE/state/fail-reload"
+ installer --remove
+ need_rc 1
+ need_err 'Prometheus reload failed. Retry removal:'
+ [ ! -e "$SCRAPE" ]
+ [ ! -e "$RULE" ]
+ need_call 'systemctl reload prometheus'
+ no_call disable
+}
+bad_arguments() {
+ setup
+ printf '%s\n' 'Usage:' \
+ 'CLAWCTL_HUB_URL=https://hub.example.com ./ops/prometheus/install-hub-probe.sh --check' \
+ 'sudo CLAWCTL_HUB_URL=https://hub.example.com ./ops/prometheus/install-hub-probe.sh' \
+ 'sudo ./ops/prometheus/install-hub-probe.sh --remove' >"$CASE/state/expected"
+ for arg in --other install ''; do
+ installer "$arg"
+ need_rc 2
+ cmp -s "$CASE/state/expected" "$CASE/state/stderr"
+ no_changes
+ done
+ installer --check --remove
+ need_rc 2
+ cmp -s "$CASE/state/expected" "$CASE/state/stderr"
+ [ ! -s "$CASE/state/calls" ]
+}
+needs_root() {
+ setup
+ SKIP_ROOT=0
+ installer
+ need_rc 1
+ need_err "Run as root: sudo CLAWCTL_HUB_URL=https://hub.example.com $SCRIPT"
+ no_changes
+ installer --remove
+ need_rc 1
+ need_err "Run as root: sudo $SCRIPT --remove"
+ no_changes
+}
+real_promtool() {
+ setup
+ rm "$CASE/bin/promtool"
+ ln -s "$REAL_PROMTOOL" "$CASE/bin/promtool"
+ installer
+ need_rc 0
+ need_out 'Prometheus probes https://hub.example.com/healthz'
+ [ -f "$SCRAPE" ]
+ [ -f "$RULE" ]
+ need_call 'systemctl reload prometheus'
+}
+
+run_case 'check prints the plan and changes nothing' check_plan
+run_case 'check reports a package that is already installed' check_installed
+run_case 'check reports a prometheus.yml that already has the scrape directory' check_directory
+run_case 'a Hub that answers 500 stops before any change' hub_failure 500 'answered HTTP 500. Expected 200 with the body "alive". Nothing was changed.'
+run_case 'a Hub that answers 200 with another body stops before any change' hub_failure body 'answered 200 with another body. Expected "alive". Nothing was changed.'
+run_case 'a Hub that does not answer stops before any change' hub_failure curl 'answered HTTP 000. Expected 200 with the body "alive". Nothing was changed.'
+run_case 'a URL with a path is refused (also: no scheme, user@host, a space, empty)' bad_urls
+run_case 'a trailing slash in the URL is accepted' trailing_slash
+run_case 'a missing prometheus.yml stops before any change' missing_prometheus
+run_case 'scrape_config_files without the scrape directory stops before any change' wrong_directory
+run_case 'install writes the exporter settings, the scrape job, the rules and the scrape directory line' install_files
+run_case 'install runs apt-get, the exporter restart, promtool and the reload in that order' install_order
+run_case 'a second install changes no file and makes no backup' second_install
+run_case 'an install with another URL replaces the target and keeps a backup of the old scrape job' new_url
+run_case 'a prometheus.yml that already has the scrape directory is not edited' install_directory
+run_case 'apt-get failing stops before any other change' apt_failure
+run_case 'an exporter that does not answer stops before Prometheus is changed' exporter_failure
+run_case 'an exporter on another address is stopped' wrong_listener
+run_case 'an exporter that gets no answer from the Hub stops before Prometheus is changed' probe_failure
+run_case 'a configuration promtool rejects is restored and not reloaded' promtool_failure
+run_case 'a failing reload restores the files' reload_failure
+run_case 'a probe that never reports 1 restores the files and reloads again' query_failure
+run_case 'missing alerts restore the files and reload again' alerts_failure
+run_case 'a failed install over an existing probe restores the previous scrape job' existing_failure
+run_case 'a restore step that fails is listed under Not restored' restore_failure
+run_case 'remove deletes the scrape job and the rules, reloads and disables the exporter' remove_probe
+run_case 'remove without a probe changes nothing' remove_absent
+run_case 'remove reports a reload that fails' remove_reload_failure
+run_case 'package startup waits for the loopback flags' package_startup
+run_case 'an installed package keeps its current mask state' installed_package
+run_case 'an operator mask is preserved' foreign_mask
+run_case 'package installation can resume after failure' package_retry
+run_case 'remove cleans up after a package installation failure' remove_package_failure
+run_case 'remove cleans up an exporter after failed installation' remove_failed_install
+run_case 'removal can resume after reload or disable fails' remove_retry
+run_case 'a missing listener stops the exporter' missing_listener
+run_case 'a failed write restores previously written files' write_failure
+run_case 'any other argument prints the usage' bad_arguments
+if [ "$(id -u)" -eq 0 ]; then
+ printf 'skip: install needs root (running as root)\n'
+else
+ run_case 'install needs root' needs_root
+fi
+if [ -n "$REAL_PROMTOOL" ]; then
+ run_case 'the generated configuration passes the real promtool' real_promtool
+else
+ printf 'skip: promtool not on PATH\n'
+fi
+if [ "$failed" -eq 0 ]; then
+ rm -rf -- "$SCRATCH"
+else
+ printf 'Scratch: %s\n' "$SCRATCH"
+fi
+printf 'passed: %s, failed: %s\n' "$passed" "$failed"
+[ "$failed" -eq 0 ]
diff --git a/ops/test-ops.sh b/ops/test-ops.sh
index 9387be8..d75c08e 100755
--- a/ops/test-ops.sh
+++ b/ops/test-ops.sh
@@ -2486,6 +2486,19 @@ else
echo "skip: promtool not in PATH, skipping ops/prometheus/rules/*.yml. Must fail in CI when CLAWCTL_REQUIRE_PROMTOOL=1."
fi
+# Hub probe installer and alert rules.
+run bash "$ROOT/ops/prometheus/test-install-hub-probe.sh"
+expect 'Hub probe installer cases pass' 0 has 'failed: 0'
+if command -v promtool >/dev/null 2>&1; then
+ run promtool test rules "$ROOT/ops/prometheus/hub-probe/rules_test.yml"
+ expect 'promtool tests Hub probe alerts' 0 has 'SUCCESS'
+elif [ "${CLAWCTL_REQUIRE_PROMTOOL:-}" = "1" ]; then
+ rc=127; output='promtool not in PATH'
+ fail 'promtool tests Hub probe alerts (CLAWCTL_REQUIRE_PROMTOOL=1)'
+else
+ echo 'skip: promtool not in PATH, skipping Hub probe alert tests.'
+fi
+
# --- ops/prometheus/install.sh only calls stage scripts that exist.
# It once called an install script that was never in this tree, so the first
# stage could not run at all.
@@ -2810,6 +2823,15 @@ expect 'listen.sh: install-prometheus.sh sets the listen addresses' 0 \
'set_args_flag /etc/default/prometheus --web.listen-address 127.0.0.1:9090' \
'set_args_flag /etc/default/prometheus-node-exporter --web.listen-address "$ADDR:9100"' \
'retry_until_bound prometheus-node-exporter'
+prometheus_rules_installed() {
+ local file=$ROOT/ops/prometheus/install-prometheus.sh
+ grep -Fq 'for rule in "$RULES"/*.yml; do' "$file" &&
+ grep -Fq '[ ! -f "$rule" ] || ! cmp -s "$rule" "/etc/prometheus/rules/${rule##*/}"' "$file" &&
+ grep -Fq 'rule file not installed: %s' "$file" &&
+ grep -Fq 'count=$((count + 1))' "$file" &&
+ ! grep -Fq -- '-ne "$want"' "$file"
+}
+expect 'listen.sh: install-prometheus.sh verifies each repository rule file' 0 prometheus_rules_installed
expect 'listen.sh: install-alertmanager.sh sets the listen addresses' 0 \
listen_exact_lines "$ROOT/ops/prometheus/install-alertmanager.sh" \
'set_args_flag /etc/default/prometheus-alertmanager --web.listen-address 127.0.0.1:9093' \
@@ -2821,7 +2843,7 @@ expect 'listen.sh: install-grafana.sh sets the listen address' 0 \
listen_no_old_urls() {
local f needle hit=""
- for f in install.sh install-prometheus.sh install-alertmanager.sh install-grafana.sh; do
+ for f in install.sh install-prometheus.sh install-alertmanager.sh install-grafana.sh install-hub-probe.sh; do
for needle in 'localhost:3000' 'localhost:9090/-/healthy' 'localhost:9100' 'localhost:9093/api'; do
if grep -qF -- "$needle" "$ROOT/ops/prometheus/$f"; then
hit="$hit ${f}:${needle}"