dev #1

Merged
alientim merged 145 commits from dev into main 2025-10-12 13:44:14 +02:00
Showing only changes of commit 41f244cc59 - Show all commits

View File

@@ -1,9 +1,20 @@
#!/bin/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)
# ============================================================================
LOGFILE="/var/log/rpi-$(date '+%Y%m%d%H%M%S').log" LOG_DIR="/var/log"
LOGFILE="$LOG_DIR/rpi-$(date '+%Y%m%d%H%M%S').log"
# Intervall aus DB (Sekunden) abrufen # Alte Logfiles löschen (älter als 30 Tage)
SLEEP=$(python3 - <<END find "$LOG_DIR" -type f -name "rpi-*.log" -mtime +30 -delete
# Intervall aus DB abrufen
SLEEP=$(python3 - <<'END'
import sqlite3 import sqlite3
conn = sqlite3.connect("/srv/poe_manager/sqlite.db") conn = sqlite3.connect("/srv/poe_manager/sqlite.db")
row = conn.execute("SELECT value FROM settings WHERE key='check_interval'").fetchone() row = conn.execute("SELECT value FROM settings WHERE key='check_interval'").fetchone()
@@ -12,8 +23,7 @@ print(row[0] if row else 300)
END END
) )
# Umrechnung falls nötig SLEEP=${SLEEP:-300}
SLEEP=${SLEEP:-300} # default 300 Sekunden
function disable_poe() { function disable_poe() {
local switch_ip=$1 local switch_ip=$1
@@ -23,7 +33,6 @@ function disable_poe() {
expect <<EOF expect <<EOF
set timeout 5 set timeout 5
spawn ssh $username@$switch_ip spawn ssh $username@$switch_ip
expect { expect {
"assword:" { send "$password\r"; exp_continue } "assword:" { send "$password\r"; exp_continue }
"Press any key" { send "\r"; exp_continue } "Press any key" { send "\r"; exp_continue }
@@ -40,8 +49,7 @@ expect "(config)#"
send "exit\r" send "exit\r"
expect "#" expect "#"
send "exit\r" send "exit\r"
expect ">" expect ">"; send "exit\r"
send "exit\r"
expect "Do you want to log out (y/n)?" { send "y\r" } expect "Do you want to log out (y/n)?" { send "y\r" }
expect eof expect eof
EOF EOF
@@ -55,7 +63,6 @@ function enable_poe() {
expect <<EOF expect <<EOF
set timeout 5 set timeout 5
spawn ssh $username@$switch_ip spawn ssh $username@$switch_ip
expect { expect {
"assword:" { send "$password\r"; exp_continue } "assword:" { send "$password\r"; exp_continue }
"Press any key" { send "\r"; exp_continue } "Press any key" { send "\r"; exp_continue }
@@ -72,30 +79,31 @@ expect "(config)#"
send "exit\r" send "exit\r"
expect "#" expect "#"
send "exit\r" send "exit\r"
expect ">" expect ">"; send "exit\r"
send "exit\r"
expect "Do you want to log out (y/n)?" { send "y\r" } expect "Do you want to log out (y/n)?" { send "y\r" }
expect eof expect eof
EOF EOF
} }
echo "" > $LOGFILE echo "" > "$LOGFILE"
while true; do while true; do
echo "--------------------------------------------------------------------" >> $LOGFILE echo "--------------------------------------------------------------------" >> "$LOGFILE"
IP_FILE=$(python3 /srv/poe_manager/generate_ips.py) 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
while IFS=: read -r rpi_ip dev_name switch_ip switch_hostname switch_port switch_user switch_pass; do if ping -c 1 -W 2 "$rpi_ip" &> /dev/null; then
ping -c 1 -W 2 "$rpi_ip" &> /dev/null echo "$(date '+%Y-%m-%d %H:%M:%S') $dev_name ist erreichbar!" >> "$LOGFILE"
if [ $? -ne 0 ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') $dev_name ist nicht erreichbar!" >> $LOGFILE
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
else else
echo "$(date '+%Y-%m-%d %H:%M:%S') $dev_name ist erreichbar!" >> $LOGFILE 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" ]; 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 fi
done < "$IP_FILE" done
rm -f "$IP_FILE" sleep "$SLEEP"
sleep $SLEEP
done done