#!/usr/software/bin/perl # $Id: //depot/prod/test/main/storage/hdd/NADQ_SEA/TEST_SCRIPTS/CMODE/log_cleanup.thpl#1 $ # Copyright (c) 2005 Network Appliance, Inc. # All rights reserved # ## @summary Mode ## log_cleanup ## ## @description ## This script will create two files 1) pre_n_post 2) Main log file ## ## ## @status Automated ## @auther vpankaj@netapp.com ## @burt 725139 ## @change YY/MM/DD from auther: Description of the change ## ############################################################################# ######################################### ### Library functions ########################################## # Compiler directives. use strict; # Module imports use TCD; ######################################### ### Initialization/declaration ######################################### my $params = NATE::ParamSet->new(global => 1); my $LOGDIR = $params->get('LOGDIR', default => undef); my $HANDLE = $params->get('HANDLE', default => undef); # Global variables my $file_C; my $log_file; my $TEST_NAME; my %result_struct; my @Step_log; opendir( DIR_LOG31, $LOGDIR ); my @files_DIR_LOG31 = readdir(DIR_LOG31); foreach my $file (@files_DIR_LOG31) { if ( (($file =~ /(.*_NADQ02.*)\.log/)||($file =~ /(NADQ02.*\d+)\.log/)||($file =~/(NVMe_MNS_Setup)\.log/)||($file =~ /(check_filer_setup.*)\.log/)||($file =~ /(SETUP_INSTALL_IMAGE)/)) && ($file !~ /results|END_FILER_CONFIG/) ){ $TEST_NAME = $1; logcomment("DEBUG:TEST_NAME = $TEST_NAME"); $file_C = $file; } } $log_file = "$LOGDIR/$file_C"; my @Testcases = ( subtest_clean => "pushing subtest contents to main log directory", end => "Pre and Post Logs" ); &main(); sub main { # Debug break point $DB::single = 2; # Create Test Case Driver object $Test = new TCD( -testcases => [@Testcases] ); if ( $Test->error ) { $Test->log( $Test->errmsg ); return $TCD::FAIL; } # Performs method callbacks $Test->run_test(); if ( $Test->error ) { $Test->log( $Test->errmsg ); return $TCD::FAIL; } exit(0); } ## end sub main sub end { $Test->description("Executing log_cleanup.thpl"); open( MAIN_LOG, "$log_file") || die "Can not open file $log_file"; my @total_data = ; logcomment("Toatl number of actual lines ".scalar @total_data ); close MAIN_LOG; foreach my $line (@total_data) { my $flag; while(1){ $flag = chomp($line); last if($flag == 0); } if ($HANDLE != 1){ next if ($line=~/\*\*(debug|Debug)\*\*/); } next if(!defined $line); next if($line=~/^\s+$/); next if($line=~/^$/); next if($line!~/(.+)/); next if ($line=~/abs-junction-path/); next if ($line=~/ZAPI/); next if ($line=~/Zapi/); next if ($line=~/deprecated/); next if ($line=~/Prototype\s+mismatch|Maybe you should just omit|Attempt to DESTROY objec|Subroutine wreck redefined|ubroutine treecompare redefined/g); next if ($line=~/Unrecognized|Dense::Clone|Dense\/Clone/g); next if ($line=~/HASH/g); next if ($line=~/uninitialized\s+value/i ); next if ($line=~/uninitialized/i ); next if ($line=~/Useless use of/i); next if ($line=~/NATE\:\:Mail|Trace begun|Email\:\:Sender|Tiny|NATE\:\:Database|Database/); next if ($line=~/say_wrapper|Attempt to DESTROY/i); next if ($line=~/NATE\:\:Error\:\:|Error\.pm/g); #next if ($line=~/----------------------------------------/); #last if ($line=~/This test:$/); if ($line ne ''){ $line =~ s/\[HDD\S+\]//g ; $line =~ s/X;X/ /g ; $line =~ s/\[\.\]//g; $line =~ s/Press to page down, for next line, or 'q' to quit\.\.\.//g; if ($line !~ /Warning: Usage of execute_raw_command is not recommended/){ if($line=~/\*\*(WARNING|FAIL|FATAL|NORUN)\*\*/){ $line=~s/(\.?)\s*Comment\s*:\s*//; }elsif(($line=~/Comment:\s*DEBUG\s*:/i)||($line=~/Comment:\s*\*\*DEBUG\*\*\s*:/i)){ $line=~s/(\.?)\s*Comment:\s*DEBUG\s*:\s*/\*\*DEBUG\*\* : / ; #Burt 862716 nmamathai $line=~s/(\.?)\s*Comment:\s*\*\*DEBUG\*\*\s*:\s*/\*\*DEBUG\*\* : / ; } push( @Step_log, $line."\n" ); #last if($line =~/END OF TEST\s+\/\s+RESULTS SUMMARY/); } } } logcomment("Toatl number of final lines ".scalar @Step_log ); =head my $cntr=0; my $Home = $ENV{HOME}; $/=undef; open( Fh, "$Home/NDATE/SUPPORT_SCRIPTS/ERROR_FILE") || print "Cannot open file $Home/SUPPORT_SCRIPTS/ERROR_FILE"; my $error_file = ; close Fh; $/="\n"; foreach my $line (@Step_log){ $cntr++; if(($line=~/\*\*(WARNING)\*\*/i)||($line=~/\*\*(FAIL)\*\*/i)||($line=~/\*\*(FATAL)\*\*/i)){ my $err_ty = $1; (my $tmp = $line)=~s/^\d+\s\d+//; my $msg = $tmp." - Line number :: $cntr"; push (@{$result_struct{$err_ty}},$msg); } if(($line=~/(TESTSCRIPT):(.*)/i)||($line=~/(TESTLOG)\s*:\s*(\S+)\/HDD/)){ $result_struct{$1} = $2; } if($line=~/\d+\s+\S+\s+Failed\*\*\s+/){ $result_struct{'SCRIPT_RESULT'} = 'FAILED'; } if(($error_file)&&($line=~/$error_file/s)){ (my $tmp = $line)=~s/^\d+\s\d+//; my $msg = $tmp." - Line number :: $cntr"; push (@{$result_struct{'ERROR_MESSAGES'}},$msg); } if($line=~/Time:\s+(\S+)/){ my (@time) = split(":",$1); my $time = "$time[0] Hours, $time[1] Minutes, ".int ($time[2])." Seconds"; $result_struct{'TIME'} = $time; } if($line=~/\*\s*SCRIPT ERROR:/){ my $msg = "Script Error found at - Line number :: $cntr"; push(@{$result_struct{'SCRIPT_ERROR'}},$msg); } } push(@Step_log,"\n#################################################################################################\n\n"); push(@Step_log,"## SCRIPT NAME : $result_struct{TESTSCRIPT}\n"); push(@Step_log,"## TOTAL TIME TAKEN : $result_struct{TIME}\n"); push(@Step_log,"## SCRIPT RESULT : $result_struct{SCRIPT_RESULT}\n"); push(@Step_log,"## LOG DIR : $result_struct{TESTLOG}\n##\n"); push(@Step_log,"\n## WARNING MESSAGES :\n##\n"); foreach(@{$result_struct{'WARNING'}}){ push(@Step_log,"## $_ \n"); } push(@Step_log,"## No Warning messages available\n##\n")if(!(@{$result_struct{'WARNING'}})); push(@Step_log,"\n## FAILURE MESSAGES:\n##\n"); foreach(@{$result_struct{'FAIL'}}){ push(@Step_log,"## $_ \n"); } push(@Step_log,"## No Failure messages available\n##\n")if(!(@{$result_struct{'FAIL'}})); push(@Step_log,"\n## FATAL MESSAGES :\n##\n"); foreach(@{$result_struct{'FATAL'}}){ push(@Step_log,"## $_ \n"); } push(@Step_log,"## No Fatal messages available\n##\n")if(!(@{$result_struct{'FATAL'}})); push(@Step_log,"\n## ERROR MESSAGES:\n##\n"); foreach(@{$result_struct{'ERROR_MESSAGES'}}){ push(@Step_log,"## $_ \n"); } push(@Step_log,"## No Error messages available\n##\n")if(!(@{$result_struct{'ERROR_MESSAGES'}})); if(defined $result_struct{'SCRIPT_ERROR'}){ push(@Step_log,"\n## SCRIPT ERROR MESSAGES:\n##\n"); foreach(@{$result_struct{'SCRIPT_ERROR'}}){ push(@Step_log,"## $_ \n"); } } push(@Step_log,"##\n#################################################################################################\n\n"); =cut open( STEP_LOG, ">$LOGDIR/$file_C") || die "Can not open file $LOGDIR/$file_C"; print( STEP_LOG @Step_log ); close STEP_LOG; if (-z "$LOGDIR/$file_C"){ ##if main log file are empty system("sudo rm -rf $LOGDIR/$file_C"); return $TCD::FAIL; } else { return $TCD::PASS; } } sub subtest_clean { logcomment("Pushing subtest contents to main log directory"); opendir( DIR_LOG, $LOGDIR ); my @files_DIR_LOG = readdir(DIR_LOG); logcomment("Total number of files present are @files_DIR_LOG"); foreach my $log (@files_DIR_LOG){ next if($log =~/cleanup/); if($log =~/\.log$/){ logcomment("Working on the file $log"); my $cnt=0; my $log_main = "$LOGDIR/$log"; while(1){ logcomment("Checking the Main log file $LOGDIR/$log for any subtest logs exists"); $/=undef; open( MAIN_LOG, "$log_main") || die "Can not open file $log_main"; logcomment("Reading the contents of Main log file $log"); my $data = ; $/="\n"; close MAIN_LOG; my @sub_dir_files = $data=~/\!\!\s+Subtest\s+(HDD\S+)/g; if(!@sub_dir_files){ logcomment("No sub-test logfiles found so going out of while loop"); last; } $cnt++; if($cnt > 50){ logcomment("number of while loop count is greater than 50 so exiting the while loop"); last; } grep{logcomment("Number of subtest files found are : $_")} @sub_dir_files; foreach(@sub_dir_files){ my $file = "$LOGDIR/../../../$_".'.log'; logcomment("Checking the subtest log file $LOGDIR/../../../$file and moving the contents to its immediate main log file"); check_recurse($log_main,$file); } } } } return $TCD::PASS; } sub check_recurse($$){ my ($main_log,$sub_log) = @_; $/=undef; open( MAIN_LOG, "$main_log") || die "Can not open file $main_log"; logcomment("Reading the contents of log file $main_log"); my $data = ; $/="\n"; close MAIN_LOG; logcomment("Sub-test log file is $sub_log and its immediate main log file is $main_log"); logcomment("Checking whether it is creating any other sub test directories or not and moving data to main log"); $/=undef; open(SUB_LOG , "$sub_log")|| die "Can not open file $sub_log"; my $data_sub = ; $/="\n"; close SUB_LOG; my @sub_dir_files1 = $data_sub=~/\!\!\s+Subtest\s+(HDD\S+)/g; grep{logcomment( "sub-directory log file $_")} @sub_dir_files1; my @main_sub; if(@sub_dir_files1){ foreach(@sub_dir_files1){ my $file = "$LOGDIR/../../../$_".'.log'; logcomment("main $sub_log sub $file"); check_recurse($sub_log,$file); } } { my %f_d_t; my @data; foreach(split(/\n/,$data)){ if($_=~/\!\!\s+Subtest\s+(\S+)\s+started\s+\(pid\s+(\d+)/g){ my $file = $1; my $pid = $2; my($date,$time) = $_=~/(\S+)\s+(\S+)/; $f_d_t{$file}{'date'} = $date; $f_d_t{$file}{'time'} = $time; $f_d_t{$file}{'pid'} = $pid; } if($_=~/\!\!\s+Subtest\s+(HDD\S+)/){ my @arr = split(/\//,$1); my $log_name = "$1".'.log'; my($date1,$time1) = $_=~/(\S+)\s+(\S+)/; if($sub_log =~/$log_name$/){ logcomment("Copying location $log_name"); push(@data,$f_d_t{$arr[-1]}{'date'}," ",$f_d_t{$arr[-1]}{'time'},' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!',"\n"); push(@data,$f_d_t{$arr[-1]}{'date'}," ",$f_d_t{$arr[-1]}{'time'}," !! Starting Subtest $arr[-1] started (pid $f_d_t{$arr[-1]}{'pid'})\n"); push(@data,$f_d_t{$arr[-1]}{'date'}," ",$f_d_t{$arr[-1]}{'time'},' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!',"\n"); my @split = split(/\d+\s+\d+\s+\S+\s+\{STAF_handle/,$data_sub); foreach my $pid_data(@split){ if($pid_data=~/$f_d_t{$arr[-1]}{'pid'}/g){ # logcomment("writing data $pid_data"); push(@data,$pid_data,"\n"); last; }else{ logcomment("No output found for the PID $f_d_t{$arr[-1]}{'pid'}"); } } push(@data,$date1," ",$time1,' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!',"\n"); push(@data,$date1," ",$time1," !! Ending Subtest $arr[-1] started (pid $f_d_t{$arr[-1]}{'pid'})\n"); push(@data,$date1," ",$time1,' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!',"\n"); next; } } push(@data,$_,"\n"); } logcomment("Changing the permisiions of log file $main_log"); system("sudo chmod 666 $main_log"); open( MAIN_LOG1, ">$main_log") || die "Can not open file $main_log"; print MAIN_LOG1 @data; logcomment("Log file modified : $main_log"); close MAIN_LOG1; } }