#!/usr/software/bin/perl # $Id: //depot/prod/test/Rsierranevada/storage/hdd/NADQ_SEA/NDATE/SUPPORT_SCRIPTS/ert_send_result.thpl#1 $ # Copyright (c) 2005 Network Appliance, Inc. # All rights reserved # ## @summary Mode ## Send ERT test result ## @description ## Send ERT test result on the console and to the email IDs mentioned ## @param FILER Required: Hostid of filer ## @param STEST_FILE Required: Name of the test suite ## @param MAIL_TO Required: Email to ## @param MAIL_FROM Required: Email from ## @param PARSE_ID Required: Test Day ## @status public ## @author maji@netapp.com use Cwd qw(abs_path); use HTML::Mail; use strict; use boot; use San; sub kill_shell_process(); sub check_shell_process_terminal(); sub terminatezomprocess(); sub trim($); sub del_parsed_dir(); ######################################## ### Initialization/declaration ######################################### use vars qw( $FILER $STEST_DEBUG $STEST_FILE $LOGLVL $LOGDIR $MAIL_TO $MAIL_FROM $MAIL_MSG $EMAIL $PARSE_ID $ERT $ERT_TEST_ID $TEST_SUITE_NAME $TEST_CONFIG ); param('FILER', '-mesg', 'HostID of filer'); param('STEST_DEBUG', '-default', '1'); param('STEST_FILE', '-mesg', 'Runlist file'); param('LOGDIR', '-default', ''); param('LOGLVL', '-default', '2'); param('MAIL_TO', '-mesg', 'email from'); param('MAIL_FROM', '-mesg', 'email from'); param('MAIL_MSG', '-default', 'n'); param('EMAIL', '-default', 'n'); param('PARSE_ID', '-mesg', 'parse id'); param('ERT', '-default', 'y'); param('ERT_TEST_ID', '-mesg', 'ERT test id'); param('TEST_SUITE_NAME' , '-mesg', 'Test_Suite_Name'); param('TEST_CONFIG', '-mesg', 'Test config'); if ( $TEST_CONFIG eq 'E' ) { $FILER =~ s/\,/\_/; my @FILER_ARR = split( /_/, $FILER ); $FILER = $FILER_ARR[0]; } my $host = host($FILER); my $Home = $ENV{HOME}; $Home = trim($Home); my $filer = $host->hostname; my $len = rindex $STEST_FILE, '.plan'; my $runlist = substr($STEST_FILE, 0, $len); my $san_obj = new San(\*ntest::NTEST_STDOUT); if ($san_obj->error()) { logresult('FAIL', "Failed to instantiate San object" . $san_obj->errmsg()); } del_parsed_dir(); my $rslt_file = "$LOGDIR/../../../HDD_SUMMARY_RESULTS"; my ($rslt_srt, $statuss); if (-e $rslt_file) { open(H_LOGFILE, "< $rslt_file ") or $san_obj->sanlog(-msg => "FILER- $filer : Failed opening $rslt_file" . scalar(localtime())); ($rslt_srt, $statuss) = ("", "Status: PASSED"); foreach my $line () { if ($rslt_srt =~ m/FAILED/) { $statuss = "Status: INSPECT"; } $rslt_srt = $rslt_srt . $line; } ## end foreach my $line () $san_obj->sanlog(-msg => "\n-----------------------------------------------------------------------\n" . " TEST RESULT - FILER: $filer $statuss" . "\n\n-----------------------------------------------------------------------\n$rslt_srt" . "\n-----------------------------------------------------------------------\n" ); } else { $san_obj->sanlog(-msg => "\n-----------------------------------------------------------------------\n" . " TEST RESULT - FILER: $filer " . "\n\n-----------------------------------------------------------------------\n" . " No result available\n-----------------------------------------------------------------------\n" ); } $san_obj->sanlog( -msg => "FILER- $filer : This FILER is free now :" . scalar(localtime())); ## Send reselt to email ids my $mail_subject = "$FILER : Final Test Result for day $ERT_TEST_ID"; my $mail_body; if ($MAIL_MSG eq 'y') { my $defailt_vals = "$Home/NDATE/default_params"; open(H_LOGFILE, "< $defailt_vals") or die "Failed opening $defailt_vals \n"; my @defaults; foreach my $line () { if ($line =~ /^EMAIL_MSG=(.*)/) { $MAIL_MSG = $1; } } } ## end if ($MAIL_MSG eq 'y') $mail_body = "Message: $MAIL_MSG\n\n" if ($MAIL_MSG ne 'null'); my $parse_file = "$LOGDIR/../../../$PARSE_ID.parsed"; if (-e $parse_file) { $mail_body = "Parser result for $PARSE_ID test\n====================================\n\n"; open(RESULT_FILE, "$parse_file" || die "Can not open file $parse_file"); foreach my $line () { $mail_body = $mail_body . $line; } close RESULT_FILE; } $mail_body = $mail_body . "\n\nLog location \n====================================\n\n"; $mail_body = $mail_body . $rslt_srt; my %mail_params = (HTML => $mail_body, Text => $mail_body, From => $MAIL_FROM, To => $MAIL_TO, Subject => $mail_subject,); $mail_params{HTML} = "
$mail_params{Text}<\><\\HTML>";
    my $mail = HTML::Mail->new(%mail_params);
    $mail->send()
      or logresult("CONFIG", "trouble sending mail");


