#!/usr/bin/perl/ - w # mon_users - monitor usr accounts # This script was created to monitor disk usage in peoples # home directories. It should work anywhere you will need to # edit all the paths. Prints out a nice little graph deal = ) $MAX_SPACE_ALLOWED = 15000000; %user_hash = {}; &get_user_info; &write_out_info ($MAX); exit(); sub get_user_info { $count = 0; open (USERS, "ls -al /houfiler_home/|") || die "Can't open ls -al +$!\n"; while (<USERS>) { #next if $count > 100; chop; $j, $j, $j, $j, $j, $month, $date, $time, $uname) = split; # If it starts with a '.', has capitals or is "" its not a username next if ($uname =~ /tim/ || $uname =~ /\./ || $uname eq "" || (lc ($un +ame) ne $uname)) ; # Make Home Directory out of username $home_dir = "/houfiler_home/$uname"; $du = `du -s -k $home_dir`; chop $du; ($disk,$junk) = split(/\t/,$du); # Store everything in a Hash Table $user_hash{"$uname"} = $disk; $count++; # print "$count\n"; } } sub sortHashByNum { $user_hash {$b} <=> $user_hash{$a}; } sub write_out_info { #header at top of each page of the report format OUTPUT_TOP = Disk Username Home Directory Space Usage (100% = 15G +) ---------------------------------------------------------------------- +--- . $~ = "OUTPUT_TOP"; write; # format for each line written to handle OUTPUT format STDOUT = @<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<<<<<<<<<<<@<<<<<<<<<@<<<<<<<<<<<<<<<< +<<<<<< $uname, $home_dir, $disk, $level . $~ = "STDOUT"; foreach $uname ( sort sortHashByNum keys %{user_hash}) { $uname =~ s/\d//g; # Make Home Directory out of username $home_dir = "/houfiler_home/$uname"; $disk = $user_hash{$uname}; $level = '*' x int (($disk*20)/$MAX_SPACE_ALLOWED+0.5); $level .= '-' x (20 - length ($level)); $level = "|".$level."|"; $disk .= "K"; # Write out formatted line to standard output write; } }

2001-03-03 Edit by Corion : Added <CODE> tags

Replies are listed 'Best First'.
Re: DiskUsage.pl
by AgentM (Curate) on Feb 14, 2001 at 00:13 UTC
    And this differs from the more dependable and useful kernel-based quota and du mechanisms how? Btw, this obviously only checks for stuff in the user's home directory hierarchy when he could have things all around the file system, not to mention that you don't even check if the files you actually counted belong to the user.

    Is this something to create a report? You could have just backticked "quota" and read the relevant stats in from there- plus users would be prohibited from using more than they would be allocated.

    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
      I am sick of everyone voting against this script it was written the way it had to be to work at the specific location so LAy OFF!! js
(sacked) Re: DiskUsage.pl
by sacked (Hermit) on Feb 13, 2001 at 23:11 UTC
    Please see Writeup Formatting Tips (especially the part about using <CODE> tags around your code).

    Did you paste this code exactly as it exists on your machine? There are a couple of syntax errors:
    while () {
    should be:
    while (<USERS>) {
    Also, a shortcut for using temp variables (or undefs), instead of
    $j, $j, $j, $j, $j, $month, $date, $time, $uname) = split;
    (which needs a leading parenthesis, btw), use an array slice to extract only the values you need:
    ($month, $date, $time, $uname) = (split)[5..8];
    --sacked
Re: DiskUsage.pl
by jeroenes (Priest) on Feb 14, 2001 at 19:12 UTC
    You might have some use for this one as well. Actually, I use the 'tdu.pl' quite often.

    Cheers,

    Jeroen
    "We are not alone"(FZ)