#!/bin/bash
#
# Usage: MONHmc { hmcsvr | rmc | disk | proc | mem }
#
# This script is not meant to be called manually. This script is called by monhmc
# command.

MAXPIDS_TOP_ALLOWS=20

# Exit codes
# 1 Invalid numbers of paramers
E_PARM_NUM=1
# 2 Invalid parameter
E_PARM_VALUE=2

# Global monitor PID list
mpid=""
numpids=0

clean_up ()
{
   /bin/rm -f $tempfile
}

buildPIDList()
{
   if [ $numpids -eq $MAXPIDS_TOP_ALLOWS ];then
      return
   fi
   procmon=$1
   while read line
   do
      # ignore this process: i.e., MONHmc script
      linecheck=`echo $line | grep MONHmc`
      if [ -z "$linecheck" -a "$line"  != "" ];then
         pidnum=`echo $line | /usr/bin/awk '{print $1}'`
         # concat all of the matching pids for the processes
         # the we're interested in.
         numpids=`expr $numpids + 1`
         if [ "$mpid" = "" ];then
            mpid=$pidnum
         else
            mpid="$mpid,$pidnum"
         fi
         if [ $numpids -eq $MAXPIDS_TOP_ALLOWS ];then
            return
         fi
      fi
done <<EOF
`/bin/ps -eo pid,cmd -www | grep -v grep | grep "$procmon"`
EOF
}

build_hmcsvr()
{
   buildPIDList "/opt/hsc/hdwr_svr/hdwr_svr"
   buildPIDList "com.ibm.hsc.common.util.CIMServerLoader"
   buildPIDList "com.ibm.websm.console.WConsole"
   buildPIDList "com.ibm.websm.bridge.WServer"
   buildPIDList "com.ibm.hwmca.fw.system.Manager"
}

build_rmc()
{
   buildPIDList "rmcd"
   buildPIDList "/usr/sbin/rsct/bin/IBM.ERrmd"
   buildPIDList "/usr/sbin/rsct/bin/IBM.ServiceRMd"
   buildPIDList "/usr/sbin/rsct/bin/IBM.CSMAgentRMd"
   buildPIDList "/usr/sbin/rsct/bin/IBM.LparCmdRMd"
   buildPIDList "/usr/sbin/rsct/bin/IBM.DMSRMd"
   buildPIDList "/usr/sbin/rsct/bin/IBM.LPRMd"
   buildPIDList "/usr/sbin/rsct/bin/IBM.HostRMd"
}

showProc()
{
   if [ ! -f ${HOME}/.toprc ];then
      /bin/cp /opt/hsc/data/toprc ${HOME}/.toprc
   fi
   /usr/bin/top -b -n 1 -p $PPID | /usr/bin/grep -i cpu[0-9,\(s]
}

showMem()
{
   /usr/bin/top -b -n 1 -p $PPID | /usr/bin/grep -i ^mem:
}

showSwap()
{
   /usr/bin/top -b -n 1 -p $PPID | /usr/bin/grep -i ^swap:
}

showDisk()
{
   /bin/df -l
}

display_mpid()
{
   if [ "$mpid" != "" ];then
      /usr/bin/top -c -b -n 1 -p $mpid | /usr/bin/grep -i -v ^cpu | /usr/bin/grep -i -v ^mem | /usr/bin/grep -i -v ^swap | /usr/bin/grep -i -v ^top
   fi
}

display_mpid_noc()
{
   if [ "$mpid" != "" ];then
      /usr/bin/top -b -n 1 -p $mpid | /usr/bin/grep -i -v ^cpu | /usr/bin/grep -i -v ^mem | /usr/bin/grep -i -v ^swap | /usr/bin/grep -i -v ^top
   fi
}

showHmcSvr()
{
   build_hmcsvr
   display_mpid
}

showRmc()
{
   build_rmc
   display_mpid_noc
}

# MAIN:

if [ $# != 1 ];then
   exit $E_PARM_NUM
fi

case "$1" in
   hmcsvr )
      showHmcSvr
      ;;
   rmc )
      showRmc
      ;;
   disk )
      showDisk
      ;;
   proc )
      showProc
      ;;
   mem )
      showMem
      ;;
   swap )
      showSwap
      ;;
   * )
      exit $E_PARM_VALUE
      ;;
esac

exit 0
