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