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 2008 Sun Microsystems, Inc.  All rights reserved.
     24 # Use is subject to license terms.
     25 #
     26 
     27 #ident	"@(#)functions.ksh	1.13	08/04/10 SMI"
     28 
     29 
     30 SCLOGGER=/usr/cluster/lib/sc/scds_syslog
     31 PKG=SUNWscmys
     32 METHOD=`basename $0`
     33 TASK_COMMAND=""
     34 
     35 ZONENAME=/usr/bin/zonename
     36 
     37 PMFADM=/usr/cluster/bin/pmfadm
     38 variables_init()
     39 {
     40 	debug_message "Function: variables_init - Begin"
     41 	${SET_DEBUG}
     42 
     43         #
     44         # Initialize some variable out of the extracted or given paramters
     45         #
     46 
     47 	MYSQL_MYSQLD=./bin/mysqld
     48 	MYSQL_PIDFILE=${MYSQL_DATADIR}/mysqld.pid
     49 	MYSQL_DEFAULT_FILE=${MYSQL_DATADIR}/my.cnf
     50 	MYSQL_MYSQLADMIN=${MYSQL_BASEDIR}/bin/mysqladmin
     51 	MYSQL_MYSQL=${MYSQL_BASEDIR}/bin/mysql
     52 	MYSQL_MYISAMCHK=${MYSQL_BASEDIR}/bin/myisamchk
     53 	MYSQL_MYSQLD_LOGFILE=${MYSQL_LOGDIR}/${MYSQL_HOST}.log
     54 
     55 	# The % character in the date format string needs to be hidden in a variable.
     56 	# Otherwise a sccs delget will mess up the date format string by 
     57 	# expanding the format string with its keywords.
     58 
     59 	PERCENT=%
     60 	MYSQL_MYSQLD_LOGFILE_OLD=${MYSQL_MYSQLD_LOGFILE}.`date +${PERCENT}y${PERCENT}m${PERCENT}d${PERCENT}H${PERCENT}M${PERCENT}S`
     61 	MYSQL_FMUSER_USER=`echo ${MYSQL_FMUSER} | cut -d'%' -f1`
     62 	MYSQL_FMUSER_PW=`echo ${MYSQL_FMUSER} | cut -d'%' -f2`
     63 	
     64 	MYSQL_TEST_DB="sc3_test_database"
     65 	MYSQL_TEST_TABLE="sc3_test_table"
     66 	MYSQL_THOROUGH_SEC=300
     67 
     68 	MYSQL_ERRORF=/tmp/${RESOURCE}.mysql.error
     69 	MYSQL_RESULTF=/tmp/${RESOURCE}.mysql.result
     70 	MYSQL_LAST_RUN_THOROUGH=/tmp/${RESOURCE}.mysql.last
     71 
     72 
     73 	MYSQL_HOUR=`date '+%H'`
     74 	MYSQL_MIN=`date '+%M'`
     75 	MYSQL_SEC=`date '+%S'`
     76 	MYSQL_THOROUGH_TIME=`expr ${MYSQL_HOUR} \* 3600 + ${MYSQL_MIN} \* 60 + ${MYSQL_SEC}`
     77 
     78 	# Retrieve MySQL Version
     79 	VERSION=`${MYSQL_MYSQLADMIN} -V | awk '{print $5}' | cut -d',' -f1`
     80 	MYSQL_VERSION=`echo ${VERSION} | cut -d'.' -f1`
     81 	MYSQL_RELEASE=`echo ${VERSION} | cut -d'.' -f2`
     82 	MYSQL_UPDATE=` echo ${VERSION} | cut -d'.' -f3`
     83 
     84 	# The MySQL agent supports the MySQL server when it is configured as a slave or a master.
     85 	# For slave configurations it will probe the slave status and complain on erros in the 
     86 	# slave configuration. If the MySQL server will be stopped, the slave processes will be stopped 
     87 	# first, and then the mysqladmin shutdown will be executed.
     88 
     89 	if [ ${MYSQL_VERSION} -lt 5 ]
     90 	then
     91 		MYSQL_SLAVE=`grep master-host= ${MYSQL_DEFAULT_FILE} | grep -v '#'`
     92 	else
     93 		
     94 		MASTER_INFO=`grep master-info= ${MYSQL_DEFAULT_FILE} | grep -v '#'|awk -F= '{print $2}'`
     95 		if [ -z "${MASTER_INFO}" ]
     96 		then
     97 			MASTER_INFO=${MYSQL_DATADIR}/master.info
     98 		fi
     99 
    100 		# The slave processes are running only if a master.info file exists, so we set the 
    101 		# MYSQL_SLAVE flag only, if the master.info file exists.
    102 
    103 		MYSQL_SLAVE=
    104 		if [ -f ${MASTER_INFO} ]
    105 		then
    106 			MYSQL_SLAVE=true
    107 		fi
    108 		
    109 		
    110 	fi
    111 
    112 	debug_message "Function: variables_init - End"
    113 }
    114 
    115 validate_options()
    116 {
    117 	debug_message "Function: validate_options - Begin"
    118 	${SET_DEBUG}
    119 
    120         #
    121         # Ensure all options are set
    122         #
    123 
    124 	rc_validate_options=0
    125 
    126         for i in RESOURCE RESOURCEGROUP MYSQL_BASEDIR MYSQL_DATADIR MYSQL_USER MYSQL_HOST MYSQL_FMUSER MYSQL_LOGDIR
    127 
    128         do
    129                 case $i in
    130                         RESOURCE)
    131                         if [ -z $RESOURCE ]; then
    132                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
    133                                 "ERROR: Option -%s not set" \
    134                                 "R" 
    135                                 rc_validate_options=1
    136                         fi;;
    137 
    138                         RESOURCEGROUP)
    139                         if [ -z $RESOURCEGROUP ]; then
    140                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
    141                                 "ERROR: Option -%s not set" \
    142                                 "G" 
    143                                 rc_validate_options=1
    144                         fi;;
    145 
    146                         MYSQL_BASEDIR)
    147                         if [ -z $MYSQL_BASEDIR ]; then
    148                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
    149                                 "ERROR: Option -%s not set" \
    150                                 "B" 
    151                                 rc_validate_options=1
    152                         fi;;
    153 
    154                         MYSQL_DATADIR)
    155                         if [ -z $MYSQL_DATADIR ]; then
    156                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
    157                                 "ERROR: Option -%s not set" \
    158                                 "D" 
    159                                 rc_validate_options=1
    160                         fi;;
    161 
    162                         MYSQL_USER)
    163                         if [ -z $MYSQL_USER ]; then
    164                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
    165                                 "ERROR: Option -%s not set" \
    166                                 "U" 
    167                                 rc_validate_options=1
    168                         fi;;
    169 
    170                         MYSQL_HOST)
    171                         if [ -z $MYSQL_HOST ]; then
    172                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
    173                                 "ERROR: Option -%s not set" \
    174                                 "H" 
    175                                 rc_validate_options=1
    176                         fi;;
    177 
    178                         MYSQL_FMUSER)
    179                         if [ -z $MYSQL_FMUSER ]; then
    180                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
    181                                 "ERROR: Option -%s not set" \
    182                                 "F" 
    183                                 rc_validate_options=1
    184                         fi;;
    185 
    186                         MYSQL_LOGDIR)
    187                         if [ -z $MYSQL_LOGDIR ]; then
    188                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
    189                                 "ERROR: Option -%s not set" \
    190                                 "L" 
    191                                 rc_validate_options=1
    192                         fi;;
    193 
    194                 esac
    195         done
    196 	debug_message "Function: validate_options - Begin"
    197 	return ${rc_validate_options}
    198 }
    199 
    200 validate() 
    201 {
    202 	#
    203 	# Validate MySQL 
    204 	#
    205 
    206         debug_message "Function: validate - Begin"
    207 	$SET_DEBUG
    208 
    209 	rc_validate=0
    210 	#
    211 	# Validate the base directory
    212 	#
    213 
    214 	if [ ! -d "${MYSQL_BASEDIR}" ]
    215 	then
    216    		# SCMSGS
    217    		# @explanation
    218    		# The defined basedirectory (-B option) don't exist.
    219    		# @user_action
    220    		# Make sure that defined basedirectory exists.
    221    		scds_syslog -p daemon.error -t $(syslog_tag) -m \
    222 			"Validate - MySQL basedirectory  %s does not exist" \
    223 			"${MYSQL_BASEDIR}"
    224 		rc_validate=1
    225 	else
    226 		debug_message "Validate - MySQL base directory ${MYSQL_BASEDIR} exists" 
    227 	fi
    228 
    229 
    230 	#
    231 	# Validate the database directory
    232 	#
    233 
    234 	if [ ! -d "${MYSQL_DATADIR}" ]
    235 	then
    236    		# SCMSGS
    237    		# @explanation
    238    		# The defined database directory (-D option) does not exist.
    239    		# @user_action
    240    		# Make sure that defined database directory exists.
    241    		scds_syslog -p daemon.error -t $(syslog_tag) -m \
    242 			"Validate - MySQL database directory %s does not exist" \
    243 			"${MYSQL_DATADIR}"
    244 		rc_validate=1
    245 	else
    246 		debug_message "Validate - MySQL database directory ${MYSQL_DATADIR} exists"
    247 	fi
    248 
    249 	#
    250 	# Validate that the mysqld is executable
    251 	#
    252 
    253         cd ${MYSQL_BASEDIR}
    254 
    255 	if [ ! -x "${MYSQL_MYSQLD}" ]
    256 	then
    257    		# SCMSGS
    258    		# @explanation
    259    		# The mysqld command don't exist or is not executable.
    260    		# @user_action
    261    		# Make sure that MySQL is installed correctly or right
    262    		# base directory is defined.
    263    		scds_syslog -p daemon.error -t $(syslog_tag) -m \
    264 			"Validate - mysqld %s non-existent executable" \
    265 			"${MYSQL_MYSQLD}"
    266 		rc_validate=1
    267 	else
    268 		debug_message "Validate - MySQL ${MYSQL_MYSQLD} exists and is executable"
    269 	fi
    270 
    271 	#
    272 	# Validate that the mysqladmin is executable
    273 	#
    274 
    275 	if [ ! -x "${MYSQL_MYSQLADMIN}" ]
    276 	then
    277    		# SCMSGS
    278    		# @explanation
    279    		# The mysqladmin command does not exist or is not executable.
    280    		# @user_action
    281    		# Make sure that MySQL is installed correctly or right
    282    		# base directory is defined.
    283    		scds_syslog -p daemon.error -t $(syslog_tag) -m \
    284 			"Validate - mysqladmin %s non-existent or non-executable" \
    285 			"${MYSQL_MYSQLADMIN}"
    286 		rc_validate=1
    287 	else
    288 		debug_message "Validate - mysqladmin ${MYSQL_MYSQLADMIN} exists and is executable"
    289 	fi
    290 
    291 	#
    292 	# Validate that the myisamchk is executable
    293 	#
    294 
    295 	if [ ! -x "${MYSQL_MYISAMCHK}" ]
    296 	then
    297    		# SCMSGS
    298    		# @explanation
    299    		# The mysqladmin command does not exist or is not executable.
    300    		# @user_action
    301    		# Make sure that MySQL is installed correctly or right
    302    		# base directory is defined.
    303    		scds_syslog -p daemon.error -t $(syslog_tag) -m \
    304 			"Validate - myisamchk %s non-existent or non-executable" \
    305 			"${MYSQL_MYISAMCHK}"
    306 		rc_validate=1
    307 	else
    308 		debug_message "Validate - myisamchk ${MYSQL_MYISAMCHK} exists and is executable"
    309 	fi
    310 
    311 	#
    312 	# Validate that my.cnf file exists
    313 	#
    314 
    315 	if [ ! -f "${MYSQL_DEFAULT_FILE}" ]
    316 	then
    317    		# SCMSGS
    318    		# @explanation
    319    		# The my.cnf configuration does not exist in the defined database
    320    		# directory.
    321    		# @user_action
    322    		# Make sure that my.cnf is placed in the defined database
    323    		# directory.
    324    		scds_syslog -p daemon.error -t $(syslog_tag) -m \
    325 			"Validate - my.cnf %s does not exist" \
    326 			"${MYSQL_DEFAULT_FILE}"
    327 		rc_validate=1	
    328 	else
    329 		debug_message "Validate - my.cnf ${MYSQL_DEFAULT_FILE} exists"
    330 	fi
    331 
    332 
    333 	#
    334 	# Validate that MySQL user exists
    335 	#
    336 
    337 	if [ -z "`getent passwd ${MYSQL_USER}`" ]
    338 	then
    339  		# SCMSGS
    340  		# @explanation
    341  		# Couldn't retrieve the defined user from nameservice.
    342  		# @user_action
    343  		# Make sure that the right user is defined or the user exists.
    344  		# Use getent passwd 'username' to verify that defined user
    345  		# exists.
    346  		scds_syslog -p daemon.error -t $(syslog_tag) -m \
    347 			"Validate - Couldn't retrieve MySQL-user <%s> from the nameservice" \
    348 			"${MYSQL_USER}"
    349 		rc_validate=1
    350 	else
    351 		debug_message "Validate - Retrieved MySQL-user ${MYSQL_USER} from the nameservice"
    352 	fi
    353 
    354 	if [ ! -d "${MYSQL_LOGDIR}" ]
    355 	then
    356    		# SCMSGS
    357    		# @explanation
    358    		# The defined (-L option) log directory does not exist.
    359    		# @user_action
    360    		# Make sure that the defined log directory exists.
    361    		scds_syslog -p daemon.error -t $(syslog_tag) -m \
    362 			"Validate - MySQL logdirectory for mysqld does not exist" \
    363 			"${MYSQL_LOGDIR}"
    364 		rc_validate=1
    365 	else
    366 		debug_message "Validate - MySQL logdirectory ${MYSQL_LOGDIR} exists"
    367 	fi
    368 
    369 	#
    370 	# Check the MySQL version
    371 	#
    372 
    373 	debug_message "Validate - MySQL version <${MYSQL_VERSION}.${MYSQL_RELEASE}.${MYSQL_UPDATE}> is being used"
    374 
    375 	if [ -z "${VERSION}" ]
    376 	then
    377    		# SCMSGS
    378    		# @explanation
    379    		# Internal error when retrieving MySQL version.
    380    		# @user_action
    381    		# Make sure that supported MySQL version is being used.
    382    		scds_syslog -p daemon.error -t $(syslog_tag) -m \
    383 			"Validate - Couldn't retrieve MySQL version number"
    384 		rc_validate=1
    385 
    386 	# Evaluate the MySQL release and update only if the MySQL version is 3.
    387 	# This avoids naming conflicts with higher versions of MySQL.
    388 
    389 	elif [ "${MYSQL_VERSION}" -eq 3 ]
    390 	then 
    391 		if [ "${MYSQL_RELEASE}" -lt 23 -a "${MYSQL_UPDATE}" -lt 54 ]
    392 		then
    393 	   		# SCMSGS
    394 	   		# @explanation
    395 	   		# An unsupported MySQL version is being used.
    396 	   		# @user_action
    397 	   		# Make sure that supported MySQL version is being used.
    398 	   		scds_syslog -p daemon.error -t $(syslog_tag) -m \
    399 				"Validate - This version of MySQL <%s> is not supported with this dataservice" \
    400 				`printf ${MYSQL_VERSION}.${MYSQL_RELEASE}.${MYSQL_UPDATE}`
    401 			rc_validate=1
    402 		fi
    403 	fi
    404 
    405         debug_message "Function: validate - End"
    406 	return ${rc_validate}
    407 }
    408 
    409 start_mysql()
    410 {
    411 	#
    412 	# Start MySQL
    413 	#
    414 
    415         start_status=0
    416 
    417         debug_message "Function: start_mysql - Begin"
    418 	$SET_DEBUG
    419 
    420 	# construct the ${PORT} variable
    421 
    422 	get_mysql_port
    423 
    424         # If MYSQL_CHECK is set run myisamchk on index
    425 
    426 	if [ ! -z "${MYSQL_CHECK}" ]; then
    427 
    428            debug_message "start_mysql - Run myisamchk on index in ${MYSQL_DATADIR}"
    429 
    430 	   ${MYSQL_MYISAMCHK} -c -s ${MYSQL_DATADIR}/*/*.MYI
    431 
    432            start_status=$?
    433 
    434            if [ ${start_status} -ne 0 ]; then
    435                 # SCMSGS
    436                 # @explanation
    437                 # mysiamchk found errors in MyISAM based tables.
    438                 # @user_action
    439                 # Consult MySQL documentation when repairing MyISAM tables.
    440                 scds_syslog -p daemon.error -t $(syslog_tag) -m \
    441                         "start_mysql - myisamchk found errors in some index in %s, perform manual repairs" \
    442                         "${MYSQL_DATADIR}"
    443 
    444              return ${start_status}
    445            fi
    446         fi
    447 
    448 	#
    449 	# IF ${MYSQL_MYSQLD_LOGFILE) EXIST MOVE IT TO ${MYSQL_MYSQLD_LOGFILE}.date
    450 	#
    451 
    452 	if [ -f "${MYSQL_MYSQLD_LOGFILE}" ]; then
    453 		debug_message "start_mysql - Move ${MYSQL_MYSQLD_LOGFILE} to ${MYSQL_MYSQLD_LOGFILE_OLD}"
    454 		mv ${MYSQL_MYSQLD_LOGFILE} ${MYSQL_MYSQLD_LOGFILE_OLD}
    455         fi
    456 
    457 	#
    458 	# Start mysqld
    459 	#
    460 
    461 	cd ${MYSQL_BASEDIR}
    462 	${MYSQL_MYSQLD} --defaults-file=${MYSQL_DEFAULT_FILE} --basedir=${MYSQL_BASEDIR} --datadir=${MYSQL_DATADIR} --user=${MYSQL_USER} --pid-file=${MYSQL_PIDFILE} ${PORT} > ${MYSQL_MYSQLD_LOGFILE} 2>&1 &
    463 
    464         debug_message "Function: start_mysql - End"
    465 	return 0
    466 }
    467 
    468 stop_mysql()
    469 {
    470 	#
    471 	# Stop MySQL
    472 	#
    473 
    474         debug_message "Function: stop_mysql - Begin"
    475 	$SET_DEBUG
    476 
    477 	# construct the ${PORT} variable
    478 
    479 	get_mysql_port
    480 
    481 	#
    482 	# Initialze SECONDS to have a valid timer
    483 	#
    484 
    485 	SECONDS=0
    486 
    487 	#
    488 	# get the stop timeout if not done already in get_fmri_parameters for smf services
    489 	#
    490 
    491 	if [ -z "${SMF_FMRI}" ]; then
    492 		STOP_TIMEOUT=`${SCHA_RESOURCE_GET} -R ${RESOURCE} -G ${RESOURCEGROUP} -O STOP_TIMEOUT`
    493 	fi
    494 
    495 	#
    496 	# calculate the MAX_STOP_TIME as 70 percent of STOP_TIMEOUT
    497 	#
    498 
    499 	MAX_STOP_TIME=`expr ${STOP_TIMEOUT} \* 70 \/ 100`
    500 
    501 	#
    502 	# If Slave stop slave first
    503 	# 
    504 
    505 	if [ ! -z "${MYSQL_SLAVE}" ]; then
    506           debug_message "Function: stop_mysql - Issue SLAVE STOP to MySQL instance"
    507 
    508           ${MYSQL_MYSQL} -h ${MYSQL_HOST} -u ${MYSQL_FMUSER_USER} -p${MYSQL_FMUSER_PW} ${PORT} -e "SLAVE STOP" > ${MYSQL_RESULTF} 2> ${MYSQL_ERRORF}
    509 
    510           if [ $? -ne 0 ]; then
    511             Msg="`cat ${MYSQL_ERRORF}`"
    512             # SCMSGS
    513             # @explanation
    514             # Couldn't stop slave instance.
    515             # @user_action
    516             # Examine the returned Sql-status message and consult MySQL
    517             # documentation.
    518             scds_syslog -p daemon.error -t $(syslog_tag) -m \
    519             "Function: stop_mysql - Sql-command SLAVE STOP returned error (%s)" \
    520             "$Msg"
    521           else
    522            debug_message "Function: stop_mysql - STOP SLAVE issued"
    523           fi
    524         fi
    525 
    526 	#
    527 	# Flush MySql tables
    528 	#
    529 
    530         debug_message "Function: stop_mysql - Flush MySql tables"
    531 
    532 	${MYSQL_MYSQLADMIN} -h ${MYSQL_HOST} -u${MYSQL_FMUSER_USER} -p${MYSQL_FMUSER_PW} ${PORT} flush-tables
    533 
    534         if [ $? -ne 0 ]; then
    535            # SCMSGS
    536            # @explanation
    537            # mysqladmin command failed to flush MySQL tables.
    538            # @user_action
    539            # Either was MySQL already down or the faultmonitor user does not have
    540            # the right permission to flush tables. The defined faultmonitor
    541            # should have Process-,Select-, Reload- and Shutdown-privileges and
    542            # for MySQL 4.0.x also Super-privileges.
    543            scds_syslog -p daemon.error -t $(syslog_tag) -m \
    544                         "stop_mysql - Failed to flush MySQL tables for %s" \
    545                         "${MYSQL_DATADIR}"
    546 
    547         else
    548            debug_message "Function: stop_mysql - MySQL tables flushed"
    549         fi
    550 
    551 	#
    552 	# Flush MySql logfiles
    553 	#
    554 
    555         debug_message "Function: stop_mysql - Flush MySql logfiles"
    556 
    557 	${MYSQL_MYSQLADMIN} -h ${MYSQL_HOST} -u${MYSQL_FMUSER_USER} -p${MYSQL_FMUSER_PW} ${PORT} flush-logs
    558 
    559         if [ $? -ne 0 ]; then
    560            # SCMSGS
    561            # @explanation
    562            # mysqladmin command failed to flush MySQL logfiles.
    563            # @user_action
    564            # Either was MySQL already down or the faultmonitor user does not have
    565            # the right permission to flush logfiles. The defined faultmonitor
    566            # should have Process-,Select-, Reload- and Shutdown-privileges and
    567            # for MySQL 4.0.x also Super-privileges.
    568            scds_syslog -p daemon.error -t $(syslog_tag) -m \
    569                         "stop_mysql - Failed to flush MySql logfiles for %s" \
    570                         "${MYSQL_DATADIR}"
    571 
    572         else
    573            debug_message "Function: stop_mysql - MySql logfiles flushed"
    574         fi
    575 
    576 	# 
    577 	# Stop MySQL
    578 	#
    579 
    580 
    581         debug_message "Function: stop_mysql - Stop through mysqladmin"
    582 
    583 	MYSQL_STOP=0
    584 
    585 	# check if mysql is running with a pid file as it always should
    586 
    587 	PID_FILE_EXISTS=0
    588 
    589 	if [ -f "${MYSQL_PIDFILE}" ]; then
    590 	   PID_FILE_EXISTS=1
    591 	fi
    592 	
    593 	${MYSQL_MYSQLADMIN} -h ${MYSQL_HOST} -u${MYSQL_FMUSER_USER} -p${MYSQL_FMUSER_PW} ${PORT} shutdown
    594 
    595         if [ $? -ne 0 ]; then
    596            # SCMSGS
    597            # @explanation
    598            # mysqladmin command failed to stop MySQL instance.
    599            # @user_action
    600            # Either was MySQL already down or the faultmonitor user does not have
    601            # the right permission to stop MySQL. The defined faultmonitor
    602            # should have Process-,Select-, Reload- and Shutdown-privileges and
    603            # for MySQL 4.0.x also Super-privileges.
    604            scds_syslog -p daemon.error -t $(syslog_tag) -m \
    605                         "stop_mysql - Failed to stop MySQL through mysqladmin for %s, send TERM signal to process" \
    606                         "${MYSQL_DATADIR}"
    607 
    608 	   MYSQL_STOP=1
    609 	else
    610 
    611            if [ ${PID_FILE_EXISTS} -ne 0 ]; then
    612 
    613               # Wait until pid is gone or MAX_STOP_TIME is exceeded
    614 
    615               while test ${SECONDS} -lt ${MAX_STOP_TIME} -a -f ${MYSQL_PIDFILE}
    616               do
    617                     sleep 5
    618               done
    619 
    620 	      if [ -f ${MYSQL_PIDFILE} ]; then
    621 
    622                  # In the SMF case we can not rely on PMF, therefore the kill is introduced.
    623                  # Because in the SMF case PMF has only the corresponding mysql pids,
    624                  # if there was no restart of the resource after it got enabled.
    625 
    626                  debug_message "Function: stop_mysql - Stop through mysqladmin unsuccessful, mysql will be killed with signal KILL"
    627    		 pid=`cat ${MYSQL_PIDFILE}`
    628    		 /bin/kill -KILL ${pid}
    629 
    630               else
    631                  debug_message "Function: stop_mysql - Stopped through mysqladmin"
    632               fi
    633 
    634            else
    635 
    636               # sleep 70 percent of STOP_TIMEOUT - seconds already spent in the stop function, because 
    637 	      # mysql was running without a pid file.
    638               # This should happen only if the pid file was deleted manually, which is illegal anyway.
    639 
    640 	      let sleep_time=${MAX_STOP_TIME}-${SECONDS}
    641               debug_message "Function: stop_mysql - Wait ${sleep_time} seconds to let the shutdown complete"
    642               sleep ${sleep_time}
    643 
    644            fi
    645 
    646         fi
    647 
    648 	if [ "${MYSQL_STOP}" -ne 0 ]; then
    649 
    650            debug_message "Function: stop_mysql - Try stop MySQL by sending signal TERM for ${MYSQL_DATADIR}"
    651 
    652            if [ -f "${MYSQL_PIDFILE}" ]
    653 	   then
    654    		pid=`cat ${MYSQL_PIDFILE}`
    655 
    656 		debug_message "stop_mysql - MySQL lockfile found"
    657 
    658 		# check if pid is still there
    659 
    660 		/bin/kill -0 ${pid}
    661 
    662 		if [ $? -eq 0 ]; then
    663 
    664 		  debug_message "stop_mysql - Send TERM to ${pid} and wait until pid-file is removed"
    665 
    666    		  /bin/kill -TERM ${pid}
    667 
    668    		  # wait until pid is gone or MAX_STOP_TIME is exceeded
    669 
    670 		  while test ${SECONDS} -lt ${MAX_STOP_TIME} -a -f ${MYSQL_PIDFILE}
    671                   do
    672                      sleep 5
    673                   done
    674 
    675                   # The timout is exceeded, and mysql is still running if the pid file still exists
    676 
    677                   if [ -f ${MYSQL_PIDFILE} ]
    678                   then
    679 
    680                      debug_message "Function: stop_mysql - Stop through kill -TERM unsuccesful, mysql will be killed with signal KILL"
    681    		     /bin/kill -KILL ${pid}
    682                      
    683                   fi
    684 		else
    685                  # SCMSGS
    686                  # @explanation
    687                  # The saved Pid didn't exist in process list.
    688                  # @user_action
    689                  # None
    690                  scds_syslog -p daemon.error -t $(syslog_tag) -m \
    691                  "stop_mysql - Pid is not running, let GDS stop MySQL for %s" \
    692                  "${MYSQL_DATADIR}"
    693 
    694 		 debug_message "stop_mysql - Pid is not running, let GDS stop MySQL"
    695                 fi
    696 	     else
    697 		debug_message "stop_mysql - mysqld lockfile not found - let GDS stop MySQL"
    698              fi
    699 
    700 	fi
    701 
    702 
    703         debug_message "Function: stop_mysql - End"
    704 	return 0
    705 }
    706 
    707 check_mysql()
    708 {
    709 
    710 	#
    711 	# Check MySQL 
    712 	#
    713 
    714         debug_message "Function: check_mysql - Begin"
    715 	$SET_DEBUG
    716 
    717 	rc_check_mysql=0
    718 
    719 	# construct the ${PORT} variable
    720 
    721 	get_mysql_port
    722 
    723 	#
    724 	# MYSQL AVAILBLE TEST
    725 	#
    726 
    727 	debug_message "check_mysql - Available test started"
    728 
    729         ${MYSQL_MYSQLADMIN} -h ${MYSQL_HOST} -u ${MYSQL_FMUSER_USER} -p${MYSQL_FMUSER_PW} ${PORT} ping > ${MYSQL_RESULTF} 2> ${MYSQL_ERRORF}
    730 
    731 	if [ -s "${MYSQL_ERRORF}" ]
    732 	then
    733   		# SCMSGS
    734   		# @explanation
    735   		# The faultmonitor can't connect to the specified MySQL
    736   		# instance.
    737   		# @user_action
    738   		# None
    739   		scds_syslog -p daemon.error -t $(syslog_tag) -m \
    740 			"check_mysql - mysqld server <%s> not working, failed to connect to MySQL" \
    741 			"${MYSQL_DATADIR}"
    742 
    743                 rc_check_mysql=100
    744                 return ${rc_check_mysql}
    745 	else
    746 		debug_message "check_mysql - MySQL is working"
    747 	fi
    748 
    749 
    750 	debug_message "check_mysql - Available test ended"
    751 
    752 
    753 	# Check is we should do thorough test
    754 
    755 	if [ ! -f "${MYSQL_LAST_RUN_THOROUGH}" ]; then
    756           MYSQL_THOROUGH_TIME_LAST=0
    757         else
    758 	  MYSQL_THOROUGH_TIME_LAST=`cat ${MYSQL_LAST_RUN_THOROUGH}`
    759         fi
    760 
    761 	# Check if we past 12/24 hour
    762 
    763 	if [ "${MYSQL_THOROUGH_TIME_LAST}" -gt "${MYSQL_THOROUGH_TIME}" ]; then
    764            MYSQL_THOROUGH_TIME_LAST=${MYSQL_THOROUGH_TIME}
    765 	   echo ${MYSQL_THOROUGH_TIME} > ${MYSQL_LAST_RUN_THOROUGH}
    766 	fi
    767 
    768 	# If it has past > ${MYSQL_THOROUGH_SEC} sec then run thorough test
    769 
    770 	s1=`expr ${MYSQL_THOROUGH_TIME} - ${MYSQL_THOROUGH_TIME_LAST}`
    771 
    772 	if [ "${s1}" -gt ${MYSQL_THOROUGH_SEC} ]; then
    773           check_mysql_thorough
    774 	  rc_check_mysql=${?}
    775 
    776 	  # UPDATE FILE
    777 	  echo ${MYSQL_THOROUGH_TIME} > ${MYSQL_LAST_RUN_THOROUGH}
    778         fi
    779 
    780         debug_message "Function: check_mysql - End"
    781         return ${rc_check_mysql}
    782 
    783 }
    784 
    785 check_mysql_thorough()
    786 {
    787 	#
    788 	# MYSQL CONSISTENCY TEST
    789 	#
    790 
    791 	$SET_DEBUG
    792 
    793 	debug_message "check_mysql - Consistency test started"
    794 
    795 	# If slave instance, check if slave connection to master is active
    796 
    797 	if [ ! -z "${MYSQL_SLAVE}" ]; then
    798 
    799 		# Use right columns for version 3 and 4
    800 
    801 		if [ "${MYSQL_VERSION}" -eq 3 ]; then
    802 		   MYSQL_STATUS_COM="/bin/cut -f1,7,10"
    803 		elif [ "${MYSQL_VERSION}" -eq 4 ]; then
    804 
    805 		   # The format of slave status changed at MySQL release 4.1
    806 		   # so we need different MYSQL_STATUS_COM commands
    807 
    808 		   if [ ${MYSQL_RELEASE} -eq 0 ]
    809 		   then
    810 		   	MYSQL_STATUS_COM="/bin/cut -f1,10,14"
    811 		   else
    812 		   	MYSQL_STATUS_COM="/usr/bin/sed s/\ /-/g |/bin/cut -f2,11,20"
    813 		   fi
    814 		elif [ "${MYSQL_VERSION}" -gt 4 ]; then
    815 		   MYSQL_STATUS_COM="/usr/bin/sed s/\ /-/g |/bin/cut -f1,2,11,20"
    816                 fi
    817 
    818 		debug_message "check_mysql - Check slave connection to master"
    819 
    820 		${MYSQL_MYSQL} -h ${MYSQL_HOST} -u ${MYSQL_FMUSER_USER} -p${MYSQL_FMUSER_PW} -B ${PORT} -e "SHOW SLAVE STATUS" >${MYSQL_RESULTF} 2> ${MYSQL_ERRORF} 
    821 
    822 		if [ $? -ne 0 ]; then
    823                    Msg="`cat ${MYSQL_ERRORF}`"
    824                    # SCMSGS
    825                    # @explanation
    826                    # The faultmonitor can't retrieve the MySQL slave status
    827                    # for the specified instance.
    828                    # @user_action
    829                    # Either was MySQL already down or the faultmonitor user
    830                    # does not have the right permission. The defined faultmonitor
    831                    # should have Process-,Select-, Reload- and
    832                    # Shutdown-privileges and for MySQL 4.0.x also
    833                    # Super-privileges. Check also the MySQL logfiles for any
    834                    # other errors.
    835                    scds_syslog -p daemon.error -t $(syslog_tag) -m \
    836                   "check_mysql - Couldn't get SHOW SLAVE STATUS for instance %s (%s)"  \
    837 		  "${MYSQL_DATADIR}" "${Msg}"
    838                 else
    839 
    840 		   # RESULT CHECK
    841 
    842 		   Res=`cat ${MYSQL_RESULTF} | tail +2 | eval ${MYSQL_STATUS_COM}`
    843 
    844 		   # Starting with version 5, a node which was acting as a slave, does always run
    845 		   # some slave processes. If the set of processes is incomplete, it is a master which
    846 		   # was acting as a slave sometime ago.
    847 		   # Entries in the field SLAVE_IO_STATE mark a node as an acting slave.
    848 		   # In version 4 and 3 we place true into the field SLAVE_IO_STATE to trigger the 
    849 		   # slave check. Starting with Version 5 we check the SLAVE_IO_STATE field.
    850 
    851 		 if [ ${MYSQL_VERSION} -lt 5 ]
    852 		 then
    853 		    SLAVE_IO_STATE="true"
    854 		    SLAVE_HOST=`echo $Res | awk '{print $1}'`
    855 		    SLAVE_RUNNING=`echo $Res | awk '{print $2}'`
    856 		    SLAVE_ERROR=`echo $Res | awk '{print $3}'`
    857 		 else
    858 
    859 		    # By the word count we can determine if an SLAVE_IO_STATE is defined,
    860 		    # and define the necessary variables according to the word count.
    861 
    862 		    WORDS=`echo $Res |wc -w`
    863 		    if [ $WORDS -gt 2 ]
    864 		    then
    865 		       SLAVE_IO_STATE=`echo $Res | awk '{print $1}'`
    866 		       SLAVE_HOST=`echo $Res | awk '{print $2}'`
    867 		       SLAVE_RUNNING=`echo $Res | awk '{print $3}'`
    868 		       SLAVE_ERROR=`echo $Res | awk '{print $4}'`
    869 		    else
    870 		       SLAVE_IO_STATE=
    871 		       SLAVE_HOST=`echo $Res | awk '{print $1}'`
    872 		       SLAVE_RUNNING=`echo $Res | awk '{print $2}'`
    873 		       SLAVE_ERROR=
    874 		    fi
    875 		 fi
    876 
    877 	         debug_message "check_mysql - Slave connection to ${SLAVE_HOST}, Connect status is ${SLAVE_RUNNING} with error ${SLAVE_ERROR}"
    878 
    879 
    880 		 if [ "${SLAVE_RUNNING}" = "No" ] && [ -n "${SLAVE_IO_STATE}" ]; then
    881                     # SCMSGS
    882                     # @explanation
    883                     # The faultmonitor has detected that the MySQL slave
    884                     # instance is not connected to the specified master.
    885                     # @user_action
    886                     # Check MySQL logfiles to determine why the slave has been
    887                     # disconnected to the master.
    888                     scds_syslog -p daemon.error -t $(syslog_tag) -m \
    889                    "check_mysql - MySQL slave instance %s is not connected to master %s with MySql error (%s)"  \
    890 		   "${MYSQL_DATADIR}" "${SLAVE_HOST}" "${SLAVE_ERROR}"
    891                  fi
    892                 fi
    893 
    894 	debug_message "check_mysql - Slave connection to master checked"
    895         fi
    896 
    897 	debug_message "check_mysql - Get defined databases"
    898 
    899 	Dbs=`${MYSQL_MYSQL} -h ${MYSQL_HOST} -u ${MYSQL_FMUSER_USER} -p${MYSQL_FMUSER_PW} -B ${PORT} -e 'show databases' | tail +2`
    900 
    901 	if [ $? -eq 0 ]; then
    902 
    903 	   debug_message "check_mysql - Got defined databases (${Db})"
    904 
    905 	   for db in ${Dbs}
    906            do
    907              debug_message "check_mysql - Use database $db and do show tables for that database"
    908 
    909 	     ${MYSQL_MYSQL} -h ${MYSQL_HOST} -u ${MYSQL_FMUSER_USER} -p${MYSQL_FMUSER_PW} -B ${PORT} -e "use ${db}; show tables" > ${MYSQL_RESULTF} 2> ${MYSQL_ERRORF}
    910 
    911              if [ $? -ne 0 ]; then
    912 	       Msg="`cat ${MYSQL_ERRORF}`"
    913   	       # SCMSGS
    914   	       # @explanation
    915   	       # The faultmonitor can't issue show tables for the specified
    916   	       # database.
    917   	       # @user_action
    918   	       # Either was MySQL already down or the faultmonitor user does not
    919   	       # have the right permission. The defined faultmonitor should
    920   	       # have Process-,Select-, Reload- and Shutdown-privileges and
    921   	       # for MySQL 4.0.x also Super-privileges. Check also the MySQL
    922   	       # logfiles for any other errors.
    923   	       scds_syslog -p daemon.error -t $(syslog_tag) -m \
    924 		  "check_mysql - Couldn't do show tables for defined database %s (%s)" \
    925 		  "${db}" "$Msg"
    926              fi
    927            done
    928 
    929 	else
    930   	  # SCMSGS
    931   	  # @explanation
    932   	  # The faultmonitor can't retrieve all defined databases for the
    933   	  # specified instance.
    934   	  # @explanation-2
    935   	  # The database in the start, stop and probe command does not exist.
    936   	  # @user_action
    937   	  # Either was MySQL already down or the faultmonitor user does not have
    938   	  # the right permission. The defined faultmonitor should have
    939   	  # Process-,Select-, Reload- and Shutdown-privileges and for MySQL
    940   	  # 4.0.x also Super-privileges. Check also the MySQL logfiles for any
    941   	  # other errors.
    942   	  # @user_action-2
    943   	  # Fix the start, stop and probe command.
    944   	  scds_syslog -p daemon.error -t $(syslog_tag) -m \
    945 		"check_mysql - Couldn't retrieve defined databases for %s " \
    946 		"${MYSQL_DATADIR}"
    947 	fi
    948 
    949 
    950 	debug_message "check_mysql - Consistency test ended"
    951 
    952 	#
    953 	# MYSQL FUNCTION TEST
    954 	#
    955 
    956 
    957 	debug_message "check_mysql - Function test started"
    958 
    959 
    960 	debug_message "check_mysql - Retrive tables from ${MYSQL_TEST_DB}"
    961 
    962 	Tab=`${MYSQL_MYSQL} -h ${MYSQL_HOST} -u ${MYSQL_FMUSER_USER} -p${MYSQL_FMUSER_PW} -B ${PORT} -e "use ${MYSQL_TEST_DB}; show tables" | tail +2 2> ${MYSQL_ERRORF}`
    963 
    964 	if [ ! -z "${Tab}" ]; then
    965 
    966 	   debug_message "check_mysql - ${MYSQL_TEST_DB} contained ${Tab}"
    967 
    968 	   debug_message "check_mysql - Drop those tables"
    969 
    970 	   for s1 in ${Tab}
    971            do
    972               ${MYSQL_MYSQL} -h ${MYSQL_HOST} -u ${MYSQL_FMUSER_USER} -p${MYSQL_FMUSER_PW} ${PORT} -e "use ${MYSQL_TEST_DB}; drop table ${s1}; COMMIT" > ${MYSQL_RESULTF} 2> ${MYSQL_ERRORF}
    973 
    974 	      if [ $? -ne 0 ]; then
    975                 Msg="`cat ${MYSQL_ERRORF}`"
    976                 # SCMSGS
    977                 # @explanation
    978                 # The faultmonitor can't drop specified table from the test
    979                 # database.
    980                 # @user_action
    981                 # Either was MySQL already down or the faultmonitor user does not
    982                 # have the right permission. The defined faultmonitor should
    983                 # have Process-,Select-, Reload- and Shutdown-privileges and
    984                 # for MySQL 4.0.x also Super-privileges. Check also the MySQL
    985                 # logfiles for any other errors.
    986                 scds_syslog -p daemon.error -t $(syslog_tag) -m \
    987                     "check_mysql - Couldn't drop table %s from database %s (%s)" \
    988                     "${MYSQL_TEST_DB}" "$s1" "$Msg"
    989               fi
    990 
    991 	   done
    992 
    993 	   debug_message "check_mysql - Tables droped"
    994 	fi
    995 
    996 	for cmd in 'USE sc3_test_database; CREATE TABLE sc3_test_table ( Number int(5) NOT NULL, PRIMARY KEY (Number)) TYPE=MyISAM;COMMIT' \
    997                    'USE sc3_test_database; INSERT INTO sc3_test_table VALUES (1000); COMMIT' \
    998 		  'USE sc3_test_database; UPDATE sc3_test_table Set Number=2000 WHERE Number=1000; COMMIT' \
    999 		  'USE sc3_test_database; DELETE FROM sc3_test_table WHERE Number=2000; COMMIT' \
   1000 		  'USE sc3_test_database; DROP TABLE sc3_test_table; COMMIT'
   1001         do
   1002 	  debug_message "check_mysql - Run Sql-command ${cmd}"
   1003 
   1004           ${MYSQL_MYSQL} -h ${MYSQL_HOST} -u ${MYSQL_FMUSER_USER} -p${MYSQL_FMUSER_PW} ${PORT} -e "${cmd}" > ${MYSQL_RESULTF} 2> ${MYSQL_ERRORF}
   1005 
   1006           if [ $? -ne 0 ]; then
   1007             rc_check_mysql=100
   1008             Msg="`cat ${MYSQL_ERRORF}`"
   1009             # SCMSGS
   1010             # @explanation
   1011             # The faultmonitor can't execute the specified SQL command.
   1012             # @user_action
   1013             # Either was MySQL already down or the faultmonitor user does not
   1014             # have the right permission. The defined faultmonitor should have
   1015             # Process-,Select-, Reload- and Shutdown-privileges and for MySQL
   1016             # 4.0.x also Super-privileges. Check also the MySQL logfiles for
   1017             # any other errors.
   1018             scds_syslog -p daemon.error -t $(syslog_tag) -m \
   1019             "check_mysql - Sql-command %s returned error (%s)" \
   1020             "${cmd}" "$Msg"
   1021             return
   1022           fi
   1023  
   1024         done
   1025 
   1026 	debug_message "check_mysql - Function test ended"
   1027 }
   1028 
   1029 get_fmri_parameters ()
   1030 {
   1031 
   1032 # extract the smf properties you need to call your agent commands
   1033 
   1034         debug_message "Function: get_fmri_parameters - Begin "
   1035         ${SET_DEBUG}
   1036 
   1037 # Resource name
   1038 
   1039 	RESOURCE=`/usr/bin/svcprop -p parameters/