Prx pure migration

#!/bin/bash

# 1. Get the list of all active multipath device names
MPATH_DEVICES=$(multipath -ll | grep -E '^[a-zA-Z0-9_-]+ \(' | awk '{print $1}')

if [ -z "$MPATH_DEVICES" ]; then
    echo "No multipath devices found. Ensure you have sudo privileges."
    exit 1
fi

for mpath in $MPATH_DEVICES; do
    echo "========================================================================================================================"
    echo " PROCESSING MPATH: $mpath"
    echo "========================================================================================================================"

    # 2. Extract slave 'sd' devices
    SLAVES=$(multipath -ll "$mpath" | grep -oE 'sd[a-z]+' | sort -u)
    DISK_COUNT=$(echo "$SLAVES" | wc -w)

    echo "Status: Found $DISK_COUNT associated disks."

    # 3. SAFETY CHECK: Must have at least 12 disks
    if [ "$DISK_COUNT" -lt 12 ]; then
        echo "ERROR: $mpath has only $DISK_COUNT disks. Minimum required for deletion is 12."
        echo "SKIPPING DELETION for this group to prevent data path exhaustion."
        echo ""
        continue
    fi

    # 4. Gather stats and sort numerically by the first stat column (Completed Reads)
    ALL_STATS=$(
        for d in $SLAVES; do
            if [ -f "/sys/block/$d/stat" ]; then
                printf "%-5s %s\n" "$d" "$(cat /sys/block/$d/stat)"
            fi
        done | sort -nk2
    )

    # 5. Display the 4 disks being kept (the 4 with the lowest reads)
    echo "[ KEEPING: Lowest 4 Read-Counts ]"
    echo "$ALL_STATS" | head -n 4 | column -t
    echo ""

    # 6. Process everything else for deletion (Leaving exactly 4)
    NUM_TO_DELETE=$((DISK_COUNT - 4))
    echo "[ DELETING: Top $NUM_TO_DELETE Busiest Paths ]"

    # Extract names of the busiest disks (everything except the first 4)
    DELETE_LIST=$(echo "$ALL_STATS" | tail -n "$NUM_TO_DELETE")

    # Display the targets
    echo "$DELETE_LIST" | column -t
    echo "--------------------------------------------------------"

    # Execute the deletion command
    echo "$DELETE_LIST" | awk '{print $1}' | while read -r drive; do
        echo "Action: multipathd del path $drive"
        multipathd del path "$drive"
    done

    echo "Done processing $mpath."
    echo "" 
done
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.15.1.108:3260 --logout
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.15.1.109:3260 --logout
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.15.1.118:3260 --logout
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.15.1.119:3260 --logout
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.16.1.108:3260 --logout
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.16.1.109:3260 --logout
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.16.1.118:3260 --logout
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.16.1.119:3260 --logout
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.15.1.108:3260 -o delete
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.15.1.109:3260 -o delete
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.15.1.118:3260 -o delete
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.15.1.119:3260 -o delete
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.16.1.108:3260 -o delete
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.16.1.109:3260 -o delete
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.16.1.118:3260 -o delete
iscsiadm -m node -T iqn.2010-06.com.purestorage:flasharray.6b8e2cb1e2a96bb2 -p 10.16.1.119:3260 -o delete