#!/bin/sh
#
# Ops Center Action script to demo the script function get_list_of_targets()
#
# Example Use Case:
#
# - Customer is using a monitoring tool for all Production Systems.
# - Monitoring must be disabled per System before submiting a patch job
# - Monitoring must be enabled again when patch job is done
# - CLI Access to Monitoring tool is accessible from EC only
#
# Goal: need an automated solution for patching large group of systems
#
# Solution: 
#
# A standard patch job will contain three seperate tasks:
#
# 1. Task: Profile to run an Action Script to disable Monitoring of later on
#          patched systems. Target for this Task: EnterPrise Controller
#
# 2. Task: Profile with Patch Baseline. Target:  All systems planned to be patched 
#
# 3. Task: Profile to run an Action Script to enable Monitoring again on previous
#          patched systems
#
# The function "get_list_of_targets()" can be used in these Action Scripts to get
# the list of system names planned to get patched in Task 2
#
# Hint: Uploading Action Scripts as 'Probe' will allow you to test them even during Simulate mode 
#

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

get_list_of_targets() {

#
# script function to extract the list of targets of the next or previous job task.
#
# Targets of the next task will get listed if this script is run during the first 
# task of a job. Otherwise the previous task will be used.
#

cacao_log=/var/cacao/instances/scn-agent/logs/cacao.0
show_job=/opt/sun/xvm/bin/show_job.pl
infile=/tmp/_get_targets_1.$$
tmpfile=/tmp/_get_targets_2.$$
awk=/usr/bin/nawk

if [ ! -f $cacao_log ]
then
  echo "ERROR: File $cacao_log not found ! Exiting ..." 1>&2
  exit 1
fi

if [ ! -x $show_job ]
then
  echo "ERROR: Script $show_job does not exist or is not executable !" 1>&2
  echo 1>&2
  echo "Note: The target of this Task must be the Enterprise Controller only ! Exiting ..." 1>&2
  exit 1
fi

my_jobid=`grep JobIDrunID $cacao_log | tail -1 | sed 's/.*JobIDrunID - //' | cut -d. -f2`

if [ -z "$my_jobid" ]
then
  echo "ERROR: Unable to determine the Job ID ! Exiting ..." 1>&2
  exit 1
fi

$show_job $my_jobid > $infile
res=$?

if [ $res -ne 0 ]
then
  echo "ERROR: $show_job has exited with error code $res ! Exiting ..." 1>&2
  exit 1
fi

lineno=`$awk '/Status       : Running/ { print NR}' $infile | tail -1`
task_lines=`$awk "NR <= $lineno && /: CompositeTierFreeTask/" $infile | wc -l`

if [ "$task_lines" -gt 1 ]
then
  # analyze the previous task for target names
  $awk "NR <= $lineno && /: CompositeTierFreeTask/ { print NR }" $infile > $tmpfile
  first_line=`tail -2 $tmpfile | head -1`
  last_line=`tail -1 $tmpfile`
else
  # analyze the next task for target names
  $awk "NR > $lineno && /: CompositeTierFreeTask/ { print NR }" $infile > $tmpfile
  first_line=`head -1 $tmpfile`
  if [ "`cat $tmpfile | wc -l`" -eq 1 ]
  then 
    last_line=`cat $infile | wc -l`
  else
    last_line=`head -2 $tmpfile | tail -1`
  fi 
fi
$awk -F / " NR >= $first_line && NR <= $last_line && /Target Name  : .*,type=OperatingSystem/  { print substr(\$2,1,length(\$2)-9) }" $infile
rm -f $infile $tmpfile
}
##########################################################################################


# 
# Example usage:
#
# call of function "get-list-of-targets" to get an output with all target names, one per line

get_list_of_targets | while read targetname
do
  echo "Target name: $targetname" >/tmp/target_list
done

