summaryrefslogtreecommitdiffstats
path: root/core/modules/smartctl/data/opt/openslx/scripts/systemd-smartctl
blob: 979fde629900f92a3adb70e69f35bb662b783f83 (plain) (blame)
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
#!/bin/ash

# Check Reallocated_Sector_Ct and Spin_Retry_Count of local disk(s)

. /opt/openslx/config

[ -z "$SLX_SMARTCTL_MIN_REALLOC" ] && SLX_SMARTCTL_MIN_REALLOC=0
DELAY=
FILES=

for dev in /dev/sd?; do
	FILE=$(mktemp)
	FILES="$FILES $FILE"
	smartctl -H -A -f "brief" "$dev" > "$FILE" || continue # should we report devices where smartctl doesn't work?
	# parse
	OVERALL=$(grep -o "test result: .*$" "$FILE" | cut -c 14-)
	[ "x$OVERALL" = "xPASSED" ] && OVERALL=""
	REALLOC=$(grep "^ *5 " "$FILE" | awk '{print $8}')
	SPINRETRY_VAL=$(grep "^ *10 " "$FILE" | awk '{print $4}')
	SPINRETRY_THR=$(grep "^ *10 " "$FILE" | awk '{print $6}')
	# report if applicable
	if [ -n "$OVERALL" ]; then
		slxlog "smartctl-fail" "Failed HDD: $dev reports health as $OVERALL" "$FILE"
		DELAY=yes
	fi
	if [ -n "$REALLOC" ] && [ "$REALLOC" -gt "$SLX_SMARTCTL_MIN_REALLOC" ]; then
		slxlog "smartctl-realloc" "Failing HDD: $dev has $REALLOC reallocated sectors!" "$FILE"
		DELAY=yes
	fi
	if [ -n "$SPINRETRY_VAL" ] && [ "$SPINRETRY_VAL" -le "$SPINRETRY_THR" ]; then
		slxlog "smartctl-spinretry" "Failing HDD: $dev has bad spin retry count! ($SPINRETRY_VAL/$SPINRETRY_THR)" "$FILE"
		DELAY=yes
	fi
done

[ -n "$DELAY" ] && sleep 2 # give slxlog a little time, as it's running async
[ -n "$FILES" ] && rm -f -- $FILES # list, no ""
# Keep it that way instead of using --delete or --sync sonce it would add up; this way we're doing it just once here

exit 0