mirror of
https://github.com/prometheus-community/smartctl_exporter.git
synced 2024-11-16 01:33:07 +01:00
collect-smartctl-json: shell fixes for script (#178)
Signed-off-by: Konstantin Shalygin <k0ste@k0ste.ru>
This commit is contained in:
parent
3c27af18ec
commit
9db6746825
1 changed files with 23 additions and 14 deletions
|
@ -8,7 +8,8 @@ data_dir="${script_dir}/smartctl-data"
|
||||||
|
|
||||||
# The original script used --xall but that doesn't work
|
# The original script used --xall but that doesn't work
|
||||||
# This matches the command in readSMARTctl()
|
# 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
|
# Determine the json query tool to use
|
||||||
if command -v jq >/dev/null; then
|
if command -v jq >/dev/null; then
|
||||||
|
@ -18,18 +19,19 @@ elif command -v yq >/dev/null; then
|
||||||
json_tool="yq"
|
json_tool="yq"
|
||||||
json_args="--unwrapScalar"
|
json_args="--unwrapScalar"
|
||||||
else
|
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
|
exit 1
|
||||||
fi
|
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
|
# Not root and sudo doesn't exist
|
||||||
echo "sudo does not exist. Please run this as root"
|
echo "sudo does not exist. Please run this as root"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
SUDO="sudo"
|
SUDO="sudo"
|
||||||
if [[ $UID = 0 ]]; then
|
if [[ "${UID}" -eq 0 ]]; then
|
||||||
# Don't use sudo if root
|
# Don't use sudo if root
|
||||||
SUDO=""
|
SUDO=""
|
||||||
fi
|
fi
|
||||||
|
@ -37,18 +39,25 @@ fi
|
||||||
[[ ! -d "${data_dir}" ]] && mkdir --parents "${data_dir}"
|
[[ ! -d "${data_dir}" ]] && mkdir --parents "${data_dir}"
|
||||||
|
|
||||||
if [[ $# -ne 0 ]]; then
|
if [[ $# -ne 0 ]]; then
|
||||||
devices="$1"
|
devices="${1}"
|
||||||
else
|
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
|
fi
|
||||||
|
|
||||||
for device in $devices; do
|
for device in "${devices[@]}"
|
||||||
|
do
|
||||||
echo -n "Collecting data for '${device}'..."
|
echo -n "Collecting data for '${device}'..."
|
||||||
data=$($SUDO smartctl $smartctl_args "${device}")
|
# shellcheck disable=SC2086
|
||||||
type=$(echo "${data}" | $json_tool $json_args '.device.type')
|
data="$($SUDO smartctl ${smartctl_args} ${device})"
|
||||||
family=$(echo "${data}" | $json_tool $json_args '.model_family' | tr ' ' '_')
|
type="$(echo "${data}" | "${json_tool}" "${json_args}" '.device.type')"
|
||||||
model=$(echo "${data}" | $json_tool $json_args '.model_name' | tr ' ' '_')
|
family="$(echo "${data}" | "${json_tool}" "${json_args}" \
|
||||||
device_name=$(basename "${device}")
|
'select(.model_family != null) | .model_family | sub(" |/" ; "_" ; "g")')"
|
||||||
echo -e "\tSaving to ${type}-${family}-${model}-${device_name}.json"
|
model="$(echo "${data}" | "${json_tool}" "${json_args}" \
|
||||||
echo "${data}" > "${data_dir}/${type}-${family}-${model}-${device_name}.json"
|
'.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
|
done
|
||||||
|
|
Loading…
Reference in a new issue