mirror of
https://github.com/prometheus-community/smartctl_exporter.git
synced 2024-11-23 01:43:07 +01:00
Chg: Impvoe the JSON collection script; now requires jq/yq
Signed-off-by: David Randall <David@NiceGuyIT.biz>
This commit is contained in:
parent
536ca0c8b8
commit
8193e8eb3b
3 changed files with 55 additions and 27 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -2,8 +2,7 @@
|
||||||
/.build
|
/.build
|
||||||
/.release
|
/.release
|
||||||
/.tarballs
|
/.tarballs
|
||||||
debug
|
debug/
|
||||||
*.json
|
|
||||||
|
|
||||||
Manifest
|
Manifest
|
||||||
smartctl_exporter
|
smartctl_exporter
|
||||||
|
|
54
collect-smartctl-json.sh
Executable file
54
collect-smartctl-json.sh
Executable file
|
@ -0,0 +1,54 @@
|
||||||
|
#! /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"
|
||||||
|
|
||||||
|
# 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 "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
|
||||||
|
# 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
|
||||||
|
# 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 '.devices[].name')
|
||||||
|
fi
|
||||||
|
|
||||||
|
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"
|
||||||
|
done
|
|
@ -1,25 +0,0 @@
|
||||||
#! /bin/bash
|
|
||||||
|
|
||||||
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
|
||||||
|
|
||||||
# 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"
|
|
||||||
|
|
||||||
[[ ! -d "${script_dir}/debug" ]] && mkdir --parents "${script_dir}/debug"
|
|
||||||
|
|
||||||
if command -v jq >/dev/null; then
|
|
||||||
devices=$(smartctl --scan --json | jq --raw-output '.devices[].name')
|
|
||||||
elif command -v yq >/dev/null; then
|
|
||||||
devices=$(smartctl --scan --json | yq --unwrapScalar '.devices[].name')
|
|
||||||
elif command -v awk >/dev/null; then
|
|
||||||
devices=$(smartctl --scan | awk '{ print($1) }')
|
|
||||||
else
|
|
||||||
devices=$(smartctl --scan | cut -d ' ' -f 1)
|
|
||||||
fi
|
|
||||||
|
|
||||||
for device in $devices; do
|
|
||||||
echo "Collecting data for '${device}'"
|
|
||||||
# shellcheck disable=SC2086
|
|
||||||
sudo smartctl $smartctl_args "${device}" > "${script_dir}/debug/$(basename "${device}").json"
|
|
||||||
done
|
|
Loading…
Reference in a new issue