#!/bin/bash
######################################################################
# Author       : Vishal Gupta
# Date Created : 05-aug-2007
# Purpose      : Merge statspack from multiple sources into single database
#
# Modification History
# Date      Ver Author         Remarks
# --------  --- -------------- ---------------------------------------
# 05/08/07  0.1 Vishal Gupta   First Draft
######################################################################

ORACLE_ENV_TYPE=$1
PERFSTAT_VERSION=$2
INSTANCE_NAME="$3"
ORACLE_SID=STATSREP
#EMAILS="vishal.gupta@rabobank.com"
EMAILS="fm.eu.DBAAlerts@rabobank.com"
TIMESTAMP=`date +%Y%m%d_%H%M%S`
LOGFILE=/appl/oracle/admin/logs/statspack_consolidation_${ORACLE_ENV_TYPE}_${PERFSTAT_VERSION}_$TIMESTAMP.log

export ORACLE_HOME=/appl/oracle/product/10.2.0
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export NLS_LANG="ENGLISH_UNITED KINGDOM.WE8ISO8859P1"
export PATH=$ORACLE_HOME/bin:$PATH

function Usage()
{
  echo "`basename $0`  <envtype> <db_version> [<instance_name>]"
  echo ""
  echo "     where <envtype>         = PROD|TEST|DEV "
  echo "           <db_version>      =  10.2 | 9.2"
  echo "           [<instance_name>] =  optionally instance for single instance consolidation"
  exit 1;
}

if [ $# -lt 2 ]
then
  Usage;
fi


case "$ORACLE_ENV_TYPE" in
   PROD|TEST|DEV) ;;
   *) Usage;;
esac

case "$PERFSTAT_VERSION" in
   10.2|9.2) ;;
   *) Usage;;
esac




sqlplus /nolog > $LOGFILE <<EOF
conn perfstat_control/tun1ngstats@${ORACLE_SID}
WHENEVER SQLERROR EXIT 1
set verify off
@`dirname $0`/statspack_consolidation.sql $ORACLE_ENV_TYPE $PERFSTAT_VERSION  "$INSTANCE_NAME"
EOF



if [ $? -eq 0 ]
then
   SUBJECT_SUFFIX="successful"
   HPOV_SEVERITY=NORMAL
else
   SUBJECT_SUFFIX="failed"
   HPOV_SEVERITY=MAJOR
fi


if [ ! -z "$INSTANCE_NAME" ]
then
  SUBJECT_SUFFIX="$INSTANCE_NAME - $SUBJECT_SUFFIX"
fi

#/opt/OV/bin/OpC/opcmsg severity=$HPOV_SEVERITY application=Oracle msg_grp=Oracle object="Statspack Consolidation"  msg_text="Statspack consolidation - $ORACLE_ENV_TYPE - $PERFSTAT_VERSION - $SUBJECT_SUFFIX" 

mailx -s "Statspack consolidation - $ORACLE_ENV_TYPE - $PERFSTAT_VERSION - $SUBJECT_SUFFIX" "$EMAILS" <<EOF
Statspack consolidation - $ORACLE_ENV_TYPE - $PERFSTAT_VERSION - $SUBJECT_SUFFIX
EOF
