Skip to content

Commit e5e3e8d

Browse files
authored
Merge branch 'dev' into status-check
2 parents 9ec2409 + 6637cc6 commit e5e3e8d

File tree

2 files changed

+54
-55
lines changed

2 files changed

+54
-55
lines changed

script-config.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,12 @@ BEFORE_HOOK_CMD=""
226226
# last command.
227227
# This option does not have any effect if CUSTOM_HOOK is set to 0
228228
# Use NAME for a friendly name, CMD for the command itself.
229+
# Set EXECUTE_BEFORE_SPINDOWN to 1, if you have hd-idle configured
230+
# to spin down your disks in the end and want the custom hook to be
231+
# executed before that. Default is execution after spindown.
229232
AFTER_HOOK_NAME=""
230233
AFTER_HOOK_CMD=""
234+
EXECUTE_BEFORE_SPINDOWN=0
231235

232236
####################### USER CONFIGURATION END #######################
233237

snapraid-aio-script.sh

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ source "$CONFIG_FILE"
3333
SUBJECT="[WARNING] - Configuration Error $EMAIL_SUBJECT_PREFIX"
3434
NOTIFY_OUTPUT="$SUBJECT"
3535
notify_warning
36-
if [ "$EMAIL_ADDRESS" ]; then
37-
trim_log < "$TMP_OUTPUT" | send_mail
38-
fi
3936
exit 1;
4037
fi
4138

