added v2.9

This commit is contained in:
2025-10-22 06:19:13 +08:00
parent a45c1a8587
commit ea9bd86003

View File

@@ -5,7 +5,7 @@
# Supports consumer and enterprise disk classification
SCRIPT_NAME=$(basename "$0")
VERSION="2.8"
VERSION="2.9"
# Color codes
RED=$(tput setaf 1)
@@ -346,7 +346,7 @@ check_mdraid() {
done
}
# Function to extract capacity in GB from various formats - IMPROVED VERSION
# Function to extract capacity in GB from various formats - FIXED VERSION
extract_capacity_gb() {
local capacity=$1
local capacity_gb=0
@@ -356,32 +356,57 @@ extract_capacity_gb() {
return
fi
# Debug: Show what we're trying to parse
# echo "DEBUG: Parsing capacity: '$capacity'" >&2
# Method 1: Try to extract bytes directly (most reliable)
if [[ $capacity =~ ([0-9,]+)\s*bytes ]]; then
local bytes=$(echo "${BASH_REMATCH[1]}" | tr -d ',')
capacity_gb=$(echo "scale=0; $bytes / 1000 / 1000 / 1000" | bc -l 2>/dev/null | cut -d. -f1)
# echo "DEBUG: Method 1 (bytes) found: $bytes bytes = $capacity_gb GB" >&2
fi
# Method 2: Try TB pattern
if [[ $capacity_gb -eq 0 ]] && [[ $capacity =~ ([0-9,.]+)\s*TB ]]; then
local size=$(echo "$capacity" | grep -oE '[0-9,.]+' | head -1 | tr -d ',')
capacity_gb=$(echo "scale=0; $size * 1000" | bc -l 2>/dev/null | cut -d. -f1)
# Method 2: Try TB pattern with various formats
if [[ $capacity_gb -eq 0 ]]; then
if [[ $capacity =~ ([0-9,.]+)\s*TB ]]; then
local size=$(echo "$capacity" | grep -oE '[0-9,.]+' | head -1 | tr -d ',')
capacity_gb=$(echo "scale=0; $size * 1000" | bc -l 2>/dev/null | cut -d. -f1)
# echo "DEBUG: Method 2 (TB) found: $size TB = $capacity_gb GB" >&2
fi
fi
# Method 3: Try GB pattern
if [[ $capacity_gb -eq 0 ]] && [[ $capacity =~ ([0-9,.]+)\s*GB ]]; then
local size=$(echo "$capacity" | grep -oE '[0-9,.]+' | head -1 | tr -d ',')
capacity_gb=$(echo "scale=0; $size" | bc -l 2>/dev/null | cut -d. -f1)
if [[ $capacity_gb -eq 0 ]]; then
if [[ $capacity =~ ([0-9,.]+)\s*GB ]]; then
local size=$(echo "$capacity" | grep -oE '[0-9,.]+' | head -1 | tr -d ',')
capacity_gb=$(echo "scale=0; $size" | bc -l 2>/dev/null | cut -d. -f1)
# echo "DEBUG: Method 3 (GB) found: $size GB = $capacity_gb GB" >&2
fi
fi
# Method 4: Try bracket format [XXX GB/TB]
if [[ $capacity_gb -eq 0 ]] && [[ $capacity =~ \[([0-9,.]+)\s*([GT])B?\] ]]; then
local size=$(echo "${BASH_REMATCH[1]}" | tr -d ',')
local unit="${BASH_REMATCH[2]}"
if [[ "$unit" == "T" ]]; then
capacity_gb=$(echo "scale=0; $size * 1000" | bc -l 2>/dev/null | cut -d. -f1)
else
capacity_gb=$(echo "scale=0; $size" | bc -l 2>/dev/null | cut -d. -f1)
if [[ $capacity_gb -eq 0 ]]; then
if [[ $capacity =~ \[([0-9,.]+)\s*([GT])B?\] ]]; then
local size=$(echo "${BASH_REMATCH[1]}" | tr -d ',')
local unit="${BASH_REMATCH[2]}"
if [[ "$unit" == "T" ]]; then
capacity_gb=$(echo "scale=0; $size * 1000" | bc -l 2>/dev/null | cut -d. -f1)
else
capacity_gb=$(echo "scale=0; $size" | bc -l 2>/dev/null | cut -d. -f1)
fi
# echo "DEBUG: Method 4 (bracket) found: $size $unit = $capacity_gb GB" >&2
fi
fi
# Method 5: Try simple number extraction for cases like "1000.000" without units
if [[ $capacity_gb -eq 0 ]]; then
local simple_number=$(echo "$capacity" | grep -oE '[0-9]+\.?[0-9]*' | head -1)
if [[ -n "$simple_number" ]]; then
# If it's a large number (likely bytes), convert to GB
if [[ $(echo "$simple_number > 1000000000" | bc 2>/dev/null) -eq 1 ]]; then
capacity_gb=$(echo "scale=0; $simple_number / 1000 / 1000 / 1000" | bc -l 2>/dev/null | cut -d. -f1)
# echo "DEBUG: Method 5 (large number) found: $simple_number = $capacity_gb GB" >&2
fi
fi
fi
@@ -393,7 +418,7 @@ extract_capacity_gb() {
fi
}
# Function to get disk capacity using multiple methods - NEW IMPROVED FUNCTION
# Function to get disk capacity using multiple methods - SIMPLIFIED AND ROBUST
get_disk_capacity() {
local disk=$1
local controller=$2
@@ -405,20 +430,25 @@ get_disk_capacity() {
local capacity_gb=0
local capacity_human="Unknown"
# Method 1: Try smartctl first
# Method 1: Try smartctl with detailed output
local info=$($smart_cmd -i "$disk" 2>/dev/null)
local capacity_field=$(echo "$info" | grep -i "User Capacity:" | cut -d: -f2 | sed 's/^[ \t]*//')
# For NVMe, try different field
if [[ -z "$capacity_field" && "$disk_type" == "NVMe" ]]; then
capacity_field=$(echo "$info" | grep -i "Total NVM Capacity" | cut -d: -f2 | sed 's/^[ \t]*//')
# Try multiple possible capacity fields
local capacity_field=""
for field in "User Capacity:" "Total NVM Capacity:" "Namespace 1 Size/Capacity:"; do
if [[ -z "$capacity_field" ]]; then
capacity_field=$(echo "$info" | grep -i "$field" | cut -d: -f2- | sed 's/^[ \t]*//')
fi
done
# If no specific field found, try to find any line with capacity information
if [[ -z "$capacity_field" ]]; then
capacity_field=$(echo "$info" | grep -i "capacity" | head -1 | cut -d: -f2- | sed 's/^[ \t]*//')
fi
# Extract capacity from the field
if [[ -n "$capacity_field" ]]; then
capacity_gb=$(extract_capacity_gb "$capacity_field")
if [[ $capacity_gb -gt 0 ]]; then
capacity_human=$(get_human_capacity "$capacity_gb")
fi
fi
# Method 2: Try block device information (fallback)
@@ -426,7 +456,6 @@ get_disk_capacity() {
local block_size=$(lsblk -b "$disk" -o SIZE -n 2>/dev/null | head -1)
if [[ -n "$block_size" && "$block_size" =~ ^[0-9]+$ ]]; then
capacity_gb=$(echo "scale=0; $block_size / 1000 / 1000 / 1000" | bc -l 2>/dev/null | cut -d. -f1)
capacity_human=$(get_human_capacity "$capacity_gb")
fi
fi
@@ -441,22 +470,21 @@ get_disk_capacity() {
else
capacity_gb=$(echo "scale=0; $size" | bc -l 2>/dev/null | cut -d. -f1)
fi
capacity_human=$(get_human_capacity "$capacity_gb")
fi
fi
echo "$capacity_gb|$capacity_human"
}
# Function to get human readable capacity
get_human_capacity() {
local capacity_gb=$1
if [[ $capacity_gb -ge 1000 ]]; then
echo "$(echo "scale=2; $capacity_gb / 1000" | bc -l) TB"
# Generate human readable capacity
if [[ $capacity_gb -gt 0 ]]; then
if [[ $capacity_gb -ge 1000 ]]; then
capacity_human="$(echo "scale=2; $capacity_gb / 1000" | bc -l) TB"
else
capacity_human="${capacity_gb} GB"
fi
else
echo "${capacity_gb} GB"
capacity_human="Unknown"
fi
echo "$capacity_gb|$capacity_human"
}
# Function to check a single disk - IMPROVED VERSION
@@ -544,7 +572,13 @@ check_disk() {
echo "Capacity: $capacity_human"
echo "Firmware: ${firmware:-Unknown}"
echo "Health: ${health_status:-Unknown}"
echo "Power On Hours: ${power_on_hours:-Unknown}"
# Only show Power On Hours if we have a valid value
if [[ -n "$power_on_hours" && "$power_on_hours" != "0" ]]; then
echo "Power On Hours: $power_on_hours"
else
echo "Power On Hours: Unknown"
fi
# Disk type specific analysis
if [[ "$disk_type" == "SATA HDD" || "$disk_type" == "SAS HDD" ]]; then