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

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

#History: 
#1. Date: 2021.8.24 Author: XuXiaojuan Modification:add Bes ocf script

#*************************************************
 
#######################################################################
# 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 Bes middleware 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="bes">
<version>1.0</version>

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

<parameters>

<parameter name="BES_HOME" unique="0" required="1">
<longdesc lang="en">
Directory containing ves
</longdesc>
<shortdesc lang="en">bes installdir</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="JAVA_HOME" unique="0" required="1">
<longdesc lang="en">
Home directory of Java.
</longdesc>
<shortdesc lang="en">Home directory of Java</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="port" unique="1" required="1">
<longdesc lang="en">
The Apusic Server Port.
</longdesc>
<shortdesc lang="en">The Bes Server Port</shortdesc>
<content type="string" default="6900" />
</parameter>

<parameter name="user" unique="1" required="1">
<longdesc lang="en">
The Bes admin name
</longdesc>
<shortdesc lang="en">The Bes admin name</shortdesc>
<content type="string" default="admin" />
</parameter>

<parameter name="password" unique="1" required="1">
<longdesc lang="en">
The Bes admin password
</longdesc>
<shortdesc lang="en">The Bes admin password</shortdesc>
<content type="string" default="Admin@1234" />
</parameter>

</parameters>

<actions>
<action name="start" timeout="120" />
<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
}

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

Bes_status() {
	pid_num_Bes=`ps -ef|grep java|grep -ic BES`
	if [ $pid_num_Bes -ge 1 ]; then
		#host_name=`hostname`
		#wget -O- -q -L http://$host_name:$OCF_RESKEY_port | tr '\012' ' ' | grep -Ei "html" >/dev/null 2>&1
		#if [ $? -eq 0 ];then
			ocf_log info "Bes is running in status"
			return $OCF_SUCCESS
		#else
		#	ocf_log debug "Bes is error"
		#	return $OCF_ERR_GENERIC
		#fi
	else
		ocf_log info "Bes server not running in status"
		return $OCF_NOT_RUNNING
	fi
}

Bes_monitor() {
    ocf_log info "Bes in monitor........"
    Bes_status
    rc=$?
    if [ ! $rc -eq 0 ]; then
        ocf_log err "Bes server not running in monitor"
        return $OCF_NOT_RUNNING
     else
        ocf_log info "Bes monitor succeeded"
        return $OCF_SUCCESS
     fi
}

Bes_start() {
    Bes_status
    if [ $? -eq 0 ]; then
	ocf_log info "Bes server already running"
	return $OCF_SUCCESS
    else
        sh $beshome/bin/startManagement >/opt/ha/var/log/bes.log 2>&1
        ret=$?
    fi	
    while true; do
	    sleep 5
	    Bes_status
	    if [ $? = $OCF_SUCCESS ]; then
	           break
	    fi
	    ocf_log info "start_bes: retry bes_staus"
    done
    ocf_log info "Bes has started";
    return $OCF_SUCCESS
    #if [ $ret -eq 0 ]; then
    #    Bes_status
    #   if [ $? -eq 0 ]; then
    #      ocf_log debug "Bes server start ok"
    #      return $OCF_SUCCESS
    #   else
    #      ocf_log debug "Bes server start failed"
    #      return $OCF_ERR_GENERIC
    #   fi
    #else
    #  ocf_log debug "Bes server start failed"
    #  return  $OCF_ERR_GENERIC
    #fi
 
    #fi	
}

Bes_stop() {
   Bes_status
   if [ $? -eq 7 ];then
        ocf_log info "Bes server already stopped"
        return $OCF_SUCCESS
   else
        sh $beshome/bin/stopManagement --user=$OCF_RESKEY_user --password=$OCF_RESKEY_password
   fi
   Bes_status
   if [ $? -eq 7 ]; then
        ocf_log info "Bes server stop ok"
        return $OCF_SUCCESS
   else
        ocf_log info "Bes server stop failed"
        return $OCF_ERR_GENERIC
   fi
}

export JAVA_HOME=$OCF_RESKEY_JAVA_HOME
# What kind of method was invoked?
case "$1" in
  start)        Bes_validate; 
		Bes_start;;
  stop)		Bes_stop;;
  status)	Bes_status;;
  monitor)	Bes_monitor;;

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

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