From e7300d63f14bcaa76a67a8e7a1576a4cdba97356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Thu, 27 Feb 2020 15:15:47 +0100 Subject: [PATCH] Choose the correct type to use with smartctl --- smart.run.test.sh | 78 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/smart.run.test.sh b/smart.run.test.sh index be4145a..b5c7d75 100755 --- a/smart.run.test.sh +++ b/smart.run.test.sh @@ -15,9 +15,71 @@ c_reset='\033[0m' temp_dir=$(mktemp -d -t smart.run.test-XXXXXX.tmp) smart_device_list="${temp_dir}/smart.device.list" - # ]]] +# Functions +## Test if a disk really support SMART [[[ +## Smartctl can give an health status even without a full support +## of SMART for some type (eg. scsi or megaraid). +## Exemple : SMART support is: Unavailable - device lacks SMART capability. +is_disk_path_support_smart() { + _disk_path="${1}" + _disk_type="${2}" + + _smarctl_support_result="${temp_dir}/smart.support.$(basename "${_disk_path}")" + + smart_support_msg="" + + [ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG : is_disk_path_support_smart func − check if SMART is supported on : ${_disk_path} with ${_disk_type} TYPE." + + ## Create file + true > "${_smarctl_support_result}" + + ## Grep only "support" lines from disk's informations + smartctl -d "${_disk_type}" -i -- "${_disk_path}" | grep -E "^SMART support is:" -- >> "${_smarctl_support_result}" + + ## If the file is not empty + if test -s "${_smarctl_support_result}"; then + ## Parse all "support" lines + while IFS= read -r _line; do + ### If a line doesn't contain enable or available + if ! printf -- '%s' "${_line}" | grep -q -E -- "(Enabled|Available)" + then + smart_support_msg="${_line}" + [ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG : is_disk_path_support_smart func − SMART is not fully supported on : ${_disk_path} with ${_disk_type} TYPE. See smartctl informations :\n${smart_support_msg}" + fi + done < "${_smarctl_support_result}" + else + smart_support_msg="ERROR : Enable to open ${_disk_path} DEVICE with ${_disk_type} TYPE. Be sure to have sufficient permission for this device." + printf "${c_redb}%-6b${c_reset}\n" "ERROR : Enable to open ${_disk_path} DEVICE with ${_disk_type} TYPE. Be sure to have sufficient permission for this device." + fi + +} +## ]]] +## Test the type of disk with smartctl [[[ +## Cause the scanned one might not be the one to use +choose_correct_type() { + _disk_path="${1}" + _scanned_disk_type="${2}" + _default_disk_type="auto" + + disk_type="" + + for _test_disk_type in "${_default_disk_type}" "${_scanned_disk_type}"; do + is_disk_path_support_smart "${_disk_path}" "${_test_disk_type}" + + ## If no message, the type is correct + if [ -z "${smart_support_msg}" ]; then + disk_type="${_test_disk_type}" + [ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG : choose_correct_disk_type func − SMART seems fully supported on : ${_disk_path} with ${_test_disk_type} TYPE." + return + fi + + done + +} +## ]]] + # Create files true > "${smart_device_list}" @@ -27,10 +89,18 @@ smartctl --scan >> "${smart_device_list}" if test -s "${smart_device_list}"; then while IFS= read -r LINE; do ## Get device path - scanned_disk=$(echo "${LINE}" | cut -d" " -f1) + disk_path=$(echo "${LINE}" | cut -d" " -f1) ## Try to determine the best type - scanned_type=$(echo "${LINE}" | cut -d" " -f3) - [ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG : smartctl scan ${scanned_disk} with type ${scanned_type}." + scanned_disk_type=$(echo "${LINE}" | cut -d" " -f3) + [ "${debug}" -eq "0" ] && printf "\n${c_magentab}%-6b${c_reset}\n" "DEBUG : smartctl scan ${disk_path} with TYPE ${scanned_disk_type}." + + choose_correct_type "${disk_path}" "${scanned_disk_type}" + ## If no correct type was found for this device + if [ -z "${disk_type}" ]; then + [ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG : SMART is not fully supported on ${disk_path}." + else + [ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG : for disk ${disk_path}, now use TYPE ${disk_type}." + fi done < "${smart_device_list}"