#! /bin/ksh
#
# @(#)sybase_ccd_toggles.shs	1.7 00/01/05 SMI
# Copyright 07/01/97 Sun Microsystems, Inc.  All Rights Reserved.
#
#  toggle the on/off of HASYBASE ccd entry
#



# ############# Main ########################## Main ##########################
USAGE_PARMS=0
#	Copyright 10/16/98 Sun Microsystems, Inc.  All Rights Reserved.
#
# @(#)sybase_boiler	1.28 98/10/16 SMI

# begin of common ha-dbms boilerplate

# remember our name
argv0=`basename $0`

# Get the BASEDIR and PRODUCTDIR settings from the installed pkgs
_basedir=`pkgparam SUNWscsyb BASEDIR 2>/dev/null`
_productdir=`pkgparam SUNWscsyb PRODUCTDIR 2>/dev/null`
_basedir=${_basedir:=""}
_productdir=${_productdir:="SUNWcluster"}

LOGGER=/usr/bin/logger

PATH=${_basedir}/opt/${_productdir}/bin:/opt/SUNWcluster/bin:${_basedir}/opt/${_productdir}/ha/sybase:${PATH}
export PATH
HA_FILES=/etc/opt/SUNWscsyb; export HA_FILES
HA_VAR=/var/opt/SUNWscsyb; export HA_VAR

# include HA utilities library
. dbms_utilities
if [ $? -ne 0 ]; then
	$LOGGER -p local7.err -t "$HA_SLOGTAG" "$argv0: Cannot find HA utilities library"
	exit 1
fi

HA_CLUSTER=$CLUSTNAME; export HA_CLUSTER

# some default files and locations
# don't move this up - it depends on HA_FILES, which gets set above
HA_DATABASES=hasybase_databases
HA_DB_SUPPORT=${HA_FILES}/hasybase_support
ds="sybase"

PREFIX="SUNWcluster.ha.${ds}"
HA_FM_NAME="fault_mon"

# verify that we have the hasybase_support file
if [ ! -r $HA_DB_SUPPORT ] ; then
  logerr "4000" \
	"file ${HA_DB_SUPPORT} does not exist or is not readable!"
  exit 2
fi


# talk_start ... - echo the args, run them, and evaluate return value
talk_start() {
  	logprint "$$: $*"
  	if $* ; then
    		return 0
  	else
    		logprint "$$: $* ... failed"
    		return 1
  	fi
}


# talk_start_bg ... - print the args, then run them in the background
talk_start_bg() {
  	logprint "$$: $*"
  	$* &
  	}


# get_pid [ -u userid ] pattern - find the process id of a running program
# if called with "-u userid", only process for that user will be considered
get_pid() {
	if [ "$1" = "-u" ] ; then
		ps_args="-f -u $2"
		shift
		shift
	else
		ps_args="-ef"
	fi
	PID=`/usr/bin/ps $ps_args | grep "\<$*\>" | nawk -v pat="$*" '
 BEGIN { 
   search_pat = substr(pat, 0, 79)
   }
 { if ( match($0, search_pat) )
     if ( ! match($0, "nawk -v pat=") )
    	print $2
  }
'`
}

	
# kill_proc pattern - find and terminate a process
kill_proc() {
  	logprint "Killing $*..." 
  	get_pid $*
  	if [ "$PID" ] ; then
    		for p in ${PID} ; do
      			logprint "Killing process id $p"
      			kill -KILL $p
    		done
  	else
    		logprint "process \"$*\" could not be located..." 
  	fi
}

#get_server_pid <server name>  - check that the SQL dataserver is running or 
#                      not on the system
get_server_pid () {

servername="RUN_$*"
SPID=`/usr/bin/ps -fe | grep "\<$servername\>$" | 
			grep -v "grep" | nawk -v pat="$servername" '
 BEGIN { 
   search_pat = substr(pat,0,79)
   }
 { if ( match($0, search_pat) )
     if ( ! match($0, "nawk -v pat=") )
       		print $2
  }
'`
}

# get_pgid pattern - get the process group id of a running server
get_pgid() {

PGID=`/usr/bin/ps -o pgid -p $1 | grep -v "PGID" | nawk '{print $1}'`

}

# kill_server - kill server processes if it cannot be shutdown normally
kill_server() {

	get_server_pid $*
	if [ "${SPID}" != "" ]; then

		for gpid in $SPID ; do
			get_pgid $gpid
				if [ "${PGID}" != "" ]; then
					kill -KILL -${PGID}
 
				fi
		done
	fi

}

 

