Enquiries: / +44 (0)20 7193 3323

Cpanel mailbox size/quota daily check

We are used to setting email accounts with the default mail quota for most of the mailboxes. However, some clients like to store a lot of emails in their accounts and we generally want to be on top of these accounts and check on how their mail account sizes and how close they are to reaching its quota. For that I created a python script that look for both size and mailbox quota and email if accounts are over certain percentage of their quotas.
import os                                                                             
import os.path                                                                        
import sys                                                                            
import smtplib                                                                        
# Import the email modules we'll need                                                 
from email.MIMEText import MIMEText                                                   
mailboxes = []
default_quota = 250*1024*1024
def checkSize(threshold, dir, level = 0):
  global mailboxes                       
  global default_quota                   
  file = os.popen('du -bs *')            
  data =                     
  for line in data.strip("\n").split("\n"):
    size, filename = tuple(line.split("\t"))
    if float(size) > float(default_quota)*(float(threshold)/100):
      fullfilename = dir + '/' + filename                        
      if level >= 1 or not os.path.isdir(fullfilename):          
        if filename != 'inbox':                                  
          quotafile = '/' + dir.split('/')[1] + '/' + dir.split('/')[2] + '/etc/' + dir.split('/')[4] + '/quota'                                                            
          quota = open(quotafile)                                                     
          for q in"\n").split("\n"):                              
            if q.split(':')[0] == filename:                                           
              quotasize = q.split(':')[1]                                             
          quotasize = 0                                                               
        if int(size) >= float(quotasize)*(float(threshold)/100):
          ratio = 0                                             
          if quotasize > 0:                                     
            ratio = float(size)/float(quotasize)                
      if level < 1 and os.path.isdir(fullfilename):             
        checkSize(threshold, fullfilename, level+1)             
if __name__ == "__main__":
  for dir in os.listdir('/home'):
    if 'mail' in os.listdir('/home/'+dir):
      checkSize(int(sys.argv[1]), '/home/'+dir+'/mail')
  log = ""
  mailboxes = sorted(mailboxes,key=lambda a: -float(a[2]))
  for size, quota, ratio, fullfilename in mailboxes:
    logline = "% 10.2f MB / % 10.2f MB (%6.2f%%)  %s\n" % (round(float(size)/(1024*1024),2), round(float(quota)/(1024*1024),2), ratio*100, fullfilename)
    log = log + logline
  print log
  if log != '' and len(sys.argv) == 3:
    # Send the message via our own SMTP server, but don't include the
    # envelope header.
    host = "localhost"
    s = smtplib.SMTP(host)
    msg = MIMEText(log)
    msg['Subject'] = "Mailbox disk usage over %d%% of their quota" % int(sys.argv[1])
    msg['From'] = ''
    for mail in sys.argv[2].split(','):
        msg['To'] = mail
        s.sendmail('', mail, msg.as_string())
Posted by ariel on December 21st, 2009
Comments: 0

Post new comment

The content of this field is kept private and will not be shown publicly.