#!/bin/sh # healthcheck.sh — RouterOS container health check for mdns-reflector # # Returns 0 (healthy) if: # 1. Health file exists and is younger than 30 seconds # 2. mdns-repeater process is running # 3. At least one interface socket is alive # # Returns 1 (unhealthy) otherwise. HEALTH_FILE="/tmp/mdns-health" # 1. Check health file exists if [ ! -f "$HEALTH_FILE" ]; then echo "unhealthy: no health file" exit 1 fi # 2. Check health file freshness (must be < 30s old) now=$(date +%s) file_ts=$(awk -F= '/^ts=/{print $2}' "$HEALTH_FILE" 2>/dev/null) if [ -z "$file_ts" ]; then echo "unhealthy: no timestamp in health file" exit 1 fi age=$((now - file_ts)) if [ "$age" -gt 30 ]; then echo "unhealthy: health file stale (${age}s old)" exit 1 fi # 3. Check process is running pid=$(pidof mdns-repeater 2>/dev/null) if [ -z "$pid" ]; then echo "unhealthy: mdns-repeater not running" exit 1 fi # 4. Check at least one interface has a socket (fd4 or fd6 open) n_ifaces=$(awk -F= '/^n_ifaces=/ {print $2}' "$HEALTH_FILE" 2>/dev/null) if [ -z "$n_ifaces" ] || [ "$n_ifaces" -eq 0 ]; then echo "unhealthy: no interfaces configured" exit 1 fi # 5. Verify sockets are actually bound to port 5353 ss -lupn 2>/dev/null | grep -q ":5353" if [ $? -ne 0 ]; then echo "unhealthy: no UDP socket on port 5353" exit 1 fi echo "healthy: ${n_ifaces} interfaces, age=${age}s" exit 0