PDA

View Full Version : Automating disk usage report to users


curtorkar
5th April 2006, 03:01
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

22hosting
5th April 2006, 13:57
du -k | awk '{ if($1 > 2048000000) print $2 }'

Will print out all the directories that are over 2GB

danf.1979
5th April 2006, 21:18
curtorkar, put this script in /usr/bin/ with permissions to be executed. You can do a cronjob with it:
Configure it for your needs

#!/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()

danf.1979
5th April 2006, 21:19
You can execute the script with:
/usr/bin/python scriptname