neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

I'm developing a script to monitor and take action against users who use to much disk space. At this point I'm simply collecting the information. Here is the code:

#!/usr/bin/perl use strict; use warnings; # This script will the top ten space users # of any directory # Neil Watson Mon Mar 1 13:05:11 EST 2004 use Getopt::Std; use File::Find; use Data::Dumper; my (%opt, $utotal, $uid, $uname, $fname, %size, $size, %files); my ($count); # options available # -d <dir to check> getopts("d:", \%opt); my $dir = $opt{d}; # get file file information find(\&wanted, $dir); # get top ten offenders print "User\t\tTotal (kb)\n"; foreach $uname (sort keys %size){ print $uname."\t\t".$size{$uname}."\n"; } #print Dumper(%files); sub wanted{ # scan only regular files if (-f){ # owner of file but skip # if owner is not a user # (uid < 500) $uid = (lstat($_))[4]; unless ($uid < 500){ $uname = getpwuid $uid; # gather name of file $fname = $File::Find::name; # size of file (kb) $size = (lstat($_))[7]; $size = int($size/1000); # store in a hoh # username {filename} => filesize $files{$uname} {$fname} = $size; # keep running total of each # user's files $size{$uname} += $size; } } }

Now that I look at my hoh data structure I'm not sure that is it ideal. For instance, how would I select the top ten space users? I think there must be a more efficient way to do this so I'm throwing this out to my fellow monks.

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Monitoring disk usage
by Tomte (Priest) on Mar 05, 2004 at 16:12 UTC

    Maybe I missed something, but I'd use quota to enforce quota-limits and Quota to code actions in perl...just my $0.02

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

      I wish I could use quotas. But, it seems that quota is somehow unstable on Redhat 9 and a Dell PE600SC. I have to come up with a work around as I haven't yet been able to determine the instability problem.

      Neil Watson
      watson-wilson.ca

        Why not just use rpm to remove your current install of quota and re-compile it from source, unless it is a kernel bug it should either run stable or die on compile? There is a perl Quota module BTW but that is just an interface onto quota AFAICT.

        You might like this:

        There are four major species of Unix sysad

        1. The TECHNICAL THUG. Usually a systems programmer who has been forced into system administration; writes scripts in a polyglot of the Bourne shell, sed, C, awk, perl, and APL.
        2. The ADMINISTRATIVE FASCIST. Usually a retentive drone (or rarely, a harridan ex-secretary) who has been forced into system administration.
        3. The MANIAC. Usually an aging cracker who discovered that neither the Mossad nor Cuba are willing to pay a living wage for computer espionage. Fell into system administration; occasionally approaches major competitors with indesp schemes.
        4. The IDIOT. Usually a cretin, morpohodite, or old COBOL programmer selected to be the system administrator by a committee of cretins, morphodites, and old COBOL programmers.

        How To Identify Your System Administrator (a field guide)

        SITUATION: Low disk space

        TECHNICAL THUG: Writes a suite of scripts to monitor disk usage, maintain a database of historic disk usage, predict future disk usage via least squares regression analysis, identify users who are more than a standard deviation over the mean, and send mail to the offending parties. Places script in cron. Disk usage does not change, since disk-hogs, by nature, either ignore script-generated mail, or file it away in triplicate.

        ADMINISTRATIVE FASCIST: Puts disk usage policy in motd. Uses disk quotas. Allows no exceptions, thus crippling development work. Locks accounts that go over quota.

        MANIAC:

        # cd /home # rm -rf `du -s * | sort -rn | head -1 | awk '{print $2}'`;

        IDIOT:

        # cd /home # cat `du -s * | sort -rn | head -1 | awk '{ printf "%s/*\n", +$2}'` | compress

        More at http://www.gnu.org/fun/jokes/know.your.sysadmin.html

        cheers

        tachyon

        Are you using reiserfs? Because as of now, linux's quota support does not work for reiserfs. (So bad it's like that, we'll have to wait till it comes out, and till then, use ext2 for disks with quota.)

Re: Monitoring disk usage
by TomDLux (Vicar) on Mar 05, 2004 at 16:29 UTC

    How long are you spending on the script?

    What is your time worth?

    Why not spend $200 on an extra 200 GB?

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      If you give a user 200GB of space he will just fill it again.

      Neil Watson
      watson-wilson.ca

        Remember that users are your complete enemies. Without them sysadmin positions wouldn't exist. Space is meant to be used.