.
This commit is contained in:
commit
b181682b20
11
Dockerfile
Normal file
11
Dockerfile
Normal file
@ -0,0 +1,11 @@
|
||||
FROM alpine:latest
|
||||
|
||||
RUN apk add --no-cache iputils coreutils tzdata
|
||||
|
||||
ENV TZ=Europe/Rome
|
||||
|
||||
WORKDIR /app
|
||||
COPY check.sh .
|
||||
RUN chmod +x check.sh
|
||||
|
||||
CMD ["./check.sh"]
|
||||
34
check.sh
Normal file
34
check.sh
Normal file
@ -0,0 +1,34 @@
|
||||
#!/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
|
||||
9
docker-compose.yml
Normal file
9
docker-compose.yml
Normal file
@ -0,0 +1,9 @@
|
||||
services:
|
||||
host-checker:
|
||||
build: .
|
||||
environment:
|
||||
- HOST_TO_CHECK=psql01.poloinformatico.it
|
||||
- INTERVAL_SECONDS=120
|
||||
volumes:
|
||||
- ./logs:/logs
|
||||
restart: always
|
||||
Loading…
Reference in New Issue
Block a user