OEM target property update 4

 #!/bin/bash


##########################################

# Part 1: Fetch Missing/Incorrect Properties

##########################################


# Define SQL query to fetch missing/incorrect properties

SQL_QUERY_MISSING="

SELECT 

  h.target_name,

  tp.property_name,

  tp.property_value,

  cd.support_group,

  cd.install_status,

  cd.oracle_home,

  cd.u_cmdb_ci_used_for,

  cd.u_cmdb_owner_group,

  cd.name,

  cd.correlation_id,

  cd.version

FROM 

  sysman.mgmt\$target h

  LEFT JOIN mgmt\$target_properties tp 

    ON h.target_name = tp.target_name

    AND tp.property_name IN ('udtp_2', 'orcl_gtp_contact', 'orcl_gtp_lifecycle_status')

  LEFT JOIN cmDAILY cd 

    ON h.target_name = cd.name

WHERE 

  (tp.property_value IS NULL OR tp.property_name IS NULL)

  OR (tp.property_name = 'orcl_gtp_contact' AND tp.property_value != cd.support_group)

  OR (tp.property_name = 'orcl_gtp_lifecycle_status' AND tp.property_value != cd.install_status)

  OR (tp.property_name = 'udtp_2' AND tp.property_value IS NULL)

  OR cd.oracle_home IS NULL;

"


# Execute SQL query and store the output in a temporary file

sqlplus -S user/password@your_db <<EOF > /tmp/missing_properties.txt

$SQL_QUERY_MISSING

EOF


##########################################

# Part 2: Fetch Associated Targets and Generate EMCLI Commands

##########################################


# Loop through each result and generate emcli update commands

while IFS=" " read -r target_name property_name property_value support_group install_status oracle_home; do

  # Skip header or empty lines

  if [[ "$target_name" == "TARGET_NAME" || -z "$target_name" ]]; then

    continue

  fi

  

  # SQL query to fetch associated target types (oracle_database, rac_database, host, agent, listener) for the target

  SQL_TARGETS="SELECT assoc_target_name, target_type 

               FROM sysman.mgmt\$target_associations 

               WHERE assoc_target_name = '$target_name';"

  

  # Execute SQL to get associated targets

  sqlplus -S user/password@your_db <<EOF > /tmp/associated_targets.txt

  $SQL_TARGETS

EOF


  # Loop through each associated target and generate the appropriate emcli command

  while IFS=" " read -r assoc_target_name target_type; do

    # Skip header or empty lines

    if [[ "$assoc_target_name" == "ASSOC_TARGET_NAME" || -z "$assoc_target_name" ]]; then

      continue

    fi


    # Generate the appropriate emcli command based on the missing/incorrect property

    if [[ "$property_name" == "orcl_gtp_contact" && "$property_value" != "$support_group" ]]; then

      emcli modify_target -name="$assoc_target_name" \

                          -type="$target_type" \

                          -properties="orcl_gtp_contact->$support_group" \

                          -separator-properties="|" \

                          -subseparator-properties="->" \

                          -on_agent

      echo "Updated orcl_gtp_contact for $assoc_target_name ($target_type) to $support_group"


    elif [[ "$property_name" == "orcl_gtp_lifecycle_status" && "$property_value" != "$install_status" ]]; then

      emcli modify_target -name="$assoc_target_name" \

                          -type="$target_type" \

                          -properties="orcl_gtp_lifecycle_status->$install_status" \

                          -separator-properties="|" \

                          -subseparator-properties="->" \

                          -on_agent

      echo "Updated orcl_gtp_lifecycle_status for $assoc_target_name ($target_type) to $install_status"


    elif [[ "$property_name" == "udtp_2" && -z "$property_value" ]]; then

      hosting_value="correct_value"  # Replace with actual logic for the correct value

      emcli modify_target -name="$assoc_target_name" \

                          -type="$target_type" \

                          -properties="udtp_2->$hosting_value" \

                          -separator-properties="|" \

                          -subseparator-properties="->" \

                          -on_agent

      echo "Updated udtp_2 for $assoc_target_name ($target_type) to $hosting_value"

    fi


  done < /tmp/associated_targets.txt


done < /tmp/missing_properties.txt

Comments

Popular posts from this blog

SQL Loader V3

Db Unix

Standby redo log add