apps/console/README.md and lib/watch/types.ts already documented WATCH_AGENT_TOKEN and RAILYARD_WATCH_LIVE as if stacks/gatus-customers/agent.sh existed. It did not -- the console side (agentChecks/recordResults, the /api/v1/watch/agent/* routes) had nothing on the other end to call it, so Watch could never actually turn on. Adds the real box 1 stack: a Gatus probe engine (docker-compose.yml, config.yaml) plus agent.sh, the bridge that pulls the console's check list, regenerates Gatus's config, reads Gatus's own results, and pushes them back -- stateless by design since the console already dedupes by last_checked_at. Plus agent.env.sample, a systemd unit and install.sh (same shape as tools/reconcile-relay-rago). Verified against real components: a real ghcr.io/twin/gatus:stable container and a scratch server wiring the exact shipped lib/watch/http.ts handlers to a MemoryWatchStore, exercised end to end over two passes against a real https://example.com check -- which caught and fixed two real bugs (Gatus refuses to start with zero endpoints; the generated config's timestamp was forcing a gatus restart on every single pass). Container, volume and image removed afterward; apps/console's existing 167 tests and 3 bundle tests pass unchanged (this item touched no console source file).
200 lines
7.0 KiB
Bash
Executable File
200 lines
7.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Railyard Watch's box 1 checks agent (apps/console/README.md's
|
|
# `stacks/gatus-customers/agent.sh`, PY2.2). This is the piece the
|
|
# RAILYARD_WATCH_LIVE / WATCH_AGENT_TOKEN flags assume exists and, before
|
|
# this file, did not: it is the only bridge between the console's list of
|
|
# customer checks and the Gatus instance in this directory that actually
|
|
# probes them.
|
|
#
|
|
# Each pass:
|
|
# 1. GET <console>/api/v1/watch/agent/checks (Bearer WATCH_AGENT_TOKEN)
|
|
# 2. rewrite config/endpoints.generated.yaml from that list; if it changed,
|
|
# restart the gatus container so the new list takes effect
|
|
# 3. GET Gatus's own /api/v1/endpoints/statuses (same box, loopback only)
|
|
# 4. POST the results batch to <console>/api/v1/watch/agent/results
|
|
#
|
|
# The console dedupes by last_checked_at (lib/watch/service.ts::recordResults),
|
|
# so resending results it already has is always safe — this script keeps no
|
|
# state of its own on purpose.
|
|
#
|
|
# Usage:
|
|
# ./agent.sh once # one pass, exit 0/1/2 (for `install.sh run` and manual checks)
|
|
# ./agent.sh # loop forever, one pass every WATCH_POLL_INTERVAL_S (default)
|
|
set -uo pipefail
|
|
|
|
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
ENV_FILE="${WATCH_AGENT_ENV_FILE:-$HERE/agent.env}"
|
|
[[ -f "$ENV_FILE" ]] && . "$ENV_FILE"
|
|
|
|
CONSOLE_URL="${WATCH_CONSOLE_URL:-}"
|
|
TOKEN="${WATCH_AGENT_TOKEN:-}"
|
|
GATUS_URL="${GATUS_URL:-http://127.0.0.1:8083}"
|
|
POLL_INTERVAL_S="${WATCH_POLL_INTERVAL_S:-30}"
|
|
HTTP_TIMEOUT_S="${WATCH_HTTP_TIMEOUT_S:-10}"
|
|
COMPOSE_FILE="${WATCH_COMPOSE_FILE:-$HERE/docker-compose.yml}"
|
|
COMPOSE_CMD="${WATCH_COMPOSE_CMD:-docker compose -f $COMPOSE_FILE}"
|
|
GENERATED="$HERE/config/endpoints.generated.yaml"
|
|
GENERATED_TMP="$GENERATED.tmp.$$"
|
|
|
|
log() { printf '%s [watch-agent] %s\n' "$(date -u +%FT%TZ)" "$*"; }
|
|
|
|
require_tools() {
|
|
for t in curl jq; do
|
|
command -v "$t" >/dev/null 2>&1 || { log "missing required tool: $t"; exit 2; }
|
|
done
|
|
}
|
|
|
|
# One pass. Returns 0 on a normal pass (including "Watch is off, nothing to
|
|
# do"), 1 on a config/auth problem worth paging on, 2 on a transient network
|
|
# failure (retried next interval, never fatal to the loop).
|
|
run_once() {
|
|
if [[ -z "$CONSOLE_URL" || -z "$TOKEN" ]]; then
|
|
log "WATCH_CONSOLE_URL and WATCH_AGENT_TOKEN are both required (see agent.env.sample)"
|
|
return 1
|
|
fi
|
|
if [[ ${#TOKEN} -lt 32 ]]; then
|
|
log "WATCH_AGENT_TOKEN looks too short (< 32 chars) — the console rejects it the same way"
|
|
return 1
|
|
fi
|
|
|
|
local body_file status
|
|
body_file="$(mktemp)"
|
|
status="$(curl -sS -m "$HTTP_TIMEOUT_S" -o "$body_file" -w '%{http_code}' \
|
|
-H "Authorization: Bearer $TOKEN" \
|
|
"$CONSOLE_URL/api/v1/watch/agent/checks")"
|
|
local curl_rc=$?
|
|
if [[ $curl_rc -ne 0 ]]; then
|
|
log "could not reach $CONSOLE_URL (curl exit $curl_rc) — will retry next interval"
|
|
rm -f "$body_file"
|
|
return 2
|
|
fi
|
|
case "$status" in
|
|
200) ;;
|
|
503)
|
|
log "Watch is not live/configured on the console yet (503) — nothing to probe"
|
|
rm -f "$body_file"
|
|
return 0
|
|
;;
|
|
401)
|
|
log "unauthorized (401) — WATCH_AGENT_TOKEN does not match the console's"
|
|
rm -f "$body_file"
|
|
return 1
|
|
;;
|
|
*)
|
|
log "unexpected status $status fetching checks"
|
|
rm -f "$body_file"
|
|
return 2
|
|
;;
|
|
esac
|
|
|
|
local checks_json
|
|
checks_json="$(cat "$body_file")"
|
|
rm -f "$body_file"
|
|
if ! jq -e '.checks | type == "array"' >/dev/null 2>&1 <<<"$checks_json"; then
|
|
log "checks response was not the expected shape, skipping this pass"
|
|
return 2
|
|
fi
|
|
local n_checks
|
|
n_checks="$(jq '.checks | length' <<<"$checks_json")"
|
|
|
|
# --- 2. rewrite the generated Gatus config, restart only if it changed ---
|
|
# No timestamp in this header: it must byte-for-byte match the previous
|
|
# pass when the check list itself hasn't changed, or every single pass
|
|
# would look "changed" and restart gatus for nothing.
|
|
{
|
|
echo "# GENERATED by agent.sh — do not edit, it is overwritten every pass."
|
|
echo "endpoints:"
|
|
jq -r '
|
|
.checks[] |
|
|
" - name: \"" + .id + "\"\n" +
|
|
" group: \"railyard-watch\"\n" +
|
|
" url: \"" + .url + "\"\n" +
|
|
" interval: " + ((.interval_s // 60) | tostring) + "s\n" +
|
|
" client:\n" +
|
|
" timeout: 15s\n" +
|
|
" conditions:\n" +
|
|
" - \"[STATUS] < 400\"\n" +
|
|
" - \"[RESPONSE_TIME] < 15000\"\n"
|
|
' <<<"$checks_json"
|
|
} >"$GENERATED_TMP"
|
|
if [[ "$n_checks" -eq 0 ]]; then
|
|
printf '# GENERATED by agent.sh — no checks right now.\nendpoints: []\n' >"$GENERATED_TMP"
|
|
fi
|
|
|
|
if ! cmp -s "$GENERATED_TMP" "$GENERATED" 2>/dev/null; then
|
|
mv "$GENERATED_TMP" "$GENERATED"
|
|
log "check list changed ($n_checks check(s)) — restarting gatus"
|
|
if ! $COMPOSE_CMD up -d --force-recreate gatus >/tmp/watch-agent-compose.log 2>&1; then
|
|
log "gatus restart failed, see /tmp/watch-agent-compose.log"
|
|
return 2
|
|
fi
|
|
sleep 3
|
|
else
|
|
rm -f "$GENERATED_TMP"
|
|
fi
|
|
|
|
if [[ "$n_checks" -eq 0 ]]; then
|
|
log "no checks to probe this pass"
|
|
return 0
|
|
fi
|
|
|
|
# --- 3. pull Gatus's own latest results ---
|
|
local statuses_file statuses_status
|
|
statuses_file="$(mktemp)"
|
|
statuses_status="$(curl -sS -m "$HTTP_TIMEOUT_S" -o "$statuses_file" -w '%{http_code}' "$GATUS_URL/api/v1/endpoints/statuses")"
|
|
if [[ "$statuses_status" != "200" ]]; then
|
|
log "gatus statuses endpoint answered $statuses_status — probably still restarting, will retry next pass"
|
|
rm -f "$statuses_file"
|
|
return 2
|
|
fi
|
|
|
|
# --- 4. translate to the console's {results:[{id,ok,at,ms}]} and push ---
|
|
# name -> id (we set Gatus's endpoint name to the check id)
|
|
# success -> ok
|
|
# timestamp -> at (RFC3339Nano; trimmed to millisecond precision)
|
|
# duration (ns) -> ms
|
|
local results_json n_results
|
|
results_json="$(jq -c '
|
|
[ .[]? | select(.group != "railyard-internal") | .name as $id | (.results // [])[] |
|
|
{
|
|
id: $id,
|
|
ok: .success,
|
|
at: (.timestamp | sub("\\.(?<ms>[0-9]{3})[0-9]*Z$"; ".\(.ms)Z")),
|
|
ms: ((.duration // 0) / 1000000)
|
|
}
|
|
] | { results: .[0:5000] }
|
|
' "$statuses_file")"
|
|
rm -f "$statuses_file"
|
|
n_results="$(jq '.results | length' <<<"$results_json")"
|
|
if [[ "$n_results" -eq 0 ]]; then
|
|
log "gatus has no results yet for $n_checks check(s) (first probe still pending)"
|
|
return 0
|
|
fi
|
|
|
|
local push_body push_status
|
|
push_body="$(mktemp)"
|
|
push_status="$(curl -sS -m "$HTTP_TIMEOUT_S" -o "$push_body" -w '%{http_code}' \
|
|
-X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
|
|
-d "$results_json" "$CONSOLE_URL/api/v1/watch/agent/results")"
|
|
if [[ "$push_status" != "200" ]]; then
|
|
log "pushing $n_results result(s) failed with $push_status: $(cat "$push_body")"
|
|
rm -f "$push_body"
|
|
return 2
|
|
fi
|
|
log "pushed $n_results result(s): $(cat "$push_body")"
|
|
rm -f "$push_body"
|
|
return 0
|
|
}
|
|
|
|
require_tools
|
|
if [[ "${1:-}" == "once" ]]; then
|
|
run_once
|
|
exit $?
|
|
fi
|
|
|
|
log "starting, polling every ${POLL_INTERVAL_S}s against $CONSOLE_URL"
|
|
while true; do
|
|
run_once || log "pass failed (see above), retrying in ${POLL_INTERVAL_S}s"
|
|
sleep "$POLL_INTERVAL_S"
|
|
done
|