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

#Copyright (c) KylinSoft Co., Ltd. [2021-2025]. All rights reserved.
#File name: oscar7
#Author: ZouZhimin
#Version: 1
#Description: Manages oscar databas as Linux-HA resource 
#Date: 2021.12.13
#Others: For oscar version 7
#Function List: 
#1.oscar_status(): report whether the database is running
#2.oscar_monitor():report whether the database seems to be working
#3.oscar_start():start the database
#4.oscar_stop():stop the database

#History: 
#1. Date: 2021.12.13 Author: ZouZhimin Modification:add oscar7 ocf script 

#*************************************************
 
#######################################################################
# Initialization:

#. ${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
#######################################################################

isql_command=`find $OCF_RESKEY_OSCAR_HOME -name "isql"`
export PATH="/opt/ShenTong/bin:${PATH}"
export LD_LIBRARY_PATH="/opt/ShenTong/bin:${LD_LIBRARY_PATH}"

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="oscar">
<version>1.0</version>

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

<parameters>

<parameter name="OSCAR_HOME" unique="0" required="1">
<longdesc lang="en">
Directory containing oscard
</longdesc>
<shortdesc lang="en">oscard installdir</shortdesc>
<content type="string" default="/opt/ShenTong" />
</parameter>

<parameter name="port" unique="0" required="1">
<longdesc lang="en">
oscard port
</longdesc>
<shortdesc lang="en">oscard port</shortdesc>
<content type="integer" default="2003" />
</parameter>

<parameter name="instance" unique="0" required="1">
<longdesc lang="en">
oscard instance
</longdesc>
<shortdesc lang="en">oscard instance</shortdesc>
<content type="string" default="OSRDB" />
</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
}

oscar_validate() {
# checking the parameters
    if [ ! -f $OCF_RESKEY_OSCAR_HOME/bin/oscar -o ! -x $OCF_RESKEY_OSCAR_HOME/bin/oscar ]; then
	exit $OCF_ERR_ARGS;
    fi
}

oscar_status() {
	pid_num_oscar=`ps cax -o pid,command | egrep -w "oscar" | grep -c oscar`
	pid_num_oscaragent=`ps cax -o pid,command |egrep -w "oscaragent" | grep -c oscaragent`
	
        if [ $pid_num_oscar -ge 1 -a $pid_num_oscaragent -ge 1 ]; then
             ocf_log debug "oscar server is running"
             return $OCF_SUCCESS
        else 
             ocf_log debug "oscar server is not runnning"
             return $OCF_NOT_RUNNING
        fi
}
oscar_monitor() {
    oscar_status
    rc=$?
    if [ ! $rc -eq 0 ]; then
        ocf_log err "oscar server not running"
        return $OCF_NOT_RUNNING
     else
	return $OCF_SUCCESS
    fi
}

oscar_start() {
    oscar_status
    if [ $? -eq 0 ]; then
	ocf_log info "oscar server already running"
	return $OCF_SUCCESS
    else
	export SZ_OSCAR_HOME=$OCF_RESKEY_OSCAR_HOME
        $OCF_RESKEY_OSCAR_HOME/admin/oscaragentd start >/var/log/oscaragent.log 2>/var/log/oscaragent.log &
        ret=$?
        sleep 3
	pid_num_oscaragent=`ps cax -o pid,command |egrep -w "oscaragent" | grep -c oscaragent`
    if [ $ret -eq 0 -a $pid_num_oscaragent -ge 1 ]; then
	echo $OCF_RESKEY_OSCAR_HOME >/var/log/oscar.log
	echo  $OCF_RESKEY_instance >>/var/log/oscar.log
        $OCF_RESKEY_OSCAR_HOME/admin/oscard start $OCF_RESKEY_instance >>/var/log/oscar.log 2>&1  &
        ret=$?
        sleep 10
	pid_num_oscar=`ps cax -o pid,command | egrep -w "oscar" | grep -c oscar`
        
       if [ $ret -eq 0 -a $pid_num_oscar -ge 1 ]; then
          ocf_log debug "oscar server start ok"
          echo "oscar server start ok"
          return $OCF_SUCCESS
       else
          ocf_log debug "oscar server start failed"
          echo "oscar server start failed"
          return $OCF_ERR_GENERIC
       fi
    else
      ocf_log debug "oscar agent start failed"
      echo "oscar agent start failed"
      return  $OCF_ERR_GENERIC
    fi
 
    fi	
}

oscar_stop() {
   oscar_status
   if [ $? -eq 7 ];then
        ocf_log debug "oscar server already stopped"
        return $OCF_SUCCESS
   else
        pid=`ps -ef|grep -v grep| grep "bin/oscar"|awk '{print $2}'`
        for pid_kill in $pid
          do
             kill -9 $pid_kill
          done
   fi
   oscar_status
   if [ $? -eq 7 ]; then
        ocf_log debug "oscar server stop ok"
        echo "oscar server stop ok"
        return $OCF_SUCCESS
   else
        ocf_log debug "oscar server stop failed"
        return $OCF_ERR_GENERIC
   fi
}

# What kind of method was invoked?
case "$1" in
  start)        oscar_validate; 
		oscar_start;;
  stop)		oscar_stop;;
  status)	oscar_status;;
  monitor)	oscar_monitor;;

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

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