monitor/check_fds.sh
2025-11-10 14:31:17 +00:00

73 lines
1.9 KiB
Bash
Executable file

#!/bin/bash
# Pfad zur Textdatei mit Hostname-IP-Paaren (pro Zeile: hostname ip)
HOST_FILE="hosts.conf"
# Pfad zum mail_fds.py-Skript
MAIL_SCRIPT="mail_fds.py"
# Pfad zur Statusdatei
STATUS_FILE="/var/www/html/status.txt"
# Funktion zum Prüfen einer IP und Schreiben des Status
check_ip() {
local hostname="$1"
local ip="$2"
local success_count=0
# echo "Prüfe $hostname ($ip) ..."
for i in {1..3}; do
if ping -c 1 -W 2 "$ip" &> /dev/null; then
((success_count++))
else
echo " Ping $i an $ip fehlgeschlagen."
fi
done
local status="online"
if [ "$success_count" -lt 2 ]; then
status="offline"
echo " Mehr als ein Ping fehlgeschlagen für $hostname ($ip). Führe $MAIL_SCRIPT aus."
./"$MAIL_SCRIPT" --subject "Ping-Fehler bei $hostname $ip" --credentials credentials.conf
else
echo " $hostname ($ip) ist erreichbar."
fi
# Status in status.conf schreiben/aktualisieren
local now=$(date +"%Y-%m-%d %H:%M")
awk -v h="$hostname" -v i="$ip" -v s="$status" -v d="$now" '
BEGIN { found=0 }
{
if ($1 == h && $2 == i) {
print h, i, s, d;
found=1;
} else {
print $0;
}
}
END {
if (!found && h != "" && i != "") {
print h, i, s, d;
}
}' "$STATUS_FILE" > "$STATUS_FILE.tmp" && mv "$STATUS_FILE.tmp" "$STATUS_FILE"
}
# Hauptskript
echo "Pruefung: $(date)"
if [ ! -f "$HOST_FILE" ]; then
echo "Fehler: Datei $HOST_FILE nicht gefunden."
exit 1
fi
if [ ! -f "$MAIL_SCRIPT" ]; then
echo "Fehler: Skript $MAIL_SCRIPT nicht gefunden."
exit 1
fi
# Statusdatei anlegen, falls nicht vorhanden
touch "$STATUS_FILE"
while read -r line; do
hostname=$(echo "$line" | awk '{print $1}')
ip=$(echo "$line" | awk '{print $2}')
if [ -n "$hostname" ] && [ -n "$ip" ]; then
check_ip "$hostname" "$ip"
fi
done < "$HOST_FILE"