Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added webhook option for Discord notifications #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions bht
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# -b, --blocks= the size of blocks in bytes. default is 32768
# -n, --stride= the number of blocks per test. default is 512
# -e, --email= email address to send notifications
# -w, --webhook= discord webhook url
# -s, --status show status of running tests
# -d, --directory path to directory with bht data. defaults to current working directory
# -t, --terminate terminte all running tests
Expand All @@ -22,6 +23,7 @@ USAGE+="login later to check on the results.]"
USAGE+="[b:blocks]:[blocks:=32768?the size of blocks in bytes.]"
USAGE+="[n:stride]:[stride:=512?the number of blocks per test.]"
USAGE+="[e:email]:[email?email address to send notifications.]"
USAGE+="[w:webhook]:[webhook?discord webhook url to send notifications.]"
USAGE+="[s:status?show status of current tests.]"
USAGE+="[d:directory]:[directory?path to directory with bht data. defaults to current working directory.]"
USAGE+="[t:terminate?terminate all running tests.]"
Expand All @@ -34,13 +36,15 @@ USAGE+="[t:terminate?terminate all running tests.]"
OPT_BLOCKS=0
OPT_STRIDE=0
OPT_EMAIL=0
OPT_WEBHOOK=0
OPT_STATUS=0
OPT_BHTDIR=0
OPT_TERMINATE=0
# option variables
BLOCKS_SIZE=32768
STRIDE_SIZE=512
EMAIL=
WEBHOOK=
BHTDIR="$(pwd)"
# misc
NL=$'\n'
Expand Down Expand Up @@ -206,8 +210,8 @@ function run_badblocks {
elif [[ $_hdd_type == "SAS" ]] ; then
smartctl --xall $D > ${_dir}/post-test.smart
fi
# when we're done, send out notification if we have EMAIL
if [[ -n $EMAIL ]] ; then
# when we're done, send out notification if we have EMAIL or WEBHOOK
if [[ -n $EMAIL || -n $WEBHOOK ]] ; then
_mesg="Model=${HDD_DATA[$_hdd].model}, Serial=${HDD_DATA[$_hdd].serial}$NL"
# grab the first part of the status string
_status=$(awk '{gsub(/\b+/,"\n",$0); print}' ${_dir}/badblocks.out \
Expand All @@ -221,7 +225,12 @@ function run_badblocks {
_mesg+="badblocks[$_status]$NL"
# append SMART check
_mesg+="$(SMART_check ${_dir}/post-test.smart)"
print "$_mesg"|mailx -s "badblocks testing of $_hdd ended" $EMAIL
if [[ -n $EMAIL ]] ; then
print "$_mesg"|mailx -s "badblocks testing of $_hdd ended" $EMAIL
fi
if [[ -n $WEBHOOK ]] ; then
curl -H "Content-Type: application/json" -d "{\"username\": \"BHT\", \"content\": \"Badblocks testing of $_hdd ended.\"}" $WEBHOOK
fi
fi
return 0
}
Expand All @@ -240,6 +249,7 @@ do
b|blocks) OPT_BLOCKS=1 && BLOCKS_SIZE=$OPTARG ;;
n|stride) OPT_STRIDE=1 && STRIDE_SIZE=$OPTARG ;;
e|email) OPT_EMAIL=1 && EMAIL=$OPTARG ;;
w|webhook) OPT_WEBHOOK=1 && WEBHOOK=$OPTARG ;;
s|status) OPT_STATUS=1 ;;
d|directory) OPT_BHTDIR=1 && BHTDIR=$OPTARG ;;
t|terminate) OPT_TERMINATE=1 ;;
Expand Down Expand Up @@ -270,14 +280,14 @@ if (( $(id -u) != 0 )) ; then
fi

# check conflicting options
# --status should not be used with --blocks, --stride, --email, --terminate
if (( $OPT_STATUS == 1 && ( $OPT_BLOCKS == 1 || $OPT_STRIDE == 1 || $OPT_EMAIL == 1 ) )) ; then
print "ERR: cannot specify --status with --terminate, --blocks, --stride, nor --email."
# --status should not be used with --blocks, --stride, --email, --terminate, --webhook
if (( $OPT_STATUS == 1 && ( $OPT_BLOCKS == 1 || $OPT_STRIDE == 1 || $OPT_EMAIL == 1 || $OPT_WEBHOOK == 1) )) ; then
print "ERR: cannot specify --status with --terminate, --blocks, --stride, --email, nor --webhook."
exit 1
fi
# --terminate should not be used with --blocks, --stride, --email
if (( $OPT_TERMINATE == 1 && ( $OPT_BLOCKS == 1 || $OPT_STRIDE == 1 || $OPT_EMAIL == 1 ) )) ; then
print "ERR: cannot specify --terminate with --blocks, --stride, nor --email."
# --terminate should not be used with --blocks, --stride, --email, --webhook
if (( $OPT_TERMINATE == 1 && ( $OPT_BLOCKS == 1 || $OPT_STRIDE == 1 || $OPT_EMAIL == 1 || $OPT_WEBHOOK == 1 ) )) ; then
print "ERR: cannot specify --terminate with --blocks, --stride, --email, nor --webhook."
exit 1
fi

Expand Down