Weblogic Start All Managed Servers WLST Script

The Objective

In this post, we are going to share the WLST jython script we have created to start all the managed servers in the weblogic domain. To be more precise, The Script will start the servers which are not already started or down from the specific weblogic domain.

Once started, The Scripts working architecture can be denoted like this

  1. Connects to AdminServer
  2. Print the Status of the servers in the domain
  3. Starts the server which is currently down or not running.

How to Use this Script?:

  1. Copy the following source code to your terminal and save it as a .py file
  2. Set the Environment using ./setDomainEnv.sh or ./setWLSEnv.sh
  3. Invoke the script with WLST with the following startup arguments (or) options
    1. username (or) key file
    2. password (or) config file
    3. Domain Admin URL

The WLST Script to start all managed servers

import sys
import re
import getopt

def connectt():
	connect(USERNAME, PASSWORD, ADMINURL)

def usage():
	toprint = '''
---------------------------------------------------------
Invoke the script using any one of the following command
---------------------------------------------------------
java weblogic.WLST startwlsdomain.py -u weblogic -p weblogic1 -a t3://mwiapp01:18001 
(or)
java weblogic.WLST startwlsdomain.py -c /home/weblogic/configfile.secure -k /home/weblogic/userkeyfile.secure -a t3://mwiapp01:18001
---------------------------------------------------------
    	'''
    	print toprint

def getdomainstatus():
	servers=cmo.getServers()
	print "-------------------------------------------------------"
	print "\t"+cmo.getName()+" domain current status"
	print "-------------------------------------------------------"
	for server in servers:
	       	status=state(server.getName(),server.getType())
	print "-------------------------------------------------------"     

# Start the servers which are down
def startmanagedservers():
        servers=cmo.getServers()
	domainRuntime()
	for server in servers:
		bean="/ServerLifeCycleRuntimes/"+server.getName()
		serverbean=getMBean(bean)
		#print "serverbean",serverbean
		#print "Current status of the server",server.getName(),"is",serverbean.getState()
		if serverbean.getState() in ("SHUTDOWN", "FAILED_NOT_RESTARTABLE"):
			print "Starting the servers which are in SHUTDOWN and FAILED_NOT_RESTARTABLE status"
			print "Starting the Server ",server.getName()
			start(server.getName(),server.getType())

	serverConfig()

# MAIN

CONFIGFILE="NULL"
KEYFILE="NULL"
USERNAME="NULL"
PASSWORD="NULL"
ADMINURL="NULL"
DOWNSERVERS=[]

if len(sys.argv) < 2:
	print "Invalid Number of arguments, Expected 3 arguments, Found [%d]"%(len(sys.argv))
	print ""

try:
	opts, args = getopt.getopt(sys.argv[1:], "h:u:p:c:k:a:", ["username=", "password=", "configfile=","keyfile=", "help", "adminurl="])
except getopt.GetoptError:
	print "Option is not valid"
	usage();
	sys.exit(2)


for o, a in opts:
	if o in ("-h", "--help"):
		usage()
		sys.exit()
	elif o in ("-c", "--configfile") and (DEFAULTFLAG != "ON"):
		print "INFO: Config file set to =>",a
		CONFIGFILE=a
	elif o in ("-k", "--keyfile") and (DEFAULTFLAG != "ON"):
		print "INFO: Key file set to =>",a
		KEYFILE=a
	elif o in ("-u", "--username"):
		print "INFO: UserName set to =>",a
		USERNAME=a
	elif o in ("-p", "--password"):
		print "INFO: Password set to => ****"
		PASSWORD=a	
	elif o in ("-a", "--adminurl"):
		print "INFO: AdminUrl set to =>",a
		ADMINURL=a
		match = re.match(r'(t3|t3s)(\:)(\/\/)(.*:)(\d+)', ADMINURL)
		if not match:
			print "\nERROR: AdminURL is wrong, Make sure you are using t3/t3s protocol"
			print "Sample AdminURL: t3://localhost:17001"
			sys.exit()	
	else:
		assert False, "ERROR: Option is not supported"
		sys.exit()


if "NULL" in USERNAME and "NULL" in PASSWORD and "NULL" in KEYFILE and "NULL" in CONFIGFILE:
	print "The Script must be started with username and password for AdminServer (or) Keyfile and configfile"
	usage()
	sys.exit()

if ADMINURL == "NULL":
	print "AdminURL is empty",ADMINURL
	usage()	
	sys.exit();



connectt()
getdomainstatus();
startmanagedservers();

The Sample Execution output of the Script

aksarav@middlewareinventory:/apps/oracle-weblogic/domains/mwidomain/bin$ java weblogic.WLST /opt/python_script/StartDomain.py -u weblogic -p weblogic1 -a t3://localhost:17001

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

INFO: UserName set to => weblogic
INFO: Password set to => ****
INFO: AdminUrl set to => t3://localhost:17001
Connecting to t3://localhost:17001 with userid weblogic ...
Successfully connected to Admin Server "AdminServer" that belongs to domain "mwidomain".

Warning: An insecure protocol was used to connect to the server. 
To ensure on-the-wire security, the SSL port or Admin port should be used instead.

-------------------------------------------------------
	mwidomain domain current status
-------------------------------------------------------
Current state of "AdminServer" : RUNNING
Current state of "mwiserver1" : SHUTDOWN
Current state of "mwiserver2" : SHUTDOWN
Current state of "mwiserver3" : SHUTDOWN
Current state of "mwiserver4" : SHUTDOWN
Current state of "mwiServer5" : SHUTDOWN
Current state of "mwiServer6" : SHUTDOWN
-------------------------------------------------------
Location changed to domainRuntime tree. This is a read-only tree 
with DomainMBean as the root MBean. 
For more help, use help('domainRuntime')

Starting the servers which are in SHUTDOWN and FAILED_NOT_RESTARTABLE status
Starting the Server  mwiserver1
Starting server mwiserver1 ......................
Server with name mwiserver1 started successfully
Starting the servers which are in SHUTDOWN and FAILED_NOT_RESTARTABLE status
Starting the Server  mwiserver2
Starting server mwiserver2 ................................
Server with name mwiserver2 started successfully
Starting the servers which are in SHUTDOWN and FAILED_NOT_RESTARTABLE status
Starting the Server  mwiserver3
Starting server mwiserver3 ...............................
Server with name mwiserver3 started successfully
Starting the servers which are in SHUTDOWN and FAILED_NOT_RESTARTABLE status
Starting the Server  mwiserver4
Starting server mwiserver4 .............................
Server with name mwiserver4 started successfully
Starting the servers which are in SHUTDOWN and FAILED_NOT_RESTARTABLE status
Starting the Server  mwiServer5
Starting server mwiServer5 ...............................
Server with name mwiServer5 started successfully
Starting the servers which are in SHUTDOWN and FAILED_NOT_RESTARTABLE status
Starting the Server  mwiServer6
Starting server mwiServer6 ................................
Server with name mwiServer6 started successfully

 

Hope it helps.

Refer our other WLST scripts here  WLST Scripts Archive

Leave your ratings here [ratings]

Cheers,
Sarav AK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading