#!/bin/sh
#
#
#*************************************************

#Copyright (c) KylinSoft Co., Ltd. [2021-2025]. All rights reserved.
#File name: weblogic
#Author: XuXiaojuan
#Version: 1
#Description: Manages Weblogic as Linux-HA resource 
#Date: 2023.12.28
#Others: 
#Function List: 
#1.Weblogic_status(): report whether the middleware queue manager is running
#2.Weblogic_monitor():report whether middleware queue manager seems to be working
#3.Weblogic_start():start the middleware
#4.Weblogic_stop():stop the middleware

#History: 
#1. Date: 2023.12.28 Author: XuXiaojuan Modification:add weblogic ocf script

#*************************************************
 
# OCF instance parameters:
#   OCF_RESKEY_WEBLOGIC_HOME=/home/weblogic/Oracle/Middleware/Oracle_Home
#   OCF_RESKEY_WEBLOGIC_domain_path=user_projects/domains/base_domain
#   OCF_RESKEY_sleeptime=5

#######################################################################
# Initialization:
#OCF_ROOT=/usr/lib/ocf
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
#######################################################################

usage() {
  cat <<UEND
	usage: $0 (start|stop|validate-all|meta-data|monitor)

	$0 manages a Weblogic as an HA resource.

	The 'start' operation starts the middleware.
	The 'stop' operation stops the middleware.
	The 'status' operation reports whether the middleware is running
	The 'monitor' operation reports whether the middleware seems to be working
	The 'validate-all' operation reports whether the parameters are valid

UEND
}

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="weblogic">
<version>1.0</version>

<longdesc lang="en">
Resource script for Weblogic. 
It manages Weblogic as an HA resource.
</longdesc>
<shortdesc lang="en">Weblogic resource agent</shortdesc>

<parameters>

<parameter name="WEBLOGIC_HOME" unique="0" required="1">
<longdesc lang="en">
Directory containing weblogic
</longdesc>
<shortdesc lang="en">Weblogic installdir</shortdesc>
<content type="string" default="/home/weblogic/Oracle/Middleware/Oracle_Home" />
</parameter>

<parameter name="WEBLOGIC_domain_path" unique="0" required="1">
<longdesc lang="en">
Directory containing weblogic domain path
</longdesc>
<shortdesc lang="en">Weblogic domain path</shortdesc>
<content type="string" default="user_projects/domains/base_domain" />
</parameter>

<parameter name="sleeptime" unique="1" required="1">
<longdesc lang="en">
The time start process need to sleep,maybe need change
</longdesc>
<shortdesc lang="en">The time start process need to sleep,maybe need change</shortdesc>
<content type="string" default="5" />
</parameter>

</parameters>

<actions>
<action name="start" timeout="300" />
<action name="stop" timeout="120" />
<action name="status" timeout="60" />
<action name="monitor" depth="0" timeout="30" interval="10" start-delay="10" />
<action name="validate-all" timeout="5" />
<action name="meta-data" timeout="5" />
</actions>
</resource-agent>
END
}

Weblogic_validate() {
# checking the parameters
    if [ ! -d $OCF_RESKEY_WEBLOGIC_HOME ]; then
	ocf_log err "$OCF_RESKEY_WEBLOGIC_HOME dosen't exist";
	exit $OCF_ERR_ARGS;
    fi
}

Weblogic_status() {
	#pid_num_weblogic=`ps -ef |grep -v grep|grep $OCF_RESKEY_WEBLOGIC_HOME|grep -c startWebLogic`
	pid_num_weblogic=`ps -ef |grep -v grep|grep -c $OCF_RESKEY_WEBLOGIC_domain_path`
	if [ $pid_num_weblogic -ge 1 ]; then
	    return $OCF_SUCCESS
        else
	    ocf_log debug "Weblogic server is not running in status"
            return $OCF_NOT_RUNNING
        fi
}

Weblogic_monitor() {
    ocf_log err "in monitor"
    Weblogic_status
    rc=$?
    if [ ! $rc -eq 0 ]; then
        #pid=`ps -ef|grep -v grep|grep startWebLogic |awk '{print $2}'`
        #for pid_kill in "$pid"
        #  do
        #     kill -9 $pid_kill
        #  done
        ocf_log err "Weblogic server is not running in monitor"
        return $OCF_NOT_RUNNING
     elif [ -f $OCF_RESKEY_WEBLOGIC_HOME/$OCF_RESKEY_WEBLOGIC_domain_path/startWebLogic.sh ]; then
        ocf_log info "Weblogic monitor succeeded"
        return $OCF_SUCCESS
     else
        ocf_log err "Weblogic monitor failed"
        return $OCF_ERR_GENERIC
     fi
}

Weblogic_start() {
    ocf_log err "in start"
    Weblogic_status
    if [ $? -eq 0 ]; then
	ocf_log info "Weblogic server already running"
	return $OCF_SUCCESS
    else
        sh $OCF_RESKEY_WEBLOGIC_HOME/$OCF_RESKEY_WEBLOGIC_domain_path/startWebLogic.sh &
        ret=$?
        sleep $OCF_RESKEY_sleeptime
    if [ $ret -eq 0 ]; then
        Weblogic_status
       if [ $? -eq 0 ]; then
          ocf_log debug "Weblogic server start ok"
          return $OCF_SUCCESS
       else
          ocf_log err "Weblogic server start failed"
          return $OCF_ERR_GENERIC
       fi
    else
      ocf_log debug "Weblogic server start failed"
      return  $OCF_ERR_GENERIC
    fi
 
    fi	
}

Weblogic_stop() {
   Weblogic_status
   if [ $? -eq 7 ];then
        ocf_log debug "Weblogic server already stopped"
        return $OCF_SUCCESS
   else
	#sh -x $OCF_RESKEY_WEBLOGIC_HOME/$OCF_RESKEY_WEBLOGIC_domain_path/bin/stopWebLogic.sh
	#ret=$?
	#if [ $ret -ne 0 ];then
        #allppid=`ps -ef|grep -v grep|grep $OCF_RESKEY_WEBLOGIC_HOME|awk '{print $2}'`
	ppid=`ps -ef|grep -v grep|grep /bin/sh|grep $OCF_RESKEY_WEBLOGIC_domain_path|grep startWebLogic.sh|awk '{print $2}'`
        for pid_kill in $ppid
        do
		chpid=`ps -ef|grep -v grep|grep $pid_kill|awk '{print $2}'`
		for tokill in $chpid
		do
		        #if [ "$pid_kill" != "$tokill" ];then	
				kill -9 $tokill
			#fi
		done

        done
	otherpid=`ps -ef|grep -v grep|grep $OCF_RESKEY_WEBLOGIC_domain_path|awk '{print $2}'`
	for other in $otherpid
	do
	   kill -9 $other
	done

	sleep 3
	#fi
   fi
   Weblogic_status
   if [ $? -eq 7 ]; then
        ocf_log debug "Weblogic server stop ok"
        return $OCF_SUCCESS
   else
        ocf_log debug "Weblogic server stop failed"
        return $OCF_ERR_GENERIC
   fi
}


# What kind of method was invoked?
case "$1" in
  start)        Weblogic_validate; 
		Weblogic_start;;
  stop)		Weblogic_stop;;
  status)	Weblogic_status;;
  monitor)	Weblogic_monitor;;

  meta-data)	meta_data; exit $OCF_SUCCESS;;
  validate-all)	Weblogic_validate; exit $OCF_SUCCESS;;

 *)		usage
		exit $OCF_ERR_UNIMPLEMENTED;;
esac
exit $?