# read_ha_databases server - find an entry in the HA_DATABASES file,
# 	parse it apart, and return in variables. Assumes that sybase_home
#	is set!
read_ha_databases() {
        set_inst_name $1
	conf_line=$(get_instance "${ds}" "$1")
	if [ "$conf_line" = "" ] ; then
		logerr "4010" "$1 missing from HA SYBASE CCD!"
		return 1
	fi

        on_off_mode=`echo "$conf_line" | cut -s -f 1`
# The following 'server_name' is and extra line for sybase
	server_name=`echo "$conf_line" | cut -s -f 2`
	logical_host=`echo "$conf_line" | cut -s -f 3`
	poll_cycle=`echo "$conf_line" | cut -s -f 4`
	connect_cycle=`echo "$conf_line" | cut -s -f 5`
	timeout=`echo "$conf_line" | cut -s -f 6`
	restart_delay=`echo "$conf_line" | cut -s -f 7`
	db_login=`echo "$conf_line" | cut -s -f 8`
	runserver_file=`echo "$conf_line" | cut -s -f 9`
	if [ "`echo $conf_line | wc -w`" -eq 11 ] ; then
		backup_server=`echo "$conf_line" | cut -s -f 10`
		runbackup_file=`echo "$conf_line" | cut -s -f 11`
	fi

        set_logical_host $logical_host
	return 0
}

# find_sybase server - finds and sets various Sybase related variables:
#	SYBASE ISQL RUN_FILE
# in case of a problem, it returns with an empty SYBASE variable
find_sybase() {
	typeset my_node
	typeset physical_hosts

  	SYBTAB=/var/opt/sybase/sybtab

  	SYBASE=""
	ISQL=""
        read_ha_databases $1
        if [ $? -ne 0 ]; then
                return 1
        fi
        my_node=`uname -n`
        physical_hosts=$(haget -f physical_hosts -h $logical_host | tr '\012' ' ')
        is_member "$my_node" "$physical_hosts"
        if [ $? -ne 0 ] ; then
                return 1
        fi

	if sybtab_line=`grep "^[	 ]*$1:" $SYBTAB` ; then
		sybase_home=`echo $sybtab_line | awk -F: '{print $2}' -`
		if [ -d $sybase_home ]; then
			if [ -x ${sybase_home}/bin/isql ] ; then
				SYBASE="$sybase_home"
				ISQL="${SYBASE}/bin/isql"
				RUN_FILE="$runserver_file"
			else
				logerr "4030" \
				"No isql executable in ${sybase_home}/bin!"
				return 1
			fi
		fi
	else
		logerr "4040" "Database '$1' not found in ${SYBTAB}"
		return 1	
	fi
    	export SYBASE ISQL RUN_FILE
	return 0

}


make_rpc_call() {
  	logprint "Calling $3 for server $2 on host $1..."
  	ha_dbms_call $1 $2 $3 
  	}

get_remote_host(){
	get_diskgroups $1 $2
	RSHHOST=""
	for X in `haget  -f private_links -h $HA_REMOTEHOST` ; do
	  net_pinghost $X > /dev/null 2>&1
	  if [ $? -eq 0 ] ; then
		RSHHOST=$X
		export RSHHOST
		return 0
	  fi
	done
	return 1
	}

#end of common ha-dbms boiler plate
#include_boiler

# abort 
abort() {
        typeset prefix

        prefix="${PREFIX}.$1"
        shift
        log_error $prefix "$*"
        exit 1

}

usage() {
	echo "usage: $arg0 instance status"
	exit 2
}

if [ $# -lt 1 ] ; then
	usage
fi
	BASEFILE="CCD"
	prog="${HA_FM_NAME}"
        # find the instance
        line=$(get_instance_dynamic "${ds}" "${1}")
        if [ "$line" = "" ] ; then
                abort "${prog}.4005" \
		"no entry for server $1 in $BASEFILE - can't $2"
        fi
        mode=`echo "$line" | cut -s -f 1`
        if [ "$2" = "start" ] ; then
                if [ "$mode" != "off" ] ; then
			exit 0
#                        abort "${prog}.4015" \
#			"server $1 is not stopped in $BASEFILE!"
                fi
        else
                if [ "$mode" != "on" ] ; then
			exit 0
#                        abort "${prog}.4025" \
#			"server $1 is not started in $BASEFILE!"
                fi
        fi

        # update the entry in the file - find the old entry, and reply
        # off with on and vice versa
        if [ "$2" = "start" ] ; then
                new_line=`echo "$line" | sed 's/^off/on/'`
        else    
                new_line=`echo "$line" | sed 's/^on/off/'`
        fi
	remove "${ds}" server $1
        add_unique_ha_instance "${ds}" "$new_line"
        if [ $? -ne 0 ] ; then
                abort "${prog}.4035" "HA-SYBASE CCD  could not be modified!"
        fi

exit 0

