#!/usr/bin/env bash # scripts/collect_token_server_diagnostics.sh # Run a set of diagnostics against the token server and the host to gather logs and outputs # Usage: sudo ./scripts/collect_token_server_diagnostics.sh https://avanzacast-servertokens.bfzqqk.easypanel.host /tmp/token-diagnostics set -euo pipefail SERVER=${1:-} OUTDIR=${2:-/tmp/token-diagnostics} mkdir -p "$OUTDIR" if [ -z "$SERVER" ]; then echo "Usage: $0 [OUTDIR]" exit 2 fi echo "Collecting diagnostics for $SERVER into $OUTDIR" echo "=== health ===" > "$OUTDIR/health.txt" 2>&1 curl -i -m 10 "$SERVER/health" >> "$OUTDIR/health.txt" 2>&1 || true echo "=== options_preflight.txt ===" > "$OUTDIR/options_preflight.txt" 2>&1 curl -i -X OPTIONS -m 10 "$SERVER/api/session" -H "Origin: http://localhost:5176" >> "$OUTDIR/options_preflight.txt" 2>&1 || true echo "=== post_create_session.txt ===" > "$OUTDIR/post_create_session.txt" 2>&1 curl -i -X POST -m 15 "$SERVER/api/session" -H "Content-Type: application/json" -d '{"room":"diag-room","username":"diag-user"}' >> "$OUTDIR/post_create_session.txt" 2>&1 || true # If server is localhost or we are running on the same host, collect system/container info echo "=== local host checks (if run on server) ===" > "$OUTDIR/host_info.txt" 2>&1 uname -a >> "$OUTDIR/host_info.txt" 2>&1 || true ps aux --sort=-%mem | head -n 50 >> "$OUTDIR/host_info.txt" 2>&1 || true # Docker checks (if docker available) if command -v docker >/dev/null 2>&1; then echo "=== docker ps ===" >> "$OUTDIR/host_info.txt" docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' >> "$OUTDIR/host_info.txt" 2>&1 || true echo "Collecting logs for services that contain 'token' in their name (if any)" >> "$OUTDIR/host_info.txt" for c in $(docker ps --format '{{.Names}}' | grep -i token || true); do echo "--- logs for $c ---" > "$OUTDIR/docker_logs_${c}.txt" 2>&1 docker logs --tail 500 "$c" >> "$OUTDIR/docker_logs_${c}.txt" 2>&1 || true done fi # Compose logs (if docker-compose.yml present) if [ -f docker-compose.yml ] || [ -f ./docker-compose.yml ]; then echo "=== docker-compose logs (tail 500) ===" >> "$OUTDIR/host_info.txt" docker-compose logs --tail=500 >> "$OUTDIR/host_info.txt" 2>&1 || true fi # systemd logs for common service names for svc in token-server backend-api avanzacast-token; do if systemctl list-units --type=service --all | grep -q "$svc"; then echo "=== journalctl for $svc ===" > "$OUTDIR/journal_${svc}.txt" 2>&1 sudo journalctl -u "$svc" -n 500 --no-pager >> "$OUTDIR/journal_${svc}.txt" 2>&1 || true fi done # network checks ss -ltnp | head -n 200 > "$OUTDIR/ss.txt" 2>&1 || true # Collect environment files if present (sanitize secrets manually afterwards) if [ -f .env ]; then cp .env "$OUTDIR/env.copy" || true fi # Pack results tar -czf "$OUTDIR.tar.gz" -C "$(dirname "$OUTDIR")" "$(basename "$OUTDIR")" || true echo "Diagnostics collected in: $OUTDIR and archive: $OUTDIR.tar.gz" exit 0