#/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";
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|