#!/bin/bash
# Scan all containers across all Proxmox hosts
# Outputs: VMID, name, host, IP config, current IP, status

set -euo pipefail

HOSTS=(
    "192.168.11.10:ml110"
    "192.168.11.11:r630-01"
    "192.168.11.12:r630-02"
)

OUTPUT_FILE="/home/intlc/projects/proxmox/CONTAINER_INVENTORY_$(date +%Y%m%d_%H%M%S).md"
CSV_FILE="/home/intlc/projects/proxmox/container_inventory_$(date +%Y%m%d_%H%M%S).csv"
TEMP_DIR=$(mktemp -d)

echo "=== Scanning All Containers Across All Hosts ==="
echo ""

# Create output files
cat > "$OUTPUT_FILE" << EOF
# Container Inventory - Complete Scan

**Generated**: $(date)
**Purpose**: Complete inventory of all containers across all Proxmox hosts

---

## All Containers

| VMID | Name | Host | Status | IP Config | Current IP | Hostname |
|------|------|------|--------|----------|------------|----------|
EOF

echo "VMID,Name,Host,Status,IP_Config,Current_IP,Hostname" > "$CSV_FILE"

for host_info in "${HOSTS[@]}"; do
    IFS=: read -r ip hostname <<< "$host_info"
    echo "Scanning $hostname ($ip)..."
    
    # Get all VMIDs for this host
    ssh -o ConnectTimeout=10 root@"$ip" "pct list 2>/dev/null | tail -n +2 | awk '{print \$1}'" > "$TEMP_DIR/${hostname}_vmids.txt" 2>/dev/null || continue
    
    if [ ! -s "$TEMP_DIR/${hostname}_vmids.txt" ]; then
        echo "  No containers found on $hostname"
        continue
    fi
    
    # Process each VMID
    while read -r vmid; do
        [ -z "$vmid" ] && continue
        
        # Get container info in one SSH call
        container_data=$(ssh -o ConnectTimeout=10 root@"$ip" "
            name=\$(pct config $vmid 2>/dev/null | grep '^name:' | cut -d: -f2 | tr -d ' ' || echo 'unnamed')
            hostname_vm=\$(pct config $vmid 2>/dev/null | grep '^hostname:' | cut -d: -f2 | tr -d ' ' || echo 'N/A')
            status=\$(pct status $vmid 2>/dev/null | grep 'status:' | awk '{print \$2}' || echo 'unknown')
            net_config=\$(pct config $vmid 2>/dev/null | grep '^net0:' || echo '')
            ip_config='N/A'
            current_ip='N/A'
            if echo \"\$net_config\" | grep -q 'ip='; then
                ip_config=\$(echo \"\$net_config\" | grep -oE 'ip=[^,]+' | cut -d= -f2)
                if [ \"\$ip_config\" = 'dhcp' ] || [ \"\$ip_config\" = 'auto' ]; then
                    if [ \"\$status\" = 'running' ]; then
                        current_ip=\$(pct exec $vmid -- hostname -I 2>/dev/null | awk '{print \$1}' || echo 'N/A')
                    fi
                else
                    current_ip=\$(echo \"\$ip_config\" | cut -d'/' -f1)
                fi
            fi
            echo \"\$name|\$hostname_vm|\$status|\$ip_config|\$current_ip\"
        " 2>/dev/null || echo "|||||")
        
        IFS='|' read -r name hostname_vm status ip_config current_ip <<< "$container_data"
        
        # Output to files
        echo "| $vmid | $name | $hostname | $status | $ip_config | $current_ip | $hostname_vm |" >> "$OUTPUT_FILE"
        echo "$vmid,\"$name\",$hostname,$status,$ip_config,$current_ip,$hostname_vm" >> "$CSV_FILE"
        
    done < "$TEMP_DIR/${hostname}_vmids.txt"
done

# Calculate summary
dhcp_count=$(grep -c "| dhcp |" "$OUTPUT_FILE" || echo "0")
static_count=$(grep -v "| dhcp |" "$OUTPUT_FILE" | grep -v "| N/A |" | grep -c "| 192.168.11" || echo "0")
total_count=$(tail -n +12 "$OUTPUT_FILE" | grep -c "^|" || echo "0")

rm -rf "$TEMP_DIR"

echo ""
echo "=== Scan Complete ==="
echo "Total containers scanned: $total_count"
echo "Output files:"
echo "  - $OUTPUT_FILE"
echo "  - $CSV_FILE"
echo ""

# Summary by IP config type
echo "=== Summary by IP Configuration ==="
echo "DHCP containers: $dhcp_count"
echo "Static IP containers: $static_count"
echo "Total containers: $total_count"
