#!/bin/ksh
#
# migrate-lpar: Version 0.1 ETY 2011/05/25
# migrate_lpar.ksh: Version 0.2 2011/05/26
#
# Purpose: Interactive script to automate collection of a kernel trace and snap
#          during an LPM event.
#
# Syntax: migrate-lpar
#

DEBUG="./migrate_lpar.out"

host=`hostname`
tcdir=/tmp/ibmsupt/testcase

cat /dev/null>$DEBUG
echo "\n Security advise: This script will prompt you for some passwords."
echo "\n These passwords will not be stored in any place."
echo
echo "Continue? [y/n]"
read resp

if [[ $resp != "y" ]]; then
  echo "\nAborting test!">> $DEBUG 2>&1
  echo "\nAborting test!"
  exit 1
fi


### Gather source and target info from user
echo
echo "**************************************"
echo "* LPM Validation and Data Collection *"
echo "**************************************"
echo "**************************************">> $DEBUG 2>&1 
echo "* LPM Validation and Data Collection *">> $DEBUG 2>&1 
echo "**************************************">> $DEBUG 2>&1 

echo
echo "Enter the source managed server name:"
read src_server
while [[ -z $src_server ]]; do
  echo "\tERROR: No source managed server specified, please re-enter:"
  read src_server
done

echo " source managed server name: $src_server">> $DEBUG 2>&1

echo "Enter the source VIO server IP address:"
read src_ip
while [[ -z $src_ip ]]; do
  echo "\tERROR: No source VIOS IP specified, please re-enter:"
  read src_ip
done

echo " source VIO server IP address: $src_ip">> $DEBUG 2>&1

echo "Enter the destination managed server name:"
read des_server
while [[ -z $des_server ]]; do
  echo "\tERROR: No destination managed server, please re-enter:"
  read des_server
done

echo " destination managed server name : $des_server">> $DEBUG 2>&1

echo "Enter the destination managed server IP address:"
read des_ip
while [[ -z $des_ip ]]; do
  echo "\tERROR: No destination managed server IP address, please re-enter:"
  read des_ip
done
echo " destination VIO server IP address: $des_ip">> $DEBUG 2>&1

echo "The following LPARs can be found on the source managed server $des_server :"
echo "Please enter the password for the padmin user of the source managed server $des_server when prompted"
ssh padmin@$src_ip lssyscfg  -r lpar |  awk 'BEGIN{FS=","}{print $1"="$2}'| awk 'BEGIN{FS="="}{print $4" "$2}'

echo "Enter the LPAR ID of the lpar to be migrated:"
read lpar
while [[ $lpar -lt 1 ]]; do
  echo "\tERROR: No client LPAR ID specified, please re-enter:"
  read lpar
done

echo "What do you want me to do with that LPAR?"
echo "Enter: v = validate, or m = migrate"
read act
while [[ -z $act ]]; do
  echo "\tERROR: No option specified, please re-enter:"
  read act
done

case $act in
 v|V) set $act=v;;
 m|M) set $act=m;;
 *) echo "\tERROR: No valid option specified, please re-enter:",,
 read act;;
esac
echo "Migration operation: $act">> $DEBUG 2>&1


echo "\nStarting the LPM operation for the following:"
echo
echo "\tSource Server:"
echo "\t\tManaged Server Name:  $src_server"
echo "\t\tManaged Server IP:    $src_ip"
echo "\t\tLPAR ID:             	$lpar"
echo "\tDestination Server:"
echo "\t\tManaged Server Name:  $des_server"
echo "\t\tManaged Server IP:    $des_ip"
echo "\t\tOperation to perform: $act"
echo
echo "Continue? [y/n]"
read resp

if [[ $resp != "y" ]]; then
  echo "\nAborting test!">> $DEBUG 2>&1
  echo "\nAborting test!"
  exit 1
fi



### Write timestamp to cfglog of both servers
ssh padmin@$src_ip echo "Begin LPM log for Managed server $server LPAR $lpar\n `date`" | alog -t cfg
ssh padmin@$des_ip echo "Begin LPM log for Managed server $server LPAR $lpar\n `date`" | alog -t cfg

### Begin migration test
echo "\nExecuting migration test ..."
echo "\tExecute migrlpar ..."
echo "\t\tRunning: migrlpar -o $act -m $src_server -t $des_server --ip $des_ip --id  $lpar -d 5"
echo "\t\tRunning: migrlpar -o $act -m $src_server -t $des_server --ip $des_ip --id  $lpar -d 5">> $DEBUG 2>&1 
ssh padmin@$src_ip migrlpar -o $act -m $src_server -t $des_server --ip $des_ip --id $lpar -d 5
echo "\nEnd LPM log for Managed server $server LPAR $lpar\n"|alog -t cfg

### Stop trace

### Collect fcstat output

### Collect snap

### Return comptrc levels to normal

### Done
echo
