Created
May 23, 2020 10:44
-
-
Save laclaro/3dc88a254685713fec1ac029798943cc to your computer and use it in GitHub Desktop.
check postfix and cyrus-imapd health with expect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright (C) <year> <name of author> | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <https://www.gnu.org/licenses/>. | |
# | |
# mailsystem_check_restart.sh uses expect to establish a connection to cyrus-imapd | |
# and postfix with openssl. If the services do not react as expected, they are restarted. | |
# The script should work with any SMTP and IMAP server. | |
# | |
# Author: Henning Hollermann - laclaro@mail.com - 2020 | |
do_restart_cyrus() { | |
echo "No reply from cyrus on port 993. Restarting cyrus-imapd." | |
/usr/sbin/service cyrus-imapd restart | |
if [ $? != 0 ]; then | |
echo "cyrus-imapd hangs, killing it by force..." | |
/usr/bin/killall -9 cyrus-master | |
/usr/sbin/service cyrus-imapd restart | |
fi | |
return 0 | |
} | |
do_restart_postfix() { | |
echo "No reply from postfix on port 25. Restarting cyrus-imapd." | |
/usr/sbin/service postfix restart | |
return 0 | |
} | |
check_cyrus() { | |
/usr/bin/expect -c ' | |
set server hollermann.eu | |
set port 993 | |
set timeout 5 | |
spawn openssl s_client -crlf -connect $server:$port | |
expect { | |
timeout { | |
puts stdout "timeout while connecting to $server"; | |
exit 1; | |
} | |
"* OK" { exit 0; } | |
eof { exit 1; } | |
} | |
}' > /dev/null | |
return $? | |
} | |
check_postfix() { | |
/usr/bin/expect -c ' | |
set timeout 5 | |
set server hollermann.eu | |
set port 25 | |
spawn openssl s_client -starttls smtp -connect $server:$port | |
expect { | |
timeout { | |
puts stdout "timeout while connecting to $server"; | |
exit 1; | |
} | |
"250 DSN" { exit 0; } | |
eof { exit 1; } | |
}' > /dev/null | |
return $? | |
} | |
if [ "x$1" == "xforce" ]; then | |
do_restart_cyrus | |
do_restart_postfix | |
else | |
echo "Info: Use \"$0 force\" to force the restart of cyrus-imapd and postfix" | |
check_cyrus || do_restart_cyrus | |
check_postfix || do_restart_postfix | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment