#!/bin/sh
#
#
# ids
#
# Description:
#
# OCF resource agent that manages an 
# IBM Informix Dynamic Server (IDS) instance 
# as an High-Availability resource.
####
#
# Author:			Lars D. Forseth, <lars.forseth@de.ibm.com> or <lars@forseth.de>
# Created:			May 25th 2007
# Last Modified:	July 30th 2007
# Support:			users@clusterlabs.org
# License:			GNU General Public License (GPL), Version 2 or later
# Copyright:		(c) 2002 - 2007 International Business Machines, Inc.
#
# This code is inspired by the db2 OCF resource agent 
# written by Alan Robertson, <alanr@unix.sh>
####
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
####
#
# Example usage as it would appear in /etc/ha.d/haresources:
#		node1 192.168.0.1 ids::/informix::ids1::onconfig.ids1
#
#
# --> Note that passing dbname and sqltestquery in heartbeat version 1 style is not supported!
#
# See usage() function below for more details...
####
#
# OCF instance parameters:
#   OCF_RESKEY_informixdir
#	OCF_RESKEY_informixserver 
#	OCF_RESKEY_onconfig
#	OCF_RESKEY_dbname
#	OCF_RESKEY_sqltestquery
####


#
# Include general OCF functions and variables (such as OCF return codes).
#
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

# Parameter defaults

OCF_RESKEY_informixdir_default=""
OCF_RESKEY_informixserver_default=""
OCF_RESKEY_onconfig_default=""
OCF_RESKEY_dbname_default="sysmaster"
OCF_RESKEY_sqltestquery_default="SELECT COUNT(*) FROM systables;"

: ${OCF_RESKEY_informixdir=${OCF_RESKEY_informixdir_default}}
: ${OCF_RESKEY_informixserver=${OCF_RESKEY_informixserver_default}}
: ${OCF_RESKEY_onconfig=${OCF_RESKEY_onconfig_default}}
: ${OCF_RESKEY_dbname=${OCF_RESKEY_dbname_default}}
: ${OCF_RESKEY_sqltestquery=${OCF_RESKEY_sqltestquery_default}}

#
# Function that displays the usage of this script.
#
ids_usage() {
	methods=`ids_methods`
	methods=`echo $methods | tr ' ' '|'`

	echo "
	usage: $0 ($methods)

	$0 manages an IBM Informix Dynamic Server (IDS) instance as an High-Availability 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
	The 'methods' operation lists the methods $0 supports
	The 'usage' operation displays this text
	The 'meta-data' operation returns the meta-data (in XML) of this resource script
    "
}


#
# Function that displays the possible methods this script supports.
#
ids_methods() {
	echo " 
	start
	stop
	status
	monitor
	validate-all
	methods
	usage
	meta-data
	"
}


#
# Function that displays the meta-data of this OCF resource agent.
#
ids_meta_data() {
    cat <<-!
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="ids">
<version>1.0</version>


<longdesc lang="en">
OCF resource agent to manage an IBM Informix Dynamic Server (IDS) instance as an High-Availability resource.
</longdesc>
<shortdesc lang="en">Manages an Informix Dynamic Server (IDS) instance</shortdesc>


<parameters>

<parameter name="informixdir" required="0">
<longdesc lang="en">
The value the environment variable INFORMIXDIR has after a typical installation of IDS.
Or in other words: the path (without trailing '/') where IDS was installed to.
If this parameter is unspecified the script will try to get the value from the shell environment.
</longdesc>
<shortdesc lang="en">
INFORMIXDIR environment variable
</shortdesc>
<content type="string" default="${OCF_RESKEY_informixdir_default}" />
</parameter>

<parameter name="informixserver" required="0">
<longdesc lang="en">
The value the environment variable INFORMIXSERVER has after a typical installation of IDS.
Or in other words: the name of the IDS server instance to manage.
If this parameter is unspecified the script will try to get the value from the shell environment.
</longdesc>
<shortdesc lang="en">
INFORMIXSERVER environment variable
</shortdesc>
<content type="string" default="${OCF_RESKEY_informixserver_default}" />
</parameter>

<parameter name="onconfig" required="0">
<longdesc lang="en">
The value the environment variable ONCONFIG has after a typical installation of IDS.
Or in other words: the name of the configuration file for the IDS instance specified in INFORMIXSERVER. 
The specified configuration file will be searched at '$INFORMIXDIR/etc/$ONCONFIG'.
If this parameter is unspecified the script will try to get the value from the shell environment.
</longdesc>
<shortdesc lang="en">
ONCONFIG environment variable
</shortdesc>
<content type="string" default="${OCF_RESKEY_onconfig_default}" />
</parameter>

<parameter name="dbname" required="0">
<longdesc lang="en">
This parameter defines which database to use in order to monitor the IDS instance.
If this parameter is unspecified the script will use the 'sysmaster' database as a default.
</longdesc>
<shortdesc lang="en">
database to use for monitoring, defaults to 'sysmaster'
</shortdesc>
<content type="string" default="${OCF_RESKEY_dbname_default}" />
</parameter>

<parameter name="sqltestquery" required="0">
<longdesc lang="en">
SQL test query to run on the database specified by the parameter 'dbname' 
in order to monitor the IDS instance and determine if it's functional or not.
If this parameter is unspecified the script will use 'SELECT COUNT(*) FROM systables;' as a default.
</longdesc>
<shortdesc lang="en">
SQL test query to use for monitoring, defaults to 'SELECT COUNT(*) FROM systables;'
</shortdesc>
<content type="string" default="${OCF_RESKEY_sqltestquery_default}" />
</parameter>

</parameters>


<actions>
<action name="start" timeout="120s" />
<action name="stop" timeout="120s" />
<action name="status" timeout="60s" />
<action name="monitor" depth="0" timeout="30s" interval="10s" />
<action name="validate-all" timeout="5s" />
<action name="meta-data" timeout="5s" />
<action name="methods" timeout="5s" />
<action name="usage" timeout="5s" />
</actions>


</resource-agent>
!
}


#
# Function that either forwards log messages to the ocf_log function
# provided by heartbeat or simply prints them to standard out via echo.
# This is determined by setting the variable "idslogger" to "echo" or "ocf".
# The default for "idslogger" is "ocf".
#
ids_log() {

	# Where should the passed log messages be passed to,
	# to the standard output via the echo command ("echo")
	# or to the ocf_log function provided by heartbeat ("ocf") ?
	# Default is "ocf".
	idslogger="ocf"

	# When the variable "idsdebug" is not set to "true"
	# this function (ids_log) will not print any info message
	# that has been forwarded to it!
	# This is done in order to spare if-statements within the 
	# other functions in this script and to centralize the decision
	# whether to have a chatty resource script or not... ;)
	# Nevertheless, error messages will always be printed!
	idsdebug=false

	# Only continue if the two expected parameters 
	# are not empty and "idsdebug" is set to "true"
	# or the message is of type "error".
	if [ $# -eq 2 -a -n "$1" -a -n "$2" ]; then
		if [ "$idsdebug" = "true" -o "$1" = "error" ]; then
			case $idslogger in
				# Print messages to stdout via echo command.
				echo)
					echo "`date +'%b %d %H:%M:%S'`: [$1] $2";;
				# Pass messages to ocf_log function.
				ocf|*)
					ocf_log "$1" "$2";;
			esac
		fi
	fi		
}


#
# Function that prints the current values of important environment variables
# needed by the script and the IDS instance itself. The just mentioned variables are:
# - INFORMIXDIR
# - INFORMIXSERVER
# - ONCONFIG
# - PATH
# - LD_LIBRARY_PATH
#
ids_debug() {
	ids_log info "called ids_debug"	

	ids_log info "INFORMIXDIR=$INFORMIXDIR"
	ids_log info "OCF_RESKEY_informixdir=$OCF_RESKEY_informixdir"
	
	ids_log info "INFORMIXSERVER=$INFORMIXSERVER"
	ids_log info "OCF_RESKEY_informixserver=$OCF_RESKEY_informixserver"
	
	ids_log info "ONCONFIG=$ONCONFIG"
	ids_log info "OCF_RESKEY_onconfig=$OCF_RESKEY_onconfig"
	
	ids_log info "PATH=$PATH"
	ids_log info "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
	
	ids_log info "dbname=$OCF_RESKEY_dbname"
	ids_log info "sqltestquery=$OCF_RESKEY_sqltestquery"

	ids_log info "this script is run as user: `id`"
	ids_log info "...in the current working directory: `pwd`"
}


#
# Function that validates if the passed parameters are valid and sets them if valid. 
# If the first three parameters have not been passed, 
# this function checks whether they have been already set in the parent's shell environment.
# The variables that are checked and set (only the capitalized ones are set) are:
# - INFORMIXDIR
# - INFORMIXSERVER
# - ONCONFIG
# - PATH
# - LD_LIBRARY_PATH
# - dbname
# - sqltestquery
#
ids_validate() {
	
	ids_log info "called ids_validate"
	rc=$OCF_SUCCESS

	# Check if INFORMIX, INFORMIXSERVER and ONCONFIG 
	# have been passed or set and validate them.
	
	# OCF vars not passed, vars empty - set and export them to the shell environment.
	if [ -n "$OCF_RESKEY_informixdir" -a -n "$OCF_RESKEY_informixserver" -a -n "$OCF_RESKEY_onconfig" ]; then
		ids_log info "ids_validate: passed vars not empty"
		
		INFORMIXDIR=$OCF_RESKEY_informixdir
		export INFORMIXDIR

		INFORM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         