Send a single email only

Gather any detected errors into a list and send them all at once.
main
mj-saunders 2022-11-03 14:20:47 +04:00
parent 7eff14d01f
commit b173ced7ec
2 changed files with 28 additions and 15 deletions

View File

@ -2,6 +2,8 @@ Subject: [AUTOMATION TEST MAIL] [URGENT] Server Report
Priority: urgent
Importance: high
Status {STATUS} detected on {SERVER} server.
An error was detected on the following server(s):
{ERR_LIST}
- OMN Watchdog

View File

@ -5,21 +5,16 @@
RECIPIENTS="saunders@openworlds.info"
#witchescauldron@openworlds.info"
send_mail() {
# Use bash's awful syntax to escape any / in the server string
SERVER=${1//\//\\\/}
STATUS=$2
cat mail_template \
| sed "s/{SERVER}/${SERVER}/g" \
| sed "s/{STATUS}/${STATUS}/g" \
| msmtp -a omn_server_watch ${RECIPIENTS}
}
URLS=(
unite.openworlds.info
openworlds.info
pad.openworlds.info
visionon.tv
activism.openworlds.info
campaign.openworlds.info
#indymedia.openworlds.info
#roadstonowhere.openworlds.info
#indymedia.hs2rebellion.earth
)
# TODO: How to check these sensibly?
SERVICES=(
@ -27,9 +22,26 @@ SERVICES=(
conference.openworlds.info
# Not much point checking the mail server
# as this script relies on it to send reports
# TODO: Separate local check just for this?
#mail.openworlds.info
)
send_mail() {
local -n error_list=$1
local error_string=""
for i in "${!error_list[@]}"; do
# Use bash's awful syntax to escape any / in the server string
code=${i//\//\\\/}
server=${error_list[$i]}
error_string+=" ${code}: ${server}\n"
done
cat mail_template \
| sed "s/{ERR_LIST}/${error_string}/g" \
| msmtp -a omn_server_watch ${RECIPIENTS}
}
declare -A FLAGGED
for uri in "${URLS[@]}"; do \
@ -38,11 +50,10 @@ for uri in "${URLS[@]}"; do \
stat="$(curl -s -o /dev/null -w "%{http_code}%{stdout}" ${full_uri})"
echo "${stat}"
if [ "${stat}" -ne "200" ]; then
# All other HTTP responses should not technically be errors
if ((${stat} >= 400 && ${stat} <= 599)); then
FLAGGED[${full_uri}]=${stat}
fi
done
for i in "${!FLAGGED[@]}"; do \
send_mail "${i}" "${FLAGGED[$i]}"
done
send_mail FLAGGED