190 lines
7.8 KiB
Bash
Executable File
190 lines
7.8 KiB
Bash
Executable File
#!/bin/bash
|
||
|
||
########################################################################
|
||
# Testing email delivery
|
||
#
|
||
# Required nagios email check plugins and zabbix_sender
|
||
#
|
||
# Author: Sergey Kalinin
|
||
# https://nuk-svk.ru
|
||
# svk@nuk-svk.ru
|
||
########################################################################
|
||
# Before, you will must settings variables into .mail_server_FQDN.env files
|
||
# and set environment variables:
|
||
# BIN_DIR - directory with nagios plugins
|
||
# ETC_DIR - directory containing .mail_server_FQDN.env
|
||
# Or put bin and var files into /usr/local/bin and /usr/local/etc respectively
|
||
#
|
||
# Used:
|
||
#
|
||
# $check_email_send_delivery.sh METHOD MAIL_SERVER
|
||
#
|
||
# Where METHOD must be like:
|
||
#
|
||
# "local" - local check
|
||
# "incoming" - from external sending to local receiving check
|
||
# "outgoing" - from local sending to external receiving check
|
||
#
|
||
# And MAIL_SERVER - must by FQDN mail server name
|
||
########################################################################
|
||
|
||
if [ -z $1 ]; then
|
||
echo "Enter check mode"
|
||
exit 1
|
||
fi
|
||
|
||
if [ -z $2 ]; then
|
||
echo "Enter mail server address"
|
||
exit 1
|
||
fi
|
||
|
||
MAIL_SERVER=$2
|
||
ZABBIX_HOST=${ZABBIX_HOST:-${MAIL_SERVER}}
|
||
BIN_DIR=${BIN_DIR:-/usr/local/bin}
|
||
ETC_DIR=${ETC_DIR:-/usr/local/etc}
|
||
|
||
RUNNING_ON_DOCKER=${RUNNING_ON_DOCKER:-TRUE}
|
||
|
||
# Include variables if runing non docker
|
||
if [ -z ${RUNNING_ON_DOCKER} ]; then
|
||
if [ -e "${ETC_DIR}/.${MAIL_SERVER}.env" ]; then
|
||
source "${ETC_DIR}/.${MAIL_SERVER}.env"
|
||
else
|
||
echo "File ${ETC_DIR}/.${MAIL_SERVER}.env not found. You must setting the variables"
|
||
exit 1
|
||
fi
|
||
fi
|
||
|
||
ZABBIX_SENDER=${ZABBIX_SENDER:-/usr/bin/zabbix_sender}
|
||
|
||
# Checking delays setting
|
||
WARNING_TIME=${WARNING_TIME:-60}
|
||
CRITICAL_TIME=${CRITICAL_TIME:-120}
|
||
TIMEOUT=${TIMEOUT:-120}
|
||
|
||
# Generate message body and subject
|
||
SUBJ="$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)"
|
||
BODY=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1)
|
||
|
||
#SUBJ="$(date). SMTP sending checking."
|
||
#BODY="SMTP sending checking. $(date)."
|
||
|
||
|
||
case "$1" in
|
||
local)
|
||
echo "> Local email service check: DOMAIN -> DOMAIN <"
|
||
|
||
#DELIVERY=$($BIN_DIR/check_email_delivery -H ${MAIL_SERVER} --mailto ${RECEIVER_EMAIL} \
|
||
# --mailfrom ${SENDER_EMAIL} --username ${RECEIVER_EMAIL} --password ${RECEIVER_PASSWORD} \
|
||
# --body 'Check email delivery')
|
||
|
||
DELIVERY=$($BIN_DIR/check_email_delivery \
|
||
--plugin "$BIN_DIR/check_smtp_send -H ${MAIL_SERVER} \
|
||
--mailto ${RECEIVER_EMAIL} --mailfrom ${SENDER_EMAIL} \
|
||
--header 'Subject: SMTP sending check' --header 'X-Your-Header: Yes'" \
|
||
--plugin "$BIN_DIR/check_imap_receive -H ${MAIL_SERVER} --ssl --port 993 \
|
||
--critical ${CRITICAL_TIME} --warning ${WARNING_TIME} --timeout ${TIMEOUT}\
|
||
--username ${RECEIVER_EMAIL} --password ${RECEIVER_PASSWORD} \
|
||
-s SUBJECT -s 'SMTP sending check' --capture-max 'Homeruns (\d+)' --delete")
|
||
|
||
SMTP_SEND=$($BIN_DIR/check_smtp_send -H ${MAIL_SERVER} \
|
||
--mailto ${RECEIVER_EMAIL} --mailfrom ${SENDER_EMAIL} \
|
||
--body 'Homeruns 5' --header 'Subject: SMTP sending check' --header 'X-Your-Header: Yes')
|
||
|
||
IMAP_RECEIVE=$($BIN_DIR/check_imap_receive -H ${MAIL_SERVER} --ssl --port 993 \
|
||
--critical ${CRITICAL_TIME} --warning ${WARNING_TIME} --timeout ${TIMEOUT} \
|
||
--username ${RECEIVER_EMAIL} --password ${RECEIVER_PASSWORD} \
|
||
-s SUBJECT -s "SMTP sending check" --capture-max "Homeruns (\d+)" --delete)
|
||
;;
|
||
incoming)
|
||
echo "> External email sending local receiving check: GOOGLE -> DOMAIN <"
|
||
# # В связи с ограничением провайдеров убрана проверка доставки
|
||
|
||
# DELIVERY=$($BIN_DIR/check_email_delivery \
|
||
# --plugin "$BIN_DIR/check_smtp_send -vvv -H ${EXT_SMTP_SERVER} --port 465 --ssl --username ${EXT_SENDER_EMAIL} \
|
||
# --password ${EXT_SENDER_PASSWORD} --mailto ${RECEIVER_EMAIL} --mailfrom ${EXT_SENDER_EMAIL} \
|
||
# --body "${BODY}" --header \"Subject: ${SUBJ}\" --header 'X-Your-Header: Yes'" \
|
||
# --plugin "$BIN_DIR/check_imap_receive -H ${MAIL_SERVER} --port 993 --ssl \
|
||
# --username ${RECEIVER_EMAIL} --password ${RECEIVER_PASSWORD} \
|
||
# -s SUBJECT -s "${SUBJ}" --capture-max \"${BODY}\" --nodelete-captured")
|
||
# --critical ${CRITICAL_TIME} --warning ${WARNING_TIME} --timeout ${TIMEOUT}\
|
||
|
||
SMTP_SEND=$($BIN_DIR/check_smtp_send -vvv -H ${EXT_SMTP_SERVER} --port 465 --ssl --username ${EXT_SENDER_EMAIL} \
|
||
--password ${EXT_SENDER_PASSWORD} --mailto ${RECEIVER_EMAIL} --mailfrom ${EXT_SENDER_EMAIL} \
|
||
--body "${BODY}" --header "Subject: ${SUBJ}" --header 'X-Your-Header: Yes')
|
||
|
||
IMAP_RECEIVE=$($BIN_DIR/check_imap_receive -H ${MAIL_SERVER} --ssl --port 993 \
|
||
--critical ${CRITICAL_TIME} --warning ${WARNING_TIME} --timeout ${TIMEOUT} \
|
||
--username ${RECEIVER_EMAIL} --password ${RECEIVER_PASSWORD} \
|
||
-s SUBJECT -s "${SUBJ}" --capture-max "${BODY}" --delete)
|
||
;;
|
||
outgoing)
|
||
echo "> Local email sending external receiving check: DOMAIN -> GOOGLE <"
|
||
|
||
DELIVERY=$($BIN_DIR/check_email_delivery \
|
||
--plugin "$BIN_DIR/check_smtp_send -H ${MAIL_SERVER}\
|
||
--mailto ${EXT_RECEIVER_EMAIL} --mailfrom ${SENDER_EMAIL} \
|
||
--body \"${BODY}\" --header \"Subject: ${SUBJ}\" --header 'X-Your-Header: Yes'" \
|
||
--plugin "$BIN_DIR/check_imap_receive -H ${EXT_IMAP_SERVER} --ssl --username ${EXT_RECEIVER_EMAIL} \
|
||
--password ${EXT_RECEIVER_PASSWORD} -s SUBJECT -s \"${SUBJ}\" --capture-max \"${BODY}\" \
|
||
--delete")
|
||
|
||
SMTP_SEND=$($BIN_DIR/check_smtp_send -H ${MAIL_SERVER}\
|
||
--mailto ${EXT_RECEIVER_EMAIL} --mailfrom ${SENDER_EMAIL} \
|
||
--body "${BODY}" --header "Subject: ${SUBJ}" --header 'X-Your-Header: Yes')
|
||
|
||
IMAP_RECEIVE=$($BIN_DIR/check_imap_receive -H ${EXT_IMAP_SERVER} --ssl --port 993 \
|
||
--critical ${CRITICAL_TIME} --warning ${WARNING_TIME} --timeout ${TIMEOUT} \
|
||
--username ${EXT_RECEIVER_EMAIL} --password ${EXT_RECEIVER_PASSWORD} \
|
||
-s SUBJECT -s "${SUBJ}" --capture-max "${BODY}" --delete)
|
||
;;
|
||
esac
|
||
#--ssl-ca-file ya.pem
|
||
|
||
DELIVERY_STATUS=$(echo "${DELIVERY}" | awk '{print $3}')
|
||
DELIVERY_TIME=$(echo "${DELIVERY}" | awk 'match($0,/delay=[0-9]+s/) {print substr($0,RSTART+6,RLENGTH-6-1)}')
|
||
DELIVERY_ELAPSED_TIME=$(echo "${DELIVERY}" | awk 'match($0,/elapsed=[0-9]+s/) {print substr($0,RSTART+8,RLENGTH-8-1)}')
|
||
|
||
echo "${DELIVERY}"
|
||
echo "${DELIVERY_STATUS}"
|
||
echo "${DELIVERY_TIME}"
|
||
echo "${DELIVERY_ELAPSED_TIME}"
|
||
|
||
SMTP_SEND_STATUS=$(echo ${SMTP_SEND} | awk '{print $3}')
|
||
SMTP_SEND_ELAPSED_TIME=$(echo ${SMTP_SEND} | awk 'match($0,/elapsed=[0-9]+s/) { print substr($0,RSTART+8,RLENGTH-8-1)}')
|
||
|
||
echo "${SMTP_SEND}"
|
||
echo "${SMTP_SEND_STATUS}"
|
||
echo "${SMTP_SEND_ELAPSED_TIME}"
|
||
|
||
IMAP_RECEIVE_STATUS=$(echo ${IMAP_RECEIVE} | awk '{print $3}')
|
||
IMAP_RECEIVE_ELAPSED_TIME=$(echo ${IMAP_RECEIVE} | awk 'match($0,/elapsed=[0-9]+s/) { print substr($0,RSTART+8,RLENGTH-8-1)}')
|
||
|
||
echo "${IMAP_RECEIVE}"
|
||
echo "${IMAP_RECEIVE_STATUS}"
|
||
echo "${IMAP_RECEIVE_ELAPSED_TIME}"
|
||
|
||
if [ $DELIVERY_STATUS ]; then
|
||
${ZABBIX_SENDER} -c /etc/zabbix/zabbix_agentd.conf -s "${ZABBIX_HOST}" -k email.delivery.${1}.status -o $DELIVERY_STATUS
|
||
fi
|
||
if [ $DELIVERY_TIME ]; then
|
||
${ZABBIX_SENDER} -c /etc/zabbix/zabbix_agentd.conf -s "${ZABBIX_HOST}" -k email.delivery.${1}.delay -o $DELIVERY_TIME
|
||
fi
|
||
if [ $DELIVERY_ELAPSED_TIME ]; then
|
||
${ZABBIX_SENDER} -c /etc/zabbix/zabbix_agentd.conf -s "${ZABBIX_HOST}" -k email.delivery.${1}.elapsed -o $DELIVERY_ELAPSED_TIME
|
||
fi
|
||
if [ $SMTP_SEND_STATUS ]; then
|
||
${ZABBIX_SENDER} -c /etc/zabbix/zabbix_agentd.conf -s "${ZABBIX_HOST}" -k email.smtp_send.${1}.status -o $SMTP_SEND_STATUS
|
||
fi
|
||
if [ $SMTP_SEND_ELAPSED_TIME ]; then
|
||
${ZABBIX_SENDER} -c /etc/zabbix/zabbix_agentd.conf -s "${ZABBIX_HOST}" -k email.smtp_send.${1}.elapsed -o $SMTP_SEND_ELAPSED_TIME
|
||
fi
|
||
if [ $IMAP_RECEIVE_STATUS ]; then
|
||
${ZABBIX_SENDER} -c /etc/zabbix/zabbix_agentd.conf -s "${ZABBIX_HOST}" -k email.imap_receive.${1}.status -o $IMAP_RECEIVE_STATUS
|
||
fi
|
||
if [ $IMAP_RECEIVE_ELAPSED_TIME ]; then
|
||
${ZABBIX_SENDER} -c /etc/zabbix/zabbix_agentd.conf -s "${ZABBIX_HOST}" -k email.imap_receive.${1}.elapsed -o $IMAP_RECEIVE_ELAPSED_TIME
|
||
fi
|
||
|
||
exit 0
|