#!/bin/bash
# Comprehensive Proxmox VE configuration review including storage
# Reviews all Proxmox hosts for configuration, storage, network, and provides recommendations

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/load-physical-inventory.sh" 2>/dev/null || true

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
MAGENTA='\033[0;35m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }
log_section() { echo -e "\n${CYAN}=== $1 ===${NC}\n"; }
log_recommendation() { echo -e "${MAGENTA}[REC]${NC} $1"; }

declare -A HOSTS
HOSTS[ml110]="${PROXMOX_HOST_ML110:-192.168.11.10}:${PROXMOX_PASS_ML110:-L@kers2010}"
HOSTS[r630-01]="${PROXMOX_HOST_R630_01:-192.168.11.11}:${PROXMOX_PASS_R630_01:-password}"
HOSTS[r630-02]="${PROXMOX_HOST_R630_02:-192.168.11.12}:${PROXMOX_PASS_R630_02:-password}"

REPORT_FILE="/tmp/proxmox-config-review-$(date +%Y%m%d_%H%M%S).txt"
echo "Proxmox VE Configuration Review - $(date)" > "$REPORT_FILE"
echo "===========================================" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"

review_host() {
    local hostname="$1"
    local ip="${HOSTS[$hostname]%%:*}"
    local password="${HOSTS[$hostname]#*:}"
    
    log_section "Reviewing $hostname (${ip})"
    echo "=== $hostname (${ip}) ===" >> "$REPORT_FILE"
    
    sshpass -p "$password" ssh -o StrictHostKeyChecking=no root@"$ip" bash <<'ENDSSH' 2>/dev/null || {
        log_error "Could not connect to ${hostname}"
        return 1
    }
        echo "=== System Information ==="
        echo "Hostname: $(hostname)"
        echo "Proxmox Version: $(pveversion -v 2>/dev/null | head -1 || echo 'Unknown')"
        echo "Kernel: $(uname -r)"
        echo "Uptime: $(uptime -p)"
        echo "Load Average: $(uptime | awk -F'load average:' '{print $2}')"
        echo ""
        
        echo "=== CPU Information ==="
        echo "CPU Model: $(lscpu | grep 'Model name' | cut -d: -f2 | xargs)"
        echo "CPU Cores: $(nproc)"
        echo "CPU Threads: $(lscpu | grep '^CPU(s):' | awk '{print $2}')"
        echo ""
        
        echo "=== Memory Information ==="
        free -h | grep -E "^Mem|^Swap"
        echo ""
        
        echo "=== Storage Information ==="
        echo "Physical Disks:"
        lsblk -o NAME,SIZE,TYPE,MOUNTPOINT,FSTYPE | grep -E "disk|NAME"
        echo ""
        echo "Filesystem Usage:"
        df -h | grep -E "Filesystem|/dev|rpool|/var/lib/vz"
        echo ""
        echo "Proxmox Storage:"
        pvesm status 2>/dev/null || echo "pvesm not available"
        echo ""
        pvesh get /nodes/$(hostname)/storage 2>/dev/null | head -50 || echo "Cannot get storage info"
        echo ""
        
        echo "=== Network Configuration ==="
        echo "Network Interfaces:"
        ip addr show | grep -E "^[0-9]+:|inet " | head -20
        echo ""
        echo "Network Bridges:"
        cat /etc/network/interfaces 2>/dev/null | grep -E "^auto|^iface|bridge" | head -20 || echo "Cannot read network config"
        echo ""
        
        echo "=== Proxmox Services Status ==="
        systemctl is-active pve-cluster >/dev/null && echo "pve-cluster: ✓ Active" || echo "pve-cluster: ✗ Inactive"
        systemctl is-active pvestatd >/dev/null && echo "pvestatd: ✓ Active" || echo "pvestatd: ✗ Inactive"
        systemctl is-active pvedaemon >/dev/null && echo "pvedaemon: ✓ Active" || echo "pvedaemon: ✗ Inactive"
        systemctl is-active pveproxy >/dev/null && echo "pveproxy: ✓ Active" || echo "pveproxy: ✗ Inactive"
        echo ""
        
        echo "=== Cluster Status ==="
        pvecm status 2>/dev/null | head -20 || echo "Not in cluster or pvecm not available"
        echo ""
        
        echo "=== VM/Container Count ==="
        echo "Containers: $(pct list 2>/dev/null | tail -n +2 | wc -l)"
        echo "VMs: $(qm list 2>/dev/null | tail -n +2 | wc -l)"
        echo ""
        
        echo "=== Storage Details ==="
        echo "LVM Volume Groups:"
        vgs 2>/dev/null || echo "No LVM VGs or vgs not available"
        echo ""
        echo "LVM Logical Volumes:"
        lvs 2>/dev/null | head -20 || echo "No LVM LVs or lvs not available"
        echo ""
        echo "ZFS Pools:"
        zpool list 2>/dev/null || echo "No ZFS pools or zpool not available"
        echo ""
        zfs list 2>/dev/null | head -20 || echo "No ZFS datasets"
        echo ""
        
        echo "=== Disk I/O ==="
        iostat -x 1 2 2>/dev/null | tail -10 || echo "iostat not available"
        echo ""
ENDSSH
    echo "" >> "$REPORT_FILE"
}

# Review all hosts
for hostname in "${!HOSTS[@]}"; do
    review_host "$hostname" | tee -a "$REPORT_FILE"
done

log_section "Generating Recommendations"

# Analyze and provide recommendations
cat >> "$REPORT_FILE" <<'ANALYSIS_END'

=== RECOMMENDATIONS AND SUGGESTIONS ===

ANALYSIS_END

log_section "Configuration Review Complete"
log_info "Full report saved to: $REPORT_FILE"
log_info "Reviewing report for recommendations..."

# Display summary
echo ""
log_section "Quick Summary"
echo "Report file: $REPORT_FILE"
echo "Review the full report for detailed recommendations."

cat "$REPORT_FILE"
