#!/usr/bin/env bash set -euo pipefail # Simple TCP healthcheck against the first server in NATS_SERVERS # Accepts schemes nats:// or tls:// first_server=$(awk '{print $1}' <<< "${NATS_SERVERS:-}") if [[ -z "${first_server}" ]]; then echo "NATS_SERVERS not set" >&2 exit 1 fi # Extract host and port scheme="${first_server%%://*}" rest="${first_server#*://}" host="${rest%:*}" port="${rest##*:}" # If rest still contains '/', strip path host="${host%%/*}" port="${port%%/*}" : "${port:=4222}" # Try TCP connect (bash /dev/tcp) timeout 3 bash -c "cat < /dev/null > /dev/tcp/${host}/${port}" 2>/dev/null || { echo "Cannot connect to ${host}:${port}" >&2 exit 1 } # Optionally add more checks later (e.g., NATS /varz on 8222) exit 0