#!/bin/bash

# VM Health Monitor Script
# This script checks if a web service is responding and reboots the VM if it's not

# Configuration
TARGET_URL="http://www.your-domain.com"
VM_ID="105"
TIMEOUT=10
MAX_RETRIES=3
LOG_FILE="/var/log/vm-health-monitor.log"

# Function to log messages with timestamp
log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}

# Function to check if URL is responding
check_url() {
    local url="$1"
    local timeout="$2"
    
    # Use curl to check if the URL responds
    if curl -s --connect-timeout "$timeout" --max-time "$timeout" "$url" > /dev/null 2>&1; then
        return 0  # Success
    else
        return 1  # Failure
    fi
}

# Function to reboot VM
reboot_vm() {
    local vm_id="$1"
    
    log_message "Attempting to reboot VM $vm_id"
    
    # Stop the VM first
    if qm stop "$vm_id"; then
        log_message "VM $vm_id stopped successfully"
        sleep 30
        
        # Start the VM
        if qm start "$vm_id"; then
            log_message "VM $vm_id started successfully"
            return 0
        else
            log_message "ERROR: Failed to start VM $vm_id"
            return 1
        fi
    else
        log_message "ERROR: Failed to stop VM $vm_id"
        return 1
    fi
}

# Main monitoring function
main() {
    log_message "Starting health check for $TARGET_URL"
    
    local retry_count=0
    
    # Retry loop
    while [ $retry_count -lt $MAX_RETRIES ]; do
        if check_url "$TARGET_URL" "$TIMEOUT"; then
            log_message "Service is responding normally (attempt $((retry_count + 1))/$MAX_RETRIES)"
            exit 0
        else
            retry_count=$((retry_count + 1))
            log_message "Service not responding (attempt $retry_count/$MAX_RETRIES)"
            
            if [ $retry_count -lt $MAX_RETRIES ]; then
                sleep 5
            fi
        fi
    done
    
    # If we reach here, all retries failed
    log_message "Service failed to respond after $MAX_RETRIES attempts. Initiating VM reboot."
    
    if reboot_vm "$VM_ID"; then
        log_message "VM reboot completed successfully"
    else
        log_message "VM reboot failed - manual intervention required"
        exit 1
    fi
}

# Check if running as root (required for qm commands)
if [ "$EUID" -ne 0 ]; then
    echo "This script must be run as root (required for Proxmox qm commands)"
    exit 1
fi

# Create log directory if it doesn't exist
mkdir -p "$(dirname "$LOG_FILE")"

# Run main function
main

exit 0