usr/bin/custom/watchdog-monitor aktualisiert
This commit is contained in:
@@ -6,6 +6,11 @@ log_file="/var/log/watchdog.log"
|
|||||||
# Configuration file
|
# Configuration file
|
||||||
config_file="/etc/watchdog.conf"
|
config_file="/etc/watchdog.conf"
|
||||||
|
|
||||||
|
# Function to log messages with timestamp
|
||||||
|
log() {
|
||||||
|
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$log_file"
|
||||||
|
}
|
||||||
|
|
||||||
# Function to update watchdog configuration
|
# Function to update watchdog configuration
|
||||||
update_watchdog_config() {
|
update_watchdog_config() {
|
||||||
# Extract watchdog IP addresses
|
# Extract watchdog IP addresses
|
||||||
@@ -16,6 +21,7 @@ update_watchdog_config() {
|
|||||||
|
|
||||||
# Backup current configuration
|
# Backup current configuration
|
||||||
sudo cp "$config_file" "$config_file.bak"
|
sudo cp "$config_file" "$config_file.bak"
|
||||||
|
log "Backup of $config_file created successfully."
|
||||||
|
|
||||||
# Clean existing configurations
|
# Clean existing configurations
|
||||||
sudo sed -i '/ping/d' "$config_file"
|
sudo sed -i '/ping/d' "$config_file"
|
||||||
@@ -25,22 +31,22 @@ update_watchdog_config() {
|
|||||||
echo "interval = 58" | sudo tee -a "$config_file" > /dev/null
|
echo "interval = 58" | sudo tee -a "$config_file" > /dev/null
|
||||||
|
|
||||||
if [ ${#ip_array[@]} -eq 0 ]; then
|
if [ ${#ip_array[@]} -eq 0 ]; then
|
||||||
echo "No IP addresses found. Watchdog configuration cleared." >> "$log_file"
|
log "No IP addresses found. Watchdog configuration cleared."
|
||||||
return 1 # No IP addresses to configure, return 1 to continue loop
|
return 1 # No IP addresses to configure
|
||||||
else
|
else
|
||||||
# Insert IPs into config
|
# Insert IPs into config
|
||||||
for ip in "${ip_array[@]}"; do
|
for ip in "${ip_array[@]}"; do
|
||||||
echo "ping = $ip" | sudo tee -a "$config_file" > /dev/null
|
echo "ping = $ip" | sudo tee -a "$config_file" > /dev/null
|
||||||
done
|
done
|
||||||
echo "Configured watchdog to ping: ${ip_array[*]}" >> "$log_file"
|
log "Configured watchdog to ping: ${ip_array[*]}"
|
||||||
|
|
||||||
# Restart watchdog service
|
# Restart watchdog service
|
||||||
if sudo systemctl restart watchdog; then
|
if sudo systemctl restart watchdog; then
|
||||||
echo "Watchdog service restarted successfully." >> "$log_file"
|
log "Watchdog service restarted successfully."
|
||||||
return 0 # Successful restart, exit function with 0 to break main loop
|
return 0 # Successful restart
|
||||||
else
|
else
|
||||||
echo "Failed to restart watchdog service." >> "$log_file"
|
log "Failed to restart watchdog service."
|
||||||
return 1 # Failed restart, continue main loop
|
return 1 # Failed restart
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
@@ -53,20 +59,20 @@ end_time=$((start_time + 300)) # 5 minutes in seconds
|
|||||||
# Main loop
|
# Main loop
|
||||||
while true; do
|
while true; do
|
||||||
if ! update_watchdog_config; then
|
if ! update_watchdog_config; then
|
||||||
echo "IP addresses found and watchdog restarted. Exiting loop." >> "$log_file"
|
log "IP addresses found and watchdog restarted. Exiting loop."
|
||||||
break
|
break
|
||||||
else
|
else
|
||||||
echo "No IP addresses found or restart failed. Waiting for $interval seconds before checking again..." >> "$log_file"
|
log "No IP addresses found or restart failed. Waiting for $interval seconds before checking again..."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if 5 minutes have passed
|
# Check if 5 minutes have passed
|
||||||
if [ "$(date +%s)" -ge "$end_time" ]; then
|
if [ "$(date +%s)" -ge "$end_time" ]; then
|
||||||
echo "5 minutes have passed. Stopping the watchdog service." >> "$log_file"
|
log "5 minutes have passed. Stopping the watchdog service."
|
||||||
sudo systemctl stop watchdog
|
sudo systemctl stop watchdog
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Waiting for $interval seconds before checking for IP addresses again..." >> "$log_file"
|
log "Waiting for $interval seconds before checking for IP addresses again..."
|
||||||
sleep $interval
|
sleep $interval
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user