Date Created: Fri 11-Jan-2008

Related Document Categories:

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/


1. Introduction
This article outlines WebSphere Application Server cluster management scripts to allow third party control programs e.g. "Control-M" to start and stop WebSphere clusters.

2. Overview
A real life scenario of a UNIX tool called Control-M which is used for large scale batch processing.

Control-M requires an interface to be able to automate the start and stop of clusters. To keep things simple the idea is to provide a shell script to Control-M which is called and a response value is returned to stdout based on the outcome of the script.

The shell scripts will be called by Control-M and will take care of calling JACL routines using the wsadmin.sh –f command. When a script has verified the status of the cluster after stopping or starting it will return an integer value of ‘0’ for success and ‘1’ for failure.

Note: Any UNIX process or shell script can be used to call start.sh and stop.sh. Also the JACL scripts as called buy the start/stop scripts can also be updated to use Jython. At some time in the future I will provide Jython scripts.

Start Cluster Stop cluster
Shell script name: start.sh Shell script name: stop.sh
Return values to stdout:
0 = Success
1 =Faliure
Return values to stdout:
0 = Success
1 =Faliure
JACL script name: <to be defined> JACL script name: <to be defined>
Timing variables:

loops – configurable amount of main loops
maxLoopTimeout – this is the timeout if server is not stooped in n-loops
interval – Polling interval to check server status (minutes)
Timing variables:

loops – configurable amount of main loops
maxLoopTimeout – this is the timeout if server is not stooped in n-loops
interval – Polling interval to check server status (minutes)
3. Location
The start.sh and stop.sh scripts exist in the <was>/bin/scripts/control folder

They reference the jacl scripts contained in the <was>/bin/scripts/admin folder

4. Start Flow

4.1 Start .sh
start.sh
#!/bin/ksh
# BASH Functions file
#
# Author: Steve Robinson
# Date Created: October 2007
# Arguments: clusterName
# Decription: This script calls WAS JACL to start a WAS cluster.
# Version: 1.2
# History:
# 24-October-2007 - version 1.0 - Created.
# 24-October-2007 - version 1.1 - Added prefix for environments for basePath
# 28-December-2007 - version 1.2 Added test to ensure cluster exists
#
###################################################################################
## Global Variables
###################################################################################
localHost=$(hostname)
_DEBUG="on"
#basePath=/apps/was/ws60/inst60/bin
basePath=$WAS_SCRIPT_ROOT

###################################################################################
## Debug Function
###################################################################################
DEBUG()
{
[ "$_DEBUG" == "on" ] && $@ || :
}

####################################################################################
## loopArgs Function
####################################################################################

loopArgs() {
DEBUG echo "Entering loopArgs.. "

count=1
until [ "$*" = "" ]
do
DEBUG echo "Argument number $count : $1 "
shift
count=`expr $count + 1`
done
DEBUG echo "Exiting loopArgs.. "

}

####################################################################################
## getStatus Function
####################################################################################
getStatus() {
DEBUG echo "Getting cluster status ..."
}

####################################################################################
## startCluster Function
####################################################################################
startCluster() {
DEBUG echo "Starting cluster ..."
#We use a retunVal to keep stdout clear
returnVal=`$basePath/wsadmin.sh -f $basePath/scripts/admin/start_clusters.jacl $clusterName`

sleeptime=5
count=1
start=$SECONDS
until [ $count = 30 ]
do
DEBUG echo "Loop $count"
DEBUG echo $SECONDS
sleep ${sleeptime}
DEBUG echo $SECONDS
statusStopped=`$basePath/wsadmin.sh -f $basePath/scripts/admin/show_cluster_status.jacl $clusterName | grep running`

if [ "$statusStopped" = "running" ]
then
DEBUG echo "Success: Cluster $clusterName is running."
#Write out a value of 0 for Control-M (Success)
DEBUG echo "exiting with value 0"
exit 0
else
DEBUG echo "Status: Called show_cluster_status.jacl: Cluster not yet running...loop again..."
fi

count=`expr $count + 1`
done
DEBUG echo "Failure: Timeout due to MAX Loop count: ... Exiting loop"
#Write out a value of 0 for Control-M (failure)
DEBUG echo "exiting with value 1"
exit 1

}

#####################################################################################
## Main Function
#####################################################################################

