Skip to content

Commit 4242258

Browse files
smartmon.sh: locate smartctl tool on non-Linux systems (#240)
Allow execution of `smartctl` even if it is installed in an unexpected (for Linux) path. Some OS, (e.g.: FreeBSD) do not install `smartctl` under the `usr/sbin/` path, but instead use another path like `/usr/local/sbin/`. This commit makes use of the `which` command to locate the `smartctl` in the `PATH` and execute from there. As such it does not make any assumption where `smartctl` is installed, improving portability. --------- Signed-off-by: cristian-caloghera <[email protected]>
1 parent 91d88dd commit 4242258

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

smartmon.sh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ format_output() {
167167
awk -F'{' "${output_format_awk}"
168168
}
169169

170-
smartctl_version="$(/usr/sbin/smartctl -V | awk 'NR==1 && $1 == "smartctl" {print $2}')"
170+
smartctl_version="$(smartctl -V | awk 'NR==1 && $1 == "smartctl" {print $2}')"
171171

172172
echo "smartctl_version{version=\"${smartctl_version}\"} 1" | format_output
173173

@@ -176,28 +176,28 @@ if [[ ${smartctl_version%.*} -lt 6 ]]; then
176176
exit 0
177177
fi
178178

179-
device_list="$(/usr/sbin/smartctl --scan-open | awk '/^\/dev/{print $1 "|" $3}')"
179+
device_list="$(smartctl --scan-open | awk '/^\/dev/{print $1 "|" $3}')"
180180

181181
for device in ${device_list}; do
182182
disk="$(echo "${device}" | cut -f1 -d'|')"
183183
type="$(echo "${device}" | cut -f2 -d'|')"
184184
active=1
185185
echo "smartctl_run{disk=\"${disk}\",type=\"${type}\"}" "$(TZ=UTC date '+%s')"
186186
# Check if the device is in a low-power mode
187-
/usr/sbin/smartctl -n standby -d "${type}" "${disk}" > /dev/null || active=0
187+
smartctl -n standby -d "${type}" "${disk}" > /dev/null || active=0
188188
echo "device_active{disk=\"${disk}\",type=\"${type}\"}" "${active}"
189189
# Skip further metrics to prevent the disk from spinning up
190190
test ${active} -eq 0 && continue
191191
# Get the SMART information and health
192-
/usr/sbin/smartctl -i -H -d "${type}" "${disk}" | parse_smartctl_info "${disk}" "${type}"
192+
smartctl -i -H -d "${type}" "${disk}" | parse_smartctl_info "${disk}" "${type}"
193193
# Get the SMART attributes
194194
case ${type} in
195-
sat) /usr/sbin/smartctl -A -d "${type}" "${disk}" | parse_smartctl_attributes "${disk}" "${type}" ;;
196-
sat+megaraid*) /usr/sbin/smartctl -A -d "${type}" "${disk}" | parse_smartctl_attributes "${disk}" "${type}" ;;
197-
scsi) /usr/sbin/smartctl -A -d "${type}" "${disk}" | parse_smartctl_scsi_attributes "${disk}" "${type}" ;;
198-
megaraid*) /usr/sbin/smartctl -A -d "${type}" "${disk}" | parse_smartctl_scsi_attributes "${disk}" "${type}" ;;
199-
nvme*) /usr/sbin/smartctl -A -d "${type}" "${disk}" | parse_smartctl_scsi_attributes "${disk}" "${type}" ;;
200-
usbprolific) /usr/sbin/smartctl -A -d "${type}" "${disk}" | parse_smartctl_attributes "${disk}" "${type}" ;;
195+
sat) smartctl -A -d "${type}" "${disk}" | parse_smartctl_attributes "${disk}" "${type}" ;;
196+
sat+megaraid*) smartctl -A -d "${type}" "${disk}" | parse_smartctl_attributes "${disk}" "${type}" ;;
197+
scsi) smartctl -A -d "${type}" "${disk}" | parse_smartctl_scsi_attributes "${disk}" "${type}" ;;
198+
megaraid*) smartctl -A -d "${type}" "${disk}" | parse_smartctl_scsi_attributes "${disk}" "${type}" ;;
199+
nvme*) smartctl -A -d "${type}" "${disk}" | parse_smartctl_scsi_attributes "${disk}" "${type}" ;;
200+
usbprolific) smartctl -A -d "${type}" "${disk}" | parse_smartctl_attributes "${disk}" "${type}" ;;
201201
*)
202202
(>&2 echo "disk type is not sat, scsi, nvme or megaraid but ${type}")
203203
exit

0 commit comments

Comments
 (0)