Home | History | Annotate | Download | only in mys
      1 #
      2 # CDDL HEADER START
      3 #
      4 # The contents of this file are subject to the terms of the
      5 # Common Development and Distribution License (the License).
      6 # You may not use this file except in compliance with the License.
      7 #
      8 # You can obtain a copy of the license at usr/src/CDDL.txt
      9 # or http://www.opensolaris.org/os/licensing.
     10 # See the License for the specific language governing permissions
     11 # and limitations under the License.
     12 #
     13 # When distributing Covered Code, include this CDDL HEADER in each
     14 # file and include the License file at usr/src/CDDL.txt.
     15 # If applicable, add the following below this CDDL HEADER, with the
     16 # fields enclosed by brackets [] replaced with your own identifying
     17 # information: Portions Copyright [yyyy] [name of copyright owner]
     18 #
     19 # CDDL HEADER END
     20 #
     21 
     22 #
     23 # Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
     24 # Use is subject to license terms.
     25 #
     26 
     27 #ident	"@(#)functions_static.ksh	1.9	07/06/06 SMI"
     28 
     29 TASK_COMMAND=""
     30 
     31 ZONENAME=/usr/bin/zonename
     32 
     33 SCLOGGER=/usr/cluster/lib/sc/scds_syslog
     34 LOGGER=/usr/bin/logger
     35 SCHA_RESOURCE_SETSTATUS=/usr/cluster/bin/scha_resource_setstatus
     36 SCHA_RESOURCE_GET=/usr/cluster/bin/scha_resource_get
     37 SCHA_RESOURCEGROUP_GET=/usr/cluster/bin/scha_resourcegroup_get
     38 PMFADM=/usr/cluster/bin/pmfadm
     39 UNAME=/usr/bin/uname
     40 ECHO=/usr/bin/echo
     41 AWK=/usr/bin/awk
     42 EGREP=/usr/bin/egrep
     43 PROJECTS=/usr/bin/projects
     44 
     45 terminate()
     46 {
     47 
     48         debug_message "Function: terminate - Begin"
     49         ${SET_DEBUG}
     50 
     51 	exiting_func=${1}
     52 	exit_code=${2}
     53 
     54 	# determine the right return code, it is either the return code from the functions or
     55 	# the appropriate smf return code
     56 
     57 	if in_cluster
     58 	then
     59 		
     60 		# called in a clustered global zone
     61 		
     62 		debug_message "Method: ${MYNAME} ${exiting_func} - End (${exit_code})"
     63 		exit ${exit_code}
     64 		
     65 	else
     66 		if [ -n "${SMF_FMRI}" ]
     67 		then
     68 			if [ "${exit_code}" -ne 0 ]
     69 			then
     70 			
     71 				# honour the gds specific probe values like 100 or 201
     72 			
     73 				if [ "${exiting_func}" == "probe" -o "${exiting_func}" == "validate" ]
     74 				then
     75 					debug_message "Method: ${MYNAME} ${exiting_func} - End (${exit_code})"
     76 					exit ${exit_code}
     77 				else
     78 					debug_message "Method: ${MYNAME} ${exiting_func} - End (${SMF_EXIT_ERR_PERM})"
     79 					exit ${SMF_EXIT_ERR_PERM}
     80 				fi
     81 			fi
     82 			
     83 			debug_message "Method: ${MYNAME} ${exiting_func} - End (${SMF_EXIT_OK})"
     84 			exit ${SMF_EXIT_OK}
     85 		else
     86 			debug_message "Method: ${MYNAME} ${exiting_func} - End (${exit_code})"
     87 			exit ${exit_code}
     88 		fi
     89 	fi
     90 
     91         debug_message "Function: terminate - End"
     92 
     93 }
     94 syslog_tag()
     95 {
     96         #
     97         # Data Service message format
     98         #
     99 
    100         ${SET_DEBUG}
    101 
    102         print "SC[${PKG:-??}.${METHOD:-??}]:${RESOURCEGROUP:-??}:${RESOURCE:-??}"
    103 }
    104 
    105 scds_syslog()
    106 {
    107 
    108         #
    109         # Log a message
    110         #
    111 
    112         ${SET_DEBUG}
    113 
    114 	if [ -f ${SCLOGGER} ]
    115 	then
    116 		${SCLOGGER} "$@" &
    117 	else
    118 	
    119 		# eliminate te -m and honour -p and -t option
    120 		while getopts 'p:t:m' opt
    121 		do
    122 			case "${opt}" in
    123 				t) TAG=${OPTARG};;
    124 				p) PRI=${OPTARG};;
    125 			esac
    126 		done
    127 	
    128 		shift $((${OPTIND} - 1))
    129 		LOG_STRING=`/usr/bin/printf "$@"`
    130 	
    131 		${LOGGER} -p ${PRI} -t ${TAG} ${LOG_STRING}
    132 	fi
    133 
    134 }
    135 
    136 rgs_zonename()
    137 {
    138 
    139 # Determine if the host specified by uname -n is combined with a zonename in the 
    140 # current resourcegroups nodelist. The seperator between nodename and zonename is ":".
    141 #
    142 # This function assumes that the resource group name preset in the variable ${RESOURCEGROUP} and should be called 
    143 #
    144 # $(rgs_zonename) 
    145 #
    146 # It passes back the zonename or nothing. 
    147 
    148         debug_message "Function: rg_zonename - Begin "
    149         ${SET_DEBUG}
    150 
    151 	nodes_zone=
    152 	nodename=`${UNAME} -n`
    153         node=`${SCHA_RESOURCEGROUP_GET} -G ${RESOURCEGROUP} -O NODELIST|grep ${nodename}`
    154 
    155 	if ${ECHO} ${node} | grep : >/dev/null 2>&1
    156 	then
    157 		nodes_zone=`${ECHO} ${node} | ${AWK} -F: '{print $2}'`
    158 		
    159 	fi
    160 
    161 	print ${nodes_zone}
    162 
    163         debug_message "Function: rg_zonename - End "
    164 }
    165 
    166 debug_message()
    167 {
    168         #
    169         # Output a debug message to syslog if required
    170         #
    171 
    172 	if [ -n "${DEBUG}" ]
    173  	then
    174 
    175 		# determine if we should display a message and do it
    176 
    177 		if [ "${DEBUG}" = "ALL" ]
    178 		then
    179                 	SET_DEBUG="set -x"
    180 
    181                 	DEBUG_TEXT=${1}
    182 
    183                 	scds_syslog -p daemon.debug -t $(syslog_tag) -m \
    184                         "%s" "${DEBUG_TEXT}"
    185 		else
    186 
    187 			# check if the actual resource matches one of the list of resources
    188 			# if it matches, display a message
    189 
    190 			DEBUG=`echo ${DEBUG}|tr "," " "`
    191 			for i in ${DEBUG}
    192 			do
    193 				if [ "${i}" = "${RESOURCE}" ]
    194 				then
    195                 			SET_DEBUG="set -x"
    196 
    197                 			DEBUG_TEXT=${1}
    198 
    199                 			scds_syslog -p daemon.debug -t $(syslog_tag) -m \
    200                         		"%s" "${DEBUG_TEXT}"
    201 				fi
    202 			done
    203 		fi
    204         else
    205                 SET_DEBUG=
    206         fi
    207 }
    208 
    209 log_message()
    210 {
    211         #
    212         # Output a message to syslog as required
    213         #
    214 
    215         debug_message "Function: log_message - Begin"
    216         ${SET_DEBUG}
    217 
    218         if [ -s "${LOGFILE}" ]
    219         then
    220                 PRIORITY=${1}
    221                 HEADER=${2}
    222 
    223 		# 
    224 		# Ensure the while loop only reads a closed file
    225 		#
    226 
    227 		strings ${LOGFILE} > ${LOGFILE}.copy
    228                 while read MSG_TXT
    229                 do
    230                         scds_syslog -p daemon.${PRIORITY} -t $(syslog_tag) -m \
    231                                 "%s - %s" \
    232                                 "${HEADER}" "${MSG_TXT}"
    233                 done < ${LOGFILE}.copy
    234 
    235         fi
    236 
    237         debug_message "Function: log_message - End"
    238 }
    239 
    240 srm_function()
    241 {
    242         debug_message "Function: srm_function - Begin"
    243         ${SET_DEBUG}
    244 
    245         USER=${1}
    246 
    247         #
    248         # If Solaris 8 just return
    249         #
    250 
    251         if [ `/usr/bin/uname -r` = "5.8" ];
    252         then
    253                 return 0
    254         fi
    255 
    256         #
    257         # Retrieve RESOURCE_PROJECT_NAME
    258         #
    259 
    260 	if in_cluster
    261 	then
    262         	RESOURCE_PROJECT_NAME=`${SCHA_RESOURCE_GET} -R ${RESOURCE} -G ${RESOURCEGROUP} -O  RESOURCE_PROJECT_NAME`
    263 
    264         	#
    265         	# Retrieve RG_PROJECT_NAME if RESOURCE_PROJECT_NAME is not set
    266         	#
    267 
    268         	if [ -z "${RESOURCE_PROJECT_NAME}" ] || [ "${RESOURCE_PROJECT_NAME}" = "default" ];then
    269 
    270                 	RESOURCE_PROJECT_NAME=`${SCHA_RESOURCEGROUP_GET} -G ${RESOURCEGROUP} -O RG_PROJECT_NAME`
    271         	fi
    272 	else
    273 		RESOURCE_PROJECT_NAME=${ZONE_PROJECT}
    274 	fi
    275 
    276         #
    277         # Return if no projects are defined
    278         #
    279 
    280         if [ -z "${RESOURCE_PROJECT_NAME}" ] || [ "${RESOURCE_PROJECT_NAME}" = "default" ]; then
    281                 return 0
    282         fi
    283 
    284         #
    285         # Validate that $USER belongs to the project defined by
    286         # ${RESOURCE_PROJECT_NAME}
    287         #
    288 
    289         PROJ_MEMBER=`${PROJECTS} ${USER} | ${EGREP} "^${RESOURCE_PROJECT_NAME} | ${RESOURCE_PROJECT_NAME} | ${RESOURCE_PROJECT_NAME}$|^${RESOURCE_PROJECT_NAME}$"`
    290 
    291         if [ -z "${PROJ_MEMBER}" ];
    292         then
    293              scds_syslog -p daemon.err -t $(syslog_tag) -m \
    294                         "srm_function - The user %s does not belongs to project %s" \
    295                         "${USER}" "${RESOURCE_PROJECT_NAME}" 
    296                 return 1
    297         else
    298                 debug_message "srm_function - User ${USER} belongs to project ${RESOURCE_PROJECT_NAME}"
    299         fi
    300 
    301         #
    302         # Set TASK_COMMAND
    303         #
    304 
    305         TASK_COMMAND="/usr/bin/newtask -p ${RESOURCE_PROJECT_NAME}"
    306 
    307         debug_message "Function: srm_function - End"
    308 
    309         return 0
    310 }
    311 
    312 zone_function()
    313 {
    314 	debug_message "Function: zone_function - Begin"
    315 	${SET_DEBUG}
    316 
    317 	#
    318 	# Initialize PZONEOPT as empty
    319 	PZONEOPT=""
    320 
    321 	#
    322 	# If Solaris does not have /usr/bin/zonename just return 0
    323 	# else add "-z <zonename>" to PZONEOPT
    324 	#
    325 
    326 	if [ -x "${ZONENAME}" ];
    327 	then
    328 		PZONEOPT="-z `${ZONENAME}`"
    329 	fi
    330 
    331 	debug_message "Function: zone_function - End"
    332 	return 0
    333 }
    334 
    335 in_cluster()
    336 {
    337         #
    338         # determine if we are started in a clustered global zone
    339         #
    340 
    341         debug_message "Function: in_cluster - Begin"
    342         ${SET_DEBUG}
    343 	
    344 	in_cluster_val=0
    345 	
    346 	if [ ! -d /etc/cluster ]
    347 	then
    348 		in_cluster_val=1
    349 	fi	
    350 
    351         debug_message "Function: in_cluster - End"
    352 	
    353 	return ${in_cluster_val}
    354 }
    355 
    356 start_dependency()
    357 {
    358 	debug_message "Function: start_dependency - Begin"
    359 	${SET_DEBUG}
    360 
    361 	# Retrieve START_TIMEOUT
    362 
    363 	if [-z "${ZONE_START_TIMOUT}" ]
    364 	then
    365 		START_TIMEOUT=`standard_resource_get START_TIMEOUT`
    366 	else
    367 		START_TIMEOUT=${ZONE_START_TIMOUT} 
    368 	fi
    369 
    370 	# 80 % of the START-TIMEOUT can be spent on waiting
    371 
    372 	MAX_START_TIMEOUT=`expr ${START_TIMEOUT} \* 80 \/ 100`
    373 
    374 	# Get current time in sec on 24h base
    375 		
    376 	CUR_HOUR=`date '+%H'`
    377 	CUR_MIN=`date '+%M'`
    378 	CUR_SEC=`date '+%S'`
    379 	CUR_TIME=`expr ${CUR_HOUR} \* 3600 + ${CUR_MIN} \* 60 + ${CUR_SEC}`
    380 
    381 	# Run a test loop until the dependent resource is up or 
    382 	# a timeout has occured
    383 
    384 	while [ 1 -eq 1 ]
    385 	do
    386 
    387 		# Get new current timeout
    388 		NEW_HOUR=`date '+%H'`
    389 		NEW_MIN=`date '+%M'`
    390 		NEW_SEC=`date '+%S'`
    391 		NEW_TIME=`expr ${NEW_HOUR} \* 3600 + ${NEW_MIN} \* 60 + ${NEW_SEC}`
    392 
    393 		# Have we exeeded timeout
    394 
    395 		s1=`expr ${CUR_TIME} + ${MAX_START_TIMEOUT}`
    396 
    397 		if [ ${s1} -le ${NEW_TIME} ]; then
    398 			scds_syslog -p daemon.err -t $(syslog_tag) -m \
    399        			"start_dependency: Exeeded ${MAX_START_TIMEOUT} seconds for waiting on dependent resource for resource ${RESOURCE} to come online"
    400 
    401 			St=1
    402 			break
    403 		fi
    404 
    405 		# Call check_start_dependency
    406 
    407 		debug_message "Function: start_dependency - Call check_start_dependency function with argument "$*
    408 
    409 		check_start_dependency $*
    410 		St=$?
    411 
    412 		if [ ${St} -eq 0 ]; then
    413 		  St=0
    414 		  break
    415 		fi
    416 
    417 		# Wait 5 seconds
    418 
    419 		sleep 5
    420 	done
    421 
    422 
    423 	debug_message "Function: start_dependency - End"
    424 
    425 	return ${St}
    426 }
    427 
    428 restart_dependency()
    429 {
    430 	debug_message "Function: restart_dependency - Begin"
    431 	${SET_DEBUG}
    432 
    433 
    434 	# Call check_restart_dependency
    435 
    436 	debug_message "Function: start_dependency - Call check_restart_dependency function with argument "$*
    437 	check_restart_dependency $*
    438 	St=$?
    439 
    440 	if [ ${St} -ne 0 ]; then
    441            scds_syslog -p daemon.err -t $(syslog_tag) -m \
    442            "restart_dependency - Dependent resource to resource %s has been restarted, restart this resource %s" \
    443            "${RESOURCE}" "${RESOURCE}"
    444 
    445 	   St=100
    446         else
    447            St=0
    448 	fi
    449 
    450 	debug_message "Function: restart_dependency - End"
    451 
    452 	return ${St}
    453 }
    454