#! /bin/bash
#
# Usage: pps [user]
#
# Show the root and rootserv processes running on all machines.
#
# Author: rdm
#
AWK=/usr/bin/awk
CAT=/bin/cat
REMSH=/usr/bin/rsh
PS=/bin/ps

USAGE='Usage: pps [user]'
MACHINES=/usr/local/root/proof/etc/cluster.conf

case $# in
    2|3|4|5|6)  echo $USAGE >&2; exit 1
esac

if [ -n "$1" ]
then
   user=$1
else
   user=p
fi

if [ -f $MACHINES ]
then
   machines=`$CAT $MACHINES`
   for i in $machines
   do
      echo "========== $i =========="
      $REMSH $i $PS -uaxw | $AWK 'BEGIN { ms = ss = 0 }
                             {if (/'$user'/ && /proofs/ &&  !/awk/) 
                                 if (substr($9,1,1) ~ /[0-9]/) {
                                    printf("%-10s %-7s %s   %6s   %s %s\n", $1,$2,$9,$10,$11,$12)
                                    if ($12 == "proofserv") ms++;
                                    if ($12 == "proofslave") ss++;
                                 } else {
                                    printf("%-10s %-7s %s %-6s %6s   %s %s\n",$1,$2,$9,$10,$11,$12,$13)
                                    if ($13 == "proofserv") ms++;
                                    if ($13 == "proofslave") ss++;}
                             }
                             END { printf("\n%d Master, %d Slave servers\n", ms, ss) }'
      echo ' '
   done
fi
