Automating disk usage report to users

Discussion in 'Programming/Scripts' started by curtorkar, Apr 5, 2006.

  1. curtorkar

    curtorkar New Member

    Hi,
    I have our server setup as /export/home for users directories. I need help in sorting out the results of du.
    Currently I use du -sk | sort -nr to get the sorted disk usage.
    I want to calculate which user directory exceeds 2G.
    If the directory tom exceeds 2G then mail tom@mydomain.com the size of his home directory.

    Thanks,
    --Walter
     
  2. 22hosting

    22hosting New Member

    du -k | awk '{ if($1 > 2048000000) print $2 }'

    Will print out all the directories that are over 2GB
    ________
    Web Shows
     
    Last edited: Aug 22, 2011
  3. danf.1979

    danf.1979 ISPConfig Developer

    curtorkar, put this script in /usr/bin/ with permissions to be executed. You can do a cronjob with it:
    Configure it for your needs
    Code:
    #!/usr/bin/env python
    import os, string, smtplib
    
    #Configure here
    path = "/home/export/"
    fromaddr =  "from_user@yourdomain.com"
    toaddrs = "youremail@youraccount.com"
    limit = 2*1024*1024
    #No more config
    
    dirs = os.listdir(path)
    
    data_list = []
    
    for dir in dirs:
    	command = "du -s %s%s" % (path, dir)
    	fileobject = os.popen(command)
    	dataline = fileobject.read()
    	fileobject.close()
    	data = dataline[:-1].split("\t")
    	data_list.append(data)
    
    email_list = []
    
    for record in data_list:
    	if record[0] != "":
    		if int(record[0]) > limit:
    			email_list.append(record)
    
    if email_list:
    	msg = str(email_list)
    	server = smtplib.SMTP("localhost")
    	server.set_debuglevel(0)
    	server.sendmail(fromaddr, toaddrs, msg)
    	server.quit()
    
     
  4. danf.1979

    danf.1979 ISPConfig Developer

    You can execute the script with:
    /usr/bin/python scriptname
     

Share This Page