105 lines
2.6 KiB
Bash
Executable File
105 lines
2.6 KiB
Bash
Executable File
#!/usr/bin/bash
|
|
|
|
# BEGIN: Variables
|
|
## Duration to wait to see if service has come back up
|
|
DURATION=10
|
|
## Wait duration to send next part of message
|
|
WAIT=1
|
|
|
|
## Test flag
|
|
TEST=0
|
|
|
|
## Email addresses
|
|
NOTIFY_EMAIL=recipient@domain.com
|
|
MAIL_FROM=username@domain.net
|
|
DAMAIN=` echo $NOTIFY_EMAIL | cut -d @ -f 2 `
|
|
|
|
# END: Variables
|
|
|
|
|
|
# BEGIN: Helper Functions
|
|
|
|
send_email () {
|
|
local MESSAGE="${1:?"send_email: No message was passed through"}"
|
|
sendmail $NOTIFY_EMAIL <<< "$MESSAGE"
|
|
|
|
}
|
|
|
|
|
|
: "
|
|
The only option is to manually telnet to the smtp port on the authoritative mailserver for the target domain. As otherwise unless a mail-host is configured as an email relay, you will be unable to send an email to a user outside of the mailservers domain (without authentication).
|
|
"
|
|
do_emergency_email () {
|
|
local MESSAGE="${1:-"The Postfix service has failed to come up on `hostname` (`hostname -i`) after a service restart. Please ssh into server to troubleshoot the issues."}"
|
|
## Getting a mailserver IP for manual message
|
|
MAILSERVER=`dig $DOMAIN mx | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -n 1`
|
|
|
|
## Setting up coprocess to send commands to telnet session coproc TELNET { telnet $MAILSERVER 25; }
|
|
|
|
## Commands to send email manually
|
|
local -a commands=(
|
|
"ehlo camtel.net\n"
|
|
"mail from: <$MAIL_FROM>\n"
|
|
"rcpt to: <$NOTIFY_EMAIL>\n"
|
|
"data\n"
|
|
"Subject: Postfix Service Failure\nFrom: $MAIL_FROM\nTo: $NOTIFY_EMAIL\n"
|
|
"$MESSAGE\n\n.\n"
|
|
"quit\n"
|
|
)
|
|
|
|
## Iterate through commands and send them to coprocesses
|
|
### We need to wait before each command as the remote mailserver will not catch everything otherwise.
|
|
for i in ${!commands[@]}; do
|
|
COMMAND="${commands[$i]}"
|
|
echo -e "$COMMAND" >&${TELNET[1]}
|
|
sleep $WAIT
|
|
done
|
|
}
|
|
|
|
# END: Helper Functions
|
|
|
|
|
|
# BEGIN: Test Check
|
|
|
|
if [[ $TEST -eq 1 ]]; then
|
|
MESSAGE_1="
|
|
This is a test message to verify that postfix can send an email
|
|
"
|
|
send_email "$MESSAGE_1"
|
|
|
|
MESSAGE_2="This is a test email to make sure the postfix crash workaround email works"
|
|
|
|
do_emergency_email "$MESSAGE_2"
|
|
exit
|
|
fi
|
|
|
|
# END: Test Check
|
|
|
|
|
|
# BEGIN: Work
|
|
|
|
if [[ `systemctl is-active postfix` != 'active' ]]; then
|
|
systemctl restart postfix
|
|
|
|
sleep $DURATION
|
|
|
|
SERVICE_STATUS=` systemctl is-active postfix `
|
|
|
|
if [[ "$SERVICE_STATUS" == 'active' ]]; then
|
|
MESSAGE="
|
|
SUBJECT: Postfix Service Failure
|
|
FROM: root
|
|
TO: $NOTIFY_EMAIL
|
|
|
|
The postfix service on `hostname` (`hostname -i`) was found to be not running.
|
|
|
|
The service has been restarted, and after waiting $DURATION seconds it was found to be $SERVICE_STATUS.
|
|
"
|
|
send_email "$MESSAGE"
|
|
else
|
|
do_emergency_email
|
|
fi
|
|
fi
|
|
|
|
# END: Work
|