#!/bin/sh

# List busy SGE nodes.

# Dave Love <fx@gnu.org>, 2008-09
# Copyright (C) 2008 The University of Liverpool
# Licence: FreeBSD <http://www.gnu.org/licenses/license-list.html#FreeBSD>

usage () {
    echo "Usage: $(basename $0) [-w]
List nodes currently running SGE jobs.
-w means separate node names with commas, to produce a list suitable
for the -w arg of pdsh."
}

sep='"\n"'
case $1 in
    --help) usage; exit;;
    -w) sep='","';;
    '') : ;;
    *) usage 1>&2; exit 1;;
esac

# The qhost output comprises a header and node lines up until the
# first node with a job in it, when the node ine is followed by
# another header and the job details.  Filter the headers and look for
# node lines, which have leading node names (no spaces).  If the
# following line has leading blanks and a number (job info), the node
# is busy.
qhost -j |
  awk '
/^HOSTNAME|^-/ {node=""}  # header

/^[[:blank:]]+(job-ID|-)/ {next}# header

/^[^[:blank:]]/ {node = $1}	# node

/^[[:blank:]]/ {		# job
    if (node) {
	printf ("%s%s", sep, node)	# from previous line
       'sep=$sep'			# shell substitution!
    }
    node=""
}'
[ $sep = '"\n"' ] && echo