@@ -107,9 +104,6 @@ function main(){
107104
SUBJECT="[WARNING] - SnapRAID already running $EMAIL_SUBJECT_PREFIX"
108105
NOTIFY_OUTPUT="$SUBJECT"
109106
notify_warning
110-
if [ "$EMAIL_ADDRESS" ]; then
111-
trim_log < "$TMP_OUTPUT" | send_mail
112-
fi
113107
exit 1;
114108
else
115109
echo "SnapRAID is not running, proceeding."
@@ -137,20 +131,16 @@ function main(){
137131
if [ ! -f "$SNAPRAID_CONF" ]; then
138132
# if running on OMV7, try to find the SnapRAID conf file automatically
139133
check_omv_version
140-
if [ "$OMV_VERSION" -ge 7 ]; then
141-
pick_snapraid_conf_file
142-
else
143-
echo "SnapRAID configuration file not found. The script cannot be run! Please check your settings, because the specified file "$SNAPRAID_CONF" does not exist."
144-
mklog "WARN: SnapRAID configuration file not found. The script cannot be run! Please check your settings, because the specified file "$SNAPRAID_CONF" does not exist."
145-
SUBJECT="[WARNING] - SnapRAID configuration file not found!"
146-
FORMATTED_CONF="\`$SNAPRAID_CONF\`"
147-
NOTIFY_OUTPUT="$SUBJECT The script cannot be run! Please check your settings, because the specified file $FORMATTED_CONF does not exist."
148-
notify_warning
149-
if [ "$EMAIL_ADDRESS" ]; then
150-
trim_log < "$TMP_OUTPUT" | send_mail
151-
fi
152-
exit 1;
153-
fi
134+
if [ "$OMV_VERSION" -ge 7 ]; then
135+
pick_snapraid_conf_file
136+
else
137+
echo "SnapRAID configuration file not found. The script cannot be run! Please check your settings, because the specified file "$SNAPRAID_CONF" does not exist."
138+
mklog "WARN: SnapRAID configuration file not found. The script cannot be run! Please check your settings, because the specified file "$SNAPRAID_CONF" does not exist."
139+
SUBJECT="[WARNING] - SnapRAID configuration file not found!"
140+
FORMATTED_CONF="\`$SNAPRAID_CONF\`"
141+
NOTIFY_OUTPUT="$SUBJECT The script cannot be run! Please check your settings, because the specified file $FORMATTED_CONF does not exist."
142+
notify_warning
143+
fi
154144
fi
155145

156146

@@ -234,9 +224,6 @@ function main(){
234224
SUBJECT="[WARNING] - Unable to continue with SYNC/SCRUB job(s). Check DIFF job output. $EMAIL_SUBJECT_PREFIX"
235225
NOTIFY_OUTPUT="$SUBJECT"
236226
notify_warning
237-
if [ "$EMAIL_ADDRESS" ]; then
238-
trim_log < "$TMP_OUTPUT" | send_mail
239-
fi
240227
exit 1;
241228
fi
242229
if [ $IGNORE_PATTERN ]; then
@@ -352,6 +339,12 @@ if [ "$SNAP_STATUS" -eq 1 ]; then
352339
fi
353340
fi
354341

342+
# Custom Hook - After (if executed before drive spin down)
343+
if [ "$CUSTOM_HOOK" -eq 1 ] && [ "$EXECUTE_BEFORE_SPINDOWN" -eq 1 ]; then
344+
echo "### Custom Hook - [$AFTER_HOOK_NAME]";
345+
bash -c "$AFTER_HOOK_CMD"
346+
fi
347+
355348
# Spin down disks (Method hd-idle - spins down all rotational devices)
356349
# NOTE: Uses hd-idle rewrite
357350

@@ -373,7 +366,7 @@ fi
373366
fi
374367

375368
# Custom Hook - After
376-
if [ "$CUSTOM_HOOK" -eq 1 ]; then
369+
if [ "$CUSTOM_HOOK" -eq 1 ] && [ "$EXECUTE_BEFORE_SPINDOWN" -ne 1 ]; then
377370
echo "### Custom Hook - [$AFTER_HOOK_NAME]";
378371
bash -c "$AFTER_HOOK_CMD"
379372
fi
@@ -431,9 +424,6 @@ function sanity_check() {
431424
SUBJECT="[WARNING] - Parity file ($i) not found! $EMAIL_SUBJECT_PREFIX"
432425
NOTIFY_OUTPUT="$SUBJECT"
433426
notify_warning
434-
if [ "$EMAIL_ADDRESS" ]; then
435-
trim_log < "$TMP_OUTPUT" | send_mail
436-
fi
437427
exit 1;
438428
fi
439429
done
@@ -452,9 +442,6 @@ function sanity_check() {
452442
SUBJECT="[WARNING] - Content file ($i) not found! $EMAIL_SUBJECT_PREFIX"
453443
NOTIFY_OUTPUT="$SUBJECT"
454444
notify_warning
455-
if [ "$EMAIL_ADDRESS" ]; then
456-
trim_log < "$TMP_OUTPUT" | send_mail
457-
fi
458445
exit 1;
459446
fi
460447
done
@@ -910,6 +897,9 @@ function notify_warning(){
910897
-d '{"content": "'"$DISCORD_SUBJECT"'"}' \
911898
"$DISCORD_WEBHOOK_URL"
912899
fi
900+
if [ "$EMAIL_ADDRESS" ]; then
901+
trim_log < "$TMP_OUTPUT" | send_mail
902+
fi
913903
}
914904

915905
function show_snapraid_info() {
@@ -971,28 +961,39 @@ function send_mail(){
971961
python3 -m markdown |
972962
sed 's/<code>/<pre>/;s%</code>%</pre>%')
973963

974-
if [ -x "$HOOK_NOTIFICATION" ]; then
975-
echo -e "Notification user script is set. Calling it now [$(date)]"
976-
$HOOK_NOTIFICATION "$SUBJECT" "$body"
977-
elif [ "$EMAIL_ADDRESS" ]; then
978-
echo -e "Email address is set. Sending email report to **$EMAIL_ADDRESS** [$(date)]"
979-
if [ -z "$MAIL_BIN" ]; then
980-
echo -e "No mail program set in MAIL_BIN, you must set it to send email."
981-
elif [ $(mailx -V | grep -c "12.5 7/5/10") -eq 1 ]; then
982-
echo -e "Incompatible version of mailx found, using sendmail instead."
983-
(
984-
echo To: "$EMAIL_ADDRESS"
985-
echo From: "$FROM_EMAIL_ADDRESS"
986-
echo "Content-Type: text/html;"
987-
echo Subject: "$SUBJECT"
988-
echo
989-
echo "$body"
990-
) | sendmail -t
964+
if [ -x "$HOOK_NOTIFICATION" ]; then
965+
echo -e "Notification user script is set. Calling it now [$(date)]"
966+
$HOOK_NOTIFICATION "$SUBJECT" "$body"
967+
elif [ "$EMAIL_ADDRESS" ]; then
968+
echo -e "Email address is set. Sending email report to **$EMAIL_ADDRESS** [$(date)]"
969+
if [ -z "$MAIL_BIN" ]; then
970+
echo -e "No mail program set in MAIL_BIN, you must set it to send email."
971+
else
972+
# Check if mailx is executable
973+
if ! command -v "$MAIL_BIN" &> /dev/null; then
974+
echo -e "$MAIL_BIN not found, you must install it to send email."
991975
else
992-
$MAIL_BIN -a 'Content-Type: text/html' -s "$SUBJECT" -r "$FROM_EMAIL_ADDRESS" "$EMAIL_ADDRESS" \
993-
< <(echo "$body")
976+
# Try to determine if the mailx version is the incompatible one
977+
MAILX_VERSION=$($MAIL_BIN -V 2>/dev/null || echo "unknown")
978+
979+
if [[ "$MAILX_VERSION" == *"12.5 7/5/10"* ]]; then
980+
echo "Incompatible version of mailx found, using sendmail instead."
981+
(
982+
echo To: "$EMAIL_ADDRESS"
983+
echo From: "$FROM_EMAIL_ADDRESS"
984+
echo "Content-Type: text/html;"
985+
echo Subject: "$SUBJECT"
986+
echo
987+
echo "$body"
988+
) | sendmail -t
989+
else
990+
$MAIL_BIN -a 'Content-Type: text/html' -s "$SUBJECT" -r "$FROM_EMAIL_ADDRESS" "$EMAIL_ADDRESS" \
991+
< <(echo "$body")
992+
fi
994993
fi
995994
fi
995+
fi
996+
996997
}
997998

998999
# Due to how process substitution and newer bash versions work, this function
@@ -1087,9 +1088,6 @@ elif [ $result -eq 2 ]; then
10871088
FORMATTED_CONF="\`$SNAPRAID_CONF\`"
10881089
NOTIFY_OUTPUT="$SUBJECT Stopping the script due to multiple SnapRAID configuration files. Please choose one config file and update your settings in the script-config file at ""$CONFIG_FILE""."
10891090
notify_warning
1090-
if [ "$EMAIL_ADDRESS" ]; then
1091-
trim_log < "$TMP_OUTPUT" | send_mail
1092-
fi
10931091
exit 1;
10941092

10951093
else
@@ -1100,9 +1098,6 @@ else
11001098
FORMATTED_CONF="\`$SNAPRAID_CONF\`"
11011099
NOTIFY_OUTPUT="$SUBJECT The script cannot be run! Please check your settings, because the specified file $FORMATTED_CONF does not exist."
11021100
notify_warning
1103-
if [ "$EMAIL_ADDRESS" ]; then
1104-
trim_log < "$TMP_OUTPUT" | send_mail
1105-
fi
11061101
exit 1;
11071102
fi
11081103
}

0 commit comments

Comments
 (0)