From bf0b5e857fa14831252c5d0d929be8bdff626067 Mon Sep 17 00:00:00 2001 From: Tim Eertmoed Date: Mon, 28 Oct 2024 18:53:03 +0100 Subject: [PATCH] usr/bin/custom/watchdog aktualisiert --- usr/bin/custom/watchdog | 90 ++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 33 deletions(-) diff --git a/usr/bin/custom/watchdog b/usr/bin/custom/watchdog index b457f0a..316b9f8 100644 --- a/usr/bin/custom/watchdog +++ b/usr/bin/custom/watchdog @@ -3,44 +3,68 @@ # Log file log_file="/var/log/watchdog.log" -# Extract watchdog IP addresses -watchdog=$(sudo cat /var/lib/dhcp/* | grep -a "option watchdog" | tail -1 | \ -awk '{for (i=3; i<=NF; i++) printf "%s ", $i}' | tr -d '";') -IFS=' ' read -r -a ip_array <<< "$watchdog" - # Configuration file config_file="/etc/watchdog.conf" -# Backup current configuration -if sudo cp "$config_file" "$config_file.bak"; then - echo "Backup of $config_file created successfully." >> "$log_file" -else - echo "Failed to create backup of $config_file." >> "$log_file" -fi +# Function to update watchdog configuration +update_watchdog_config() { + # Extract watchdog IP addresses + watchdog=$(sudo cat /var/lib/dhcp/* | grep -a "option watchdog" | tail -1 | \ + awk '{for (i=3; i<=NF; i++) printf "%s ", $i}' | tr -d '";') + + IFS=' ' read -r -a ip_array <<< "$watchdog" -# Clean existing configurations -sudo sed -i '/ping/d' "$config_file" -sudo sed -i '/^interval/d' "$config_file" + # Backup current configuration + if sudo cp "$config_file" "$config_file.bak"; then + echo "Backup of $config_file created successfully." >> "$log_file" + else + echo "Failed to create backup of $config_file." >> "$log_file" + fi -# Set interval -echo "interval = 60" | sudo tee -a "$config_file" > /dev/null + # Clean existing configurations + sudo sed -i '/ping/d' "$config_file" + sudo sed -i '/^interval/d' "$config_file" -if [ ${#ip_array[@]} -eq 0 ]; then - echo "No IP addresses found. Watchdog configuration cleared." >> "$log_file" - sudo systemctl stop watchdog -else - for ip in "${ip_array[@]}"; do - echo "ping = $ip" | sudo tee -a "$config_file" > /dev/null - done - echo "Configured watchdog to ping: ${ip_array[*]}" >> "$log_file" -fi + # Set interval + echo "interval = 60" | sudo tee -a "$config_file" > /dev/null -# Restart watchdog service -if sudo systemctl restart watchdog; then - echo "Watchdog service restarted successfully." >> "$log_file" -else - echo "Failed to restart watchdog service." >> "$log_file" -fi + if [ ${#ip_array[@]} -eq 0 ]; then + echo "No IP addresses found. Watchdog configuration cleared." >> "$log_file" + else + for ip in "${ip_array[@]}"; do + echo "ping = $ip" | sudo tee -a "$config_file" > /dev/null + done + echo "Configured watchdog to ping: ${ip_array[*]}" >> "$log_file" + fi -# Version 1.1: -# Created 2024 by Tim Eertmoed @ WiS IT-Solutions GmbH, Germany to work on Raspian as custom pxe init script. + # Restart watchdog service + if sudo systemctl restart watchdog; then + echo "Watchdog service restarted successfully." >> "$log_file" + else + echo "Failed to restart watchdog service." >> "$log_file" + fi +} + +# Main loop +start_time=$(date +%s) +end_time=$((start_time + 300)) # 5 minutes in seconds +interval=30 # Initial interval of 30 seconds + +while true; do + update_watchdog_config + + current_time=$(date +%s) + + # Check if 5 minutes have passed + if [ "$current_time" -ge "$end_time" ]; then + echo "5 minutes have passed. Stopping the watchdog service." >> "$log_file" + sudo systemctl stop watchdog + break # Exit the loop + fi + + echo "Waiting for $interval seconds before checking for IP addresses again..." >> "$log_file" + sleep $interval +done + +# Version 1.4: +# Created 2024 by Tim Eertmoed @ WiS IT-Solutions GmbH, Germany to work on Raspian as custom PXE init script.