Michael Wallace
Michael Wallace

Reputation: 1

How do I set email configuration when service fails with Office 365 in Linux Red Hat Enterprise 9.3?

I am writing a Bash shell script to send an email notification when docker fails and then export the file. Networking has provided the email server, smtp and port number. All of these configuration I have included in my script. In addition, I am using postfix, but having problems with the hostname. When I go to restart the postfix service I receive this error message listed below. By the way, we are using Office 365.

postfix: warning: /etc/postfix/main.cf, line 118: overriding earlier entry: myorigin=$myhostname /usr/sbin/postconf: warning: /etc/postfix/main.cf, line 118: overriding earlier entry: myorigin=$myhostname postfix/postlog: warning: /etc/postfix/main.cf, line 118: overriding earlier entry: myorigin=$myhostname

When I execute the script, it does not tell the docker service has failed in the log file and does not send an e-mail notification.

My previous script sent out log script every hour indicating that service is up and running.

SERVICE_NAME="docker.service"
LOG_FILE="/var/log/docker-error-tmp.log"
EMAIL_TO="[email protected]"
SUBJECT="Docker Service Failure Alert"
BODY="The Docker Service on $(hostname) is down.  Please check the service status."
TMP_FILE="/tmp/docker_error.log"


# Function to check journalctl logs
check_journalctl() {
    journalctl -u $SERVICE_NAME --since "10  minutes ago" | grep -i 'level=error' | grep -i 'msg="failed"'
}

# Function to check /var/log/messages
check_var_log_messages() {
    grep -i 'level=error' /var/log/messages | grep -i 'msg="failed"'
}


# Check for errors in journalctl and /var/log/messages
ERROR_LOG_JOURNALCTL=$(check_journalctl)
ERROR_LOG_MESSAGES=$(check_var_log_messages)

# Log the timestamp and hostname
if [ ! -z "$ERROR_LOG_JOURNALCTL" ] || [ ! -z "$ERROR_LOG_MESSAGES" ]; then
    echo "Service $SERVICE_NAME has failed or encountered an error. Here are the details:" >> $LOG_FILE
    [ ! -z "$ERROR_LOG_JOURNALCTL" ] && echo "$ERROR_LOG_JOURNALCTL" >> $LOG_FILE
    [ ! -z "$ERROR_LOG_MESSAGES" ] && echo "$ERROR_LOG_MESSAGES" >> $LOG_FILE

#Restart Docker Service
    echo "Restarting the service..." >> $LOG_FILE
    systemctl restart $SERVICE_NAME
    echo "Service $SERVICE_NAME has been restarted." >> $LOG_FILE

#Send E-mail notification
{
  echo "Subject: $SUBJECT"
  echo "TO: $EMAIL_TO"
  echo
  echo "Service $SERVICE_NAME has failed or encountered an error.  Here are the details."
        [ ! -z "$ERROR_LOG_JOURNALCTL" ] && echo "$ERROR_LOG_JOURNALCTL"
        [ ! -z "$ERROR_LOG_MESSAGES" ] && echo "$ERROR_LOG_MESSAGES"
  echo "The service has been restarted"
} >$TMP_FILE

# Send Email using Postfix
sendmail -t < $TMP_FILE

# Clean up temp file
  rm -f $TMP_FILE
  else

Upvotes: 0

Views: 34

Answers (0)

Related Questions