## Send result to email ids end

my $logdirt = "$Home/NDATE/TEST_LOGS";

my $new_log_dir = `cd $LOGDIR/../../.. ; pwd`;
$san_obj->sanlog(-msg =>
      "----------------------------------------------------------------------");
$san_obj->sanlog(
     -msg => "FILER- $filer : Log location $new_log_dir" . scalar(localtime()));
$san_obj->sanlog(-msg =>
      "----------------------------------------------------------------------");

kill_shell_process();

$san_obj->sanlog( -msg => "FILER- $filer : Stopping background shell process");
$san_obj->sanlog(-msg =>
      "----------------------------------------------------------------------");

if (($LOGLVL <= 3)) {
    $san_obj->sanlog(-msg => "Cleaning Directories");

} ## end if (($LOGLVL <= 3))

my $user;
sub kill_shell_process()
{
        ##kill the shell process associated with the current ntest process
	# Check 1 :
	my $runlistprocess = `ps -ef | grep check | grep $runlist`;
	 if ($runlistprocess =~ /sh/g)
        {
            foreach $_ (split(/\n/, $runlistprocess))
              {
                if ($_ =~ /\s+(\d+)\s+(.*)\/check.sh/)
                    {
                        system("kill -9 $1 2>/dev/null");
                    }
              }
        }
        $san_obj->sanlog(-msg => "FILER- $filer : Killing the shell process associated with this ntest");
	$san_obj->sanlog(-msg =>
      		"----------------------------------------------------------------------");

       	#Check 2: 
	open(FH,"<$Home/NDATE/TEST_LISTS/$STEST_FILE");
        while()
        {
           if( $_ =~ /PID\s*=\s*(\d+)/ )
           {
                 system("kill -9 $1 2>/dev/null");
           }
        }
        close(FH);

     my ($tty, $ps);
        $tty = `tty`;
        $tty =~ s/^ //;
        $tty =~ s/\s$//;
	$tty =~ s/\/dev\///;
	$user=`whoami`;
	$ps = `ps -ef | grep $tty | grep /NDATE/SUPPORT_SCRIPTS/check.sh | grep $user`;
        if ($ps =~ /sh/g)
        {
            foreach $_ (split(/\n/, $ps))
              {
                if ($_ =~ /\s+(\d+)\s+(.*)\/check.sh/)
                    {
                        system("kill -9 $1 2>/dev/null");
                    }
              }
        }
#check for shell process without ntest
check_shell_process_terminal();
}

