A shell script for checking the hard disk

First,

sudo apt-get install smartmontools

 

The script:

 #!/bin/bash

# Check if a hard disk device parameter has been provided
if [[ -z "$1" ]]; then
    echo "Error: Hard disk device parameter not provided."
    exit 1
fi

# Accept the hard disk device as a parameter
DEVICE="$1"

format_uptime() {
    local total_hours=$1
    local years
    local months
    local days
    local hours

    # Calculate years, months, days, and hours
    years=$((total_hours / (24 * 365)))
    total_hours=$((total_hours % (24 * 365)))

    months=$((total_hours / (24 * 30)))
    total_hours=$((total_hours % (24 * 30)))

    days=$((total_hours / 24))
    hours=$((total_hours % 24))
    
    # Set pluralization based on the number
    years_plural=$( [ "$years" -eq 1 ] && echo "" || echo "s" )
    months_plural=$( [ "$months" -eq 1 ] && echo "" || echo "s" )
    days_plural=$( [ "$days" -eq 1 ] && echo "" || echo "s" )
    hours_plural=$( [ "$hours" -eq 1 ] && echo "" || echo "s" )


    # Choose the display format based on the duration
    if [[ $years -gt 0 ]]; then
        echo "${years}year${years_plural} ${months}month${months_plural} ${days}day${days_plural} ${hours}hour${hours_plural}"
    elif [[ $months -gt 0 ]]; then
        echo "${months}month${months_plural} ${days}day${days_plural} ${hours}hour${hours_plural}"
    elif [[ $days -gt 0 ]]; then
        echo "${days}day${days_plural} ${hours}hour${hours_plural}"
    else
        echo "${hours}hour${hours_plural}"
    fi
}

# Run smartctl to retrieve hard disk information
SMART_DATA=$(sudo smartctl -a "$DEVICE")

# Extract the power-on hours and temperature
POWER_ON_HOURS=$(echo "$SMART_DATA" | grep -i "Power_On_Hours" | awk '{print $10}')
TEMPERATURE=$(echo "$SMART_DATA" | grep -i "Temperature_Celsius" | awk '{print $10}')

# Extract the hours component from Power_On_Hours
POWER_ON_HOURS_FORMATTED=$(echo "$POWER_ON_HOURS" | awk -F'h' '{print $1 " hours"}')
FORMATTED_UPTIME=$(format_uptime $POWER_ON_HOURS_FORMATTED)

# Check if the extracted values are empty
if [ -z "$POWER_ON_HOURS_FORMATTED" ]; then
    POWER_ON_HOURS_FORMATTED="Data not retrieved"
fi

if [ -z "$TEMPERATURE" ]; then
    TEMPERATURE="Data not retrieved"
fi

# Extract disk capacity and usage percentage
CAPACITY_RAW=$(df -h | grep "$DEVICE" | awk '{print $2}')
USAGE_RAW=$(df -h | grep "$DEVICE" | awk '{print $5}')

# Determine the capacity unit
CAPACITY_NUM=$(echo "$CAPACITY_RAW" | sed 's/[^0-9]//g')
if [ "$CAPACITY_NUM" -lt 1024 ]; then
    CAPACITY_FORMATTED=$(echo "$CAPACITY_RAW" | awk '{print $1}')
else
    CAPACITY_FORMATTED=$(echo "$CAPACITY_RAW" | numfmt --to=iec-i --suffix=B --format="%.2f" )
fi

# Combine the message
MESSAGE="Lenovo Think Centre (Bitcoin Node)\n\nHard disk uptime: ${FORMATTED_UPTIME}\nHard disk temperature: ${TEMPERATURE} °C\nHard disk capacity: ${CAPACITY_FORMATTED}\nUsage: ${USAGE_RAW}\n"

# Check health status
HEALTH_CHECK=$(echo "$SMART_DATA" | grep "SMART overall-health self-assessment test result:")

# Determine if there are any health issues
if [[ "$HEALTH_CHECK" != *"PASSED"* ]]; then
    MESSAGE+="\nWarning: Hard disk health issue detected!"
    MESSAGE+="\nRecommended to use the following commands for further diagnostics::\n"
    MESSAGE+="\nsudo smartctl -t short $DEVICE   # Run a short self-test\n"
    MESSAGE+="\nsudo smartctl -t long $DEVICE    # Run a long self-test\n"
else
    MESSAGE+="\nDisk status is normal."
fi

# echo -e $MESSAGE
# Send the report email
echo -e "From: nb@laonan.net\nTo: nb@hilltroll.com\nSubject: Disk Health Report\n\n$MESSAGE" | ssmtp nb@hilltroll.com
Laonan :
Only for HDDs, not SSDs.
For example, "name@something.com". If someone replies to you it will be via email.
For example, "http://someaddress.com"