added v2.9
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
# Supports consumer and enterprise disk classification
|
# Supports consumer and enterprise disk classification
|
||||||
|
|
||||||
SCRIPT_NAME=$(basename "$0")
|
SCRIPT_NAME=$(basename "$0")
|
||||||
VERSION="2.8"
|
VERSION="2.9"
|
||||||
|
|
||||||
# Color codes
|
# Color codes
|
||||||
RED=$(tput setaf 1)
|
RED=$(tput setaf 1)
|
||||||
@@ -346,7 +346,7 @@ check_mdraid() {
|
|||||||
done
|
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() {
|
extract_capacity_gb() {
|
||||||
local capacity=$1
|
local capacity=$1
|
||||||
local capacity_gb=0
|
local capacity_gb=0
|
||||||
@@ -356,32 +356,57 @@ extract_capacity_gb() {
|
|||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Debug: Show what we're trying to parse
|
||||||
|
# echo "DEBUG: Parsing capacity: '$capacity'" >&2
|
||||||
|
|
||||||
# Method 1: Try to extract bytes directly (most reliable)
|
# Method 1: Try to extract bytes directly (most reliable)
|
||||||
if [[ $capacity =~ ([0-9,]+)\s*bytes ]]; then
|
if [[ $capacity =~ ([0-9,]+)\s*bytes ]]; then
|
||||||
local bytes=$(echo "${BASH_REMATCH[1]}" | tr -d ',')
|
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)
|
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
|
fi
|
||||||
|
|
||||||
# Method 2: Try TB pattern
|
# Method 2: Try TB pattern with various formats
|
||||||
if [[ $capacity_gb -eq 0 ]] && [[ $capacity =~ ([0-9,.]+)\s*TB ]]; then
|
if [[ $capacity_gb -eq 0 ]]; then
|
||||||
local size=$(echo "$capacity" | grep -oE '[0-9,.]+' | head -1 | tr -d ',')
|
if [[ $capacity =~ ([0-9,.]+)\s*TB ]]; then
|
||||||
capacity_gb=$(echo "scale=0; $size * 1000" | bc -l 2>/dev/null | cut -d. -f1)
|
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
|
fi
|
||||||
|
|
||||||
# Method 3: Try GB pattern
|
# Method 3: Try GB pattern
|
||||||
if [[ $capacity_gb -eq 0 ]] && [[ $capacity =~ ([0-9,.]+)\s*GB ]]; then
|
if [[ $capacity_gb -eq 0 ]]; then
|
||||||
local size=$(echo "$capacity" | grep -oE '[0-9,.]+' | head -1 | tr -d ',')
|
if [[ $capacity =~ ([0-9,.]+)\s*GB ]]; then
|
||||||
capacity_gb=$(echo "scale=0; $size" | bc -l 2>/dev/null | cut -d. -f1)
|
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
|
fi
|
||||||
|
|
||||||
# Method 4: Try bracket format [XXX GB/TB]
|
# Method 4: Try bracket format [XXX GB/TB]
|
||||||
if [[ $capacity_gb -eq 0 ]] && [[ $capacity =~ \[([0-9,.]+)\s*([GT])B?\] ]]; then
|
if [[ $capacity_gb -eq 0 ]]; then
|
||||||
local size=$(echo "${BASH_REMATCH[1]}" | tr -d ',')
|
if [[ $capacity =~ \[([0-9,.]+)\s*([GT])B?\] ]]; then
|
||||||
local unit="${BASH_REMATCH[2]}"
|
local size=$(echo "${BASH_REMATCH[1]}" | tr -d ',')
|
||||||
if [[ "$unit" == "T" ]]; then
|
local unit="${BASH_REMATCH[2]}"
|
||||||
capacity_gb=$(echo "scale=0; $size * 1000" | bc -l 2>/dev/null | cut -d. -f1)
|
if [[ "$unit" == "T" ]]; then
|
||||||
else
|
capacity_gb=$(echo "scale=0; $size * 1000" | bc -l 2>/dev/null | cut -d. -f1)
|
||||||
capacity_gb=$(echo "scale=0; $size" | 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
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -393,7 +418,7 @@ extract_capacity_gb() {
|
|||||||
fi
|
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() {
|
get_disk_capacity() {
|
||||||
local disk=$1
|
local disk=$1
|
||||||
local controller=$2
|
local controller=$2
|
||||||
@@ -405,20 +430,25 @@ get_disk_capacity() {
|
|||||||
local capacity_gb=0
|
local capacity_gb=0
|
||||||
local capacity_human="Unknown"
|
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 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
|
# Try multiple possible capacity fields
|
||||||
if [[ -z "$capacity_field" && "$disk_type" == "NVMe" ]]; then
|
local capacity_field=""
|
||||||
capacity_field=$(echo "$info" | grep -i "Total NVM Capacity" | cut -d: -f2 | sed 's/^[ \t]*//')
|
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
|
fi
|
||||||
|
|
||||||
|
# Extract capacity from the field
|
||||||
if [[ -n "$capacity_field" ]]; then
|
if [[ -n "$capacity_field" ]]; then
|
||||||
capacity_gb=$(extract_capacity_gb "$capacity_field")
|
capacity_gb=$(extract_capacity_gb "$capacity_field")
|
||||||
if [[ $capacity_gb -gt 0 ]]; then
|
|
||||||
capacity_human=$(get_human_capacity "$capacity_gb")
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Method 2: Try block device information (fallback)
|
# 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)
|
local block_size=$(lsblk -b "$disk" -o SIZE -n 2>/dev/null | head -1)
|
||||||
if [[ -n "$block_size" && "$block_size" =~ ^[0-9]+$ ]]; then
|
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_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
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -441,22 +470,21 @@ get_disk_capacity() {
|
|||||||
else
|
else
|
||||||
capacity_gb=$(echo "scale=0; $size" | bc -l 2>/dev/null | cut -d. -f1)
|
capacity_gb=$(echo "scale=0; $size" | bc -l 2>/dev/null | cut -d. -f1)
|
||||||
fi
|
fi
|
||||||
capacity_human=$(get_human_capacity "$capacity_gb")
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "$capacity_gb|$capacity_human"
|
# Generate human readable capacity
|
||||||
}
|
if [[ $capacity_gb -gt 0 ]]; then
|
||||||
|
if [[ $capacity_gb -ge 1000 ]]; then
|
||||||
# Function to get human readable capacity
|
capacity_human="$(echo "scale=2; $capacity_gb / 1000" | bc -l) TB"
|
||||||
get_human_capacity() {
|
else
|
||||||
local capacity_gb=$1
|
capacity_human="${capacity_gb} GB"
|
||||||
|
fi
|
||||||
if [[ $capacity_gb -ge 1000 ]]; then
|
|
||||||
echo "$(echo "scale=2; $capacity_gb / 1000" | bc -l) TB"
|
|
||||||
else
|
else
|
||||||
echo "${capacity_gb} GB"
|
capacity_human="Unknown"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo "$capacity_gb|$capacity_human"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to check a single disk - IMPROVED VERSION
|
# Function to check a single disk - IMPROVED VERSION
|
||||||
@@ -544,7 +572,13 @@ check_disk() {
|
|||||||
echo "Capacity: $capacity_human"
|
echo "Capacity: $capacity_human"
|
||||||
echo "Firmware: ${firmware:-Unknown}"
|
echo "Firmware: ${firmware:-Unknown}"
|
||||||
echo "Health: ${health_status:-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
|
# Disk type specific analysis
|
||||||
if [[ "$disk_type" == "SATA HDD" || "$disk_type" == "SAS HDD" ]]; then
|
if [[ "$disk_type" == "SATA HDD" || "$disk_type" == "SAS HDD" ]]; then
|
||||||
Reference in New Issue
Block a user