NASutils Knowledge BaseUnofficialPolski

Bash · lesson 28 of 30 · Tools for the NASRead smartctl

The script reads the output of smartctl -A, marks four error counters above zero and shows the temperature and the days of work.

Programming on the NAS › Bash in 30 lessons

28-disk-health.sh Download 28-disk-health.sh

#!/usr/app/TurboShell/bin/bash
# Reads the output of smartctl -A and shows what needs a look
set -euo pipefail
bad=0
while read -r id name _ _ _ _ _ _ _ raw _; do
  [[ $id =~ ^[0-9]+$ ]] || continue
  case $name in
    Reallocated_Sector_Ct | Current_Pending_Sector | Offline_Uncorrectable | UDMA_CRC_Error_Count)
      mark=ok
      if ((raw > 0)); then
        mark=LOOK
        bad=$((bad + 1))
      fi
      printf '%-5s %s %s\n' "$mark" "$name" "$raw"
      ;;
    Temperature_Celsius) echo "      temperature $raw C" ;;
    Power_On_Hours) echo "      on for $((raw / 24)) days" ;;
  esac
done
echo "$bad values need a look"
((bad == 0))

Terminal

$ bash 28-disk-health.sh < smartctl.txt; echo "exit code $?"
LOOK  Reallocated_Sector_Ct 8
      on for 1095 days
      temperature 38 C
ok    Current_Pending_Sector 0
ok    Offline_Uncorrectable 0
LOOK  UDMA_CRC_Error_Count 2
2 values need a look
exit code 1

Exercise

Mark a temperature of 50 C or more.