1#!/bin/sh
2#
3#
4
5# If there is a global system configuration file, suck it in.
6#
7
8newline="
9" # A single newline
10
11if [ -r /etc/defaults/periodic.conf ]
12then
13    . /etc/defaults/periodic.conf
14    source_periodic_confs
15fi
16
17: ${daily_scrub_zfs_default_threshold=35}
18
19case "$daily_scrub_zfs_enable" in
20    [Yy][Ee][Ss])
21	echo
22	echo 'Scrubbing of zfs pools:'
23
24	if [ -z "${daily_scrub_zfs_pools}" ]; then
25		daily_scrub_zfs_pools="$(zpool list -H -o name)"
26	fi
27
28	rc=0
29	for pool in ${daily_scrub_zfs_pools}; do
30		# sanity check
31		_status=$(zpool list "${pool}" 2> /dev/null)
32		if [ $? -ne 0 ]; then
33			rc=2
34			echo "   WARNING: pool '${pool}' specified in"
35			echo "            '/etc/periodic.conf:daily_scrub_zfs_pools'"
36			echo "            does not exist"
37			continue
38		fi
39		_status=${_status##*$newline}
40		case ${_status} in
41		*FAULTED*)
42			rc=3
43			echo "Skipping faulted pool: ${pool}"
44			continue ;;
45		*UNAVAIL*)
46			rc=4
47			echo "Skipping unavailable pool: ${pool}"
48			continue ;;
49		esac
50
51		# determine how many days shall be between scrubs
52		eval _pool_threshold=\${daily_scrub_zfs_$(echo "${pool}"|tr  ".:-" "_")_threshold}
53		if [ -z "${_pool_threshold}" ];then
54			_pool_threshold=${daily_scrub_zfs_default_threshold}
55		fi
56
57		_last_scrub=$(zpool history ${pool} | \
58		    egrep "^[0-9\.\:\-]{19} zpool scrub ${pool}\$" | tail -1 |\
59		    cut -d ' ' -f 1)
60		if [ -z "${_last_scrub}" ]; then
61			# creation time of the pool if no scrub was done
62			_last_scrub=$(zpool history ${pool} | \
63			    sed -ne '2s/ .*$//p')
64		fi
65		if [ -z "${_last_scrub}" ]; then
66			echo "   skipping scrubbing of pool '${pool}':"
67			echo "      can't get last scrubbing date"
68			continue
69		fi
70
71		# Now minus last scrub (both in seconds) converted to days.
72		_scrub_diff=$(expr -e \( $(date +%s) - \
73		    $(date -j -v -70M -f %F.%T ${_last_scrub} +%s) \) / 60 / 60 / 24)
74		if [ ${_scrub_diff} -lt ${_pool_threshold} ]; then
75			echo "   skipping scrubbing of pool '${pool}':"
76			echo "      last scrubbing is ${_scrub_diff} days ago, threshold is set to ${_pool_threshold} days"
77			continue
78		fi
79
80		_status="$(zpool status ${pool} | grep scan:)"
81		case "${_status}" in
82			*"scrub in progress"*)
83				echo "   scrubbing of pool '${pool}' already in progress, skipping:"
84				;;
85			*"resilver in progress"*)
86				echo "   resilvering of pool '${pool}' is in progress, skipping:"
87				;;
88			*"none requested"*)
89				echo "   starting first scrub (since reboot) of pool '${pool}':"
90				zpool scrub ${pool}
91				[ $rc -eq 0 ] && rc=1
92				;;
93			*)
94				echo "   starting scrub of pool '${pool}':"
95				zpool scrub ${pool}
96				[ $rc -eq 0 ] && rc=1
97				;;
98		esac
99
100		echo "      consult 'zpool status ${pool}' for the result"
101	done
102	;;
103
104    *)
105	rc=0
106	;;
107esac
108
109exit $rc
110