#!/usr/bin/env bash # Liveness check for the CoAP server. Exits 0 if healthy, 1 if not. # ./healthcheck.sh quiet, for cron/systemd/monitoring # ./healthcheck.sh -v print what it found # Retargets with COAP_HOST / COAP_PORT. CLIENT=${CLIENT:-$(dirname "$0")/target/release/client} verbose=false; [[ $1 == -v ]] && verbose=true say() { $verbose && echo "$@"; } host=${COAP_HOST:-127.0.0.1}; port=${COAP_PORT:-5683} [[ -x $CLIENT ]] || { echo "healthcheck: no client binary at $CLIENT (cargo build --release)"; exit 1; } reply=$("$CLIENT" get /health 2>&1) # A bound socket proves nothing on UDP - the round trip is the real test. if [[ $reply != "<- 2.05 "* ]]; then echo "UNHEALTHY: coap://$host:$port/health -> ${reply:-no reply}" exit 1 fi body=${reply#<- 2.05 } if [[ $body != *'"status":"ok"'* ]]; then echo "UNHEALTHY: unexpected body from coap://$host:$port/health -> $body" exit 1 fi uptime=$(sed -n 's/.*"uptime":\([0-9]*\).*/\1/p' <<<"$body") say "HEALTHY: coap://$host:$port up ${uptime}s (code 2.05)" exit 0