ZFS Check Pools Script

Um zfs Pools automatisch zu prüfen, habe ich vor ein paar Jahren einmal ein Shell Script erstellt, welches via cronjob in regelmässigen Abständen aufgerufen wird. Das Ergebnis wird via e-mail zugestellt.

Einfach die Variablen der eigenen Umgebung anpassen.

  #!/bin/bash
  # zpoolcheck Script Ver. 1.0
  # Date: 03.08.2012
  # by: Thomas Weidner
  # The script starts a scrub on each ZFS pool (only one at time)
  # and will send an e-mail with the result after the scrub is complete.


  # e-mail variables
 TO=tom@dvs8818.de
 LOG=/var/log/zpoolresult
 SUBJECTOK="Zpool status from nexenta-01"
 SUBJECTERROR="WARNING zpool error from nexenta-01"

 # work variables
 ERROR=0
 SEP=" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
 RUNNING=1

 # commands & configuration
 ZPOOL=/sbin/zpool

 DATE="`date +"%Y-%m-%d %H:%M:%S"`"

 echo $DATE > $LOG


 # find all pools
 pools=$($ZPOOL list -H -o name)


 # for each pool
for pool in $pools; do
    # start scrub for $pool
    echo "starting scrub on $pool" >> $LOG
    zpool scrub $pool
    RUNNING=1
    # wait until scrub for $pool has finished running
    while [ $RUNNING = 1 ];     do
        # still running?
        if $ZPOOL status -v $pool | grep -q "scrub in progress"; then
            sleep 60
        # not running
        else
            # finished with this pool, exit
            echo "scrub ended on $pool" >> $LOG
            echo "`$ZPOOL status -v $pool`" >> $LOG
            echo $SEP >> $LOG
            RUNNING=0
            # check for errors
            if ! $ZPOOL status -v $pool | grep -q "No known data errors"; then
                echo "data errors detected on $pool" >> $LOG
                ERROR=1
            fi
        fi
    done
done

 # send result via e-mail

if [ $ERROR = 1 ]; then

                        mailx -s "$SUBJECTERROR" $TO < $LOG
                else
                        mailx -s "$SUBJECTOK" $TO < $LOG

fi

Shell Scripte