Add script 'remove-fatal-errors-in-log-file.sh'..

This commit is contained in:
Christoph 2023-01-29 23:40:06 +01:00
parent 16b4442361
commit 56f999b211

View File

@ -0,0 +1,346 @@
#!/usr/bin/env bash
script_name="$(basename $(realpath $0))"
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/${script_name%%.*}.conf"
LOCK_DIR="/tmp/${script_name%%.*}.LOCK"
log_file="${LOCK_DIR}/${script_name%%.*}.log"
# ----------
# Base Function(s)
# ----------
clean_up() {
# Perform program exit housekeeping
rm -rf "$LOCK_DIR"
blank_line
exit $1
}
echononl(){
if $terminal ; then
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n " $*\\c" 1>&2
else
echo -e -n " $*" 1>&2
fi
rm /tmp/shprompt$$
fi
}
fatal(){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
echo ""
echo -e " \033[31m\033[1mScript was interupted\033[m!"
else
echo " [ Fatal ]: $*"
echo ""
echo " Script was terminated...."
fi
echo ""
clean_up 1
}
error (){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
else
echo "[ Error ]: $*"
fi
echo ""
}
warn (){
echo ""
if $terminal ; then
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
else
echo "[ Warning ]: $*"
fi
echo ""
}
warn_only_terminal () {
if $terminal ; then
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
fi
}
info (){
if $terminal ; then
echo ""
echo -e " [ \033[32m\033[1mInfo\033[m ] $*"
echo ""
fi
}
ok (){
if $terminal ; then
echo ""
echo -e " [ \033[32m\033[1mOk\033[m ] $*"
echo ""
fi
}
echo_done() {
if $terminal ; then
echo -e "\033[75G[ \033[32mdone\033[m ]"
fi
}
echo_ok() {
if $terminal ; then
echo -e "\033[75G[ \033[32mok\033[m ]"
fi
}
echo_warn() {
if $terminal ; then
echo -e "\033[75G[ \033[33mwarn\033[m ]"
fi
}
echo_failed(){
if $terminal ; then
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
fi
}
echo_skipped() {
if $terminal ; then
echo -e "\033[75G[ \033[90m\033[1mskipped\033[m ]"
fi
}
echo_wait(){
if $terminal ; then
echo -en "\033[75G[ \033[5m\033[1m...\033[m ]"
fi
}
trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
}
blank_line() {
if $terminal ; then
echo ""
fi
}
is_number() {
return $(test ! -z "${1##*[!0-9]*}" > /dev/null 2>&1);
# - also possible
# -
#[[ ! -z "${1##*[!0-9]*}" ]] && return 0 || return 1
#return $([[ ! -z "${1##*[!0-9]*}" ]])
}
# ----------
# - Some checks ..
# ----------
# - Running in a terminal?
# -
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
# ==========
# - Begin Main Script
# ==========
# ----------
# - Headline
# ----------
if $terminal ; then
clear
echo ""
echo -e "\033[1m----------\033[m"
echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m"
echo -e "\033[1m----------\033[m"
fi
# ----------
# Read Configurations from $conf_file
# ----------
# - Give your default values here
# -
DEFAULT_MAIL_LOG="/var/log/mail.log"
DEFAULT_CONFLICTING_SCRIPTS=""
DEFAULT_COMPANY="O.OPEN"
DEFAULT_CONTENT_TYPE='Content-Type: text/plain;\n charset="utf-8"'
DEFAULT_SENDER_ADDRESS="${script_name%%.*}@$(hostname -f)"
DEFAULT_ALERT_EMAIL_ADDRESSES="ckubu@oopen.de"
if [[ -f "$conf_file" ]]; then
source "$conf_file"
else
warn_only_terminal "No configuration file '$conf_file' present.\n
Loading default values.."
fi
[[ -n "$MAIL_LOG" ]] || MAIL_LOG="${DEFAULT_MAIL_LOG}"
[[ -n "$(trim $alert_email_addresses)" ]] || alert_email_addresses=("${DEFAULT_ALERT_EMAIL_ADDRESSES[@]}")
if [[ ${#alert_email_addresses} -gt 0 ]] ; then
for _email in $alert_email_addresses ; do
alert_email_arr+=("$_email")
done
fi
[[ -n "$sender_address" ]] || sender_address="${DEFAULT_SENDER_ADDRESS}"
[[ -n "$content_type" ]] || content_type="${DEFAULT_CONTENT_TYPE}"
[[ -n "$company" ]] || company="${DEFAULT_COMPANY}"
# -------------
# - Job is already running?
# -------------
# - If job already runs, stop execution..
# -
if mkdir "$LOCK_DIR" 2> /dev/null ; then
## - Remove lockdir when the script finishes, or when it receives a signal
trap "clean_up 1" SIGHUP SIGINT SIGTERM
else
datum="$(date +"%d.%m.%Y %H:%M")"
msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\n Exiting now.."
echo ""
echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running."
echo ""
echo -e " Exiting now.."
echo ""
for _email in ${alert_email_arr[@]} ; do
echo -e "To:${_email}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \
| sendmail -F "Error `hostname -f`" -f $sender_address $_email
done
exit 1
fi
if $terminal ; then
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo " Give E-Mail LOG-file here ...."
echo ""
echo -e " Type \033[33m<return>\033[m to accept the default."
echo ""
echo ""
_MAIL_LOG=$MAIL_LOG
MAIL_LOG=""
echononl "Mail LOG-file [\033[1m$_MAIL_LOG\033[m]: "
read MAIL_LOG
if [[ -z "$MAIL_LOG" ]] ; then
MAIL_LOG=$_MAIL_LOG
elif [[ ! -f "$MAIL_LOG" ]] ; then
MAIL_LOG=""
fi
while [[ -z "$MAIL_LOG" ]] ; do
echo ""
error "Log-file \033[1m${MAIL_LOG}\033[m does not exist! Try again.."
echononl " \033[33mMail LOG-file [\033[1m${_MAIL_LOG}\033[33m]\033[m: "
read MAIL_LOG
if [[ -z "$MAIL_LOG" ]] ; then
MAIL_LOG=$_MAIL_LOG
elif [[ ! -f "$MAIL_LOG" ]] ; then
MAIL_LOG=""
fi
done
echo ""
echo ""
echo -e " \033[1m------------------\033[m"
echo -e " \033[32mParameter Summary:\033[m"
echo -e " \033[1m------------------\033[m"
echo ""
echo " E-Mail LOG file.............: $MAIL_LOG"
echo ""
fi
if $terminal ; then
echo ""
echo ""
echo -e " \033[1mMain part of script ..\033[m"
echo ""
fi
_err_msg="$(grep -E "^$(LC_ALL="en_US.UTF-8" date +'%b %d ' | sed -e 's/0\([0-9]\)/ \1/g')" $MAIL_LOG | grep " fatal:")"
if [[ -n "$_err_msg" ]] ; then
if $terminal; then
error "Found entries 'fatal' in LOG-file \033[1m${MAIL_LOG}\033[m .."
else
error "Found entries 'fatal' in LOG-file '${MAIL_LOG}'.."
fi
echo -e "$_err_msg"
echo ""
if $terminal; then
echononl "Try to delete lines conatining \033[1mfatal:\033[m in LOG-file \033[1m${MAIL_LOG}\033[m .."
else
info "Try to delete lines conatining 'fatal:' in LOG-file '${MAIL_LOG}'.."
fi
string_to_delete=" fatal: "
sed -i "/${string_to_delete}/d" ${MAIL_LOG} > ${log_file} 2>&1
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat ${log_file})"
else
echo_ok
fi
else
blank_line
info "No entry 'fatal' found in LOG-file \033[1m${MAIL_LOG}\033[m ."
blank_line
fi
clean_up 0