smartctl_exporter/collect-smartctl-json.sh

70 lines
2.1 KiB
Bash
Raw Normal View History

#! /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()
smartctl_args="--json --info --health --attributes --tolerance=verypermissive \
--nocheck=standby --format=brief --log=error"
collect-smartctl-json: shell fixes for script (#188) * Remove `"`, `(` and `)` from file names (see #188) * Added regex ignore for some devices, fixes: ``` Collecting data for '/dev/sda'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sda.json Collecting data for '/dev/sdb'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sdb.json Collecting data for '/dev/bus/0'...jq: error (at <stdin>:34): null (null) cannot be matched, as it is not a string Saving to null-null--0.json Collecting data for '/dev/bus/0'...jq: error (at <stdin>:34): null (null) cannot be matched, as it is not a string Saving to null-null--0.json Collecting data for '/dev/nvme0'... Saving to nvme-null-HUSMR7632BHP301-nvme0.json Collecting data for '/dev/nvme1'... Saving to nvme-null-HUSMR7632BHP301-nvme1.json Collecting data for '/dev/nvme2'... Saving to nvme-null-HUSMR7632BHP301-nvme2.json Collecting data for '/dev/nvme3'... Saving to nvme-null-HUSMR7632BHP301-nvme3.json Collecting data for '/dev/nvme4'... Saving to nvme-null-HUSMR7632BHP301-nvme4.json ``` The `/dev/bus/*` is some pseudo-device comes with Broadcom driver, after fix: ``` Collecting data for '/dev/sda'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sda.json Collecting data for '/dev/sdb'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sdb.json Collecting data for '/dev/nvme0'... Saving to nvme-null-HUSMR7632BHP301-nvme0.json Collecting data for '/dev/nvme1'... Saving to nvme-null-HUSMR7632BHP301-nvme1.json Collecting data for '/dev/nvme2'... Saving to nvme-null-HUSMR7632BHP301-nvme2.json Collecting data for '/dev/nvme3'... Saving to nvme-null-HUSMR7632BHP301-nvme3.json Collecting data for '/dev/nvme4'... Saving to nvme-null-HUSMR7632BHP301-nvme4.json ``` Signed-off-by: Konstantin Shalygin <k0ste@k0ste.ru>
2023-12-30 10:55:10 +01:00
# Ignore this devices
smartctl_ignore_dev_regex="^(/dev/bus)"
# 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
echo -e "One of 'yq' or 'jq' is required. Please try again after \
installing one of them"
exit 1
fi
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}" -eq 0 ]]; then
# Don't use sudo if root
SUDO=""
fi
[[ ! -d "${data_dir}" ]] && mkdir --parents "${data_dir}"
if [[ $# -ne 0 ]]; then
devices="${1}"
else
devices="$(smartctl --scan --json | "${json_tool}" "${json_args}" \
collect-smartctl-json: shell fixes for script (#188) * Remove `"`, `(` and `)` from file names (see #188) * Added regex ignore for some devices, fixes: ``` Collecting data for '/dev/sda'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sda.json Collecting data for '/dev/sdb'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sdb.json Collecting data for '/dev/bus/0'...jq: error (at <stdin>:34): null (null) cannot be matched, as it is not a string Saving to null-null--0.json Collecting data for '/dev/bus/0'...jq: error (at <stdin>:34): null (null) cannot be matched, as it is not a string Saving to null-null--0.json Collecting data for '/dev/nvme0'... Saving to nvme-null-HUSMR7632BHP301-nvme0.json Collecting data for '/dev/nvme1'... Saving to nvme-null-HUSMR7632BHP301-nvme1.json Collecting data for '/dev/nvme2'... Saving to nvme-null-HUSMR7632BHP301-nvme2.json Collecting data for '/dev/nvme3'... Saving to nvme-null-HUSMR7632BHP301-nvme3.json Collecting data for '/dev/nvme4'... Saving to nvme-null-HUSMR7632BHP301-nvme4.json ``` The `/dev/bus/*` is some pseudo-device comes with Broadcom driver, after fix: ``` Collecting data for '/dev/sda'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sda.json Collecting data for '/dev/sdb'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sdb.json Collecting data for '/dev/nvme0'... Saving to nvme-null-HUSMR7632BHP301-nvme0.json Collecting data for '/dev/nvme1'... Saving to nvme-null-HUSMR7632BHP301-nvme1.json Collecting data for '/dev/nvme2'... Saving to nvme-null-HUSMR7632BHP301-nvme2.json Collecting data for '/dev/nvme3'... Saving to nvme-null-HUSMR7632BHP301-nvme3.json Collecting data for '/dev/nvme4'... Saving to nvme-null-HUSMR7632BHP301-nvme4.json ``` Signed-off-by: Konstantin Shalygin <k0ste@k0ste.ru>
2023-12-30 10:55:10 +01:00
".devices[].name | select(test(\"${smartctl_ignore_dev_regex}\") | not)")"
mapfile -t devices <<< "${devices[@]}"
fi
for device in "${devices[@]}"
do
echo -n "Collecting data for '${device}'..."
# shellcheck disable=SC2086
data="$($SUDO smartctl ${smartctl_args} ${device})"
# Accommodate a smartmontools pre-7.3 bug
data=${data#" Pending defect count:"}
type="$(echo "${data}" | "${json_tool}" "${json_args}" '.device.type')"
family="$(echo "${data}" | "${json_tool}" "${json_args}" \
collect-smartctl-json: shell fixes for script (#188) * Remove `"`, `(` and `)` from file names (see #188) * Added regex ignore for some devices, fixes: ``` Collecting data for '/dev/sda'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sda.json Collecting data for '/dev/sdb'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sdb.json Collecting data for '/dev/bus/0'...jq: error (at <stdin>:34): null (null) cannot be matched, as it is not a string Saving to null-null--0.json Collecting data for '/dev/bus/0'...jq: error (at <stdin>:34): null (null) cannot be matched, as it is not a string Saving to null-null--0.json Collecting data for '/dev/nvme0'... Saving to nvme-null-HUSMR7632BHP301-nvme0.json Collecting data for '/dev/nvme1'... Saving to nvme-null-HUSMR7632BHP301-nvme1.json Collecting data for '/dev/nvme2'... Saving to nvme-null-HUSMR7632BHP301-nvme2.json Collecting data for '/dev/nvme3'... Saving to nvme-null-HUSMR7632BHP301-nvme3.json Collecting data for '/dev/nvme4'... Saving to nvme-null-HUSMR7632BHP301-nvme4.json ``` The `/dev/bus/*` is some pseudo-device comes with Broadcom driver, after fix: ``` Collecting data for '/dev/sda'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sda.json Collecting data for '/dev/sdb'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sdb.json Collecting data for '/dev/nvme0'... Saving to nvme-null-HUSMR7632BHP301-nvme0.json Collecting data for '/dev/nvme1'... Saving to nvme-null-HUSMR7632BHP301-nvme1.json Collecting data for '/dev/nvme2'... Saving to nvme-null-HUSMR7632BHP301-nvme2.json Collecting data for '/dev/nvme3'... Saving to nvme-null-HUSMR7632BHP301-nvme3.json Collecting data for '/dev/nvme4'... Saving to nvme-null-HUSMR7632BHP301-nvme4.json ``` Signed-off-by: Konstantin Shalygin <k0ste@k0ste.ru>
2023-12-30 10:55:10 +01:00
'select(.model_family != null) | .model_family | sub(" |/" ; "_" ; "g")
| sub("\"|\\(|\\)" ; "" ; "g")')"
model="$(echo "${data}" | "${json_tool}" "${json_args}" \
collect-smartctl-json: shell fixes for script (#188) * Remove `"`, `(` and `)` from file names (see #188) * Added regex ignore for some devices, fixes: ``` Collecting data for '/dev/sda'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sda.json Collecting data for '/dev/sdb'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sdb.json Collecting data for '/dev/bus/0'...jq: error (at <stdin>:34): null (null) cannot be matched, as it is not a string Saving to null-null--0.json Collecting data for '/dev/bus/0'...jq: error (at <stdin>:34): null (null) cannot be matched, as it is not a string Saving to null-null--0.json Collecting data for '/dev/nvme0'... Saving to nvme-null-HUSMR7632BHP301-nvme0.json Collecting data for '/dev/nvme1'... Saving to nvme-null-HUSMR7632BHP301-nvme1.json Collecting data for '/dev/nvme2'... Saving to nvme-null-HUSMR7632BHP301-nvme2.json Collecting data for '/dev/nvme3'... Saving to nvme-null-HUSMR7632BHP301-nvme3.json Collecting data for '/dev/nvme4'... Saving to nvme-null-HUSMR7632BHP301-nvme4.json ``` The `/dev/bus/*` is some pseudo-device comes with Broadcom driver, after fix: ``` Collecting data for '/dev/sda'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sda.json Collecting data for '/dev/sdb'... Saving to sat-Intel_S4510_S4610_S4500_S4600_Series_SSDs-INTEL_SSDSC2KB240G8-sdb.json Collecting data for '/dev/nvme0'... Saving to nvme-null-HUSMR7632BHP301-nvme0.json Collecting data for '/dev/nvme1'... Saving to nvme-null-HUSMR7632BHP301-nvme1.json Collecting data for '/dev/nvme2'... Saving to nvme-null-HUSMR7632BHP301-nvme2.json Collecting data for '/dev/nvme3'... Saving to nvme-null-HUSMR7632BHP301-nvme3.json Collecting data for '/dev/nvme4'... Saving to nvme-null-HUSMR7632BHP301-nvme4.json ``` Signed-off-by: Konstantin Shalygin <k0ste@k0ste.ru>
2023-12-30 10:55:10 +01:00
'.model_name | sub(" |/" ; "_" ; "g") | 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