Skip to content

Instantly share code, notes, and snippets.

@subutux
Created May 20, 2015 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subutux/7ae0d03018a2b32ca175 to your computer and use it in GitHub Desktop.
Save subutux/7ae0d03018a2b32ca175 to your computer and use it in GitHub Desktop.
Alert dovecot imap users about them exceeding quotum using IMAP ALERT (compatible with outlook 2007-2013)
#!/bin/bash
#
# Author: Stijn Van Campenhout <stijn.vancampenhout@gmail.com>
#
# Calculate the percentage of the maildir quota using a mysql calculation.
# Compatible with dovecot & postfixadmin, using the quota2 format.
#
# Add this to the dovecot (2.x) configuration:
## /etc/dovecot/conf.d/10-master.conf
#service imap-postlogin {
# # all post-login scripts are executed via script-login binary
# executable = script-login /location/of/postlogin.sh
# # the script process runs as the user specified here (v2.0.14+):
# user = $default_internal_user
# # this UNIX socket listener must use the same name as given to imap executable
# unix_listener imap-postlogin {
# }
#}
#
# Save this file to /location/of/postlogin.sh
# Configure:
#DB stuff
DB="my_postfixadmin_mail_db"
DB_USER="mail_db_user"
DB_PASS="mail_db_secret"
SQL="select round((quota2.bytes/mailbox.quota * 100),0) as percent FROM mailbox,quota2 WHERE mailbox.username = quota2.username AND mailbox.username='${USER}';"
#Settings
# 90% seems reasonable.
MAX_PERCENTAGE=90
#Only display once an hour. Don't let them get to mad.
GRACE_PERIOD=3600
RESULT=$(echo "${SQL}" | mysql -u ${DB_USER} -p${DB_PASS} -D${DB} -ss);
TIME_FILE=/var/spool/quota-warnings/${USER}.time;
LAST_WARN_FILE=/var/log/quota-warnings/${USER}.warn;
TIME=$(date +"%s");
if [ $RESULT -gt $MAX_PERCENTAGE ];then
#Read the last time of warning
if [ -f $TIME_FILE ];then
LAST_WARNING=$(cat ${TIME_FILE});
else
#Write the current time
echo $TIME > $TIME_FILE;
fi
DIFF=$(expr $TIME - $LAST_WARNING);
#If the time is larger than the grace period
if [ $DIFF -gt $GRACE_PERIOD ];then
# write to time file
echo $TIME > $TIME_FILE;
# and display warning
echo -n -e "* OK [ALERT] Account ${USER} is using $RESULT% of mailbox space!\r\n" > ${LAST_WARN_FILE}
echo -n -e "* OK [ALERT] Account ${USER} is using $RESULT% of mailbox space!\r\n"
fi
else
echo "" > ${LAST_WARN_FILE}
fi
exec "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment