-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
spindown_timer.sh
executable file
·417 lines (372 loc) · 12.9 KB
/
spindown_timer.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env bash
# ##################################################
# TrueNAS HDD Spindown Timer
# Monitors drive I/O and forces HDD spindown after a given idle period.
#
# Version: 2.0.1
#
# See: https://github.com/ngandrass/truenas-spindown-timer
#
#
# MIT License
#
# Copyright (c) 2022 Niels Gandraß <niels@gandrass.de>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ##################################################
TIMEOUT=3600 # Default timeout before considering a drive as idle
POLL_TIME=600 # Default time to wait during a single iostat call
IGNORED_DRIVES="" # Default list of drives that are never spun down
MANUAL_MODE=0 # Default manual mode setting
QUIET=0 # Default quiet mode setting
VERBOSE=0 # Default verbosity level
DRYRUN=0 # Default for dryrun option
SHUTDOWN_TIMEOUT=0 # Default shutdown timeout (0 == no shutdown)
declare -A DRIVES # Associative array for detected drives
DISK_PARM_TOOL=camcontrol # Default disk parameter tool to use (camcontrol OR hdparm)
##
# Prints the help/usage message
##
function print_usage() {
cat << EOF
Usage: $0 [-h] [-q] [-v] [-d] [-m] [-t TIMEOUT] [-p POLL_TIME] [-i DRIVE] [-s TIMEOUT]
Monitors drive I/O and forces HDD spindown after a given idle period.
Resistant to S.M.A.R.T. reads.
A drive is considered as idle and is spun down if there has been no I/O
operations on it for at least TIMEOUT seconds. I/O requests are detected
during intervals with a length of POLL_TIME seconds. Detected reads or
writes reset the drives timer back to TIMEOUT.
Options:
-q : Quiet mode. Outputs are suppressed if flag is present.
-v : Verbose mode. Prints additonal information during execution.
-d : Dry run. No actual spindown is performed.
-m : Manual mode. If this flag is set, the automatic drive detection
is disabled.
This inverts the -i switch which then needs to be used to supply
each drive to monitor. All other drives will be ignored.
-t TIMEOUT : Number of seconds to wait for I/O in total before considering
a drive as idle.
-p POLL_TIME : Number of seconds to wait for I/O during a single iostat call.
-i DRIVE : In automatic drive detection mode (default): Ignores the given
drive and never issue a spindown command for it.
In manual mode [-m]: Only monitor the specified drives.
Multiple drives can be given by repeating the -i switch.
-s TIMEOUT : Shutdown timeout, if no drive is active for TIMEOUT seconds,
the system will be shut down
-h : Print this help message.
Example usage:
$0
$0 -q -t 3600 -p 600 -i ada0 -i ada1
$0 -q -m -i ada6 -i ada7 -i da0
EOF
}
##
# Writes argument $1 to stdout if $QUIET is not set
#
# Arguments:
# $1 Message to write to stdout
##
function log() {
if [[ $QUIET -eq 0 ]]; then
echo $1
fi
}
##
# Writes argument $1 to stdout if $VERBOSE is set and $QUIET is not set
#
# Arguments:
# $1 Message to write to stdout
##
function log_verbose() {
if [[ $VERBOSE -eq 1 ]]; then
if [[ $QUIET -eq 0 ]]; then
echo $1
fi
fi
}
##
# Writes argument $1 to stderr. Ignores $QUIET.
#
# Arguments:
# $1 Message to write to stderr
##
function log_error() {
>&2 echo "[ERROR]: $1"
}
##
# Determines which tool to access disk parameters is available. This
# differentiates between TrueNAS Core and TrueNAS SCALE.
#
# Return: Command to use to access disk parameters
#
##
function detect_disk_parm_tool() {
local SUPPORTED_DISK_PARM_TOOLS
SUPPORTED_DISK_PARM_TOOLS="camcontrol hdparm"
for tool in ${SUPPORTED_DISK_PARM_TOOLS}; do
if [[ -n $(which ${tool}) ]]; then
echo "${tool}"
return
fi
done
log_error "No supported disk parameter tool found."
exit 1
}
##
# Detects all connected drives and whether they are ATA or SCSI drives.
# Drives listed in $IGNORE_DRIVES will be excluded.
#
# Note: This function populates the $DRIVES array directly.
##
function detect_drives() {
local DRIVE_IDS
# Detect relevant drives identifiers
if [[ $MANUAL_MODE -eq 1 ]]; then
# In manual mode the ignored drives become the explicitly monitored drives
DRIVE_IDS=" ${IGNORED_DRIVES} "
else
DRIVE_IDS=`iostat -x | grep -E '^(ada|da|sd)' | awk '{printf $1 " "}'`
DRIVE_IDS=" ${DRIVE_IDS} " # Space padding must be kept for pattern matching
# Remove ignored drives
for drive in ${IGNORED_DRIVES[@]}; do
DRIVE_IDS=`sed "s/ ${drive} / /g" <<< ${DRIVE_IDS}`
done
fi
# Detect protocol type (ATA or SCSI) for each drive and populate $DRIVES array
local DISK_IS_ATA
for drive in ${DRIVE_IDS}; do
case $DISK_PARM_TOOL in
"camcontrol") DISK_IS_ATA=$(camcontrol identify $drive |& grep -E "^protocol(.*)ATA");;
"hdparm") DISK_IS_ATA=$(hdparm -I "/dev/$drive" |& grep -E "^ATA device");;
esac
if [[ -n $DISK_IS_ATA ]]; then
DRIVES[$drive]="ATA"
else
DRIVES[$drive]="SCSI"
fi
done
}
##
# Retrieves the list of identifiers (e.g. "ada0") for all monitored drives.
# Drives listed in $IGNORE_DRIVES will be excluded.
#
# Note: Must be run after detect_drives().
##
function get_drives() {
echo "${!DRIVES[@]}"
}
##
# Waits $1 seconds and returns a list of all drives that didn't
# experience I/O operations during that period.
#
# Devices listed in $IGNORED_DRIVES will never get returned.
#
# Arguments:
# $1 Seconds to listen for I/O before drives are considered idle
##
function get_idle_drives() {
# Wait for $1 seconds and get active drives
local IOSTAT_OUTPUT=`iostat -x -z -d $1 2`
case $DISK_PARM_TOOL in
"camcontrol")
local CUT_OFFSET=`grep -no "extended device statistics" <<< ${IOSTAT_OUTPUT} | tail -n1 | grep -Eo '^[^:]+'` ;;
"hdparm")
local CUT_OFFSET=`grep -no "Device" <<< ${IOSTAT_OUTPUT} | tail -n1 | grep -Eo '^[^:]+'` ;;
esac
local ACTIVE_DRIVES=`tail -n +$((CUT_OFFSET+2)) <<< ${IOSTAT_OUTPUT} | awk '{printf $1}{printf " "}'`
# Remove active drives from list to get idle drives
local IDLE_DRIVES=" $(get_drives) " # Space padding must be kept for pattern matching
for drive in ${ACTIVE_DRIVES}; do
IDLE_DRIVES=`sed "s/ ${drive} / /g" <<< ${IDLE_DRIVES}`
done
echo ${IDLE_DRIVES}
}
##
# Checks if all not ignored drives are idle.
#
# returns 0 if all drives are idle, 1 if at least one drive is spinning
#
# Arguments:
# $1 list of idle drives as returned by get_idle_drives()
##
function all_drives_are_idle() {
local DRIVES=" $(get_drives) "
for drive in ${DRIVES}; do
if [[ ! $1 =~ $drive ]]; then
return 1
fi
done
return 0
}
##
# Determines whether the given drive $1 understands ATA commands
#
# Arguments:
# $1 Device identifier of the drive
##
function is_ata_drive() {
if [[ ${DRIVES[$1]} == "ATA" ]]; then echo 1; else echo 0; fi
}
##
# Determines whether the given drive $1 is spinning
#
# Arguments:
# $1 Device identifier of the drive
##
function drive_is_spinning() {
case $DISK_PARM_TOOL in
"camcontrol")
# camcontrol differentiates between ATA and SCSI drives
if [[ $(is_ata_drive $1) -eq 1 ]]; then
if [[ -z $(camcontrol epc $1 -c status -P | grep 'Standby') ]]; then echo 1; else echo 0; fi
else
# Reads STANDBY values from the power condition mode page (0x1a).
# THIS IS EXPERIMENTAL AND UNTESTED due to the lack of SCSI drives :(
#
# See: /usr/share/misc/scsi_modes and the "SCSI Commands Reference Manual"
if [[ -z $(camcontrol modepage $1 -m 0x1a |& grep -E "^STANDBY(.*)1") ]]; then echo 1; else echo 0; fi
fi
;;
"hdparm")
# It is currently unknown if hdparm also needs to differentiates between ATA and SCSI drives
if [[ -z $(hdparm -C "/dev/$1" | grep 'standby') ]]; then echo 1; else echo 0; fi
;;
esac
}
##
# Forces the spindown of the drive specified by parameter $1 trough camcontrol
#
# Arguments:
# $1 Device identifier of the drive
##
function spindown_drive() {
if [[ $(drive_is_spinning $1) -eq 1 ]]; then
if [[ $DRYRUN -eq 0 ]]; then
case $DISK_PARM_TOOL in
"camcontrol")
if [[ $(is_ata_drive $1) -eq 1 ]]; then
# Spindown ATA drive
camcontrol standby $1
else
# Spindown SCSI drive
camcontrol stop $1
fi
;;
"hdparm")
hdparm -q -y "/dev/$1"
;;
esac
fi
log "$(date '+%F %T') Spun down idle drive: $1"
else
log_verbose "$(date '+%F %T') Drive is already spun down: $1"
fi
}
##
# Generates a list of all active timeouts
##
function get_drive_timeouts() {
echo -n "$(date '+%F %T') Drive timeouts: "
for x in "${!DRIVE_TIMEOUTS[@]}"; do printf "[%s]=%s " "$x" "${DRIVE_TIMEOUTS[$x]}" ; done
echo ""
}
##
# Main program loop
##
function main() {
if [[ $DRYRUN -eq 1 ]]; then log "Performing a dry run..."; fi
# Determine disk parameter tool to use
# (Differentiates between TrueNaS Core and TrueNAs SCALE)
DISK_PARM_TOOL=$(detect_disk_parm_tool)
log_verbose "Using disk parameter tool: ${DISK_PARM_TOOL}"
# Initially identify drives to monitor
detect_drives
for drive in ${!DRIVES[@]}; do
log_verbose "Detected drive ${drive} as ${DRIVES[$drive]} device"
done
log "Monitoring drives with a timeout of ${TIMEOUT} seconds: $(get_drives)"
log "I/O check sample period: ${POLL_TIME} sec"
if [ ${SHUTDOWN_TIMEOUT} -gt 0 ]; then
log "System will be shut down after ${SHUTDOWN_TIMEOUT} seconds of inactivity"
fi
# Init timeout counters for all monitored drives
declare -A DRIVE_TIMEOUTS
for drive in $(get_drives); do
DRIVE_TIMEOUTS[$drive]=${TIMEOUT}
done
log_verbose "$(get_drive_timeouts)"
# Init shutdown counter
SHUTDOWN_COUNTER=${SHUTDOWN_TIMEOUT}
# Drive I/O monitoring loop
while true; do
local IDLE_DRIVES=$(get_idle_drives ${POLL_TIME})
for drive in "${!DRIVE_TIMEOUTS[@]}"; do
if [[ $IDLE_DRIVES =~ $drive ]]; then
DRIVE_TIMEOUTS[$drive]=$((DRIVE_TIMEOUTS[$drive] - POLL_TIME))
if [[ ! ${DRIVE_TIMEOUTS[$drive]} -gt 0 ]]; then
DRIVE_TIMEOUTS[$drive]=${TIMEOUT}
spindown_drive ${drive}
fi
else
DRIVE_TIMEOUTS[$drive]=${TIMEOUT}
fi
done
log_verbose "$(get_drive_timeouts)"
if [ ${SHUTDOWN_TIMEOUT} -gt 0 ]; then
if all_drives_are_idle "${IDLE_DRIVES}"; then
SHUTDOWN_COUNTER=$((SHUTDOWN_COUNTER - POLL_TIME))
if [[ ! ${SHUTDOWN_COUNTER} -gt 0 ]]; then
log_verbose "Shutting down system"
shutdown -p now
fi
else
SHUTDOWN_COUNTER=${SHUTDOWN_TIMEOUT}
fi
log_verbose "Shutdown timeout: ${SHUTDOWN_COUNTER}"
fi
done
}
# Parse arguments
while getopts ":hqvdmt:p:i:s:" opt; do
case ${opt} in
t ) TIMEOUT=${OPTARG}
;;
p ) POLL_TIME=${OPTARG}
;;
i ) IGNORED_DRIVES="$IGNORED_DRIVES ${OPTARG}"
;;
s ) SHUTDOWN_TIMEOUT=${OPTARG}
;;
q ) QUIET=1
;;
v ) VERBOSE=1
;;
d ) DRYRUN=1
;;
m ) MANUAL_MODE=1
;;
h ) print_usage; exit
;;
: ) print_usage; exit
;;
\? ) print_usage; exit
;;
esac
done
main # Start main program