#! /bin/ksh #set -x ################################################################################ # # # Script: $EMCLI_HOME/em_relocate_checker.sh all_sids # # # # Purpose: Script to check running database instances are # # associated with the correct hostname in EM. # # # # If targets have moved as a result of a HP Service Guard # # package failover, then the database and listener targets # # are migrated within EM12c from one host to another. # # # # This script assumes the following: # # # # 1) EMCLI has been installed and configured # # http://www.snapdba.com/2013/02/installing-emcli-in-em12c/ # # 2) EMCLI has been setup to use 'auto-login' # # 3) Only database and listener targets will be migrated # # 4) EMREP is the name of your EM database repository and preferred # # credentials have been defined for EMREP within Cloud Control. # # This is defined using the variable $EMDB below. # # # # Exception ORACLE_SIDs can be passed into the script like so: # # # # em_relocate_checker.sh "test|test2|test3" < list of SIDs # # em_relocate_checker.sh test < 1 single SID # # em_relocate_checker.sh all_sids < all SIDs (default) # # # # Change Log: # # 19/02/2013 GH Initial Release # # 21/02/2013 GH Some minor tweaks made # # 08/07/2013 GH Added check to exit if previous run already running # # 08/07/2013 GH Added EMCLI SYNC check to see if OMS is up # # 31/10/2013 GH Further tweaks to account for EM database targets with # # domain appended etc. # # 03/12/2014 GH EMREP references updated with $EMDB variable # # # ################################################################################ ########################################################### # Check for presence of lock file, and existing processes # ########################################################### LOCKFILE=/tmp/em_check.lock if [[ -f $LOCKFILE ]] then if ps -p $(<$LOCKFILE) >/dev/null then echo "Job is still running..." exit fi fi # Create new lock file with PID if nothing already running echo $$ > $LOCKFILE ################################# # Setting environment variables # ################################# # Setting environment variables export EXCEPTION_SID=$1 export AGENT_HOME=/emagent/agent12c export AGENT_PORT=3872 export DOMAIN=local.domain export EMDB=EMREP export MAIL_LIST=mail@example.com export EMCLI_HOME=/emagent/emcli # Static variables export DEST_HOST=`hostname`.$DOMAIN export DATE=`date +%d%m%y` export LOGFILE=$EMCLI_HOME/em_targets_relocated_${DATE}.log # Pull JDK path from agent config export SET_JAVA_HOME=`grep JAVA_HOME $AGENT_HOME/agent_inst/sysman/config/emd.properties` export $SET_JAVA_HOME echo "JAVA_HOME: $JAVA_HOME" # Pull EM bin path from agent config export SET_EMD_ROOT=`grep emdRoot= $AGENT_HOME/agent_inst/sysman/config/emd.properties` export $SET_EMD_ROOT echo "EMD_ROOT: $emdRoot \n" export PATH=$JAVA_HOME/bin:$PATH ############################### # Checking EMCLI status is OK # ############################### export EM_STATUS=`$EMCLI_HOME/emcli sync` if [ "$EM_STATUS" = "Synchronized successfully" ]; then echo "OMS is up and reachable by EMCLI :)" echo else echo "EMCLI SYNC command from `hostname` failed with: \n $EM_STATUS \n \n The OMS may be down!" | mailx -s "EMCLI Alert: `hostname` unable to reach OMS" $MAIL_LIST exit fi ######################################################################## # Create a function to deal with the relocation of targets if required # ######################################################################## function MigrateTarget { echo "Starting migration of $DB_TARGET and $LSN_TARGET targets within EM12c..." echo echo "Establishing connection to OMS..." echo echo "Moving database target $DB_TARGET from $SRC_HOST to $DEST_HOST..." $EMCLI_HOME/emcli relocate_targets -src_agent=$SRC_HOST:$AGENT_PORT -dest_agent=$DEST_HOST:$AGENT_PORT -target_name=$DB_TARGET -target_type=oracle_database -copy_from_src -force=yes -ignoreTimeSkew=yes echo echo "Moving listener target $LSN_TARGET from $SRC_HOST to $DEST_HOST..." $EMCLI_HOME/emcli relocate_targets -src_agent=$SRC_HOST:$AGENT_PORT -dest_agent=$DEST_HOST:$AGENT_PORT -target_name=$LSN_TARGET -target_type=oracle_listener -copy_from_src -force=yes -ignoreTimeSkew=yes echo echo "Clearing all severity states..." echo $emdRoot/bin/emctl clearstate agent echo echo "Uploading agent metrics..." echo $emdRoot/bin/emctl upload agent echo echo "Checking status of relocated targets..." echo $EMCLI_HOME/emcli get_targets -targets="${DB_TARGET}:oracle_database;${LSN_TARGET}:oracle_listener" echo echo "EM migration of targets completed" } ######################################################### # Run EM relocate checks for all running PMON processes # ######################################################### for SID in `ps -ef args | grep [o]ra_pmon | egrep -iv $EXCEPTION_SID | awk -F_ '{print $3}' | sort` do echo "PMON process for '${SID}' database instance found, running checks... \n" SID_UC=`echo $SID | tr '[:lower:]' '[:upper:]'` # Get EM repostory target name for working SID DB_TARGET=`$EMCLI_HOME/emcli execute_sql -sql="select target_name from sysman.mgmt_targets where \ target_type ='oracle_database' and upper(target_name) like '${SID_UC}%%';" -targets="${EMDB}:oracle_database" -credential_set_name="DBCredsSYSDBA"| awk 'NR==8'` LSN_TARGET=`$EMCLI_HOME/emcli execute_sql -sql="select target_name from sysman.mgmt_targets where \ target_type ='oracle_listener' and upper(target_name) like 'LISTENER_${SID_UC}%%';" -targets="${EMDB}:oracle_database" -credential_set_name="DBCredsSYSDBA"|grep $DOMAIN` # Connect to OMS and get current hostname SRC_HOST=`$EMCLI_HOME/emcli execute_sql -sql="select host_name from sysman.mgmt_targets where \ target_name = '${DB_TARGET}'" -targets="${EMDB}:oracle_database" -credential_set_name="DBCredsSYSDBA"|grep $DOMAIN` if [ "$DEST_HOST" = "$SRC_HOST" ]; then echo "EM target hostname maches this host (${DEST_HOST}). No relocation required!" echo else # When target is not found in EM, send an warning email if [ "$SRC_HOST" = "" ]; then echo "Target not found or not registered in EM!" echo echo "The $SID_UC database was found running on $DEST_HOST but was not found as a target within Enterprise Manager.\n \n Please register the target if applicable, or check the log file for errors: \n ${DEST_HOST}:${LOGFILE}" | mailx -s "EMCLI Alert: $SID_UC target found running on $DEST_HOST..." $MAIL_LIST else echo "EM target hostname (${SRC_HOST}) differs from this host (${DEST_HOST}). Executing relocation Script..." # Call MigrateTarget function to relocate targets MigrateTarget > $LOGFILE echo "EM migration of targets completed!" # Email confirmation and status cat $LOGFILE | mailx -s "EMCLI Alert: $SID_UC targets relocated from $SRC_HOST to $DEST_HOST" $MAIL_LIST echo "Output logged to $LOGFILE" echo fi fi done # Remove lock file from earlier rm $LOCKFILE ################# # End of script # #################