usr/local/bin/custom/poe.sh aktualisiert
This commit is contained in:
@@ -1,9 +1,20 @@
|
||||
#!/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
|
||||
SLEEP=$(python3 - <<END
|
||||
# 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()
|
||||
@@ -12,8 +23,7 @@ print(row[0] if row else 300)
|
||||
END
|
||||
)
|
||||
|
||||
# Umrechnung falls nötig
|
||||
SLEEP=${SLEEP:-300} # default 300 Sekunden
|
||||
SLEEP=${SLEEP:-300}
|
||||
|
||||
function disable_poe() {
|
||||
local switch_ip=$1
|
||||
@@ -23,7 +33,6 @@ function disable_poe() {
|
||||
expect <<EOF
|
||||
set timeout 5
|
||||
spawn ssh $username@$switch_ip
|
||||
|
||||
expect {
|
||||
"assword:" { send "$password\r"; exp_continue }
|
||||
"Press any key" { send "\r"; exp_continue }
|
||||
@@ -40,8 +49,7 @@ expect "(config)#"
|
||||
send "exit\r"
|
||||
expect "#"
|
||||
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
|
||||
@@ -55,7 +63,6 @@ function enable_poe() {
|
||||
expect <<EOF
|
||||
set timeout 5
|
||||
spawn ssh $username@$switch_ip
|
||||
|
||||
expect {
|
||||
"assword:" { send "$password\r"; exp_continue }
|
||||
"Press any key" { send "\r"; exp_continue }
|
||||
@@ -72,30 +79,31 @@ expect "(config)#"
|
||||
send "exit\r"
|
||||
expect "#"
|
||||
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
|
||||
echo "" > "$LOGFILE"
|
||||
|
||||
while true; do
|
||||
echo "--------------------------------------------------------------------" >> $LOGFILE
|
||||
IP_FILE=$(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
|
||||
ping -c 1 -W 2 "$rpi_ip" &> /dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') $dev_name ist nicht erreichbar!" >> $LOGFILE
|
||||
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 1 -W 2 "$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" ]; 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
|
||||
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
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') $dev_name ist erreichbar!" >> $LOGFILE
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') $dev_name PoE auf Port $switch_port am Switch $switch_hostname aktiviert." >> "$LOGFILE"
|
||||
fi
|
||||
fi
|
||||
done < "$IP_FILE"
|
||||
rm -f "$IP_FILE"
|
||||
sleep $SLEEP
|
||||
done
|
||||
sleep "$SLEEP"
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user