6488beb1aba222c80c5d29043e4700fd0ff8d6e5
[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 PATH=/usr/sbin:/sbin:/etc/bin:$PATH
10
11 pools="$*"
12 maxsleeptime=360
13 mailto=root
14
15 if [ -z "$pools" ]; then
16    echo "-E- Usage: $0 <pools>"
17    exit 1
18 fi
19
20 for i in $pools
21 do
22   # Check to see if any zfs filesystem has a scrub being performed on it now.
23   # If it does, we cannot perform more than one scrub operation at a time.
24   while true; do
25     zpool status | grep scrub: | grep "in progress" > /dev/null 2>&1
26     if [ $? == 0 ]; then
27         # Another zpool scrub operation is already running
28         # Wait until it is done before continuing
29         ransleep=$(($RANDOM % $maxsleeptime))
30         sleep $ransleep
31     else
32         # Another zpool scrub operation is not running
33         break
34     fi
35   done
36
37   date=`date`
38   echo "$date: Scrub started for zfs pool $i"
39   zpool scrub $i
40
41   # Wait until the scrub completes, and check for any errors
42   while true; do
43     zpool status $i | grep scrub: | grep "in progress" > /dev/null 2>&1
44     if [ $? == 0 ]; then
45         # Our zpool scrub operation is still running
46         # Wait until it is done before continuing
47         ransleep=$(($RANDOM % $maxsleeptime))
48         sleep $ransleep
49     else
50         # Our scrub operation has completed
51         break
52     fi
53   done
54
55   date=`date`
56   echo "$date: Scrub completed for zfs pool $i"
57
58   # Check for any scrub errors
59   zpool status $i | grep scrub: | grep "with 0 errors" > /dev/null 2>&1
60   if [ $? != 0 ]; then
61     # The scrub found errors
62     zpool status $i | /usr/bin/mailx -s "zpool scrub $i found errors" $mailto 
63   fi 
64
65 done
66
67