#!/bin/ksh
###############################################################################
#
# Sun Cluster Sun MTP Data Service version 1.0
#
# Sun MTP Probe script for scmtp.
#
###############################################################################
EMP_SETUP=none
EMP_MONITOR=none
EMP_SRV_ID=none
EMP_DEBUG=OFF

typeset opt

while getopts 'S:M:I:D:' opt
do
  case "$opt" in
    S)
    # Setup path name
    EMP_SETUP=$OPTARG
    ;;
    M)
    # Monitor option
    EMP_MONITOR=$OPTARG
    ;;
    I)
    # Data service identifier
    EMP_SRV_ID="$OPTARG"
    ;;
    D)
    # Debug option
    EMP_DEBUG=$OPTARG
    ;;
    *)
    kixlog "mtp_probe: warning option $OPTARG unknown"
    ;;
  esac
done

if [ "$EMP_SETUP" = "none" ]
then
    kixlog "mtp_probe: Error. Setup file has not been specified"
    exit 101
fi

. $EMP_SETUP
if [ "$EMP_DEBUG" = "ON" ]
then
  kixlog "mtp_probe: $0 started with \"$*\" parameters"
fi

if [ "$EMP_MONITOR" = "none" ]
then
    kixlog "mtp_probe: Error. Monitor option has not been specified"
    exit 102
fi
if [ "$EMP_SVR_ID" = "none" ]
then
    kixlog "mtp_probe: Error. Data service identifier has not been specified"
    exit 103
fi

number_processed_options=0; export number_processed_options

echo "$EMP_MONITOR" | grep -i SYS_PROCS > /dev/null
if [ $? -eq 0 ]
then
  number_processed_options=`expr $number_processed_options + 1`

  pmfadm -q $EMP_SRV_ID
  ret_status=$?
  if [ "$EMP_DEBUG" = "ON" ]
  then
    kixlog "mtp_probe: checking for MTP system processes, status=$ret_status"
  fi
  if [ $ret_status -ne 0 ]
  then
    kixlog "mtp_probe: pmfadm -q $EMP_SRV_ID failed"
    exit 104
  fi

  ret_str=`kixdump -hproc_main`
  ret_status=$?
  if [ "$EMP_DEBUG" = "ON" ]
  then
    kixlog "mtp_probe: checking for unikixmain, status=$ret_status-$ret_str"
  fi
  if [ $ret_status -ne 0 ]
  then
    kixlog "mtp_probe: ERROR, kixdump -hproc_main failed"
    exit 105
  else
    if [ ! "$ret_str" = "ACTIVE" ]
    then
      kixlog "mtp_probe: ERROR, unikixmain is not alive"
      exit 106
    fi 
  fi
fi

echo "$EMP_MONITOR" | grep -i APPL_DEFS > /dev/null
if [ $? -eq 0 ]
then
  number_processed_options=`expr $number_processed_options + 1`

  ret_str=`kixdump -hsystem_active`
  ret_status=$?
  if [ "$EMP_DEBUG" = "ON" ]
  then
    kixlog "mtp_probe: checking for MTP status, status=$ret_status-$ret_str"
  fi
  if [ $ret_status -ne 0 ]
  then
    kixlog "mtp_probe: ERROR, kixdump -hsystem_active failed"
    exit 107
  else
    if [ ! "$ret_str" = "ACTIVE" ]
    then
      kixlog "mtp_probe: ERROR, MTP system is not active"
      exit 108
    fi 
  fi
fi

if [ "$EMP_DEBUG" = "ON" ]
then
  kixlog "mtp_probe: returns 0"
fi
exit 0
