From eea34a5b5cc52b59826b7a68e255994049c38297 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Sun, 8 Oct 2023 20:36:51 +0200 Subject: [PATCH] add script to monitor disk power state --- examples/disk-state.sh | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 examples/disk-state.sh diff --git a/examples/disk-state.sh b/examples/disk-state.sh new file mode 100644 index 0000000..fd3e814 --- /dev/null +++ b/examples/disk-state.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +# +# Watch disk power state (HDD spin-up and spin-down events). +# + +# Arguments. +disk='/dev/disk/by-id/ata-WDC_WD40EFRX-68N32N0_WD-WCC7K0CPF0N1' + +# Assert that this script is run as root. +if [ "$(id -u)" -ne 0 ]; then + printf '%s\n' 'Error: This script has to be run as root user.' 1>&2 + exit 1 +fi + +trap ctrl_c INT +function ctrl_c() { + echo "End: $(date +"%Y-%m-%dT%H:%M:%S%z")" + exit 0 +} + +function notify-spin-down(){ + while hdparm -C "${disk}" | grep 'active/idle' >/dev/null; do + sleep 15s + done + echo "$(date +"%Y-%m-%dT%H:%M:%S%z"): ${disk} has spun down" +} +function notify-spin-up(){ + while hdparm -C "${disk}" | grep 'standby' >/dev/null; do + sleep 15s + done + echo "$(date +"%Y-%m-%dT%H:%M:%S%z"): ${disk} has spun up" +} +function main(){ + echo "Start: $(date +"%Y-%m-%dT%H:%M:%S%z")" + hdparm -C "${disk}" | grep 'active/idle' >/dev/null && notify-spin-down + hdparm -C "${disk}" | grep 'standby' >/dev/null && { + while :; do + notify-spin-up + notify-spin-down + done + } + echo "${disk} has unknown state" +} +main