main() {
DEBUG echo "Status: Entering Main.. "
DEBUG echo "Status: Getting Cluster $clusterName ..."
#$basePath/wsadmin.sh -f ./show_clusters.jacl
# By enclosing an expression in backticks, you tell the shell to assign the result of a Linux command to a variable, instead of printing it to the screen.
# Cannot use just backticks to capture the output into a variable as wsadmin writes to stdout so had to use grep

statusStopped=`$basePath/wsadmin.sh -f $basePath/scripts/admin/show_cluster_status.jacl $clusterName | grep stopped`
DEBUG echo "Status: statusStopped=$statusStopped"
#If statusStopped is null then it must be running or partially started

if [ "$statusStopped" = "stopped" ]
then
DEBUG echo "Status: Cluster $clusterName is stopped."
DEBUG echo "Status: Calling start cluster: $clusterName"
#Starting the cluster
startCluster
else
statusRunning=`$basePath/wsadmin.sh -f $basePath/scripts/admin/show_cluster_status.jacl $clusterName | grep running`
if [ "$statusRunning" = "running" ]
then
DEBUG echo "Success: Cluster $clusterName is already running"
DEBUG echo "exiting with value 0"
exit 0

else
DEBUG echo "Falure: Error when calling show_cluster_status.jacl"
DEBUG echo "exiting with value 1"
exit 1

fi

fi


DEBUG echo "Exit: Exiting Main and Terminating script ... "

}


####################################################################################
## Main Entry Point
####################################################################################
DEBUG echo There are $# arguments to $0: $*
#Checking environment variables
if [ ! "$1" ]
then
DEBUG echo "Falure: You must pass in a cluster name"
DEBUG echo "exiting with value 1"
exit 1
else

#DEBUG echo "First argument $1"
clusterName=$1
DEBUG echo "clusterName = $clusterName"

#check if environment variable exists

if [ ${#WAS_SCRIPT_ROOT} = 0 ]
then
#Environment variable doesn't exist
DEBUG echo "Falure: Environment Variable $WAS_SCRIPT_ROOT doesn't exist"
DEBUG echo "exiting with value 1"
exit 1
fi

#check if cluster exists
statusTrue=`$basePath/wsadmin.sh -f $basePath/scripts/admin/cluster_exists.jacl $clusterName | grep true`
if [ "$statusTrue" = "true" ]
then
#Call main loop and pass the command line args to main function
DEBUG echo "basePath=$basePath"
main $*
fi
#Cluster doesn't exist
DEBUG echo "Falure: Cluster $clusterName doesn't exist"
DEBUG echo "exiting with value 1"
exit 1

fi


5. Stop Flow

5.1 Stop .sh
stop.sh

#!/bin/ksh
# BASH Functions file
#
# Author: Steve Robinson
# Date Created: October 2007
# Arguments: clusterName
# Decription: This script calls WAS JACL to stop a WAS cluster.
# Version: 1.2
# History:
# 24-October-2007 - version 1.0 - Created.
# 24-October-2007 - version 1.1 - Added prefix for environments for basePath
# 28-December-2007 - version 1.2 Added test to ensure cluster exists
###################################################################################
## Global Variables
###################################################################################
localHost=$(hostname)
_DEBUG="on"
#basePath=/apps/was/ws60/inst60/bin
basePath=$WAS_SCRIPT_ROOT

###################################################################################
## Debug Function
###################################################################################
DEBUG()
{
[ "$_DEBUG" == "on" ] && $@ || :
}

####################################################################################
## loopArgs Function
####################################################################################

loopArgs() {
DEBUG echo "Entering loopArgs.. "

count=1
until [ "$*" = "" ]
do
DEBUG echo "Argument number $count : $1 "
shift
count=`expr $count + 1`
done
DEBUG echo "Exiting loopArgs.. "

}

####################################################################################
## getStatus Function
####################################################################################
getStatus() {
DEBUG echo "Getting cluster status ..."
}

####################################################################################
## stopCluster Function
####################################################################################
stopCluster() {
DEBUG echo "Stopping cluster ..."
#We use a retunVal to keep stdout clear
returnVal=`$basePath/wsadmin.sh -f $basePath/scripts/admin/stop_clusters.jacl $clusterName`

sleeptime=5
count=1
start=$SECONDS
until [ $count = 30 ]
do
DEBUG echo "Loop $count"
DEBUG echo $SECONDS
sleep ${sleeptime}
DEBUG echo $SECONDS
statusStopped=`$basePath/wsadmin.sh -f $basePath/scripts/admin/show_cluster_status.jacl $clusterName | grep stopped`

if [ "$statusStopped" = "stopped" ]
then
DEBUG echo "Success: Cluster $clusterName is stopped."
#Write out a value of 0 for Control-M (Success)
DEBUG echo "exiting with value 0"
exit 0
else
DEBUG echo "Status: Called show_cluster_status.jacl: Cluster not yet stopped...loop again..."
fi

count=`expr $count + 1`
done
DEBUG echo "Failure: Timeout due to MAX Loop count: ... Exiting loop"
#Write out a value of 0 for Control-M (failure)
DEBUG echo "exiting with value 1"
exit 1

}

#####################################################################################
## Main Function
#####################################################################################

main() {
DEBUG echo "Status: Entering Main.. "
DEBUG echo "Status: Getting Cluster $clusterName ..."
#$basePath/wsadmin.sh -f ./show_clusters.jacl
# By enclosing an expression in backticks, you tell the shell to assign the result of a Linux command to a variable, instead of printing it to the screen.
# Cannot use just backticks to capture the output into a variable as wsadmin writes to stdout so had to use grep

statusRunning=`$basePath/wsadmin.sh -f $basePath/scripts/admin/show_cluster_status.jacl $clusterName | grep running`
DEBUG echo "Status: statusRunning=$statusRunning"
#If statusRunning is null then it must be stopped or partially started

if [ "$statusRunning" = "running" ]
then
DEBUG echo "Status: Cluster $clusterName is running."
DEBUG echo "Status: Calling stop cluster: $clusterName"
#Stopping the cluster
stopCluster
else
statusStopped=`$basePath/wsadmin.sh -f $basePath/scripts/admin/show_cluster_status.jacl $clusterName | grep stopped`
if [ "$statusStopped" = "stopped" ]
then
DEBUG echo "Success: Cluster $clusterName is already stopped"
DEBUG echo "exiting with value 0"
exit 0

else
DEBUG echo "Falure: Error when calling show_cluster_status.jacl"
DEBUG echo "exiting with value 1"
exit 1

fi

fi


DEBUG echo "Exit: Exiting Main and Terminating script ... "

}


####################################################################################
## Main Entry Point
####################################################################################
DEBUG echo There are $# arguments to $0: $*
#Checking environment variables
if [ ! "$1" ]
then
DEBUG echo "Falure: You must pass in a cluster name"
DEBUG echo "exiting with value 1"
exit 1
else

#DEBUG echo "First argument $1"
clusterName=$1
DEBUG echo "clusterName = $clusterName"

#check if environment variable exists
if [ ${#WAS_SCRIPT_ROOT} = 0 ]
then
#Environment variable doesn't exist
DEBUG echo "Falure: Environment Variable $WAS_SCRIPT_ROOT doesn't exist"
DEBUG echo "exiting with value 1"
exit 1
fi

#check if cluster exists
statusTrue=`$basePath/wsadmin.sh -f $basePath/scripts/admin/cluster_exists.jacl $clusterName | grep true`
if [ "$statusTrue" = "true" ]
then
#Call main loop and pass the command line args to main function
DEBUG echo "basePath=$basePath"
main $*
fi
#Cluster doesn't exist
DEBUG echo "Falure: Cluster $clusterName doesn't exist"
DEBUG echo "exiting with value 1"
exit 1
fi

6. show_cluster_status.jacl

set i 0
set clusterName [lindex $argv $i]
set clusterId [$AdminConfig getid /ServerCluster:$clusterName/]
puts "ClusterName is: $clusterName"

if {[llength $clusterId] == 1} {
#puts "Cluster '$clusterName' exists."
set clusterObject [$AdminControl completeObjectName type=Cluster,name=$clusterName,*]
#puts "Checking cluster status"
set clusterStatus [$AdminControl getAttribute $clusterObject state]
#puts "Current status of $clusterName is $clusterStatus"
set running "websphere.cluster.running"
set partialstart "websphere.cluster.partial.start"
set starting "websphere.cluster.starting"
set stopped "websphere.cluster.stopped"

if {[string compare $clusterStatus $starting] == 0} {
puts "starting"
exit
}

if {[string compare $clusterStatus $stopped] == 0} {
puts "stopped"
exit

}

if {[string compare $clusterStatus $running] == 0} {
puts "running"
exit

}

if {[string compare $clusterStatus $partialstart ] == 0} {
puts "partialstart "
exit

}




} else
{
puts "Error"
{

7. cluster_exists.jacl



puts "evaluating if argc"
puts "$argc"

if {$argc > 0} {
puts "$argc"
set i 0
set clusterName [lindex $argv $i]
set clusterId [$AdminConfig getid /ServerCluster:$clusterName/]
puts "ClusterName is: $clusterName"
if {[llength $clusterId] != 0} {
puts "true"
} else {
puts "false"
}

} else {
puts "Please supply cluster names as command line argument."
puts "Usage: wsadmin.sh -f cluster_exists.jacl clusterName"

}
External Dependencies

stop.sh and start.sh require stop_clusters.jacl, show_cluster_status.jacl and cluster_exists.jacl

stop_clusters.jaclcluster_exists.jaclshow_cluster_status.jaclstart_clusters.jacl


Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/

Steve Robinson - IBM Champion 2013

About Me

Steve Robinson has been working in IT for over 20 years and has provided solutions for many leading brands around the world. Steve specialises in JEE, DevOps and Thought Leadership.

In January 2013, I was awarded the prestigous 'IBM Champion' accolade.


  • Linked In
  • Twitter
  • About Me
  • Contact Me

Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

Courses

  • WebSphere Courses

Articles

WebSphere Categories

Other Categories