This script is targeted to be part of a mechanism to monitor the proper operation of a system sending emails.
It checks that the IMAP Server received a message with a certain SUBJECT in
the last TIMEOUT minutes.
If such a message is missing during the TIMEOUT
time frame, it will send an email notification.
It therefore expects that some other application sends messages with the given SUBJECT with a periodicity that is slightly less than TIMEOUT minutes.
It can help detect the following:
- That a mail server is down or limited (quota reached),
- That an application can no longer send emails (credentials, server access);
- That an application no longer sends functional alerts (by simulating alert generating event sources on virtual inputs or devices).
Add monitor_email.py
to your system.
Add a file to /etc/cron.d/
as show below. There are of course other
methods to execute the script.
SHELL=/bin/sh
SCRIPT_ROOT=/path/to/scripts/
LOG_DIR=/path/to/logs
MONITOR_SUBJECT="Monitor Test Mail"
MAIL_SERVER="imap.example.com"
MAIL_USER="[email protected]"
MAIL_PASSWORD="Password"
NOTIFICATION_MAILS="[email protected]"
# m h dom mon dow user command
# `sendmonitoremail` is not provided, this is a script that sends or triggers the sending of the monitoring message.
# It could be configured & sent from your application server that would be different from the monitoring server.
*/15 * * * * some_user ${SCRIPT_ROOT}/sendmonitoremail $MAIL_USER "$MONITOR_SUBJECT" > $LOG_DIR/sendMonitorMail.log 2>&1
5,20,35,50 * * * * some_user ${SCRIPT_ROOT}/monitor_email.py --imap-host $MAIL_SERVER --timeout 20 --user $MAIL_USER --password "$MAIL_PASSWORD" --emails "$NOTIFICATION_MAILS" --subject "$MONITOR_SUBJECT" > $LOG_DIR/monitorMail.log 2>&1
If you like, you can use send_email.py
to send the test messages.
However, I recommend to send them using the application that you really
want to send them from for end-to-end monitoring.
In my case, this allowed me to test an email service while I did not have access to the third party application to set up end-to-end testing.
SHELL=/bin/sh
SCRIPT_ROOT=/path/to/scripts/
LOG_DIR=/path/to/logs
MONITOR_SUBJECT="Monitor Test Mail"
SMTP_SERVER="smtp.example.com"
SMTP_USER="[email protected]"
SMTP_PASSWORD="Password"
SMTP_PORT=587
TEST_EMAILS="[email protected]"
# You can optionally add "--message" to the command below
0,15,30,45 * * * * some_user ${SCRIPT_ROOT}/send_email.py --user $SMTP_USER --password "$SMTP_PASSWORD" --emails "$TEST_MAILS" --smtp-port "$SMTP_PORT" --subject "$MONITOR_SUBJECT" > $LOG_DIR/monitorSendMail.log 2>&1
You can provide the password as an environment variable instead of using
the --password
argument. This can be done by setting the EMAIL_PASSWORD
environment variable before running the script. For example:
EMAIL_PASSWORD=your_password ${SCRIPT_ROOT}/monitor_email.py --imap-host $MAIL_SERVER --timeout 20 --user $MAIL_USER --emails "$NOTIFICATION_MAILS" --subject "$MONITOR_SUBJECT" > $LOG_DIR/monitorMail.log 2>&1
Or, for sending emails:
EMAIL_PASSWORD=your_password ${SCRIPT_ROOT}/send_email.py --user $SMTP_USER --emails "$TEST_MAILS" --smtp-port "$SMTP_PORT" --subject "$MONITOR_SUBJECT" > $LOG_DIR/monitorSendMail.log 2>&1
This is a quick and not so dirty script which can be improved upon, but does the job.