Skip to content

Instantly share code, notes, and snippets.

@fonic
Created September 5, 2022 06:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fonic/97c32695ea087a0215363f8b3b334d9c to your computer and use it in GitHub Desktop.
Save fonic/97c32695ea087a0215363f8b3b334d9c to your computer and use it in GitHub Desktop.
smart-status.sh - Query SMART status of disk drives (SSD/HDD) using smartctl
#!/usr/bin/env bash
# -------------------------------------------------------------------------
# -
# Fonic <https://github.com/fonic> -
# Date: 07/22/21 - 06/30/22 -
# -
# Based on: -
# Initramfs script 'initrc-init.sh', -
# 'man 8 smartctl', section 'EXIT STATUS' -
# -
# -------------------------------------------------------------------------
# Check command line
if (( $# < 1 )); then
echo -e "\e[1mUsage:\e[0m ${0##*/} DEVICE..."
exit 2
fi
# Check if root
if (( ${EUID} != 0 )); then
echo -e "\e[1;31mOnly root can do this.\e[0m"
exit 1
fi
# Query SMART status of device(s)
#
# NOTE on smartctl exit code (from man page):
#
# If all is well with the disk, the exit status (return value) of smartctl is 0 (all bits turned off). If a
# problem occurs, or an error, potential error, or fault is detected, then a non-zero status is returned. In
# this case, the eight different bits in the exit status have the following meanings for ATA disks; some of
# these values may also be returned for SCSI disks.
#
# Bit 0: Command line did not parse.
# Bit 1: Device open failed, device did not return an IDENTIFY DEVICE structure, or device is in a low-power
# mode (see '-n' option above).
# Bit 2: Some SMART or other ATA command to the disk failed, or there was a checksum error in a SMART data
# structure (see '-b' option above).
# Bit 3: SMART status check returned "DISK FAILING".
# Bit 4: We found prefail Attributes <= threshold.
# Bit 5: SMART status check returned "DISK OK" but we found that some (usage or prefail) Attributes have been
# <= threshold at some time in the past.
# Bit 6: The device error log contains records of errors.
# Bit 7: The device self-test log contains records of errors. [ATA only] Failed self-tests outdated by a
# newer successful extended self-test are ignored.
#
for device; do
echo
echo -e "\e[1mSMART status of device '${device}':\e[0m"
smartctl --info --health --attributes "${device}"
retval=$?
message="\e[1mOverall assessment:\e[0m "
if (( ${retval} == 0 )); then
message+="\e[1;32mHEALTHY\e[0m"
elif (( (retval & 1) != 0 || (retval & 2) != 0 || (retval & 4) != 0 )); then # bit 0 || bit 1 || bit 2
message+="\e[1;31mERROR (smartctl exit code: ${retval})\e[0m"
elif (( (retval & 8) != 0 )); then # bit 3
message+="\e[1;31mFAILING\e[0m"
elif (( (retval & 16) != 0 || (retval & 32) != 0 )); then # bit 4 || bit 5
message+="\e[1;33mPREFAIL\e[0m"
else
message+="\e[1;33mUNKNOWN (smartctl exit code: ${retval})\e[0m"
fi
message+=" | \e[1mError log:\e[0m "
if (( (retval & 64) != 0 || (retval & 128) != 0 )); then # bit 6 || bit 7
message+="\e[1;33mCONTAINS ERRORS\e[0m"
else
message+="\e[1;32mCLEAN\e[0m"
fi
echo -e "${message}"
done
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment