#!/bin/bash
# Comprehensive script to verify 250GB drives on R630-01
# Run this script on R630-01 (192.168.11.11) as root

set -e

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

echo "=========================================="
echo "R630-01 250GB Drive Verification"
echo "=========================================="
echo "Date: $(date)"
echo "Hostname: $(hostname)"
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo -e "${RED}ERROR: Please run as root${NC}"
    exit 1
fi

echo -e "${BLUE}=== Step 1: All Block Devices ===${NC}"
echo "-----------------------------------"
echo "Listing all block devices with details:"
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT,FSTYPE,MODEL,SERIAL,STATE | grep -E "NAME|sd"
echo ""

echo -e "${BLUE}=== Step 2: All Physical Disks ===${NC}"
echo "-----------------------------------"
echo "All disks detected by system:"
fdisk -l 2>/dev/null | grep -E "^Disk /dev/sd" | sort -k2
echo ""

echo -e "${BLUE}=== Step 3: Identifying 250GB Drives ===${NC}"
echo "-----------------------------------"
echo "Drives around 250GB size (232-260 GB):"
echo ""
found_250gb=false
for disk in /dev/sd[a-z]; do
    if [ -b "$disk" ]; then
        size_info=$(fdisk -l "$disk" 2>/dev/null | grep "^Disk $disk" | awk '{print $3, $4}')
        if [ ! -z "$size_info" ]; then
            size_num=$(echo "$size_info" | grep -oE '[0-9]+' | head -1)
            size_unit=$(echo "$size_info" | grep -oE '[A-Za-z]+' | head -1)
            
            # Check if size is around 250GB (232-260 GB range)
            if [ "$size_unit" = "GiB" ] || [ "$size_unit" = "GB" ]; then
                if [ "$size_num" -ge 232 ] && [ "$size_num" -le 260 ]; then
                    echo -e "${GREEN}  ✓ $disk: ${size_info}${NC}"
                    found_250gb=true
                    
                    # Check if it's mounted or in use
                    mount_point=$(lsblk -n -o MOUNTPOINT "$disk" 2>/dev/null | head -1)
                    if [ ! -z "$mount_point" ]; then
                        echo -e "    ${YELLOW}  WARNING: Mounted at $mount_point${NC}"
                    fi
                    
                    # Check if it has partitions
                    partitions=$(lsblk -n -o NAME "$disk" 2>/dev/null | grep -v "^$(basename $disk)$" | wc -l)
                    if [ "$partitions" -gt 0 ]; then
                        echo -e "    ${YELLOW}  WARNING: Has $partitions partition(s)${NC}"
                        lsblk "$disk"
                    else
                        echo -e "    ${GREEN}  ✓ No partitions (available for OSD)${NC}"
                    fi
                fi
            fi
        fi
    fi
done

if [ "$found_250gb" = false ]; then
    echo -e "${YELLOW}  No 250GB drives found in expected size range${NC}"
    echo "  Checking all drives for reference:"
    fdisk -l 2>/dev/null | grep -E "^Disk /dev/sd" | awk '{print "    " $0}'
fi
echo ""

echo -e "${BLUE}=== Step 4: Current Ceph OSD Status ===${NC}"
echo "-----------------------------------"
if command -v ceph &> /dev/null; then
    echo "Current OSD tree:"
    ceph osd tree 2>/dev/null || echo -e "${YELLOW}Ceph command failed or not accessible${NC}"
    echo ""
    echo "OSD details:"
    ceph osd df 2>/dev/null || echo -e "${YELLOW}Ceph command failed${NC}"
    echo ""
    echo "Ceph health:"
    ceph health 2>/dev/null || echo -e "${YELLOW}Ceph command failed${NC}"
else
    echo -e "${YELLOW}Ceph command not found${NC}"
fi
echo ""

echo -e "${BLUE}=== Step 5: Storage Pools ===${NC}"
echo "-----------------------------------"
if command -v pvesm &> /dev/null; then
    echo "Proxmox storage pools:"
    pvesm status 2>/dev/null || echo -e "${YELLOW}pvesm command failed${NC}"
else
    echo -e "${YELLOW}pvesm command not found${NC}"
fi
echo ""

echo -e "${BLUE}=== Step 6: Volume Groups ===${NC}"
echo "-----------------------------------"
if command -v vgs &> /dev/null; then
    echo "LVM Volume Groups:"
    vgs 2>/dev/null || echo -e "${YELLOW}vgs command failed${NC}"
    echo ""
    echo "Physical Volumes:"
    pvs 2>/dev/null || echo -e "${YELLOW}pvs command failed${NC}"
else
    echo -e "${YELLOW}LVM commands not found${NC}"
fi
echo ""

echo -e "${BLUE}=== Step 7: Unused/Available Disks ===${NC}"
echo "-----------------------------------"
echo "Checking for disks that could be used for Ceph OSD:"
echo ""
available_count=0
for disk in /dev/sd[a-z]; do
    if [ -b "$disk" ]; then
        # Skip if it's the boot disk (sda) or current OSD (sdb)
        if [ "$disk" = "/dev/sda" ] || [ "$disk" = "/dev/sdb" ]; then
            continue
        fi
        
        # Check if disk has mount point
        mount_point=$(lsblk -n -o MOUNTPOINT "$disk" 2>/dev/null | head -1)
        if [ -z "$mount_point" ]; then
            # Check if disk is in a volume group
            in_vg=$(pvs 2>/dev/null | grep "$disk" | wc -l)
            if [ "$in_vg" -eq 0 ]; then
                size_info=$(fdisk -l "$disk" 2>/dev/null | grep "^Disk $disk" | awk '{print $3, $4}')
                if [ ! -z "$size_info" ]; then
                    echo -e "${GREEN}  ✓ $disk: ${size_info} - Potentially available${NC}"
                    available_count=$((available_count + 1))
                    
                    # Show disk details
                    echo "    Details:"
                    lsblk "$disk" | sed 's/^/      /'
                fi
            fi
        fi
    fi
done

if [ "$available_count" -eq 0 ]; then
    echo -e "${YELLOW}  No obviously available disks found${NC}"
    echo "  All disks may be in use or need further investigation"
fi
echo ""

echo -e "${BLUE}=== Step 8: PERC Controller Status (if available) ===${NC}"
echo "-----------------------------------"
if command -v omreport &> /dev/null; then
    echo "PERC controller virtual disks:"
    omreport storage vdisk 2>/dev/null || echo -e "${YELLOW}omreport not available or no PERC controller${NC}"
    echo ""
    echo "PERC controller physical disks:"
    omreport storage pdisk 2>/dev/null || echo -e "${YELLOW}omreport not available${NC}"
else
    echo -e "${YELLOW}Dell OpenManage tools not installed${NC}"
    echo "  To install: apt-get install srvadmin-all"
fi
echo ""

echo "=========================================="
echo -e "${GREEN}Verification Complete${NC}"
echo "=========================================="
echo ""
echo "Summary:"
echo "--------"
echo "1. Review the 250GB drives identified above"
echo "2. Check if any are available (no partitions, not mounted, not in VG)"
echo "3. If available, you can create a Ceph OSD with:"
echo ""
echo -e "   ${YELLOW}ceph-volume lvm create --data /dev/sdX${NC}"
echo ""
echo "   (Replace sdX with the actual device, e.g., sdc, sdd, etc.)"
echo ""
echo "4. After creating OSD, verify with:"
echo "   ceph osd tree"
echo "   ceph health"
echo ""

