#!/usr/bin/csh -f

# The next line does 99% of the work: The output from ruptime is filtered
# by grep to exclude machines which are down. Then tr removes the commas
# at the end of fields. Finally sort orders the data on numeric field 8
# (load), numeric field 4 (users) and alpha field 1 (machine name)
# The use of double-quotes around the backquotes (command substitution)
# causes each line of output from the pipeline to be written into a single
# array location, for easy processing below.

set loadData = "`ruptime | grep -v down | tr -d ',' | sort -k 8n -k 4n -k 1`"

echo CheckLoad: $#loadData machines up.

if ( $#loadData == 0 ) exit 

# the use of () in the next line is required since $loadData[1] is a multi-word
# value.
set minLoad  = ( $loadData[1] )
set minUsers =   $minLoad[4]
set minLoad  =   $minLoad[8]
echo Minimum load = $minLoad, Minimum Users = $minUsers

# iterate until out of machines or until load/users rises
while ( $#loadData )

  set lineData = ( $loadData[1] )
  if ( $lineData[8] != $minLoad || $lineData[4] != $minUsers ) exit
  echo Load=$lineData[8], Users=$lineData[4], Machine=$lineData[1]
  shift loadData

end

