#!/usr/bin/perl
#
# Initialize variables
#   Note that JobCurrentStartDate is NOT returned if a job is idle
#
### $ENV{CONDOR_CONFIG}="/opt/condor/etc/condor_config";
$condor_q = "/usr/bin/condor_q";
#$condor_args = "-format \"%d.\" ClusterId -format \"%d \" ProcId -format \"%s \" Owner -format \"%d \" RequestCpus -format \"%d \" JobStatus -format \"%d \" IsAnalyJob -format \"%s \" localQue -format \"%d \" ServerTime -format \"%d \" JobCurrentStartDate -format \"%s \" AccountingGroup -format '\n' In 2\>\&1";
$condor_args = "-af ClusterId Owner RequestCpus jobStatus IsAnalyJob localQue ServerTime JobCurrentStartDate AccountingGroup RequestLMEMTask";
$makePlots = "/bin/bash /var/www/html/Monitoring/make_status_plots.sh";
#
# Define our functions
#
# count_types accumulates calibration, non-Analysis and Analysis counts
#   for both running and idle jobs
#   May need mods later when calibration come to us via Panda
#
sub count_types
{
    $runProdCount = 0;
    $idleProdCount = 0;
    $runAnalyCount = 0;
    $idleAnalyCount = 0;
    $runCalibCount = 0;
    $idleCalibCount = 0;
    $runMP8Count = 0;
    $idleMP8Count = 0;
    $connectRun = 0;
    $connectIdle = 0;
    $runCores = 0;
    $idleLMEMCount = 0;
    $runLMEMCount = 0;
    foreach $line (<FID>)  {
      chomp $line;
      ($ID,$owner,$cores,$runState,$isAnaly,$queName,$nowTime,$strtTime,$AccGrp,$LMEMTask) = split(/\s+/,$line);
      if ( $ID == "Error:" ) {
	  break;
      }
      if ( $runState == 2 ) {
         $runTime = $nowTime - $strtTime;
	 $runCores+=$cores;
	 if ( $queName eq "Splitter" || $AccGrp =~ /group_calibrate/ ) {
	     $runCalibCount++;
	 }
# This runTime test is used to distinguish between pilots that will not
# pick up a job, and those that do.  Don't count the former.
         elsif ( $runTime > 180 ) {
	     if ( $isAnaly eq "true" ) {
		 $runAnalyCount++;
	     }
	     elsif ( $queName eq "MP8" ) {
		 $runMP8Count++;
	     }
             else {
		 $runProdCount++;
	     }
	     if ( $LMEMTask ne "undefined" ) {
		 if ( $LMEMTask == 1 ) {
		     $runLMEMCount++;
		 }
	     }
	     if ( $owner eq "atlasconnect" ) {
		 $connectRun++;
	     }
	 }
      }
      elsif ( $runState == 1 ) {
	  if ( $queName eq "Splitter" || $AccGrp =~ /group_calibrate/ ) {
	      $idleCalibCount++;
	  }
          elsif ( $isAnaly eq "true" ) {
	      $idleAnalyCount++;
	  }
	  elsif ( $queName eq "MP8" ) {
	      $idleMP8Count++;
	  }
          else {
	      $idleProdCount++;
	  }
	  if ( $LMEMTask ne "undefined" ) {
	      if ( $LMEMTask == 1 ) {
		  $idleLMEMCount++;
	      }
	  }
	  if ( $owner eq "atlasconnect" ) {
	      $connectIdle++;
	  }
      }
    }
#
# Output running and idle production count, followed by running and 
#     idle analysis count and running and idle Calibration job count
#
    return $runProdCount, $idleProdCount, $runAnalyCount, $idleAnalyCount, $runCalibCount, $idleCalibCount, $runMP8Count, $idleMP8Count, $connectRun, $connectIdle, $runLMEMCount, $idleLMEMCount, $runCores;
}

#
# Wrapper for count_types, the also does file open/close.
#
sub count_wrapper
{
    $sched = $_[0];
    open (FID, "$condor_q $sched $condor_args | ");
    ($rProd,$iProd,$rAna,$iAna,$rCalib,$iCalib,$rMP8,$iMP8,$rConnect,$iConnect,$rLMEM,$iLMEM,$rCores) = count_types();
    close(FID);
    return $rProd, $iProd, $rAna, $iAna, $rCalib, $iCalib, $rMP8, $iMP8, $rConnect, $iConnect, $rLMEM, $iLMEM, $rCores;
}

#
#========================= Work Begins here ============================
#
use Sys::Hostname;
my $host = hostname();
#
#@schedulers = ('-name gate02.grid.umich.edu', '-name gate01.aglt2.org',
#   '-name umt3int01.aglt2.org', '-name umt3int02.aglt2.org',
#   '-name umt3int03.aglt2.org', '-name umopt1.aglt2.org');
#
# Do gate02
#
($runGate02,$idleGate02,$junk1,$junk2,$junk3,$junk4,$junk5,$junk6,$junk7,$junk8,$junk9,$junk10,$coresG02) = count_wrapper("-name gate02.grid.umich.edu");
#print " run count is $runGate02, idle count is $idleGate02\n";
#
# gate01
#
($runProd01,$idleProd01,$runAnaly01,$idleAnaly01,$runCalib01,$idleCalib01,$runMP801,$idleMP801,$runConnect,$idleConnect,$runLMEMg01,$idleLMEMg01,$coresG01) = count_wrapper("-name gate01.aglt2.org");

$runMP8 = $runMP801;
$idleMP8 = $idleMP801;

