From 859759a253e0b8643c92cc77fe3f6f1cb4b11e29 Mon Sep 17 00:00:00 2001 From: Tim Eertmoed Date: Mon, 28 Oct 2024 20:10:09 +0100 Subject: [PATCH] usr/bin/custom/watchdog-monitor aktualisiert --- usr/bin/custom/watchdog-monitor | 50 +++++++++++---------------------- 1 file changed, 16 insertions(+), 34 deletions(-) diff --git a/usr/bin/custom/watchdog-monitor b/usr/bin/custom/watchdog-monitor index 91b2335..801ae21 100644 --- a/usr/bin/custom/watchdog-monitor +++ b/usr/bin/custom/watchdog-monitor @@ -11,16 +11,11 @@ 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 '";') - - # Split the watchdog IPs into an array - IFS=',' read -r -a ip_array <<< "$watchdog" + + IFS=', ' read -r -a ip_array <<< "$watchdog" # Split IPs by comma or space # 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 + sudo cp "$config_file" "$config_file.bak" # Clean existing configurations sudo sed -i '/ping/d' "$config_file" @@ -29,48 +24,35 @@ update_watchdog_config() { # Set interval echo "interval = 58" | sudo tee -a "$config_file" > /dev/null - # Check if IP addresses are found and add them to the config - if [ ${#ip_array[@]} -gt 0 ]; then + if [ ${#ip_array[@]} -eq 0 ]; then + echo "No IP addresses found. Watchdog configuration cleared." >> "$log_file" + return 0 # No IP addresses to configure + else + # Insert IPs into config for ip in "${ip_array[@]}"; do echo "ping = $ip" | sudo tee -a "$config_file" > /dev/null - echo "Configured watchdog to ping: $ip" >> "$log_file" done + echo "Configured watchdog to ping: ${ip_array[*]}" >> "$log_file" - # Restart watchdog service since IP addresses were found - if sudo systemctl restart watchdog; then - echo "Watchdog service restarted successfully." >> "$log_file" - else - echo "Failed to restart watchdog service." >> "$log_file" - fi - return 1 # Indicate that IP addresses were found + # Restart watchdog service + sudo systemctl restart watchdog + return 1 # IP addresses were configured fi - - echo "No IP addresses found. Watchdog configuration cleared." >> "$log_file" - return 0 # Indicate that no IP addresses were found } - -# 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 - # Check if IP addresses are found and watchdog is updated if update_watchdog_config; then echo "IP addresses found and watchdog restarted. Exiting loop." >> "$log_file" - break # Exit the loop if IP addresses were found + break else echo "No IP addresses found. Waiting for $interval seconds before checking again..." >> "$log_file" fi - current_time=$(date +%s) - - # Check if 5 minutes have passed - if [ "$current_time" -ge "$end_time" ]; then + # Check time limit + if [ "$(date +%s)" -ge "$end_time" ]; then echo "5 minutes have passed. Stopping the watchdog service." >> "$log_file" sudo systemctl stop watchdog - break # Exit the loop + break fi echo "Waiting for $interval seconds before checking for IP addresses again..." >> "$log_file"