#!/bin/bash
# Gather information about all undocumented containers on main-pve

echo "=== Container Information Gathering ==="
echo ""

# List of undocumented containers
CONTAINERS="103 105 108 109 113 114 115 200"
VM="110"

ssh root@10.0.10.3 << 'EOF'
echo "VMs:"
echo "===="
echo ""

# VM 110
echo "VM 110 (debian):"
qm config 110 | grep -E "^(cores|memory|net0|bootdisk):"
echo ""

echo "Containers:"
echo "==========="
echo ""

for CT in 103 105 108 109 113 114 115 200; do
    echo "CT $CT:"

    # Get container name
    NAME=$(pct config $CT | grep "^hostname:" | awk '{print $2}')
    echo "  Name: $NAME"

    # Get IP address
    IP=$(pct config $CT | grep "^net0:" | grep -oP 'ip=\K[^,]+' || echo "DHCP")
    echo "  IP: $IP"

    # Get resources
    CORES=$(pct config $CT | grep "^cores:" | awk '{print $2}')
    MEM=$(pct config $CT | grep "^memory:" | awk '{print $2}')
    echo "  Resources: ${CORES} cores, ${MEM}MB RAM"

    # Check if running
    STATUS=$(pct status $CT | awk '{print $2}')
    echo "  Status: $STATUS"

    # Try to get running processes if container is running
    if [ "$STATUS" = "running" ]; then
        echo "  Top processes:"
        pct exec $CT -- ps aux --sort=-%mem | head -6 | tail -5 | awk '{printf "    - %s (PID %s, MEM %s%%)\n", $11, $2, $4}'

        # Check for Docker
        if pct exec $CT -- which docker >/dev/null 2>&1; then
            echo "  Docker containers:"
            pct exec $CT -- docker ps --format "    - {{.Names}} ({{.Image}})" 2>/dev/null || echo "    (Docker installed but no containers)"
        fi

        # Check listening ports
        echo "  Listening ports:"
        pct exec $CT -- ss -tlnp 2>/dev/null | grep LISTEN | awk '{print "    - " $4}' | head -5
    fi

    echo ""
done

echo ""
echo "=== Quick Summary ==="
echo ""
echo "Undocumented containers:"
for CT in 103 105 108 109 113 114 115 200; do
    NAME=$(pct config $CT | grep "^hostname:" | awk '{print $2}')
    IP=$(pct config $CT | grep "^net0:" | grep -oP 'ip=\K[^,]+' || echo "DHCP")
    STATUS=$(pct status $CT | awk '{print $2}')
    printf "CT %3d: %-20s %-15s %s\n" $CT "$NAME" "$IP" "$STATUS"
done

EOF
