# NagBOT.pl - A little perl program to snoop the locations of user homedirs from an NT PDC, # query the location with diruse and E-mail the users that are consuming too much space. #!/perl/bin/perl -w use Win32; use Win32::AdminMisc; use Win32::NetResource; use Win32::MAPI qw(Yes No); use diagnostics; open (HISTORY, ">>Mail.txt"); if (defined $ARGV[0]){ # make sure the user formatted the command switches correctly my $domain = $ARGV[0]; # ARGV[0] is the first value after the name of the script when you ran it. my $trigger = $ARGV[1]; # ARGV[1] is of course the second my $pdc = Win32::AdminMisc::GetPDC("$domain"); #get the name of th PDC for the mentioned domain my $homedir_path = ""; #declare and set homedipath to null my $user_name = "";# declare user_name my @dom_users=(); #declare the dom_users array print STDOUT "The PDC for the $domain domain is $pdc\n"; Win32::AdminMisc::GetUsers("$pdc","", \@dom_users);# query the PDC for the list of all users and crap it into the @dom_users array print STDOUT "Received the user list from $pdc\n"; foreach (@dom_users){ #here is the loop to get the homedir path or each user and check the size of the homedir. Win32::AdminMisc::UserGetMiscAttributes($pdc, $_ , \%user_info); $homedir_path = $user_info{"USER_HOME_DIR"}; $homedir_path =~ tr/a-z/A-Z/; $user_name = $_; if ((defined $homedir_path) and ($homedir_path =~ /^\\\\GCC/)) { find_homedir_size($homedir_path,$user_name,$trigger); } else { } } } else { # print the instructions print_instructions(); } close HISTORY; # bring on the subroutines................................................................................. sub find_homedir_size { my ($homedir_path,$user_name,$trigger) = @_; # carry over values from where the sub was called. chomp (my @output = `diruse -m $homedir_path`); # run diruse against the users homedir share to get the space used foreach (@output) { if ($_ =~ /^\s+([0-9\\.]+)\s+([0-9\\.]+)\s+TOTAL:/) { #use UNIX regular expressions to check each line of output #from the diruse command and grab only the one value #on the line that I want. print HISTORY "$user_name\t"; print STDOUT "$user_name\t"; print HISTORY "$1 total Mbytes\t"; print STDOUT "$1 total Mbytes\t"; print HISTORY "$2 total files\n"; print STDOUT "$2 total files\n"; if (defined $trigger) { if ($1 > $trigger) { #check to see if the homedirsize stored in the magic variable $1 # is greater than the trigger. mail_user($user_name,$trigger,$1,$2); # if the trigger is "triggered" then use the mail_user sub } } } } } sub mail_user { #the subroutine to actually send mail to the user. my($obj)=new Win32::MAPI(UseDefProfile => Yes); $obj->Logon() || die "Can't logon!"; my ($user_name,$trigger,$mbytes,$files) = @_; # carry over values from where the sub was called. my %data=(); $data{To} = [cspjk0]; $data{Subject} = "You are using over $trigger megabytes in your home directory."; $data{Text} = "This is an automated message. For informational purposes only.\n\n"; $data{Text} .= "Your Home Directory (H:\\) is currently consuming $mbytes Megabytes.\n\n"; $data{ShowDialog} = No; $obj->Send(\%data); $obj->LastError; print "$obj->LastError\n"; undef $obj; } sub print_instructions { # print the instructions if the user doesn't use any parameters. print "\n\nNAGbot.pl... A little PERL script to scan all the home directories in a domain\n"; print "and mail users who are using too much space.\nUSAGE:\n"; print "PERL NAGBOT.pl DOMAIN TRIGGER\n"; print " | |\n"; print "The domain to query The trigger in megabytes\n\n"; print "ex. C:\\PERL NAGBOT.PL DOMAIN 100 [Mails all users using over 100Mb in their h:\\]\n"; print "C:\\PERL NAGBOT.PL DOMAIN [Prints a report of all users homedirs in DOMAIN]\n\n"; print "Yes.. you DO have to mention the PERL interpreter before the name of the script\n"; print "ie. \"C:\\DIR\\PERL nagbot.pl.... yadda yadda yadda\"\n\n"; }