Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collect-smartctl-json: shell fixes for script #178

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions collect-smartctl-json.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ data_dir="${script_dir}/smartctl-data"

# The original script used --xall but that doesn't work
# This matches the command in readSMARTctl()
smartctl_args="--json --info --health --attributes --tolerance=verypermissive --nocheck=standby --format=brief --log=error"
smartctl_args="--json --info --health --attributes --tolerance=verypermissive \
--nocheck=standby --format=brief --log=error"

# Determine the json query tool to use
if command -v jq >/dev/null; then
Expand All @@ -18,37 +19,45 @@ elif command -v yq >/dev/null; then
json_tool="yq"
json_args="--unwrapScalar"
else
echo "One of 'yq' or 'jq' is required. Please try again after installing one of them"
echo -e "One of 'yq' or 'jq' is required. Please try again after \
installing one of them"
exit 1
fi

if [[ $UID != 0 ]] && ! command -v sudo >/dev/null; then
if [[ ! "${UID}" -eq 0 ]] && ! command -v sudo >/dev/null; then
# Not root and sudo doesn't exist
echo "sudo does not exist. Please run this as root"
exit 1
fi

SUDO="sudo"
if [[ $UID = 0 ]]; then
if [[ "${UID}" -eq 0 ]]; then
# Don't use sudo if root
SUDO=""
fi

[[ ! -d "${data_dir}" ]] && mkdir --parents "${data_dir}"

if [[ $# -ne 0 ]]; then
devices="$1"
devices="${1}"
else
devices=$(smartctl --scan --json | $json_tool $json_args '.devices[].name')
devices="$(smartctl --scan --json | "${json_tool}" "${json_args}" \
'.devices[].name')"
mapfile -t devices <<< "${devices[@]}"
fi

for device in $devices; do
for device in "${devices[@]}"
do
echo -n "Collecting data for '${device}'..."
data=$($SUDO smartctl $smartctl_args "${device}")
type=$(echo "${data}" | $json_tool $json_args '.device.type')
family=$(echo "${data}" | $json_tool $json_args '.model_family' | tr ' ' '_')
model=$(echo "${data}" | $json_tool $json_args '.model_name' | tr ' ' '_')
device_name=$(basename "${device}")
echo -e "\tSaving to ${type}-${family}-${model}-${device_name}.json"
echo "${data}" > "${data_dir}/${type}-${family}-${model}-${device_name}.json"
# shellcheck disable=SC2086
data="$($SUDO smartctl ${smartctl_args} ${device})"
type="$(echo "${data}" | "${json_tool}" "${json_args}" '.device.type')"
family="$(echo "${data}" | "${json_tool}" "${json_args}" \
'select(.model_family != null) | .model_family | sub(" |/" ; "_" ; "g")')"
model="$(echo "${data}" | "${json_tool}" "${json_args}" \
'.model_name | sub(" |/" ; "_" ; "g")')"
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"
done