http://qs321.pair.com?node_id=94118
Category: NT Admin
Author/Contact Info

djw
Description: ** UPDATE - Code format is aweful below, but looks normal when you hit the d/l code link. **


When setting up an NT network you usually enter a Home Directory path for all users somewhere on the network. Usually with "\\server\share\%USERNAME%" for the path. In NT this automatically creates a directory under "\\server\share" for each user - and the directory name is the network username of the user (in my case for example, 'djw').

So if I had this setup on a server called "perl" and a sharename of "users" I might have something that looks like this:
    \\perl\users\
      - djw\
      - redmist\
      - danger\
      - Macphisto\
      - ar0n\
      - CowboyNeal\
      - boo_radley\
What this program does is go through each users's directory and reports on the usage. Right now it gives totals for users with more than 1GB, users with less than 1GB, and users with no usage. It also prints out the total share usage, along with individual statistics.

I use this as a simple reporting tool - not a spy tool. =)

Enjoy,
djw

#/usr/bin/perl -w
use File::Find;
use strict;

# Written by David Wakeman.  
# dwakeman@umagroup.com
#
# This program is free
# software; you can redistribute it and/or modify it
# under the same terms as Perl itself.  Use under
# GNU General Public License or Larry Wall's "Artistic
# License".


# Change this option to suit your needs.  Make sure
# you escape all backslashes with a backslash. eg:
# f:\\folder\\another folder\\here

my $dir = "m:\\";

# Change this to use whatever filename you desire.

my $file = "usage-report.txt";



# Don't change anything below here unless you know
# what you are doing.

my @dirlist = <$dir*>;
my $sum = 0;
my $mbSum = 0;
my $gbSum = 0;
my $totalLargeSum = 0;
my $totalSmallSum = 0;
my $totalSum;
my %smallUsage = ();
my %largeUsage = ();
my %noUsage = ();


#-------------------------------------------
# the meat of the program - calls all
# our subs.

open(FILE, "+>$file") || die "Can't open file: ($!)\n";

findUsage();
getTotals();

close(FILE);


#-------------------------------------------
# and below are all our subroutines

sub findUsage
{
    foreach my $directory (@dirlist)
    {
        print "Checking $directory...";
        
        find sub { $sum += -s }, $directory;            # Find goes th
+rough all sub dirs
        $totalSum += $sum;                    # Used for later
        $directory =~ /m:\\(\w+)/;                # take out the m:/ i
+n the dir name
        my $key = $1;                        #
        if ($sum > 1000000000)                    # Check if > 1 GB
        {    
            $gbSum = $sum / 1073741824;            # Convert $sum into
+ GB from b
            $gbSum =~ /(\d+)\.(\d\d\d)/;
            $largeUsage{$key} = "$1" . "." . "$2";        
        } elsif ($sum > 1) {                    # Check if > 1 b
            $mbSum = $sum / 1048576;            # Convert $sum into MB
+ from b
            $mbSum =~ /(\d+)\.(\d\d)/;                
            $smallUsage{$key} = "$1" . "." . "$2";
        } else {                        # user share is blank
            $noUsage{$key} = "0";
        }

        print "completed\n";            
        $sum = 0;                        # Clear sum for next dir
    }
    $totalSum = $totalSum / 1073741824;                # Convert to GB
    $totalSum =~ /(\d+)\.(\d\d\d)/;
    $totalSum = "$1" . "." . "$2";
}


sub getTotals
{
    print FILE "===========================\n\n";
    print FILE "Total Usage:\t $totalSum GB\n";

    foreach my $user (sort keys %largeUsage)            # get our tota
+l %largeUsage
    {
        $totalLargeSum += $largeUsage{$user};
    }

    print FILE "Large usage:\t $totalLargeSum GB\n";

    foreach my $user (sort keys %smallUsage)            # get our tota
+l %smallUsage
    {
        $totalSmallSum += $smallUsage{$user};
    }

    $totalSmallSum = $totalSmallSum / 1024;                # Switching
+ our total into
    $totalSmallSum =~ /(\d+)\.(\d\d\d)/;                # GB from MB.
    $totalSmallSum = "$1" . "." . "$2";
    
    print FILE "Small usage:\t $totalSmallSum GB\n\n";
    print FILE "===========================\n\n";
    
    largeUsage();
    smallUsage();
    noUsage();

}

sub largeUsage
{
    print FILE "Users with > 1GB of usage:\n";
    print FILE "\#--------------------------------\n\n";
    foreach my $user (sort keys %largeUsage)            # print out ou
+r %largeUsage
    {                                # hash.
        print FILE "USER  :\t $user\n";
        print FILE "USAGE :\t $largeUsage{$user}" . " GB" . "\n\n";
    }
}


sub smallUsage
{
    print FILE "\nUsers with < 1GB of usage:\n";
    print FILE "\#--------------------------------\n\n";
    foreach my $user (sort keys %smallUsage)            # Print out ou
+r %smallUsage
    {                                # hash.
        print FILE "USER  :\t $user\n";
        print FILE "USAGE :\t $smallUsage{$user}" . " MB" . "\n\n";
    }
}

sub noUsage
{
    print FILE "\nUsers with no usage on $dir\n";
    print FILE "\#--------------------------------\n\n";
    foreach my $user (sort keys %noUsage)                # print out o
+ur %noUsage
    {                                # hash.
        print FILE "USER :\t $user\n";
    }
}