check-host/check.sh
2025-07-14 17:56:31 +02:00

35 lines
931 B
Bash

#!/bin/sh
HOST="${HOST_TO_CHECK:-example.com}"
INTERVAL="${INTERVAL_SECONDS:-300}"
LOGFILE="/logs/${HOST}.log"
mkdir -p /logs
echo "Monitoring $HOST every $INTERVAL seconds..."
while true; do
OUTPUT=$(ping -c 1 -W 2 "$HOST" 2>&1)
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
if echo "$OUTPUT" | grep -q "bad address"; then
REASON="DNS resolution failed"
elif echo "$OUTPUT" | grep -q "Network is unreachable"; then
REASON="No route to network"
elif echo "$OUTPUT" | grep -q "ping: permission denied"; then
REASON="ICMP not permitted (try running as root or with CAP_NET_RAW)"
elif echo "$OUTPUT" | grep -q "100% packet loss"; then
REASON="Timed out (no response)"
else
REASON="Unknown error: $OUTPUT"
fi
echo "[$TIMESTAMP] Host unreachable: $HOST — Reason: $REASON" | tee -a "$LOGFILE"
fi
sleep "$INTERVAL"
done