Choose the correct type to use with smartctl

This commit is contained in:
Jeremy Gardais 2020-02-27 15:15:47 +01:00
parent 17734c77a1
commit e7300d63f1
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 74 additions and 4 deletions

View File

@ -15,9 +15,71 @@ c_reset='\033[0m'
temp_dir=$(mktemp -d -t smart.run.test-XXXXXX.tmp) temp_dir=$(mktemp -d -t smart.run.test-XXXXXX.tmp)
smart_device_list="${temp_dir}/smart.device.list" 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 # Create files
true > "${smart_device_list}" true > "${smart_device_list}"
@ -27,10 +89,18 @@ smartctl --scan >> "${smart_device_list}"
if test -s "${smart_device_list}"; then if test -s "${smart_device_list}"; then
while IFS= read -r LINE; do while IFS= read -r LINE; do
## Get device path ## Get device path
scanned_disk=$(echo "${LINE}" | cut -d" " -f1) disk_path=$(echo "${LINE}" | cut -d" " -f1)
## Try to determine the best type ## Try to determine the best type
scanned_type=$(echo "${LINE}" | cut -d" " -f3) scanned_disk_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}." [ "${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}" done < "${smart_device_list}"