feat(proxmox): check_vm as cronjob

Signed-off-by: Tuan-Dat Tran <tuan-dat.tran@tudattr.dev>
This commit is contained in:
Tuan-Dat Tran
2025-09-02 19:52:49 +02:00
parent 7aa16f3207
commit a1da69ac98
2 changed files with 25 additions and 18 deletions

View File

@@ -1,11 +1,10 @@
#!/bin/bash
# Configuration
VM_ID=303
TARGET_IP="192.168.20.36" # Replace with the IP of your VM
VM_ID=$1
TARGET_IP=$2
PORT=22
CHECK_INTERVAL=300 # 5 minutes in seconds
LOG_FILE="/var/log/vm_monitor.log"
LOG_FILE="/var/log/vm_monitor_${VM_ID}.log"
# Function to log messages
log_message() {
@@ -65,19 +64,12 @@ restart_vm() {
log_message "VM $VM_ID has been restarted."
}
# Main loop
# Main execution
log_message "Starting monitoring of VM $VM_ID on port $PORT..."
log_message "Press Ctrl+C to exit."
while true; do
# Check if port 22 is open
if ! check_port; then
restart_vm
else
log_message "Port $PORT is reachable. VM is running normally."
fi
# Wait for the next check
log_message "Sleeping for $CHECK_INTERVAL seconds..."
sleep $CHECK_INTERVAL
done
# Check if port 22 is open
if ! check_port; then
restart_vm
else
log_message "Port $PORT is reachable. VM is running normally."
fi