#
# Now the T3 machines
#
($runT3I1,$idleT3I1,$junk1,$junk2,$runCalI1,$idleCalI1,$junk5,$junk6,$junk7,$junk8,$runLMEMI01,$idleLMEMI01,$coresI01) = count_wrapper("-name umt3int01.aglt2.org");
($runT3I2,$idleT3I2,$junk1,$junk2,$runCalI2,$idleCalI2,$junk5,$junk6,$junk7,$junk8,$runLMEMI02,$idleLMEMI02,$coresI02) = count_wrapper("-name umt3int02.aglt2.org");
($runT3I3,$idleT3I3,$junk1,$junk2,$runCalI3,$idleCalI3,$junk5,$junk6,$junk7,$junk8,$runLMEMI03,$idleLMEMI03,$coresI03) = count_wrapper("-name umt3int03.aglt2.org");
($runT3I4,$idleT3I4,$junk1,$junk2,$runCalI4,$idleCalI4,$junk5,$junk6,$junk7,$junk8,$runLMEMI04,$idleLMEMI04,$coresI04) = count_wrapper("-name umt3int04.aglt2.org");
($runT3I5,$idleT3I5,$junk1,$junk2,$runCalI5,$idleCalI5,$junk5,$junk6,$junk7,$junk8,$runLMEMI05,$idleLMEMI05,$coresI05) = count_wrapper("-name umt3int05.aglt2.org");
#
# Grab the rest of the splitter stuff from the condor central manager node
#
($junk1,$junk2,$junk3,$junk4,$runCalHead,$idleCalHead,$junk5,$junk6,$junk7,$junk8,$junk9,$junk10,$coresBat) = count_wrapper("-name aglbatch.aglt2.org");
# ($junk1,$junk2,$junk3,$junk4,$runCalSplit,$idleCalSplit,$junk5,$junk6,$junk7,$junk8,$junk9) = count_wrapper("-name splitter.aglt2.org");
#
# Now, accumulate all of this.
#
$runCores = $coresG02 + $coresG01+ $coresI01 + $coresI02 + $coresI03 + $coresI04 + $coresI05 + $coresBat;
#
$runGate01 = $runProd01 + $runAnaly01;
$idleGate01 = $idleProd01 + $idleAnaly01;
#
$runCount = $runGate02 + $runGate01;
$idleCount = $idleGate02 + $idleGate01;
#
$runT3 = $runT3I1 + $runT3I2 + $runT3I3 + $runT3I4 + $runT3I5;
$idleT3 = $idleT3I1 + $idleT3I2 + $idleT3I3 + $idleT3I4 + $idleT3I5;
#
$runCalib = $runCalib01 + $runCalI1 + $runCalI2 + $runCalI3 + $runCalI4 + $runCalI5 + $runCalHead;
$idleCalib = $idleCalib01 + $idleCalI1 + $idleCalI2 + $idleCalI3 + $idleCalI4 + $idleCalI5 + $idleCalHead;
$runLMEM = $runLMEMI01 + $runLMEMI02 + $runLMEMI03 + $runLMEMI04 + $runLMEMI05 + $runLMEMg01;
$idleLMEM = $idleLMEMI01 + $idleLMEMI02 + $idleLMEMI03 + $idleLMEMI04 + $idleLMEMg04 + $idleLMEMg05 + $idleLMEMg01;
#$runCalib = $runCalib + $runCalI1 + $runCalI2 + $runCalI3 + $runCalI4 + $runCalI5 + $runCalHead + $runCalSplit;
#$idleCalib = $idleCalib + $idleCalI1 + $idleCalI2 + $idleCalI3 + $idleCalI4 + $idleCalI5 + $idleCalHead + $idleCalSplit;
#
# Finished, output
#
# We are going to output the following, in the first file, in ASCII:
# date and time, somehow (seconds since 0, YYYY MM DD hh mm)
# Total T2 Run and Idle excluding calibration
# ATLAS T2 Run and Idle excluding calibration
# Analysis Run and Idle
# T3 Run and Idle (although we'll likely ignore the latter as it is usually so LARGE
# Calibration jobs Run and Idle (added 7/31/2009)
# -- We'd put this out more cleanly except for the desire to maintain
# -- the history we've accumulated to date.
# -- Time zones are fussy.  Need to fix back records to make time stuff
# --   consistent and clean.
# atlasconnect jobs Run and Idle (added 2/7/2014)
# Number of running cores (added 3/2/2014)
# LMEM jobs Run and Idle (added 5/9/2017)
#
# We are going to output the following, in the second file, in ASCII:
# date and time, somehow (seconds since 0, YYYY MM DD hh mm)
# Number of running MP8 jobs
# Number of idle MP8 jobs
#
$ENV{TZ}='/usr/share/zoneinfo/UTC';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime(time());
$year = $year + 1900;
$mon = $mon + 1;
print "".time()." $year $mon $mday $hour $min ";
print "$runCount $idleCount $runGate01 $idleGate01 $runAnaly01 $idleAnaly01 $runT3 $idleT3 $runCalib $idleCalib $runConnect $idleConnect $runCores $runLMEM $idleLMEM\n";
#
if ($#ARGV > -1) {
    open(MPmon,">>@ARGV[0]");
    print MPmon "".time()." $year $mon $mday $hour $min ";
    print MPmon "$runMP8 $idleMP8\n";
    close(MPmon);
}
#
$ENV{TZ}='/usr/share/zoneinfo/EST5EDT';
system("$makePlots");
