#!/usr/bin/perl use strict; use warnings; use File::Find; use threads; my $size_threshold = 5 * 1024 * 1024; #point at which to abort directory travers ing. my @dirs = ( "/nas/fs001", "/nas/fs002", "/nas/fs003", "/nas/fs003", "/nas/fs004", "/nas/fs005", "/nas/fs006", "/nas/fs007", "/nas/fs008", "/nas/fs009", "/nas/fs010", "/nas/fs011", "/nas/fs012", "/nas/fs013", "/nas/fs014" ); @dirs = ( "/usr/local/apache" ); @dirs = ( "/nas/fs001", "/nas/fs002" ); #"/usr/local/apache"; my $customer_file = "/usr/local/apache/htdocs/dusage/disk_usage.conf"; my $max_depth = 7; my $debug = 1; sub dusage { my $dir = pop; # 1 arg only, because that lets me thread. my $tsize; my %rtree; my $datafile = $dir; $datafile =~ s,/,,g; print "Opening $datafile for output"; open ( OUTPUT, ">$datafile.csv" ) or die $!; find ( sub { if ( -f && ! -l ) { my $filesize = -s $_; $tsize += $filesize; #chop up the path, populate rtree at each of traverse_depth levels my @directory_structure = split ( '/', $File::Find::name ); pop(@directory_structure); # we'll never want the trailing filename for ( my $depth = 0; $depth <= $max_depth; $depth++) { if ( $#directory_structure < $depth ) { next }; my $thispath = join ( '/', @directory_structure[0..$depth]); $rtree{$thispath} += $filesize; } } } , $dir ); foreach my $key ( keys ( %rtree ) ) { my $indent = ( $key =~ tr,/,, ); print OUTPUT $indent, ",", $key, ",", $rtree{$key},"\n"; } close (OUTPUT); } #main foreach my $directory ( @dirs ) { dusage ( $directory ); }