From 927f6878632162d4e0eae9edf61b559729e885a6 Mon Sep 17 00:00:00 2001 From: ergosteur <1992147+ergosteur@users.noreply.github.com> Date: Thu, 6 Aug 2026 15:44:49 +0000 Subject: [PATCH] Add Proxmox VM and LXC watchdog scripts Cron-driven liveness checks that ping each guest, restart it via qm/pct if unreachable, and log plus email the outcome. Guest lists are configured in the associative array at the top of each script. Co-Authored-By: Claude Opus 5 --- lxc-watchdog.sh | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ vm-watchdog.sh | 59 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100755 lxc-watchdog.sh create mode 100755 vm-watchdog.sh diff --git a/lxc-watchdog.sh b/lxc-watchdog.sh new file mode 100755 index 0000000..2ce2e5e --- /dev/null +++ b/lxc-watchdog.sh @@ -0,0 +1,74 @@ +#!/bin/bash +# +# WatchDog script for Proxmox LXC Containers +# v2.0 +# +# Install using `crontab -e` like this: * * * * * /root/lxc_watchdog.sh +# + +# Configurable Settings +declare -A Containers=( + ["AdGuardLXC"]="100 192.168.31.10" + ["MainLXC"]="101 192.168.31.100" +) + +logFile="/var/log/proxmox_lxc_watchdog.log" +email="your_email@example.com" + +# Log function +log () { + output="[LXC WatchDog] $(date '+%Y-%m-%d %H:%M:%S') $1" + echo "$output" >> "$logFile" + logger "$output" +} + +# Send email alert +sendAlert () { + subject="[Proxmox LXC WatchDog] $1" + body="$2" + echo -e "$body" | mail -s "$subject" "$email" +} + +# Check LXC Container function +checkContainer () { + local containerName=$1 + local containerId=$2 + local containerIp=$3 + + if /usr/bin/ping -c 1 "$containerIp" &> /dev/null; then + local containerStatus + containerStatus=$(/usr/sbin/pct status "$containerId" | awk '{print $2}') + if [[ "$containerStatus" == "running" ]]; then + local containerUptime + containerUptime=$(cat /proc/uptime | awk '{print $1}' | cut -d. -f1) + log "$containerName ($containerIp) is alive. Uptime: $containerUptime seconds." + else + log "$containerName ($containerIp) is not running. Attempting to start..." + if /usr/sbin/pct start "$containerId"; then + log "$containerName started successfully." + sendAlert "$containerName Started" "The LXC container '$containerName' was started by the watchdog script." + else + log "Failed to start $containerName. Manual intervention required." + sendAlert "$containerName Start Failed" "The LXC container '$containerName' failed to start. Please check Proxmox immediately." + fi + fi + else + log "$containerName ($containerIp) is down (no ping). Attempting to restart..." + if /usr/sbin/pct stop "$containerId" && /usr/sbin/pct start "$containerId"; then + log "$containerName restarted successfully." + sendAlert "$containerName Restarted" "The LXC container '$containerName' was restarted by the watchdog script." + else + log "Failed to restart $containerName. Manual intervention required." + sendAlert "$containerName Restart Failed" "The LXC container '$containerName' failed to restart. Please check Proxmox immediately." + fi + fi +} + +# Main Logic +for containerName in "${!Containers[@]}"; do + read -r containerId containerIp <<< "${Containers[$containerName]}" + checkContainer "$containerName" "$containerId" "$containerIp" & +done + +# Wait for all background checks to complete +wait diff --git a/vm-watchdog.sh b/vm-watchdog.sh new file mode 100755 index 0000000..58d98f4 --- /dev/null +++ b/vm-watchdog.sh @@ -0,0 +1,59 @@ +#!/bin/bash +# +# WatchDog script for Proxmox VMs +# v2.0 +# +# Install using `crontab -e` like this: * * * * * /root/watchdog.sh +# +# Configurable Settings +declare -A VMs=( + ["pfsense"]="2801 10.20.28.1" +) + +logFile="/var/log/proxmox_watchdog.log" +email="alerts@example.com" + +# Log function +log () { + output="[WatchDog] $(date '+%Y-%m-%d %H:%M:%S') $1" + echo "$output" >> "$logFile" + logger "$output" +} + +# Send email alert +sendAlert () { + subject="[Proxmox WatchDog] $1" + body="$2" + echo -e "$body" | mail -s "$subject" "$email" +} + +# Check VM function +checkVM () { + local vmName=$1 + local vmId=$2 + local vmIp=$3 + + if /usr/bin/ping -c 1 "$vmIp" &> /dev/null; then + local vmUptime + vmUptime=$(/usr/sbin/qm status "$vmId" -verbose | grep uptime | cut -f2 -d' ') + log "$vmName ($vmIp) is alive. Uptime: $vmUptime seconds." + else + log "$vmName ($vmIp) is down. Attempting restart..." + if /usr/sbin/qm stop "$vmId" && /usr/sbin/qm start "$vmId"; then + log "$vmName restarted successfully." + sendAlert "$vmName Restarted" "The VM '$vmName' was restarted by the watchdog script." + else + log "Failed to restart $vmName. Manual intervention required." + sendAlert "$vmName Restart Failed" "The VM '$vmName' failed to restart. Please check Proxmox immediately." + fi + fi +} + +# Main Logic +for vmName in "${!VMs[@]}"; do + read -r vmId vmIp <<< "${VMs[$vmName]}" + checkVM "$vmName" "$vmId" "$vmIp" & +done + +# Wait for all background checks to complete +wait