Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

memory leak

Status
Not open for further replies.

timgerr

IS-IT--Management
Joined
Jan 22, 2004
Messages
364
Location
US
Hey all, I have written a script that will directory walk through my Win32 file server and make sure a certain group is contained on every directory. I am traversing 5 Terabytes of data so there are many folders. The problem that I am having is that the longer I run my script the more ram it takes up. I am not doing any collections so I don’t understand why it is taking up more and more ram. Here is the script, any help would be welcomed.

Code:
# This scrip will travers a given depth of folders and see if an admin (or user)
# is not in that group.  When you call the script, you have to give 3 arguments
# from the command line
# 	1. firs you must give a number of levels that you want to dig down into 
# 	2. You must give a starting point to that you want to search\traverse
# 	3. you give a path and file name of an output file that will be written
#		to when a fooder does not have the group and correct rights

# This will walk a directory when given 2 arguments, the depth and the directory
# For some reason when I run this script I get 2 lines or errors all the time so I 
# call a clear screen to clear the errors.  It has somting to do when I call the 
# Win32::Perms and File::DirWalk


use File::DirWalk;
use Win32::Perms;

# Clear the screen
system("cls");

#  Setting up the sub rutiene
sub getperms;
sub ERROR;


# Getting command line arguments
my($arg1,$arg2,$arg3) = @ARGV;
chomp($arg1,$arg2,$arg3);
# changing the color to regular in the command prompt
system("color 07");



# Making sure that all the arguments are filled
if(length($arg1) < 1 or length($arg2) <1 or length($arg3) <1)
{
	system("color A");
	print "You need to give 3 arguments\n\nThe 1st is the folder depth that you want to search.\n\n";
	print "The 2nd is the path you want to traverse\n\n";
	print "The 3rd is the path and file name for the log file such as c:\\somting_err.txt\n\n";
	print "EXAMPLE somthing.pl 3 t:\\A_Directory \"c:\\program files\\temp\\error log.txt\"\n\n";
	print "REMEMBER to add quotes \" \" to any path if there are any spaces in that path\n\n";
	exit;
} 

# Changing the output color
system("color 4e");
print "Folder_Look_For_Admin_Recrus.exe v1.0\n";


my $x = 0; 
# Setting up and executing the directory walk
my $dw = new File::DirWalk;
my @all;
$dw->onDirEnter(sub {
	my ($dir) = @_;
 	getperms $dir;
# 	if($x == 1000){
#		print $dir . "\n";
# 		$x = 0;
# 	}
# 	$x++;
    return File::DirWalk::SUCCESS;
});
# Running directory walk
$dw->setDepth($arg1);
$dw->walk("$arg2");



sub ERROR
{
	my($error) = @_;
	chomp($error);
	open(WRITER, ">>". $arg3);
	print WRITER "lala " . $error . "\n";
	close(WRITER);
}


# This is the perms sub
sub getperms {
	# We need to have a collection varieble to add all the output into.  We will use this variable to parse information
	$strMaster = "";

  my ($obj) = @_;
  $Perm = new Win32::Perms($obj);
#   print "Permissions for:  $obj ,\n";
  $strMaster = $obj;

	$read 	= 'READ_CONTROL SYNCHRONIZE FILE_READ_EA FILE_READ_ATTRIBUTES';
	$write 	= 'SYNCHRONIZE FILE_WRITE_EA FILE_WRITE_ATTRIBUTES';
	$RW		= 'READ_CONTROL SYNCHRONIZE FILE_READ_EA FILE_WRITE_EA FILE_READ_ATTRIBUTES FILE_WRITE_ATTRIBUTES';
	$RE		= 'READ_CONTROL SYNCHRONIZE FILE_READ_EA FILE_EXECUTE FILE_READ_ATTRIBUTES';
	$mod	= 'DELETE READ_CONTROL SYNCHRONIZE FILE_READ_EA FILE_WRITE_EA FILE_EXECUTE FILE_READ_ATTRIBUTES FILE_WRITE_ATTRIBUTES';
	$full 	= 'STANDARD_RIGHTS_ALL FILE_READ_EA FILE_WRITE_EA FILE_EXECUTE FILE_DELETE_CHILD FILE_READ_ATTRIBUTES FILE_WRITE_ATTRIBUTES';


  $Perm->Dump(\@List);

  foreach $acct (@List) {
    my(@Permissions);
    next if ($acct->{Entry} ne "DACL");

    Win32::Perms::DecodeMask($acct->{Mask},\@Permissions);

 
    my $final = join(" ",@Permissions);

    
    if($final =~ m/$full/)
    {
#     	print ",$acct->{Account} , Full";
    	$strMaster = $strMaster . ",$acct->{Account} <> Full";
    	
    }
    elsif ($final =~ m/$read/)
    {
#     	print ",$acct->{Account} , Read";
    	$strMaster = $strMaster . ",$acct->{Account} <> Read";
    }
    elsif ($final =~ m/$write/)
    {
#     	print ",$acct->{Account} , Write";
    	$strMaster = $strMaster . ",$acct->{Account} <> Write";
    }
    elsif ($final =~ m/$RW/)
    {
#     	print ",$acct->{Account} , Read Write";
    	$strMaster = $strMaster . ",$acct->{Account} <> Read Write";
    }
    elsif ($final =~ m/$RE/)
    {
#     	print ",$acct->{Account} , Read Execute";
    	$strMaster = $strMaster . ",$acct->{Account} <> Read Execute"
    }
    elsif ($final =~ m/$mod/)
    {
#     	print ",$acct->{Account} , Modify";
    	$strMaster = $strMaster . ",$acct->{Account} <> Modify";
    }
    else 
    {
#     	print ",$acct->{Account} , SPECIAL <--> $final";
    	$strMaster = $strMaster . ",$acct->{Account} <> SPECIAL <--> $final";
    }

  }
  
	# Change this search to serch for the group that you are looking for
	if($strMaster !~ m/Administrators <> Full/){   # <--------------------- Change this group, this is the group that you want to search for
		# Print error line to log file
		ERROR $strMaster;
		print $strMaster . "\n";
		
		
	}
   	
 
} 
print ("\n\nThe End\n\n");
system("color e");
thanks,
-T

-How important does a person have to be before they are considered assassinated instead of just murdered?
-Need more cow bell!!!

 
I just noticed that I cant spell worth a darn :).

-T

-How important does a person have to be before they are considered assassinated instead of just murdered?
-Need more cow bell!!!

 
I guess it might have to do with the dir walk, would that be holding the collection?


-How important does a person have to be before they are considered assassinated instead of just murdered?
-Need more cow bell!!!

 
I'd agree that the dir walk is likely to be memory hog.

I was also looking at the variable $strMaster which is used in the getperms() sub. I notice that you're not using strict or warnings, and so you don't have to declare variables like $strMaster.

As a starting point can I suggest you start your script with:

use strict;
use warnings;

then declare variables with "my" so that you're completely sure about their scope and lifetime.

And yes, things can always do with a bit more cow bell :-)


Mike

When working on any project the value of other people is exactly that - they are other people, with views that don't necessarily match yours. This mismatch, between their views and the view you've been contentedly assuming is right, is where that value lies.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top