File: //lib64/nagios/plugins/check_a2_imap_auth
#!/bin/bash
# Nagios plugin to check Dovecot authentication via IMAP
# Usage: ./check_dovecot_auth.sh <hostname>
HOST=$(hostname)
DEFAULT_USER="authcheck@www${HOST}"
PASSWORD='~\4T]Fn~{3a~lj!"!??dEPXY'
PORT=993
TIMEOUT=5
# Resolve hostname variations
if [[ "$HOST" =~ ^a2sd|^a2ssr|^a2ls|^a2d ]]; then
HOST=${HOST/#a2/www}
elif [[ "$HOST" =~ ^supercp ]]; then
HOST="www$HOST"
elif [[ "$HOST" =~ ^thss ]]; then
HOST=${HOST/#thss/wwwthss}
elif [[ "$HOST" =~ ^th ]]; then
HOST=${HOST/#ths/wwwth}
elif [[ "$HOST" =~ ^mi3|^az1 ]]; then
HOST="www$HOST"
else
HOST=${HOST/#a2s/www}
fi
# Encode credentials for AUTH PLAIN (Base64)
AUTH_STRING=$(printf "\0%s\0%s" "$DEFAULT_USER" "$PASSWORD" | base64 -w 0)
# IMAP authentication command using AUTH PLAIN
IMAP_COMMAND="A AUTHENTICATE PLAIN $AUTH_STRING"
LOGOUT_COMMAND="B LOGOUT"
# Connect to Dovecot IMAP and attempt authentication
RESPONSE=$(echo -e "$IMAP_COMMAND\n$LOGOUT_COMMAND" | \
openssl s_client -quiet -connect "$HOST:$PORT" -crlf -ign_eof 2>/dev/null | \
grep -E "^A OK")
if [[ -n "$RESPONSE" ]]; then
echo "OK: IMAP Authentication is working"
exit 0
else
echo "CRITICAL: IMAP Authentication is not working on $HOST"
exit 2
fi