Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
#!/usr/bin/env perl 

###############################################################################
#                                                                             #
#   Script to check the status of DQM Harvesting jobs                         #
#                                                                             #
#   It basically checks whether the root tree is present for all jobs         #
#   If not, it resubmits the failed jobs on request                           #
#                                                                             #
#  Usage: check_harvesting.pl [crab_status_output]                            #
#                                                                             #
#  NOTE: it needs to be run from the directory from which you submitted       #
#        multicrab jobs                                                       #
#                                                                             #
#  Optional argument:                                                         #
#   crab_status_output: file containing output of "multicrab -status" command #
#                       if not given, the script will get it itself           #
#                                                                             #
#  Author:        Vuko Brigljevic, Rudjer Boskovic Institute                  #
#  First Version: January 22, 2010                                            #
#                                                                             #
###############################################################################




use strict;




# Gets the crab status from input or produce it

my $nargs = @ARGV;


my $crabstatus = "";

if ($nargs>0 && -e $ARGV[0] ) {
    $crabstatus= $ARGV[0];
}

if ($crabstatus eq "") {
    $crabstatus = "multicrabstatus.out";
    print "Getting multicrab jobs status ... \n";
    system "multicrab -status > $crabstatus ";
    print "Done \n";
}




# Open the multicrab cfg file to extract the list of job ids
#

my $crabcfg = "multicrab.cfg";

if (! -r $crabcfg ) {
    print "No multicrab.cfg in this directory, quitting... \n";
    exit;
}


open (CRABCFG,$crabcfg);

#my @lines=<STDIN>;
my @lines=<CRABCFG>;

my $line="";

my $tempfile=".check_harvesting_output";

my $castorbase="/castor/cern.ch";

my $job;

my $nsuccess=0;
my $njobs=0;
my $nfailed=0;

my @failedjobs=();

foreach $line (@lines) {

    if ( $line =~ /\[(.*?)\]/ )
    {
	if ( $1 ne "MULTICRAB") {
	    $job = $1;
	    $njobs++;
	}
    }

    if ($line =~ /USER.user_remote_dir/) {

	my @words = split (/ = /,$line);
	chop($words[1]);
	my $castordir=$castorbase.$words[1];  # ADD JOB NAME IN CASTOR DIRECTORY!
	# MODIFICATION: to account for the fact that multicrab adds job name in directory tree for output!!!
	$castordir=$castordir."/".$job;
#	print "CASTOR DIR: $castordir \n";
	# this lines needs to be commented out to account for the above modification
#	chop($castordir); #remove newline at the end

	if ( -e $tempfile ) {
	    system "rm $tempfile";
	}

	my $rfcmd="rfdir $castordir > $tempfile";

	# Check if root file present in output directory
	system $rfcmd ;
	open(LIST,$tempfile);

	my $tmpline;
	my $nrootfiles=0;
	while ($tmpline = <LIST> ) {
	    if ( $tmpline =~ /root/ ) {
		$nrootfiles++;
	    }
	}
	if ($nrootfiles > 0) {
	    $nsuccess++;
	} else {
	    $failedjobs[$nfailed]=$job;
	    $nfailed++;
	}
    }
}



print "Number of jobs                               : $njobs \n";
print "Number of successful jobs (root file present : $nsuccess \n";
print "Number of failed or still running jobs       : $nfailed \n";

my %jobstatus;

##################################################
# Get job status for jobs without root output

foreach $job (@failedjobs) {

    my $jobstatus  = get_multicrab_job_status($job,$crabstatus);
    $jobstatus{$job} = $jobstatus;

    print " $job : $jobstatus \n";
}

if ($nfailed == 0) {
    exit;
}

# Resubmit failed jobs if desired

print "Would you like to resubmit the failed jobs? [y/n] \n";

my $reply=<STDIN>;
chop ($reply);

print "reply: $reply \n";

if ( $reply eq "y" || $reply eq "Y" ) {

    print "Resubmitting jobs... \n";

    foreach $job (@failedjobs) {


	my $status = $jobstatus{$job};

	my $retrieve = 0;
	my $resubmit = 0;

	if ( $status eq "Scheduled" 
	     || $status eq "Pending"
	     || $status eq "Running" ) {
	    print "$job running: leave it in peace... \n";
	} elsif ($status eq "Aborted") {
	    $resubmit = 1;
	} elsif ($status eq "Done") {
	    $retrieve = 1;
	    $resubmit = 1;
	} elsif ($status eq "Retrieved") {
	    $resubmit = 1;
	}

	if ($retrieve) {
	    print "retrieving... \n";
	    system "crab -c $job -getoutput all";
	}
	if ($resubmit) {
	    print "resubmitting... \n";
	    system "crab -c $job -resubmit all";
	}

    }

}





##################################################################################

sub get_multicrab_job_status {
    
    #-------------------------------------------------------------------#
    # Aim: Get the CRAB job status of a multicrab job                   #
    #      by parsing the "multicrab -status" output                    #
    #                                                                   #
    #  two input arguments                                              #
    #      $job         : CRAB job name                                 #
    #      $crabstatus  : file with output of "multicrab -status"       #
    #-------------------------------------------------------------------#


    if ( scalar(@_) != 2 ) {
	print "get_multicrab_job_status() called with wrong number of arguments: @_ \n";
	return "";
    }
    
    my ($job, $crabstatus) = @_ ;

    open(CRABSTATUS,$crabstatus);
   
    my $jobfound=0;
    my $linecounter=0;
    my $jobstatus="undefined";
    while ( <CRABSTATUS>) {
	if ($linecounter > 0) {
	    if ($linecounter == 2) { 
		# This is the line with the job status
		
		my @words = split (" ");
		$jobstatus=$words[1];
		last;
	    }
	    $linecounter++;
	}
	if ($jobfound) {
	    if (/ID/ && /STATUS/ ) {
		$linecounter++
		}
	    next;
	}
	
	if (/$job/) {
	    $jobfound = 1;
	}
    }

    close(CRABSTATUS);

    return $jobstatus;
    
}