#!/usr/local/bin/perl # This script was written for www.philforhumanity.com. # If you have any questions, issues, or enhancement requests, please contact us via the website. # Use this script at your own risk. # Configuration Variables my $AWK = "/bin/awk"; my $CT = "/usr/atria/bin/cleartool"; my $DF = "/bin/df"; my $DU = "/bin/du"; my $ECHO = "/bin/echo"; my $GREP = "/bin/grep"; my $LS = "/bin/ls"; my $MAILX = "/bin/mailx"; my $MAX_DAYS_CO = 30; # Maximum number of days a file can be checked out for my $MAX_DAYS_IDLE = 30; # MAximum number of days a View can be idle for my $MAX_DAYS_OLD = 60; # Maximum number of days a View can be old for my $MAX_VIEW_SIZE = 200000; # Maximum View size allowed in kilobytes my $PARTITION_ERROR_LIMIT = 90; # View storage partition full error limit my $PARTITION_WARNING_LIMIT = 80; # View storage partition full warning limit my $REGISTRY_CHECK = "/usr/atria/etc/rgy_check"; my $TAIL = "/bin/tail"; my @VIEW_STORAGE_PARTITIONS = ("/view1", "/view2", "/view3", "/view4"); # All View storage partitions # Numerically sorting function sub numerically { $b <=> $a } # Verify arguments my $test = 0; # The test argument is for testing the script without sending error messages to users if (scalar(@ARGV) > 1) { die "ERROR: Too many arguments.\nFormat: check_Views.pl [test]\n\n"; } else { if (scalar(@ARGV) == 1) { if (@ARGV[0] eq "test") { $test = 1; } else { die "ERROR: Incorrect argument.\nFormat: check_Views.pl [test]\n\n"; } } } # Local variables my $directory; my @email; my $found; my $line; my @output; my $output; my $owner; my $size; my @temp; my @temp2; my $temp; my $view_name; my $view_path; # For each View, check the View foreach $line (`$CT lsview`) { # Split each line for View info @line = split (/\s+/, $line); # Parse the line variables $view_name = $line[1]; $view_path = $line[2]; # If test, then pause between testing of Views and print View name if ($test == 1) { sleep 30; print "\nSTARTING: $view_name\n"; } # Check View storage path is in a valid View storage partition $found = 0; foreach $directory (@VIEW_STORAGE_PARTITIONS) { if ($view_path =~ m!^$directory!) { $found = 1; } } # If not a valid View storage directory, then print error message if ($found == 0) { print "ERROR: View $view_name has incorrect View path of $view_path.\n"; } # Check if View storage path exists if (! -e $view_path) { print "ERROR: View $view_name has non-existent path $view_path.\n"; next; # Don't bother continuing to test this View if the View path does not exist. } # Check if path is a directory if (! -d $view_path) { # Print results print "ERROR: View $view_name has a non-directory as the View path of $view_path.\n"; next; } # Change to the View path directory if (! chdir ($view_path)) { # Print results print "ERROR: View $view_name has a View path $view_path that can not be accessed.\n"; next; } # Get the View's owner id $output = `$LS -l . | $TAIL -1`; @output = split (/\s+/, $output); $owner = $output[2]; # Get listing in password file. @output = getpwnam($owner); # Check if account exists if (@output == ()) { # Print results print "WARNING: Unable to find account $owner for View $view_name.\n The View should probably be deleted.\n"; } # Check if account is active if (($output[1] eq "*") || ($output[8] eq "/bin/date") || ($output[8] eq "/bin/false")) { # Print results print "WARNING: The account $owner for View $view_name has been de-activated.\n The View should probably be deleted.\n"; } # Get the size of View's storage directory in kilobytes $size = int (`$DU -ks`); # Check the size of View is over the limit if ($size > $MAX_VIEW_SIZE) { $size = $size / 1000; # Convert from KB to MB if ($test == 0) { # Email results @email = ("$owner,\n\nYou have a View, $view_name, that is too large at $size megabytes. Please clean up or delete this View. Make sure you save or checkin any checked out files and View private files that you wish to keep.\n\n\nRegards,\n\nClearCase Administrator\n"); `$ECHO \"@email\" | $MAILX -s "View $view_name is too large at $size megabytes!" $owner`; } else { # Display results print "WARNING: View $view_name is too large at $size megabytes!\n"; } } # Check if element * /main/LATEST is a rule in the config spec, if so then check for checked out files if (`$CT setview -exec \"$CT catcs | $GREP '\^element' | $GREP '\/main\/LATEST'| $GREP '\\\*' \" $view_name `) { # Get list of all checked out files for View @output = `$CT setview -exec \"$CT lsco -cview -avobs -fmt \'\%Ad \%Xn\\n\' \" $view_name`; } else { # Alert user or user running this script if ($test == 0) { # Email results @email = ("$owner,\n\nYou have a View, $view_name, that does not have \"element star /main/LATEST\" in it. Please correct this, if it is a mistake. Otherwise, ignore this email.\n\n\nRegards,\n\nClearCase Administrator\n"); `$ECHO \"@email\" | $MAILX -s "View $view_name possibly has an incomplete config spec" $owner`; next; } else { print "WARNING: View $view_name does not have \"element \* /main/LATEST\" in its config spec.\n"; next; } } # Check if error when the View was started in the previous section if ( (grep (/ERROR/, @output) != ()) || (grep (/Error/, @output) != ()) || (grep (/Warning/, @output) != ()) || (grep (/WARNING/, @output) != ()) ) { # Print results print "ERROR: View $view_name did not start properly.\n", @output; next; } # Check if the checked out files have been checked out for more than $MAX_DAYS_CO days @temp2 = (); foreach $temp (@output) { @temp = split (/\s+/, $temp); if ($temp[0] > $MAX_DAYS_CO) { @temp2 = (@temp2, $temp); } } # Sort array @temp2 = sort numerically @temp2; # If check outs have been found over than $MAX_DAYS_CO days, than email uses. if ((@temp2 != ()) and ($test == 0)) { # Email results @email = ("$owner,\n\nYou have at least one checked out file that has been checked out for over $MAX_DAYS_CO days in View $view_name. Please check in or un-check out these files listed below as soon as possible.\n\n\nRegards,\n\nClearCase Administrator\n\n\nDays\nOld File\n---- ----\n", @temp2); `$ECHO \"@email\" | $MAILX -s "Checked out files that are over $MAX_DAYS_CO days old for View $view_name." $owner`; } # Check if the View's storage directory is over $MAX_DAYS_IDLE days idle if (-M $view_path > $MAX_DAYS_IDLE) { if ($test == 0) { # Email results @email = ("$owner,\n\nYou have a View, $view_name, that has been idle for over $MAX_DAYS_IDLE days. Please delete this View if you are not using it. Make sure you save or checkin any checked out files and View private files that you wish to keep.\n\n\nRegards,\n\nClearCase Administrator\n"); `$ECHO \"@email\" | $MAILX -s "View $view_name is over $MAX_DAYS_IDLE days idle." $owner`; } else { print "WARNING: View $view_name is over $MAX_DAYS_IDLE days idle.\n"; } } # Check if the View created age is over $MAX_DAYS_OLD days if (-M "$view_path/.view" > $MAX_DAYS_OLD) { if ($test == 0) { # Email results @email = ("$owner,\n\nYou have a View, $view_name, was created over $MAX_DAYS_OLD days ago. Please delete or renew this View. Make sure you save or checkin any checked out files and View private files that you wish to keep.\n\n\nRegards,\n\nClearCase Administrator\n"); `$ECHO \"@email\" | $MAILX -s "View $view_name is over $MAX_DAYS_OLD days old." $owner`; } else { print "WARNING: View $view_name is over $MAX_DAYS_OLD days old.\n"; } } # End of foreach View loop } # Check each partition if the disk space full is too high foreach $line (@VIEW_STORAGE_PARTITIONS) { $output = eval `$DF -k $line | $TAIL -1 | $AWK '{print \$1}' `; # Check percentage if ($output >= $PARTITION_ERROR_LIMIT) { # Print results print "ERROR: View Partition $line is over $PARTITION_ERROR_LIMIT% full!\n"; } elsif ($output >= $PARTITION_WARNING_LIMIT) { # Print results print "WARNING: View Partition $line is over $PARTITION_WARNING_LIMIT% full.\n"; } } # If you do not allow local Views in a particular region, such as on Windows, then you can use this section of code. #@output = `$CT lsview -region | $GREP -v | $GREP -v \"\" `; #if (@output != ()) #{ # # Print results # print "WARNING: Local Windows Views found. They should not exist.\n @output\n"; #} # Ask the ClearCase Registry for any View issues @output = `$REGISTRY_CHECK -views`; if (@output[0] ne "No registry errors/inconsistencies detected.\n") { print "\n",@temp,"\n"; } # Exit exit 0;