Saturday, September 29, 2012

Disk Usage Summary per User and Time

If you are a system administrator, you often face disk utilisation dilemma. On one hand, you need to clean up those old and unwanted files. On the other hand, you cannot do so because they are owned by other users and you need their permission.

Below script summarise users' disk utilisation over certain duration. Hopefully this can help user to determine when they need to housekeep.

# cat b.py
#! /usr/bin/python

import fileinput, time, re


fmt="%-15s %8s %8s %8s %8s %8s %8s %8s %8s"
def print_line():
    print fmt % ('-'*15,'-'*8,'-'*8,'-'*8,'-'*8,'-'*8,'-'*8,'-'*8, '-'*8)
def print_header():
    print fmt % ('User','0-1m','1m-3m','3m-6m','6m-1y','1y-2y','2y-3y','3y-  ', 'Total')
def print_footer():
    print '\nNote: Size in GB'


# match directory, link, file
p=re.compile("^[ ]*[1-9][0-9]* [dl-]")


now=int(time.time())


y0m0=0
y0m1=1*30*86400
y0m3=3*30*86400
y0m6=6*30*86400
y1m0=1*365*86400
y2m0=2*365*86400
y3m0=3*365*86400
yxm0=100*365*86400
tranges=(
    [y0m0, y0m1],
    [y0m1, y0m3],
    [y0m3, y0m6],
    [y0m6, y1m0],
    [y1m0, y2m0],
    [y2m0, y3m0],
    [y3m0, yxm0]
)


users=set()
summary=dict()
total_time=dict()
total_user=dict()


for line in fileinput.input():

    if p.match(line):

        (block, perm, link, user, group, size, epoch, others)=line.split(None,7)
        iblock=int(block)
        dt=now-int(epoch)
        users.add(user)

        # summary per user+duration
        cnt=0
        for (t1,t2) in tranges:
            if t1<=dt and dt<t2:
                key=(user,cnt)
                if key in summary:
                    summary[key]+=iblock
                else:
                    summary[key]=iblock

                # total per duration
                if cnt in total_time:
                    total_time[cnt]+=iblock
                else:
                    total_time[cnt]=iblock

            cnt+=1

        # total per user
        if user in total_user:
            total_user[user]+=iblock
        else:
            total_user[user]=iblock


allusers=list(users)
allusers.sort()
factor=1024.0*1024.0


print_header()
print_line()


for user in allusers:
    print "%-15s" % user,
    for cnt in range(len(tranges)):
        key=(user,cnt)
        if key in summary:
            gb=summary[key]/factor
        else:
            gb=0.0
        print "%8.2lf" % gb,

    # user total
    gb=total_user[user]/factor
    print "%8.2lf" % gb


print_line()


print "%15s" % "Total:",
total=0.0
for cnt in range(len(tranges)):
    if cnt in total_time:
        gb=total_time[cnt]/factor
    else:
        gb=0.0
    print "%8.2lf" % gb,
    total+=gb
print "%8.2lf" % total


print_footer()


Here is a sample output from my 16GB SSD netbook

# ls -lRs --time-style=+%s /var | ./b.py
User                0-1m    1m-3m    3m-6m    6m-1y    1y-2y    2y-3y    3y-      Total
--------------- -------- -------- -------- -------- -------- -------- -------- --------
avahi-autoipd       0.00     0.00     0.00     0.00     0.00     0.00     0.00     0.00
chihung             0.00     0.00     0.00     0.41     0.00     0.00     0.00     0.41
colord              0.00     0.00     0.00     0.00     0.00     0.00     0.00     0.00
daemon              0.00     0.00     0.00     0.00     0.00     0.00     0.00     0.00
libuuid             0.00     0.00     0.00     0.00     0.00     0.00     0.00     0.00
lightdm             0.00     0.00     0.00     0.00     0.00     0.00     0.00     0.00
lp                  0.00     0.00     0.00     0.00     0.00     0.00     0.00     0.00
man                 0.00     0.00     0.00     0.00     0.00     0.00     0.00     0.00
mysql               0.03     0.00     0.00     0.00     0.00     0.00     0.00     0.03
root                0.21     0.07     0.04     0.02     0.00     0.00     0.00     0.35
speech-dispatcher     0.00     0.00     0.00     0.00     0.00     0.00     0.00     0.00
syslog              0.00     0.00     0.00     0.00     0.00     0.00     0.00     0.00
www-data            0.00     0.00     0.00     0.00     0.00     0.00     0.00     0.00
--------------- -------- -------- -------- -------- -------- -------- -------- --------
         Total:     0.24     0.07     0.04     0.43     0.00     0.00     0.00     0.79

Note: Size in GB

0 Comments:

Post a Comment

<< Home