#!/bin/bash
# Basic Ceph diagnostic - checks infrastructure without hanging
# Run on R630-01 as root

set -e

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

echo "=========================================="
echo "Basic Ceph Infrastructure Check"
echo "=========================================="
echo ""

echo -e "${BLUE}=== 1. Ceph Command Availability ===${NC}"
echo "-----------------------------------"
which ceph && ceph --version || echo -e "${RED}ceph command not found${NC}"
which pveceph && echo "pveceph: $(pveceph --version 2>/dev/null || echo 'available')" || echo -e "${YELLOW}pveceph not found${NC}"
echo ""

echo -e "${BLUE}=== 2. Bootstrap Keyring Locations ===${NC}"
echo "-----------------------------------"
echo "Standard location:"
ls -la /var/lib/ceph/bootstrap-osd/ceph.keyring 2>/dev/null && echo -e "${GREEN}  ✓ Found${NC}" || echo -e "${YELLOW}  ✗ Not found${NC}"
echo ""

echo "Proxmox location:"
ls -la /etc/pve/priv/ceph.client.bootstrap-osd.keyring 2>/dev/null && echo -e "${GREEN}  ✓ Found${NC}" || echo -e "${YELLOW}  ✗ Not found${NC}"
echo ""

echo "All Ceph keyrings in /etc/pve/priv/:"
ls -la /etc/pve/priv/ceph* 2>/dev/null | head -10 || echo "  No Ceph keyrings found"
echo ""

echo -e "${BLUE}=== 3. Ceph Services (Non-blocking) ===${NC}"
echo "-----------------------------------"
echo "Ceph target status:"
systemctl is-active ceph.target 2>/dev/null && echo -e "${GREEN}  ✓ Active${NC}" || echo -e "${YELLOW}  ✗ Not active${NC}"
echo ""

echo "Ceph monitor services:"
systemctl list-units --type=service --no-pager | grep ceph-mon | head -5 || echo "  No monitor services"
echo ""

echo "Ceph OSD services:"
systemctl list-units --type=service --no-pager | grep ceph-osd | head -5 || echo "  No OSD services"
echo ""

echo -e "${BLUE}=== 4. Network Ports ===${NC}"
echo "-----------------------------------"
echo "Port 6789 (Ceph monitors):"
ss -tlnp 2>/dev/null | grep 6789 || netstat -tlnp 2>/dev/null | grep 6789 || echo "  No listeners on port 6789"
echo ""

echo -e "${BLUE}=== 5. Ceph Configuration Files ===${NC}"
echo "-----------------------------------"
[ -f "/etc/ceph/ceph.conf" ] && echo -e "${GREEN}  ✓ /etc/ceph/ceph.conf exists${NC}" || echo -e "${YELLOW}  ✗ /etc/ceph/ceph.conf not found${NC}"
[ -f "/etc/pve/ceph.conf" ] && echo -e "${GREEN}  ✓ /etc/pve/ceph.conf exists${NC}" || echo -e "${YELLOW}  ✗ /etc/pve/ceph.conf not found${NC}"
echo ""

echo -e "${BLUE}=== 6. Ceph Directories ===${NC}"
echo "-----------------------------------"
echo "Ceph directories:"
[ -d "/var/lib/ceph" ] && echo "  /var/lib/ceph: $(ls -d /var/lib/ceph/* 2>/dev/null | wc -l) items" || echo "  /var/lib/ceph: does not exist"
[ -d "/etc/pve/priv" ] && echo "  /etc/pve/priv: exists" || echo "  /etc/pve/priv: does not exist"
echo ""

echo -e "${BLUE}=== 7. Quick Ceph Test (5 second timeout) ===${NC}"
echo "-----------------------------------"
echo "Testing ceph health (5 second timeout):"
timeout 5 ceph health 2>&1 && echo -e "${GREEN}  ✓ Cluster accessible${NC}" || echo -e "${RED}  ✗ Cluster not accessible or timeout${NC}"
echo ""

echo -e "${BLUE}=== 8. Proxmox Ceph Status ===${NC}"
echo "-----------------------------------"
if command -v pveceph &> /dev/null; then
    echo "pveceph status (non-blocking):"
    timeout 5 pveceph status 2>&1 | head -10 || echo "  pveceph status timed out or failed"
else
    echo -e "${YELLOW}pveceph not available${NC}"
fi
echo ""

echo "=========================================="
echo -e "${GREEN}Basic Diagnostic Complete${NC}"
echo "=========================================="
echo ""
echo "Key Findings:"
echo "  - Check bootstrap keyring locations above"
echo "  - Check if Ceph services are running"
echo "  - Check if cluster is accessible (timeout test)"
echo "  - Check if pveceph is available"
echo ""

