add script to monitor disk power state

This commit is contained in:
Daniel Langbein 2023-10-08 20:36:51 +02:00
parent b2e0d29801
commit eea34a5b5c
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002

44
examples/disk-state.sh Normal file
View File

@ -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