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

#Copyright (c) KylinSoft Co., Ltd. [2021-2025]. All rights reserved.
#File name: DMDB8
#Author: LiangXin
#Version: 1
#Description: Manages DM database  as Linux-HA resource 
#Date: 2020.8.31
#Others: 
#Function List: 
#1.dmdb_status(): report whether the database is running
#2.dmdb_monitor():report whether the database seems to be working
#3.dmdb_start():start the database
#4.dmdb_stop():stop the database

#History: 
#1. Date: 2020.8.31 Author: LiangXin Modification:add DMDB8 ocf script 

#*************************************************

#######################################################################
# Initialization:
#OCF_ROOT=/usr/lib/ocf
#. ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs
#. /opt/NeoShineHA/lib/ocf/resource.d/heartbeat/.ocf-shellfuncs

# 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 DM Database as an HA resource.

	The 'start' operation starts the database.
	The 'stop' operation stops the database.
	The 'status' operation reports whether the database is running
	The 'monitor' operation reports whether the database 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="DMDB">
<version>1.0</version>

<longdesc lang="en">
Resource script for DMDB. 
It manages a DM Database instance as an HA resource.
</longdesc>
<shortdesc lang="en">DMDB resource agent</shortdesc>

<parameters>

<parameter name="datadir" unique="0" required="0">
<longdesc lang="en">
Directory containing databases
</longdesc>
<shortdesc lang="en">DMDB datadir</shortdesc>
<content type="string" default="/opt/dmdbms/" />
</parameter>

<parameter name="instancedir" unique="1" required="0">
<longdesc lang="en">
The DB databases instance path
</longdesc>
<shortdesc lang="en">DMDB instance dir</shortdesc>
<content type="string" default="/opt/dmdbms/data/DAMENG" />
</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
}

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$OCF_RESKEY_datadir/bin"
export DM_HOME="$OCF_RESKEY_datadir"

dmdb_validate() {
# checking the parameters
    	if [ ! -d $OCF_RESKEY_datadir ]; then
		ocf_log err "Datadir $OCF_RESKEY_datadir dosen't exist";
		exit $OCF_ERR_GENERIC;
    	fi
}

dmdb_status() {
	pid=`ps cax -o pid,command | egrep -w "dmserver" | grep -c dmserver`;
	if [ $pid -ge 1 ]; then
                ocf_log debug "DM database server is running"
		return $OCF_SUCCESS;
	else	
		ocf_log debug "DM database server is not running"
		return $OCF_NOT_RUNNING;
	fi
}

dmdb_monitor() {
	dmdb_status
        rc=$?
        if [ ! $rc -eq 0 ]; then
        	ocf_log err "DMDatabase is not running";
        	return $OCF_NOT_RUNNING;
     	fi
 
        if [ ! -f $OCF_RESKEY_datadir/bin/dmserver ];then
       		ocf_log err "DMDB monitor failed:";
        	return $OCF_ERR_GENERIC;
        fi

	return $OCF_SUCCESS
}

dmdb_start() {
    	dmdb_status

    	if [ $? == 0 ]; then
		ocf_log info "DM database server already running"
		return $OCF_SUCCESS;
    	fi

    	$OCF_RESKEY_datadir/bin/dmserver $OCF_RESKEY_instancedir/dm.ini  -noconsole &
    	ret=$?
    	if [ $ret -eq 0 ]; then
       		sleep 3
       		pid=`ps cax -o pid,command | egrep -w "dmserver" | grep -c dmserver`
       		touch /var/run/dmserver.pid
       		pidnum=`ps cax -o pid,command | egrep -w "dmserver" | cut -c 1-5`
       		echo $pidnum > /var/run/dmserver.pid
    
       		if [ $pid -ge 1 ]; then
          		ocf_log debug "DM database server start ok"
          		return $OCF_SUCCESS;
       		else
          		ocf_log debug "DM database server start failed"
          		return $OCF_ERR_GENERIC;
       		fi
    	else
      		ocf_log debug "DM database server start failed"
      		return  $OCF_ERR_GENERIC;
    	fi
}

dmdb_stop() {
 	pid=`ps cax -o pid,command | egrep -w "dmserver" | grep -c dmserver`
 	if [ $pid -ge 1 ]; then
   		killall dmserver > /dev/null 2>&1
   		ret=$?
   		sleep 3
   		pid=`ps cax -o pid,command | egrep -w "dmserver" | grep -c dmserver`
   		if [ $pid -ge 1 ];then
      			killall -9 dmserver > /dev/null 2>&1
      			ret=$?
   		fi
   		if [ $ret -eq 0 ]; then
     			ocf_log debug "DM database server stop ok"
     			rm -f /var/run/dmserver.pid 
     			return $OCF_SUCCESS;
   		else
     			ocf_log debug "DM database server stop failed"
     			return $OCF_ERR_GENERIC;
   		fi
 	else
   		ocf_log debug "DM database server not running"
   		return $OCF_SUCCESS;
 	fi

}

# What kind of method was invoked?
case "$1" in
  	start)		dmdb_validate; 
			dmdb_start;;
  	stop)		dmdb_stop;;
  	status)		dmdb_status;;
  	monitor)	dmdb_monitor;;
  	meta-data)	meta_data; exit $OCF_SUCCESS;;
  	validate-all)	dmdb_validate; exit $OCF_SUCCESS;;
 	*)		usage
			exit $OCF_ERR_UNIMPLEMENTED;;
esac
exit $?
