2023-11-16 02:35:53 +01:00
|
|
|
#! /bin/bash
|
|
|
|
|
|
|
|
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
|
|
|
|
|
|
|
# Data directory to dump smartctl output
|
|
|
|
# This directory will be created if it doesn't exist
|
|
|
|
data_dir="${script_dir}/smartctl-data"
|
|
|
|
|
|
|
|
# The original script used --xall but that doesn't work
|
|
|
|
# This matches the command in readSMARTctl()
|
2023-11-20 16:33:28 +01:00
|
|
|
smartctl_args="--json --info --health --attributes --tolerance=verypermissive \
|
|
|
|
--nocheck=standby --format=brief --log=error"
|
2023-11-16 02:35:53 +01:00
|
|
|
|
2023-12-30 10:55:10 +01:00
|
|
|
# Ignore this devices
|
|
|
|
smartctl_ignore_dev_regex="^(/dev/bus)"
|
|
|
|
|
2023-11-16 02:35:53 +01:00
|
|
|
# Determine the json query tool to use
|
|
|
|
if command -v jq >/dev/null; then
|
|
|
|
json_tool="jq"
|
|
|
|
json_args="--raw-output"
|
|
|
|
elif command -v yq >/dev/null; then
|
|
|
|
json_tool="yq"
|
|
|
|
json_args="--unwrapScalar"
|
|
|
|
else
|
2023-11-20 16:33:28 +01:00
|
|
|
echo -e "One of 'yq' or 'jq' is required. Please try again after \
|
|
|
|
installing one of them"
|
2023-11-16 02:35:53 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-11-20 16:33:28 +01:00
|
|
|
if [[ ! "${UID}" -eq 0 ]] && ! command -v sudo >/dev/null; then
|
2023-11-16 02:35:53 +01:00
|
|
|
# Not root and sudo doesn't exist
|
|
|
|
echo "sudo does not exist. Please run this as root"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
SUDO="sudo"
|
2023-11-20 16:33:28 +01:00
|
|
|
if [[ "${UID}" -eq 0 ]]; then
|
2023-11-16 02:35:53 +01:00
|
|
|
# Don't use sudo if root
|
|
|
|
SUDO=""
|
|
|
|
fi
|
|
|
|
|
|
|
|
[[ ! -d "${data_dir}" ]] && mkdir --parents "${data_dir}"
|
|
|
|
|
|
|
|
if [[ $# -ne 0 ]]; then
|
2023-11-20 16:33:28 +01:00
|
|
|
devices="${1}"
|
2023-11-16 02:35:53 +01:00
|
|
|
else
|
2023-11-20 16:33:28 +01:00
|
|
|
devices="$(smartctl --scan --json | "${json_tool}" "${json_args}" \
|
2023-12-30 10:55:10 +01:00
|
|
|
".devices[].name | select(test(\"${smartctl_ignore_dev_regex}\") | not)")"
|
2023-11-20 16:33:28 +01:00
|
|
|
mapfile -t devices <<< "${devices[@]}"
|
2023-11-16 02:35:53 +01:00
|
|
|
fi
|
|
|
|
|
2023-11-20 16:33:28 +01:00
|
|
|
for device in "${devices[@]}"
|
|
|
|
do
|
2023-11-16 02:35:53 +01:00
|
|
|
echo -n "Collecting data for '${device}'..."
|
2023-11-20 16:33:28 +01:00
|
|
|
# shellcheck disable=SC2086
|
|
|
|
data="$($SUDO smartctl ${smartctl_args} ${device})"
|
2024-11-08 09:02:29 +01:00
|
|
|
# Accommodate a smartmontools pre-7.3 bug
|
|
|
|
data=${data#" Pending defect count:"}
|
2023-11-20 16:33:28 +01:00
|
|
|
type="$(echo "${data}" | "${json_tool}" "${json_args}" '.device.type')"
|
|
|
|
family="$(echo "${data}" | "${json_tool}" "${json_args}" \
|
2023-12-30 10:55:10 +01:00
|
|
|
'select(.model_family != null) | .model_family | sub(" |/" ; "_" ; "g")
|
|
|
|
| sub("\"|\\(|\\)" ; "" ; "g")')"
|
2023-11-20 16:33:28 +01:00
|
|
|
model="$(echo "${data}" | "${json_tool}" "${json_args}" \
|
2023-12-30 10:55:10 +01:00
|
|
|
'.model_name | sub(" |/" ; "_" ; "g") | sub("\"|\\(|\\)" ; "" ; "g")')"
|
2023-11-20 16:33:28 +01:00
|
|
|
device_name="$(basename "${device}")"
|
|
|
|
echo -e "\tSaving to ${type}-${family:=null}-${model}-${device_name}.json"
|
|
|
|
echo "${data}" > \
|
|
|
|
"${data_dir}/${type}-${family:=null}-${model}-${device_name}.json"
|
2023-11-16 02:35:53 +01:00
|
|
|
done
|