In need of monitoring the quota for a couple of IMAP accounts I found a nice Python script called imapQuotaChecker.py, which I modified to make it read the configuration and several account names and passwords from a config file and to make it check these different accounts.
Config file:
- [setup]
- # Quota percentage above which a notification will be sent
- breach_level=90
- # Parameters of the account to check the quota for
- imap_host=imap.host.tld
- # Login with IMAPS
- use_ssl=yes
- # output on console?
- verbose=yes
- # Email address to send notifications to
- send_address=user@domain.tld
- # SMTP Parameters
- smtp_host=smtp.host.tld
- smtp_authentication = 0 # set to 1 if your SMTP server requires authentication, 0 if not
- # Following used only when authentication is set to 1
- smtp_user=username
- smtp_pass=password
- [accounts]
- user1@domain.tld: imap1_password
- user2@domain.tld: imap2_password
Script:
#!/usr/bin/python
"""
imapQuotaChecker.py 1.1
Last updated: May 27, 2011
Copyright (c) 2006 http://www.broobles.com/
Website: http://www.broobles.com/scripts/imapquotachecker/
Description:
===========
Checks the quota of an IMAP account and sends an email notification if the
specified quota warning level has been breached.
Usage:
=====
- Modify the IMAP and SMTP parameters in /etc/imapQuotaChecker.conf
- Upload the script and the config file into a non-public location on your webserver
- Create a cron job to invoke the script as often as you wish
History:
=======
1.1. (May 27, 2011, jsilence AT hackerlab DOT de):
- Read configuration and accounts from config file
- Can now check multiple IMAP accounts
- Verbose mode prints quota usage on console
1.0 (June 8, 2006):
- Initial Release
Licence:
=======
This script is free software; you can redistribute it and/or modify it under the terms of
the GNU Lesser General Public License as published by the Free Software Foundation;
either version 2.1 of the License, or (at your option) any later version.
Please visit http://www.gnu.org/copyleft/lesser.html for more information.
"""
import imaplib, smtplib, re, ConfigParser
from email.Utils import formatdate
def main():
config = ConfigParser.RawConfigParser()
config.read('/etc/imapQuotaChecker.conf')
breach_level=config.get('setup','breach_level')
imap_host=config.get('setup','imap_host')
use_ssl=config.getboolean('setup', 'use_ssl')
verbose=config.getboolean('setup','verbose')
send_address=config.get('setup','send_address')
smtp_host=config.get('setup','smtp_host')
smtp_authentication=config.get('setup','smtp_authentication')
smtp_user=config.get('setup','smtp_user')
smtp_pass=config.get('setup','smtp_pass')
userlist=config.options('accounts')
for imap_user in userlist:
# Error "command LOGIN illegal in state LOGOUT" if I create the imap
# object outside the for loop. Strange, but this works.
if use_ssl:
imap = imaplib.IMAP4_SSL(imap_host)
else:
imap = imaplib.IMAP4(imap_host)
imap_pswd=config.get('accounts', imap_user)
imap.login(imap_user, imap_pswd)
quotaStr = imap.getquotaroot("INBOX") [1][1][0]
p = re.compile('\d+')
r = p.findall(quotaStr)
fQuota = (float(r[0])/float(r[1])) * 100
if verbose:
print("%s %4.2f%%" % (imap_user, fQuota))
if fQuota > breach_level:
emailNotification(fQuota)
imap.logout()
def emailNotification(quota):
""" Email the notification """
# Form the message
headers = "From: %s\r\n" % send_address
headers += "To: %s\r\n" % send_address
headers += "Date: %s\r\n" % formatdate()
headers += "Subject: IMAP Quota Check WARNING [%4.2f%%]\r\n" % quota
body = "Account %s" + " on %s is above the warning limit, the quota is currently at %4.2f%%" % (imap_user, imap_host,quota)
fullMessage = headers + "\r\n" + body + "\r\n.\r\n"
# Send it
smtpsession = smtplib.SMTP(smtp_host)
try:
if smtp_authentication:
smtpsession.login(smtp_user,smtp_pass)
smtpresult = smtpsession.sendmail("%s" % send_address, "%s" % send_address, fullMessage)
finally:
try: smtpsession.quit()
except: pass
if __name__ == '__main__': main()Call the script from crontab.
Have fun!
- BLÄTTERN / CHRONOLOGISCH
- « Lazy People’s Guide to … RCS
- » mod4.cc als URL Shortener in Tweetbot
KOMMENTARE / WERDEN MODERIERT





