stable #2
@@ -3,44 +3,68 @@
|
|||||||
# Log file
|
# Log file
|
||||||
log_file="/var/log/watchdog.log"
|
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
|
# Configuration file
|
||||||
config_file="/etc/watchdog.conf"
|
config_file="/etc/watchdog.conf"
|
||||||
|
|
||||||
# Backup current configuration
|
# Function to update watchdog configuration
|
||||||
if sudo cp "$config_file" "$config_file.bak"; then
|
update_watchdog_config() {
|
||||||
echo "Backup of $config_file created successfully." >> "$log_file"
|
# Extract watchdog IP addresses
|
||||||
else
|
watchdog=$(sudo cat /var/lib/dhcp/* | grep -a "option watchdog" | tail -1 | \
|
||||||
echo "Failed to create backup of $config_file." >> "$log_file"
|
awk '{for (i=3; i<=NF; i++) printf "%s ", $i}' | tr -d '";')
|
||||||
fi
|
|
||||||
|
|
||||||
# Clean existing configurations
|
IFS=' ' read -r -a ip_array <<< "$watchdog"
|
||||||
sudo sed -i '/ping/d' "$config_file"
|
|
||||||
sudo sed -i '/^interval/d' "$config_file"
|
|
||||||
|
|
||||||
# Set interval
|
# Backup current configuration
|
||||||
echo "interval = 60" | sudo tee -a "$config_file" > /dev/null
|
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
|
||||||
|
|
||||||
if [ ${#ip_array[@]} -eq 0 ]; then
|
# Clean existing configurations
|
||||||
echo "No IP addresses found. Watchdog configuration cleared." >> "$log_file"
|
sudo sed -i '/ping/d' "$config_file"
|
||||||
sudo systemctl stop watchdog
|
sudo sed -i '/^interval/d' "$config_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
|
|
||||||
|
|
||||||
# Restart watchdog service
|
# Set interval
|
||||||
if sudo systemctl restart watchdog; then
|
echo "interval = 60" | sudo tee -a "$config_file" > /dev/null
|
||||||
echo "Watchdog service restarted successfully." >> "$log_file"
|
|
||||||
else
|
|
||||||
echo "Failed to restart watchdog service." >> "$log_file"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Version 1.1:
|
if [ ${#ip_array[@]} -eq 0 ]; then
|
||||||
# Created 2024 by Tim Eertmoed @ WiS IT-Solutions GmbH, Germany to work on Raspian as custom pxe init script.
|
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
|
||||||
|
|
||||||
|
# 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.
|
||||||
|
|||||||
Reference in New Issue
Block a user