smart-tests 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env bash
  2. # This script will report S.M.A.R.T. stats and perform tests on all available disks
  3. # Copyright 2021 Bryan C. Roessler
  4. init() {
  5. if [[ $EUID != 0 ]]; then
  6. echo "Must run as root!" && exit 1
  7. fi
  8. [[ ! -x $(command -v smartctl) ]] && echo "smartctl not found! Please install smartmontools." && exit 1
  9. if _input=$(getopt -o +slihatd -l short,long,info,health,all,temp,daemon,help -- "$@"); then
  10. eval set -- "$_input"
  11. while true; do
  12. case "$1" in
  13. --short|-s)
  14. SHORT="true"
  15. ;;
  16. --long|-l)
  17. LONG="true"
  18. ;;
  19. --info|-i)
  20. INFO="true"
  21. ;;
  22. --health|-h)
  23. HEALTH="true"
  24. ;;
  25. --all|-a)
  26. ALL="true"
  27. ;;
  28. --temp|-t)
  29. TEMP="true"
  30. ;;
  31. --daemon|-d)
  32. DAEMON="true"
  33. ;;
  34. --help)
  35. printhelp
  36. ;;
  37. --)
  38. shift
  39. break
  40. ;;
  41. esac
  42. shift
  43. done
  44. else
  45. echo "Error: incorrect option provided" && printhelp && exit 1
  46. fi
  47. }
  48. printhelp() {
  49. cat <<- 'EOF'
  50. USAGE:
  51. smart-tests [OPTION]...
  52. OPTIONS
  53. --short,-s
  54. Perform S.M.A.R.T short self-test
  55. --long,-l
  56. Perform S.M.A.R.T long self-test
  57. --info,-i
  58. Print S.M.A.R.T info
  59. --health,-h
  60. Perform S.M.A.R.T health assessment
  61. --temp,-t
  62. Report disk temperatures
  63. --daemon,-d
  64. Run in daemon mode for automatic health checks
  65. --all,-a
  66. Run on all drives (default)
  67. --help
  68. Print this help dialog and exit
  69. EOF
  70. }
  71. main() {
  72. mapfile -t drives_scanned < <(smartctl --scan)
  73. for drive in "${drives_scanned[@]}"; do
  74. name=$(echo "$drive" | cut -f1 -d" ")
  75. type=$(echo "$drive" | cut -f3 -d" ")
  76. echo "$name ($type)"
  77. suffix=("-d" "$type" "$name")
  78. [[ -v SHORT ]] && smartctl -t short "${suffix[@]}"
  79. [[ -v LONG ]] && smartctl -t long "${suffix[@]}"
  80. [[ -v INFO ]] && smartctl -i "${suffix[@]}"
  81. [[ -v HEALTH ]] && smartctl -H "${suffix[@]}"
  82. [[ -v ALL ]] && smartctl -a "${suffix[@]}"
  83. [[ -v TEMP ]] && smartctl -a "${suffix[@]}" | grep "Current Drive Temperature:"
  84. [[ -v DAEMON ]] && smartctl -t short "${suffix[@]}"
  85. done
  86. }
  87. init "$@"
  88. main "$@"