diff --git a/scrubPool.sh b/scrubPool.sh new file mode 100644 index 0000000..9cde606 --- /dev/null +++ b/scrubPool.sh @@ -0,0 +1,126 @@ +#!/bin/sh +############################################################################# +# Script aimed at scrubing all zpools in order to find checksum errors +# +# Author: fritz from NAS4Free forum +# +############################################################################# + +# Initialization of the script name +readonly SCRIPT_NAME=`basename $0` # The name of this file + +# set script path as working directory +cd "`dirname $0`" + +# Import required scripts +. "config.sh" +. "common/commonLogFcts.sh" +. "common/commonMailFcts.sh" +. "common/commonLockFcts.sh" + +# Record the timestamp corresponding to the start of the script execution +readonly START_TIMESTAMP=`$BIN_DATE +"%s"` + +# Initialization of the constants +readonly LOGFILE="$CFG_LOG_FOLDER/$SCRIPT_NAME.log" +readonly TMPFILE_ARGS="$CFG_TMP_FOLDER/$SCRIPT_NAME.$$.args.tmp" + +# Pool name +POOL_NAME="" + +# Set variables corresponding to the input parameters +ARGUMENTS="$@" + + +################################## +# Check script input parameters +# +# Params: all parameters of the shell script +# return : 1 if an error occured, 0 otherwise +################################## +parseInputParams() { + local opt + + # parse the optional parameters + # (there should be none) + while getopts ":" opt; do + case $opt in + \?) + echo "Invalid option: -$OPTARG" + return 1 ;; + esac + done + + # Remove the optional arguments parsed above. + shift $((OPTIND-1)) + + # Check if the number of mandatory parameters + # provided is as expected + if [ "$#" -ne "1" ]; then + echo "Exactly one mandatory argument should be provided" + return 1 + fi + + POOL_NAME="$1" + + return 0 +} + +################################## +# Check if scrubing is still in progress for a given pool +# Return : 0 if scrub is in progress, +# 1 otherwise +################################## +scrubInProgress() { + if $BIN_ZPOOL status $POOL_NAME | grep "scrub in progress">/dev/null; then + return 0 + else + return 1 + fi +} + +################################## +# Scrub all pools +# Return : 0 no problem detected +# 1 problem detected +################################## +main() { + + # Starting scrubbing + log_info "$LOGFILE" "Starting scrubbing of pool: $POOL_NAME" + $BIN_ZPOOL scrub $POOL_NAME + + # Waiting for the end of the scrubbing + while scrubInProgress; do sleep 10; done; + log_info "$LOGFILE" "Scrub finished for pool: $POOL_NAME" + + # Check if the pool is healthy + if ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE=1 $BIN_ZPOOL status -x $POOL_NAME | grep "is healthy">/dev/null; then + ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE=1 $BIN_ZPOOL status -x $POOL_NAME | log_info "$LOGFILE" + return 0 + else + $BIN_ZPOOL status -v $POOL_NAME | log_error "$LOGFILE" + return 1 + fi +} + + + +# Parse and validate the input parameters +if ! parseInputParams $ARGUMENTS > "$TMPFILE_ARGS"; then + log_info "$LOGFILE" "-------------------------------------" + cat "$TMPFILE_ARGS" | log_error "$LOGFILE" + get_log_entries_ts "$LOGFILE" "$START_TIMESTAMP" | sendMail "$SCRIPT_NAME : Invalid arguments" +else + log_info "$LOGFILE" "-------------------------------------" + cat "$TMPFILE_ARGS" | log_info "$LOGFILE" + + # run script if possible (lock not existing) + run_main "$LOGFILE" "$SCRIPT_NAME" + # in case of error, send mail with extract of log file + [ "$?" -eq "2" ] && get_log_entries_ts "$LOGFILE" "$START_TIMESTAMP" | sendMail "$SCRIPT_NAME : issue occured during execution" +fi + +$BIN_RM "$TMPFILE_ARGS" +exit 0 +