Skip to content

Instantly share code, notes, and snippets.

@v1k0d3n
Created August 9, 2024 18:17
Show Gist options
  • Save v1k0d3n/395ba184887b0c4e1bbdb49d0e9f0ca9 to your computer and use it in GitHub Desktop.
Save v1k0d3n/395ba184887b0c4e1bbdb49d0e9f0ca9 to your computer and use it in GitHub Desktop.
Get Disk Information

Details

Using oc debug commands, this AI-generated gobbledygook script will collect all of the relevant useful information that can be gathered about attached physical disks. This script will evolve over time, but will use a custom image for running oc debug commands.

While you're here, I would suggest checking out my other dirty hack for getting NIC information!

NOTE: *By default, the variable is used for an SNO. If you want to use another node, just change it to the standard node/<node-name> syntax and this script will work just fine.

NODE_NAME=$(oc get no -o name)

oc debug $NODE_NAME --image=fedora:latest --tty=true --as-root=true -- /bin/bash -c "
    # Inform the user that the process will take some time
    echo 'This process may take a while, depending on the number of connected disks on this system.'
    echo 'Please wait while we gather the data. This process could take several minutes.'

    # Start the timer
    SECONDS=0

    # Background loop to print a dot every 10 seconds
    (
        while true; do
            sleep 10
            echo -n '.'
        done
    ) &

    DOT_PID=\$!

    # Install necessary tools silently
    dnf install -y util-linux sysstat fio jq > /dev/null 2>&1
    
    # Create a temporary file to store the output
    tmpfile=\$(mktemp)
    
    # Write the header to the temporary file
    printf '%-10s %-10s %-10s %-10s %-15s %-15s\n' 'Disk' 'Size (G)' 'Type' 'HDD/SSD' 'Read IOPS' 'Write IOPS' > \$tmpfile

    # Loop through each physical disk
    for disk in \$(lsblk -nd --output NAME,TYPE | awk '\$2 == \"disk\" {print \$1}'); do
        # Get disk size
        size=\$(lsblk -nd --output SIZE -b /dev/\$disk | awk '{print \$1/1024/1024/1024}')
        
        # Get rotational status
        rotational=\$(cat /sys/block/\$disk/queue/rotational 2>/dev/null)
        
        # Set type based on rotational status
        if [ \"\$rotational\" = \"1\" ]; then
            type='HDD'
        elif [ \"\$rotational\" = \"0\" ]; then
            type='SSD'
        else
            type='Unknown'
        fi

        # Get disk type label
        disk_type=\$(lsblk -nd --output TYPE /dev/\$disk)

        # Run fio to get read and write IOPS
        fio_output=\$(fio --name=test --filename=/dev/\$disk --ioengine=libaio --rw=randrw --bs=4k --direct=1 --size=1G --numjobs=1 --runtime=30 --time_based --group_reporting --output-format=json)
        
        read_iops=\$(echo \"\$fio_output\" | jq '.jobs[0].read.iops')
        write_iops=\$(echo \"\$fio_output\" | jq '.jobs[0].write.iops')

        # Print the information in the structured format
        printf '%-10s %-10s %-10s %-10s %-15s %-15s\n' \"\$disk\" \"\${size}G\" \"\$disk_type\" \"\$type\" \"\$read_iops\" \"\$write_iops\" >> \$tmpfile
    done

    # Stop the dot-printing background process
    kill \$DOT_PID

    # Print the output
    echo
    cat \$tmpfile

    # Remove the temporary file
    rm -f \$tmpfile

    # Display the elapsed time
    echo 'Process completed in '\$SECONDS' seconds.'
"

Example Output:

Starting pod/roderika-debug-mc5mq ...
To use host binaries, run `chroot /host`
Pod IP: 192.168.3.99
If you don't see a command prompt, try pressing enter.
.................
Disk       Size (G)   Type       HDD/SSD    Read IOPS       Write IOPS
sda        931G       disk       HDD        2759.074698     2747.741742
sdb        7448G      disk       HDD        3719.442685     3710.442985
sdc        5587.5G    disk       HDD        4014.032866     4009.833006
sdd        3725.5G    disk       HDD        180.293990      181.593947
sde        931G       disk       HDD        168.915992      170.815422
Process completed in 178 seconds.

Removing debug pod ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment