Monday, December 8, 2025

finalize

 #!/bin/bash

# ===============================================================

# adop finalize monitor for environments using EBSapps.env

# Works 100% with: cd ~; . ./EBSapps.env run

# ==============================================================


# ----------- Change only these 4 lines -------------------------

MAIL_TO="appsdba-team@company.com,oncall@company.com"

MAIL_FROM="ebs-prod@company.com"

HOST_SHORT="PROD"                      # for email subject

TEAMS_WEBHOOK=""                        # optional: add if you want Teams post

# -------------------------------------------------------------


# Source the universal EBSapps.env (this is the key line)

cd $HOME

. ./EBSapps.env run > /dev/null 2>&1


# Verify we are in RUN edition (should always be true on run filesystem)

if [ "$FILE_EDITION" != "run" ]; then

    echo "Error: Not on run filesystem. Current: $FILE_EDITION"

    exit 1

fi


# Get active patching session

SESSION_ID=$(sqlplus -s apps/${APPS_PWD} <<EOF

set pages 0 head off feed off

select max(adop_session_id) from ad_adop_sessions where active_flag='Y';

EOF

)


[ -z "$SESSION_ID" ] || [ "$SESSION_ID" = " " ] && { echo "No active cycle"; exit 0; }


# Get current phase (finalize or cutover)

PHASE_STATUS=$(sqlplus -s apps/${APPS_PWD} <<EOF

set pages 0 head off

select phase||'|'||status

from ad_adop_session_phases

where adop_session_id=$SESSION_ID

  and phase in ('finalize','cutover')

order by start_date desc

fetch first 1 rows only;

EOF

)


[ -z "$PHASE_STATUS" ] && { echo "Finalize/cutover not started"; exit 0; }


PHASE=$(echo $PHASE_STATUS | cut -d'|' -f1)

STATUS=$(echo $PHASE_STATUS | cut -d'|' -f2)


# Progress calculation (only meaningful during finalize)

PROGRESS="Not started"

ETA="N/A"

if [[ "$PHASE" == "finalize" && "$STATUS" =~ RUNNING|COMPLETING ]]; then

    PROGRESS=$(sqlplus -s apps/${APPS_PWD} <<EOF

set pages 0 head off

select round(100 * (total_actions_completed / nullif(total_actions,0)), 2) || '%'

from ad_adop_session_actions where adop_session_id=$SESSION_ID;

EOF

)


    ELAPSED_MIN=$(sqlplus -s apps/${APPS_PWD} <<EOF

set pages 0 head off

select round((sysdate - start_date)*1440)

from ad_adop_session_phases

where adop_session_id=$SESSION_ID and phase='finalize';

EOF

)


    if (( $(echo "$PROGRESS < 99.5" | bc -l 2>/dev/null) )); then

        REMAINING_MIN=$(echo "scale=0; (100 - $PROGRESS) / 100 * $ELAPSED_MIN / ( $PROGRESS / 100 )" | bc -l 2>/dev/null)

        ETA=$(date -d "+ ${REMAINING_MIN%.*} minutes" +"%H:%M %Z" 2>/dev/null || echo "Calculating...")

    else

        ETA="Any minute now"

    fi

fi


# Build & send email

HOST=$(hostname)

cat << EOF > /tmp/adop_finalize_email.txt

Subject: [EBS $HOST_SHORT] adop $PHASE = $PROGRESS


EBS ADOP Finalize Monitor - $(date)


Environment   : $HOST (Production)

Session ID    : $SESSION_ID

Current Phase : $PHASE

Status        : $STATUS

Progress      : $PROGRESS

Estimated End : $ETA


Manual check:

cd ~; . ./EBSapps.env run; adop -status -detail


— Automated message from DBA team

EOF


# Send only when running

if [[ "$STATUS" == "RUNNING" || "$STATUS" == "COMPLETING" ]]; then

    mailx -s "[EBS $HOST_SHORT] adop $PHASE = $PROGRESS" -r "$MAIL_FROM" "$MAIL_TO" < /tmp/adop_finalize_email.txt


    # Optional: post to Teams

    [ -n "$TEAMS_WEBHOOK" ] && curl -H "Content-Type: application/json" -d "{\"text\":\"EBS $HOST_SHORT: adop $PHASE = **$PROGRESS** (ETA $ETA)\"}" "$TEAMS_WEBHOOK"

fi


echo "$(date) - $PHASE $PROGRESS - email sent"

No comments:

Post a Comment