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

#Copyright (c) KylinSoft Co., Ltd. [2021-2025]. All rights reserved.
#File name: influxdb
#Author: ZouZhimin
#Version: 1
#Description: Manages influxdb database as Linux-HA resource
#Date: 2023.12.19
#Others: For influxdb version: 1.8.10 
#Function List: 
#1.influxdb_status(): report whether the database is running
#2.influxdb_monitor():report whether the database seems to be working
#3.influxdb_start():start the database
#4.influxdb_stop():stop the database

#History: 
#1. Date: 2023.12.19 Author: ZouZhimin Modification:add influxdb ocf script 

#*************************************************
 
# OCF instance parameters:
# OCF_RESKEY_InfluxDB_HOME=/home/influxdb
#######################################################################
# 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
#######################################################################

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

	$0 manages a influxdb 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
}

OCF_RESKEY_influxd_path_default="/usr/lib/influxdb/scripts/influxd-systemd-start.sh"
OCF_RESKEY_configfile_path_default="/etc/influxdb/influxdb.conf"

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

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

<parameters>
<parameter name="influxd_path" unique="0" required="1">
<longdesc lang="en">
influxdb script influxd-systemd-start.sh path, or binary influxd path 
</longdesc>
<shortdesc lang="en">influxdb installdir</shortdesc>
<content type="string" default="${OCF_RESKEY_influxd_path_default}" />
</parameter>

<parameter name="configfile_path" unique="0" required="1">
<longdesc lang="en">
influxdb configfile path
</longdesc>
<shortdesc lang="en">influxdb conf path</shortdesc>
<content type="string" default="${OCF_RESKEY_configfile_path_default}" />
</parameter>
</parameters>

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

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

influxdb_status() {
	pid_num_influxdb=`ps ax | egrep -w influxd  | grep -v grep | grep -c influxd`
	if [ $pid_num_influxdb -ge 1 ]; then
	   return $OCF_SUCCESS
   else
	   ocf_log debug "influxdb server is not running in status"
         return $OCF_NOT_RUNNING
   fi
}

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

influxdb_start() {
   ocf_log info "in start"
   influxdb_status
   if [ $? -eq 0 ]; then
	   ocf_log info "influxdb server already running"
	   return $OCF_SUCCESS
   else
      pattern="influxd$"
      if [[ "$OCF_RESKEY_influxd_path" =~ $pattern ]]; then
         $OCF_RESKEY_influxd_path -config $OCF_RESKEY_configfile_path > /dev/null 2>&1 &
      else
         sh $OCF_RESKEY_influxd_path >/dev/null 2>&1  &
      fi
      
      ret=$?
      if [ $ret -eq 0 ]; then
         influxdb_status
         if [ $? -eq 0 ]; then
            ocf_log debug "influxdb server start ok"
            return $OCF_SUCCESS
         else
            ocf_log err "influxdb server start failed"
            return $OCF_ERR_GENERIC
         fi
      else
         ocf_log debug "influxdb server start failed"
         return  $OCF_ERR_GENERIC
      fi
   fi	
}

influxdb_stop() {
   influxdb_status
   if [ $? -eq 7 ];then
      ocf_log debug "influxdb server already stopped"
      return $OCF_SUCCESS
   else
      killall influxd
   fi
   sleep 1
   influxdb_status
   if [ $? -eq 7 ]; then
      ocf_log debug "influxdb server stop ok"
      return $OCF_SUCCESS
   else
      ocf_log debug "influxdb server stop failed"
      return $OCF_ERR_GENERIC
   fi
}

case "$1" in
   start)         influxdb_validate; 
		  influxdb_start;;
   stop)	  influxdb_stop;;
   status)        influxdb_status;;
   monitor)	  influxdb_monitor;;
   meta-data)	  meta_data; exit $OCF_SUCCESS;;
   validate-all)  influxdb_validate; exit $OCF_SUCCESS;;

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