123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- #!/bin/bash
-
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
-
- EMAIL_ADDRESS="blaine@nas.home.lan"
-
- DEL_THRESHOLD=1
-
- SYNC_WARN_THRESHOLD=-1
-
- SCRUB_PERCENT=15
- SCRUB_AGE=20
-
- SMART_LOG=1
-
- LOG_FILE="/tmp/snapRAID.log"
- SNAPRAID_BIN="/usr/bin/snapraid"
- MAIL_BIN="/usr/bin/mail"
-
- TERSE=2
-
- EMAIL_SUBJECT_PREFIX="[`hostname`] SnapRAID - "
- TMP_OUTPUT="/tmp/snapRAID.out"
- SYNC_WARN_FILE="/tmp/snapRAID.warnCount"
- SYNC_WARN_COUNT=""
-
- CONTENT_FILE=`grep -v '^$\|^\s*\#' /etc/snapraid.conf | grep snapraid.content | head -n 1 | cut -d " " -f2`
- PARITY_FILE=`grep -v '^$\|^\s*\#' /etc/snapraid.conf | grep snapraid.parity | head -n 1 | cut -d " " -f2`
-
- exec >> $LOG_FILE
-
- echo "[`date`] SnapRAID Job started."
- echo "SnapRAID DIFF Job started on `date`" > $TMP_OUTPUT
- echo "----------------------------------------" >> $TMP_OUTPUT
-
-
- if [ ! -e $CONTENT_FILE ]; then
- echo "[`date`] ERROR - Content file ($CONTENT_FILE) not found!"
- echo "ERROR - Content file ($CONTENT_FILE) not found!" >> $TMP_OUTPUT
- exit 1;
- fi
-
- if [ ! -e $PARITY_FILE ]; then
- echo "[`date`] ERROR - Parity file ($PARITY_FILE) not found!"
- echo "ERROR - Parity file ($PARITY_FILE) not found!" >> $TMP_OUTPUT
- exit 1;
- fi
-
- echo "[`date`] Running DIFF Command."
- $SNAPRAID_BIN diff >> $TMP_OUTPUT
- wait
-
- echo "----------------------------------------" >> $TMP_OUTPUT
- echo "SnapRAID DIFF Job finished on `date`" >> $TMP_OUTPUT
- JOBS_DONE="DIFF"
-
- DEL_COUNT=$(grep -w '^ \{1,\}[0-9]* removed$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)
- ADD_COUNT=$(grep -w '^ \{1,\}[0-9]* added$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)
- MOVE_COUNT=$(grep -w '^ \{1,\}[0-9]* moved$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)
- COPY_COUNT=$(grep -w '^ \{1,\}[0-9]* copied$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)
- UPDATE_COUNT=$(grep -w '^ \{1,\}[0-9]* updated$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)
-
- if [ -z "$DEL_COUNT" -o -z "$ADD_COUNT" -o -z "$MOVE_COUNT" -o -z "$COPY_COUNT" -o -z "$UPDATE_COUNT" ]; then
-
- echo "[`date`] ERROR - failed to get one or more count values. Unable to proceed. Exiting script."
- if [ $EMAIL_ADDRESS ]; then
- $MAIL_BIN -s "$EMAIL_SUBJECT_PREFIX WARNING - Unable to proceed with SYNC/SCRUB job(s). Check DIFF job output inside." "$EMAIL_ADDRESS" < $TMP_OUTPUT
- fi
- exit 1;
- fi
-
- echo "SUMMARY of changes - Added [$ADD_COUNT] - Deleted [$DEL_COUNT] - Moved [$MOVE_COUNT] - Copied [$COPY_COUNT] - Updated [$UPDATE_COUNT]" >> $TMP_OUTPUT
-
- if [ $DEL_COUNT -gt 0 -o $ADD_COUNT -gt 0 -o $MOVE_COUNT -gt 0 -o $COPY_COUNT -gt 0 -o $UPDATE_COUNT -gt 0 ]; then
-
-
- if [ $DEL_COUNT -lt $DEL_THRESHOLD ]; then
-
- echo "Deleted files ($DEL_COUNT) did not exceed threshold ($DEL_THRESHOLD), proceeding with sync job." >> $TMP_OUTPUT
- echo "[`date`] Changes detected [A-$ADD_COUNT,D-$DEL_COUNT,M-$MOVE_COUNT,C-$COPY_COUNT,U-$UPDATE_COUNT] and deleted files ($DEL_COUNT) is below threshold ($DEL_THRESHOLD). Running SYNC Command."
- DO_SYNC=1
- else
-
- echo "Number of deleted files ($DEL_COUNT) exceeded threshold ($DEL_THRESHOLD)." >> $TMP_OUTPUT
- echo "[`date`] WARNING - Deleted files ($DEL_COUNT) exceeded threshold ($DEL_THRESHOLD). Check $TMP_OUTPUT for details."
-
- if [ $SYNC_WARN_THRESHOLD -gt -1 ]; then
-
- echo "Forced sync is enabled." >> $TMP_OUTPUT
- echo "[`date`] Forced sync is enabled."
-
- SYNC_WARN_COUNT=$(sed 'q;/^[0-9][0-9]*$/!d' $SYNC_WARN_FILE 2>/dev/null)
- SYNC_WARN_COUNT=${SYNC_WARN_COUNT:-0}
- if [ $SYNC_WARN_COUNT -ge $SYNC_WARN_THRESHOLD ]; then
-
- echo "Number of warning(s) ($SYNC_WARN_COUNT) has reached/exceeded threshold ($SYNC_WARN_THRESHOLD). Forcing a sync job to run." >> $TMP_OUTPUT
- echo "[`date`] Number of warning(s) ($SYNC_WARN_COUNT) has reached/exceeded threshold ($SYNC_WARN_THRESHOLD). Forcing a sync job to run."
- DO_SYNC=1
- else
-
- ((SYNC_WARN_COUNT += 1))
- echo $SYNC_WARN_COUNT > $SYNC_WARN_FILE
- echo "$((SYNC_WARN_THRESHOLD - SYNC_WARN_COUNT)) warning(s) till forced sync. NOT proceeding with sync job." >> $TMP_OUTPUT
- echo "[`date`] $((SYNC_WARN_THRESHOLD - SYNC_WARN_COUNT)) warning(s) till forced sync. NOT proceeding with sync job."
- DO_SYNC=0
- fi
- else
-
- echo "Forced sync is not enabled. NOT proceeding with sync job. Please run sync manually if this is not an error condition." >> $TMP_OUTPUT
- echo "[`date`] Forced sync is not enabled. Check $TMP_OUTPUT for details. NOT proceeding with sync job."
- DO_SYNC=0
- fi
- fi
- else
-
- echo "[`date`] No change detected. Not running SYNC job."
- DO_SYNC=0
- fi
-
- if [ $DO_SYNC -eq 1 ]; then
- echo "SnapRAID SYNC Job started on `date`" >> $TMP_OUTPUT
- echo "----------------------------------------" >> $TMP_OUTPUT
- $SNAPRAID_BIN sync | sed -e 's/\r/\n/g' >> $TMP_OUTPUT
-
- wait
- echo "----------------------------------------" >> $TMP_OUTPUT
- echo "SnapRAID SYNC Job finished on `date`" >> $TMP_OUTPUT
- JOBS_DONE="$JOBS_DONE + SYNC"
-
- sed -i 's/^Everything OK/SYNC_JOB--Everything OK/g;s/^Nothing to do/SYNC_JOB--Nothing to do/g' $TMP_OUTPUT
-
- if [ -e $SYNC_WARN_FILE ]; then
- rm $SYNC_WARN_FILE
- fi
- $SNAPRAID_BIN scrub -p new
- fi
-
- if [ $SCRUB_PERCENT -gt 0 ]; then
-
- if [ $DEL_COUNT -gt $DEL_THRESHOLD -a $DO_SYNC -eq 0 ]; then
-
- echo "[`date`] Scrub job cancelled as parity info is out of sync (deleted files threshold has been breached)."
- else
-
-
- if [ $DO_SYNC -eq 1 -a -z "$(grep -w "SYNC_JOB-" $TMP_OUTPUT)" ]; then
-
- echo "[`date`] WARNING - check output of SYNC job. Could not detect marker <SYNC_JOB-->. Not proceeding with SCRUB job."
- echo "WARNING - check output of SYNC job. Could not detect marker <SYNC_JOB-->. Not proceeding with SCRUB job." >> $TMP_OUTPUT
- else
-
- echo "[`date`] Running SCRUB Command."
- echo "SnapRAID SCRUB Job started on `date`" >> $TMP_OUTPUT
- echo "----------------------------------------" >> $TMP_OUTPUT
- $SNAPRAID_BIN scrub -p $SCRUB_PERCENT -o $SCRUB_AGE | sed -e 's/\r/\n/g' >> $TMP_OUTPUT
-
- wait
- echo "----------------------------------------" >> $TMP_OUTPUT
- echo "SnapRAID SCRUB Job finished on `date`" >> $TMP_OUTPUT
- JOBS_DONE="$JOBS_DONE + SCRUB"
-
- sed -i 's/^Everything OK/SCRUB_JOB--Everything OK/g;s/^Nothing to do/SCRUB_JOB--Nothing to do/g' $TMP_OUTPUT
- fi
- fi
- else
- echo "[`date`] Scrub job is not scheduled. Not running SCRUB job."
- fi
-
- if [ $SMART_LOG -eq 1 ]; then
- $SNAPRAID_BIN smart >> $TMP_OUTPUT
- wait
- fi
-
-
- if [ $EMAIL_ADDRESS ]; then
- echo "[`date`] Email address is set. Sending email report to <$EMAIL_ADDRESS>"
-
- if [ $DEL_COUNT -gt $DEL_THRESHOLD -a $DO_SYNC -eq 0 ]; then
-
- $MAIL_BIN -s "$EMAIL_SUBJECT_PREFIX WARNING $SYNC_WARN_COUNT - Number of deleted files ($DEL_COUNT) exceeded threshold ($DEL_THRESHOLD)" "$EMAIL_ADDRESS" < $TMP_OUTPUT
- elif [ -z "${JOBS_DONE##*"SYNC"*}" -a -z "$(grep -w "SYNC_JOB-" $TMP_OUTPUT)" ]; then
-
- $MAIL_BIN -s "$EMAIL_SUBJECT_PREFIX WARNING - SYNC job ran but did not complete successfully" "$EMAIL_ADDRESS" < $TMP_OUTPUT
- elif [ -z "${JOBS_DONE##*"SCRUB"*}" -a -z "$(grep -w "SCRUB_JOB-" $TMP_OUTPUT)" ]; then
-
- $MAIL_BIN -s "$EMAIL_SUBJECT_PREFIX WARNING - SCRUB job ran but did not complete successfully" "$EMAIL_ADDRESS" < $TMP_OUTPUT
- else
-
- if [ $TERSE -gt 1 ]; then
-
- sed -i '$!N; /^\([0-9]\).*\n\1.*$/!P; D' $TMP_OUTPUT
- sed -i '/^[1-8]%.*$/d' $TMP_OUTPUT
- elif [ $TERSE -gt 0 ]; then
-
- sed -i '$!N; /^\([0-9]*\)%.*\n\1.*$/!P; D' $TMP_OUTPUT
- fi
- $MAIL_BIN -s "$EMAIL_SUBJECT_PREFIX INFO - $JOBS_DONE Jobs COMPLETED" "$EMAIL_ADDRESS" < $TMP_OUTPUT
- fi
- fi
-
- echo "[`date`] All jobs ended."
-
- exit 0;
|