#!/bin/bash
# 
# Nicholas Musolino, April 2010

printf "%-4s %-13s %-5s %s\n" "NODE" "STATE" "JID" "OWNER OF JOB ON CPU0"
qnodes :group0 | gawk 'BEGIN{ RS="\n\n" }
       {
	name = $1;
 	for (i=1; i<=NF; i++) {
	 if ($i == "state") statevalue = $(i+2);
	 if ($i == "jobs") {
	   match($(i+2), /[[:digit:]]+\./)
	   jobIDstring = substr($(i+2),RSTART,RLENGTH-1)   
	 }
	}
	printf "%-4s %-13s %-5s\n", name, statevalue, jobIDstring;
	}' | while read line
do
  if [ $(echo "$line" | wc --words| cut -d" " -f1) -gt 2 ]; then
      jobID=$(echo $line | awk '{print $3}');
      owner=$(qstat -f $jobID | gawk '$1 ~ /Job_Owner/ { 
	ownerstring=$3;
	split(ownerstring,ownervalues,"@")
	print ownervalues[1]
	}')
      echo "$line $owner"
  else
      echo "$line"
  fi
done


