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

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

#History: 
#1. Date: 2020.8.31 Author: XuXiaojuan Modification:add Tas ocf script
#2. Date: 2021.9.13 Author: ZouZhimin  Modification:modify the desciption of Tas
#*************************************************
 
#######################################################################
# 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="Tas">
<version>1.0</version>

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

<parameters>

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

<parameter name="port" unique="1" required="1">
<longdesc lang="en">
The Tas Server Port.
</longdesc>
<shortdesc lang="en">The Tas Server Port</shortdesc>
<content type="string" default="8080" />
</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
}

#OCF_RESKEY_TAS_HOME="/usr/local/TAS2"
#OCF_RESKEY_port=8080

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

Tas_status() {
        pidnum=`ps -ef | grep  java | grep -c tas | grep -v grep`
	if [ $pidnum -gt 0 ];then
		host_name=`hostname`
                wget -O- -q -L http://$host_name:$OCF_RESKEY_port/tas-console  | tr '\012' ' ' | grep -Ei "html" >/dev/null 2>&1
                if [ $? -eq 0 ];then         
                   ocf_log debug "Tas server is running"
		   return $OCF_SUCCESS
                else 
                   ocf_log debug "Tas server is error"
                   return $OCF_ERR_GENERIC
                fi
        else
	    ocf_log debug "Tas server not running"
            return $OCF_NOT_RUNNING
        fi
}

Tas_monitor() {
    Tas_status
    rc=$?
    if [ ! $rc -eq 0 ]; then
        pid=`ps -ef|grep -v grep|grep tas|awk '{print $2}'`
        for pid_kill in "$pid"
          do
             kill -9 $pid_kill
          done
        ocf_log err "Tas server not running"
        return $OCF_NOT_RUNNING
    else
        ocf_log info "Tas monitor succeeded"
        return $OCF_SUCCESS
        return $OCF_ERR_GENERIC
    fi
}

Tas_start() {
    Tas_status
    if [ $? -eq 0 ]; then
	ocf_log info "Tas server already running"
	return $OCF_SUCCESS
    else
        sh $OCF_RESKEY_TAS_HOME/bin/StartTAS.sh
        ret=$?
        #sleep $OCF_RESKEY_sleeptime
    if [ $ret -eq 0 ]; then
    	Tas_status
        if [ $? -eq 0 ]; then
          ocf_log debug "Tas server start ok"
          return $OCF_SUCCESS
       else
          ocf_log debug "Tas server start failed"
          return $OCF_ERR_GENERIC
       fi
    else
      ocf_log debug "Tas server start failed"
      return  $OCF_ERR_GENERIC
    fi
 
    fi	
}

Tas_stop() {
   Tas_status
   if [ $? -eq 7 ];then
        ocf_log debug "Tas server already stopped"
        return $OCF_SUCCESS
   else
        sh $OCF_RESKEY_TAS_HOME/bin/StopTAS.sh
   fi
   Tas_status
   if [ $? -eq 7 ]; then
        ocf_log debug "Tas server stop ok"
        return $OCF_SUCCESS
   else
        ocf_log debug "Tas server stop failed"
        return $OCF_ERR_GENERIC
   fi
}

# What kind of method was invoked?
case "$1" in
  start)        Tas_validate; 
		Tas_start;;
  stop)		Tas_stop;;
  status)	Tas_status;;
  monitor)	Tas_monitor;;

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

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