sub check_shell_process_terminal()
{
my ($ps, $tty_own, $ps1, $process_kill);

$ps=`ps -ef | grep /NDATE/SUPPORT_SCRIPTS/check.sh | grep pts | grep $user`;

my @array_ps = ();

foreach my $process_IDS (split(/\n/, $ps))
{
        if ($process_IDS =~ /\s+(pts\/\d+)\s+/)
        {
        my $ps_IDS = $1;
        push(@array_ps, $ps_IDS);
        }
}

#my @array_ps = split(/\n/, $ps);
#Removing duplicate terminal ids

my %test_hash;
my @temp_arr;

foreach (@array_ps)
{
   if ($test_hash{$_})
        {
        next;
        } else {
        $test_hash{$_} = 1;
        push (@temp_arr, $_);
        }
}

        $tty_own = `tty`;
        $tty_own =~ s/^ //;
        $tty_own =~ s/\s$//;
        $tty_own =~ s/\/dev\///;

        foreach my $ty (@temp_arr)
        {
        if ($ty =~ /$tty_own/)
        {
        next;
        }
        $ps1= `ps -ef | grep $ty | grep ntest | grep $user`;
        foreach my $out (split(/\n/, $ps1))
        {
        if ($out =~ /NDATE\/NATE\/bin\/ntest/)
        {
# print "This terminal id $ty has a ntest process running\n";
        last;
        } else
        {
        $process_kill = `ps -ef | grep $ty | grep $user`;
        if ($process_kill =~ /\/NDATE\/SUPPORT_SCRIPTS\/check.sh/)
        {
        foreach $_ (split(/\n/, $process_kill))
        {
        if ($_ =~  /\s+(\d+)\s+(.*)\/check.sh/)
        {
        system("kill -9 $1 2>/dev/null");
# print "DEBUG3: Process ids to be killed are $1\n";
        last;
        }
        }
        }
        }
}
}
terminatezomprocess();
}
# Sub to terminate zombie process of check.sh.
sub terminatezomprocess()
        {
        my $cmd = "ps -ef | grep check | grep \"?\" | grep $user";
        my $check_ps = `$cmd`;

        my $cmd_ntest = "ps -ef | grep ntest | grep $user";
        my $ntest_ps = `$cmd_ntest`;

#Capturing the individual ntest processes
        my @ntest_arr = split("\n", $ntest_ps);
        my @up_ntest_arr;
        foreach my $pro (@ntest_arr) {
        if ($pro !~ /grep ntest/) {
                push(@up_ntest_arr, $pro);
        }
        }
if (!(@up_ntest_arr))
        {
        my @check_arr = split("\n", $check_ps);
        foreach my $pro_ch (@check_arr)
         {
                if ($pro_ch !~ /grep check/) {
                        $pro_ch =~/\s+(\d+)\s+/;
                        system("kill -9 $1 2>/dev/null");
                  }
         }
        }
}

# Delete subtestlog dir
sub del_parsed_dir()
{
	my $dummy_file;
	$dummy_file = "$Home/NDATE/SUPPORT_SCRIPTS/PARSED_OUTPUT_$TEST_SUITE_NAME.txt";
    system("rm -rf $dummy_file");
	my $san_obj = new San(\*ntest::NTEST_STDOUT);
 	   if ($san_obj->error()) {
	   logresult('FAIL', "Failed to instantiate San object" . $san_obj->errmsg());
	   }
    $san_obj->sanlog(-msg => "Deleted $dummy_file\n");
    $san_obj->sanlog(-msg => "----------------------------------------------------------------------");
}
							
	
sub trim($) {
    my $string = shift;
    $string =~ s/^\s+|\s+$//gm;
    s/^\"(.*)\"$/$1/ || s/^\'(.*)\'$/$1/;
    return $string;
}