Added an already running check
[zfs-nexenta/.git] / zfs-scrub
1 #!/bin/bash
2
3 # Author: Alan J. Pippin
4 # Description: This script will attempt to scrub a given pool.
5 #              This script ensures that only 1 scrub operation is 
6 #              running at any given time. This serializes the zfs 
7 #              scrub process for any pool.
8
9 # source our configuration
10 config="${0%/*}/zfs-scripts.conf"
11 [ -e "${config}.dist" ] && . ${config}.dist
12 [ -e "${config}" ] && . ${config}
13
14 exec >> $logdir/zfs-scrub.log 2>&1
15
16 pools="$*"
17 maxsleeptime=360
18
19 if [ -z "$pools" ]; then
20    echo "-E- Usage: $0 <pools>"
21    exit 1
22 fi
23
24 for i in $pools
25 do
26   # Check to see if any zfs filesystem has a scrub being performed on it now.
27   # If it does, we cannot perform more than one scrub operation at a time.
28   while true; do
29     zpool status | grep scrub: | grep "in progress" > /dev/null 2>&1
30     if [ $? == 0 ]; then
31         # Another zpool scrub operation is already running
32         # Wait until it is done before continuing
33         ransleep=$(($RANDOM % $maxsleeptime))
34         sleep $ransleep
35     else
36         # Another zpool scrub operation is not running
37         break
38     fi
39   done
40
41   date=`date`
42   echo "$date: Scrub started for zfs pool $i"
43   zpool scrub $i
44
45   # Wait until the scrub completes, and check for any errors
46   while true; do
47     zpool status $i | grep scrub: | grep "in progress" > /dev/null 2>&1
48     if [ $? == 0 ]; then
49         # Our zpool scrub operation is still running
50         # Wait until it is done before continuing
51         ransleep=$(($RANDOM % $maxsleeptime))
52         sleep $ransleep
53     else
54         # Our scrub operation has completed
55         break
56     fi
57   done
58
59   date=`date`
60   echo "$date: Scrub completed for zfs pool $i"
61
62   # Check for any scrub errors
63   zpool status $i | grep scrub: | grep "with 0 errors" > /dev/null 2>&1
64   if [ $? != 0 ]; then
65     # The scrub found errors
66     zpool status $i | $mailx -s "zpool scrub $i found errors" $mailto 
67   fi 
68
69 done
70
71