Improve readability

main
mj-saunders 2022-11-03 16:18:06 +04:00
parent 01295e851f
commit 0bffafcc82
1 changed files with 47 additions and 40 deletions

View File

@ -30,38 +30,8 @@ SERVICES=(
#mail.openworlds.info
)
rotate_log()
main()
{
local -n log=$1
local max_log=4096 # 4M as we get size in K below
local log_size=$(du -k "${log}" | cut -f 1)
if ((${log_size} > ${max_log})); then
# Clobber any previous backup
mv -f ${log} ${log}.1
fi
touch ${log}
}
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[@]} \
|| echo "[ERROR] Failed to send mail!"
}
declare -A FLAGGED
rotate_log LOG_FILE
@ -88,7 +58,6 @@ ret=0
if ((${#FLAGGED[@]} > 0)); then
echo "== Error(s) detected. Sending mail to:"
for rcp in ${RECIPIENTS[@]}; do
echo " ${rcp}"
done
@ -101,3 +70,41 @@ else
fi
exit ${ret}
}
rotate_log()
{
local -n log=$1
local max_log=4096 # 4M as we get size in K below
touch ${log}
local log_size=$(du -k "${log}" | cut -f 1)
if ((${log_size} > ${max_log})); then
# Clobber any previous backup
mv -f ${log} ${log}.1
touch ${log}
fi
}
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[@]} \
|| echo "[ERROR] Failed to send mail!"
}
main