Saturday 25 October 2014

Unix Script to Get DSLog after Datastage job finish


#!/usr/bin/ksh
# --------------------------------------------------------------------------------------------------
# name: GetDSLog.ksh
#
# purpose:    
# extract datastage log entries for a given job and save them to
# a designated file on the server.
#
# usage: GetDSLog <dsjob> <logfile> <Project>
#
# parameters:
#
#     $1 - <dsjob> is the name of the DataStage job, required
# $2 - <logfile> is the name including path to write the log entries to, required
#     $3 - <Project> is the name of the datastage project, required. If not supplied,
# the project is looked up in the app_parameter table.
# examples:    
#
# GetDSLog.ksh Dsjob sequencer
 "/datastage/common/log/member.log"
#
#
# -----------------------------------------------------------------------------
# set environment. Lookup the SetEnv.ksh in the user's home directory first
# and then in the /ohd/common/bin directory.
# -----------------------------------------------------------------------------
# set -vx

# -----------------------------------------------------------------------------
# write log entries
# $1 is the actual message to log
# -----------------------------------------------------------------------------
upLog()
{
\echo "$1"
}

# -----------------------------------------------------------------------------
# abend the job and return -1 exit code
# $1 is the actual message to display
# -----------------------------------------------------------------------------
glAbend()
{
upLog "$1"
  exit 1
}      

# -----------------------------------------------------------------------------
# assign a job parameter
# $1 is the name of the parameter
# $2 is the value
# -----------------------------------------------------------------------------
AssignParam()
{
param=$1
value=$2
if [[ -n $(\echo "$JobParam" | \grep -w $param) ]]; then
ParamList=$ParamList" -param $param=$value"
\echo "$param=$value" >> $CurrParams
fi
}

# -----------------------------------------------------------------------------
# Execute/run a job
# return: 0 if successful
# 1 otherwise
# -----------------------------------------------------------------------------
RunAndCheck()
{
#
# reset the extract job if it is not in a runable state
#
   dsJStat=$($DSBin/dsjob -jobinfo $dsProject $eljob | \awk -F: '{print $2}')

if [[ -z $dsJStat ]]; then
upLog "unable to get job status"
return 1
fi

RunFailed=$(\echo $dsJStat |\grep "RUN FAILED")
NotCompiled=$(\echo $dsJStat |\grep "NOT COMPILED")
Stopped=$(\echo $dsJStat |\grep "STOPPED")
Unknown=$(\echo $dsJStat |\grep "UNKNOWN")

   if [[ -n $RunFailed || -n $NotCompiled || -n $Stopped || -n $Unknown ]]; then
upLog "restting......: $eljob"
    $DSBin/dsjob -run -mode RESET $dsProject $eljob
    if [[ $? != 0 ]]; then
upLog "unable to reset job $eljob!"
return 1
fi
fi

ParamList=" "
  JobParam=$($DSBin/dsjob -lparams $dsProject $eljob)
AssignParam "jobName" "$dsjob"
AssignParam "logFileName" "$logto"

$DSBin/dsjob -run $ParamList -jobstatus -warn $MaxWarns $dsProject $eljob > /dev/null

   rc=$?
   upLog "ds exit code..: $rc"

   dsJStat=$($DSBin/dsjob -jobinfo $dsProject $eljob | \awk -F: '{print $2}')

   if [[ -n $(\echo "$dsJStat" | \grep 'RUN OK') ]]; then Stat=OK; fi
   if [[ -n $(\echo "$dsJStat" | \grep 'RUN with WARNINGS') ]]; then Stat=WARNING; fi
   if [[ -n $(\echo "$dsJStat" | \grep 'RUN FAILED') ]]; then Stat=FATAL; fi

   if [[ $Stat = FATAL ]]; then

      dsID=$($DSBin/dsjob -lognewest $dsProject $eljob $Stat| \awk -F= '{print $NF}')
      dsErr=$($DSBin/dsjob -logdetail $dsProject $eljob $dsID)
      upLog "Status........: fail!"
upLog "$dsErr"
      return 1

   else

      upLog "Status........: success!"
return 0
fi
}

# -----------------------------------------------------------------------------
# main
# -----------------------------------------------------------------------------
if [ $# -lt 2 ] ; then
  printf "Error: $0 - Required parameter(s) was not supplied!\n\n"
  printf "Usage: $0 <dslog> <logfile> [<Project>]\n\n"
  printf "where: <dsjob>     : is the name of the DataStage job and is required\n"
  printf "       <logfile>   : is the name of the log file including path, required.\n"
  printf "       <Project>   : is the name of the project and is optional. If not supplied\n"
  printf "                     the default project in the parameter file is used.\n\n"
  exit 1
fi

Me=$(\echo $0 | \awk -F/ '{print $NF}')
mypath=$(\echo $0 | \awk -F"$Me" '{print $1}')

dsjob=$1
logto=$2
dsProject=$3

export DSHOME=`cat /.dshome`
DSBin=$DSHOME/bin
Start=$(\date +%Y%m%d%H%M%S)
MaxWarns=50

filename=$(\echo $logto |\awk -F/ '{print $NF}')
if [[ -n $(\echo $logto |\grep "\/") ]]; then
logpath=$(\echo $logto |\awk -F"/$filename" '{print $1}')
else
logpath="./"
fi

if [[ $logpath = "./" || $logpath = "." ]]; then logpath=$PWD; fi

if [[ ! -d $logpath ]]; then
glAbend "the directory given for the log file ($logto) does not exist!"
fi

logto="$logpath/$filename"

eljob="jcExtractLog.$(\echo $dsjob |\sed 's/\./_/g')"

upLog "Extract Job Started"
upLog "dsJob.........: $dsjob"
upLog "log file......: $logto"
upLog "Project.......: $dsProject"

RunAndCheck

\echo "------------------------------" >> $logFile

exit 0



No comments:

Post a Comment