110 lines
3.2 KiB
Bash
110 lines
3.2 KiB
Bash
#!/bin/bash
|
|
# ============================================================================
|
|
# PoE Device Check Script
|
|
# - prüft Erreichbarkeit von Geräten
|
|
# - startet PoE-Port bei Ausfall neu
|
|
# - loggt Ereignisse
|
|
# - löscht alte Logfiles (>30 Tage)
|
|
# ============================================================================
|
|
|
|
LOG_DIR="/var/log"
|
|
LOGFILE="$LOG_DIR/rpi-$(date '+%Y%m%d%H%M%S').log"
|
|
|
|
# Alte Logfiles löschen (älter als 30 Tage)
|
|
find "$LOG_DIR" -type f -name "rpi-*.log" -mtime +30 -delete
|
|
|
|
# Intervall aus DB abrufen
|
|
SLEEP=$(python3 - <<'END'
|
|
import sqlite3
|
|
conn = sqlite3.connect("/srv/poe_manager/sqlite.db")
|
|
row = conn.execute("SELECT value FROM settings WHERE key='check_interval'").fetchone()
|
|
conn.close()
|
|
print(row[0] if row else 300)
|
|
END
|
|
)
|
|
|
|
SLEEP=${SLEEP:-300}
|
|
|
|
function disable_poe() {
|
|
local switch_ip=$1
|
|
local switch_port=$2
|
|
local username=$3
|
|
local password=$4
|
|
expect <<EOF
|
|
set timeout 5
|
|
spawn ssh $username@$switch_ip
|
|
expect {
|
|
"assword:" { send "$password\r"; exp_continue }
|
|
"Press any key" { send "\r"; exp_continue }
|
|
-re ".*> $" { }
|
|
}
|
|
send "configure terminal\r"
|
|
expect "(config)#"
|
|
send "interface $switch_port\r"
|
|
expect "(eth-$switch_port)#"
|
|
send "no power-over-ethernet\r"
|
|
expect "(eth-$switch_port)#"
|
|
send "exit\r"
|
|
expect "(config)#"
|
|
send "exit\r"
|
|
expect "#"
|
|
send "exit\r"
|
|
expect ">"; send "exit\r"
|
|
expect "Do you want to log out (y/n)?" { send "y\r" }
|
|
expect eof
|
|
EOF
|
|
}
|
|
|
|
function enable_poe() {
|
|
local switch_ip=$1
|
|
local switch_port=$2
|
|
local username=$3
|
|
local password=$4
|
|
expect <<EOF
|
|
set timeout 5
|
|
spawn ssh $username@$switch_ip
|
|
expect {
|
|
"assword:" { send "$password\r"; exp_continue }
|
|
"Press any key" { send "\r"; exp_continue }
|
|
-re ".*> $" { }
|
|
}
|
|
send "configure terminal\r"
|
|
expect "(config)#"
|
|
send "interface $switch_port\r"
|
|
expect "(eth-$switch_port)#"
|
|
send "power-over-ethernet\r"
|
|
expect "(eth-$switch_port)#"
|
|
send "exit\r"
|
|
expect "(config)#"
|
|
send "exit\r"
|
|
expect "#"
|
|
send "exit\r"
|
|
expect ">"; send "exit\r"
|
|
expect "Do you want to log out (y/n)?" { send "y\r" }
|
|
expect eof
|
|
EOF
|
|
}
|
|
|
|
echo "" > "$LOGFILE"
|
|
|
|
while true; do
|
|
echo "--------------------------------------------------------------------" >> "$LOGFILE"
|
|
python3 /srv/poe_manager/generate_ips.py | while IFS=: read -r rpi_ip dev_name switch_ip switch_hostname switch_port switch_user switch_pass; do
|
|
if ping -c 2 -W 1 "$rpi_ip" &> /dev/null; then
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') $dev_name ist erreichbar!" >> "$LOGFILE"
|
|
else
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') $dev_name ist nicht erreichbar!" >> "$LOGFILE"
|
|
|
|
# Nur PoE neu starten, wenn Port vorhanden ist
|
|
if [ -n "$switch_port" ] && [ "$switch_port" != "None" ]; then
|
|
disable_poe "$switch_ip" "$switch_port" "$switch_user" "$switch_pass"
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') $dev_name PoE auf Port $switch_port am Switch $switch_hostname deaktiviert." >> "$LOGFILE"
|
|
sleep 2
|
|
enable_poe "$switch_ip" "$switch_port" "$switch_user" "$switch_pass"
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') $dev_name PoE auf Port $switch_port am Switch $switch_hostname aktiviert." >> "$LOGFILE"
|
|
fi
|
|
fi
|
|
done
|
|
sleep "$SLEEP"
|
|
done
|