Sunday, October 4, 2015

Jython script for Server Stop, Start, restart using wsadmin

Server Control from wsadmin

The building blocks for this script are as follows:

  1. Get Node name 
  2. Get the Server name
  3. Get the server state
  4. Using the AdminControl stop/start/restart

How to get Node Name in wsadmin using Jython?


We can use the nodeName using AdminControl  with getNode method or completeObjectName querying.

wsadmin>nodeName=AdminControl.getNode()
wsadmin>print nodeName
ubuntu-was8Node01

wsadmin>objn = AdminControl.completeObjectName('WebSphere:type=Server,*')

wsadmin>print objn
WebSphere:name=server1,process=server1,platform=proxy,node=ubuntu-was8Node01,j2eeType=J2EEServer,version=8.5.5.6,type=Server,mbeanIdentifier=cells/ubuntu-was8Node01Cell/nodes/ubuntu-was8Node01/servers/server1/server.xml#Server_1183121908656,cell=ubuntu-was8Node01Cell,spec=1.0,processType=UnManagedProcess

wsadmin>print AdminControl.getAttribute(objn, 'nodeName')
ubuntu-was8Node01


In the above interaction we have experimented in two options directly fetching using getNode() method it is actually pulls the details from the deployment manager to which wsadmin connected. The other option is going to fetch the server wide node name associated details.

How to get the Server Name in wsadmin?


If you need the server name for stop or start you need to fetch it using queryName method where you need to mention the type as Server as shown below. From that Object you need to fetch the attribute name.

wsadmin>wsadminSvr =AdminControl.queryNames("node="+AdminControl.getNode( )+",type=Server,*" )
wsadmin>wsadminSvr
'WebSphere:name=server1,process=server1,platform=proxy,node=ubuntu-was8Node01,j2eeType=J2EEServer,version=8.5.5.6,type=Server,mbeanIdentifier=cells/ubuntu-was8Node01Cell/nodes/ubuntu-was8Node01/servers/server1/server.xml#Server_1183121908656,cell=ubuntu-was8Node01Cell,spec=1.0,processType=UnManagedProcess'


wsadmin>s = AdminControl.getAttribute(wsadminSvr, "name" )
wsadmin>s
'server1'

How to get server name when WebSphere Cell is stopped?


This is the common requirement for the automations, where we don't want to hard code the server names. We need the list of the servers when the Dmgr not running. The AdminConfig object is available even when the environment is down.

wsadmin>serverlist=AdminConfig.list('Server').split('\n')
wsadmin>print serverlist
['server1(cells/ubuntu-was8Node01Cell/nodes/ubuntu-was8Node01/servers/server1|server.xml#Server_1183121908656)', 'webserver1(cells/ubuntu-was8Node01Cell/nodes/ubuntu-was8Node01/servers/webserver1|server.xml#Server_1443584628692)']

wsadmin>serverlist[0].split('(')
['server1', 'cells/ubuntu-was8Node01Cell/nodes/ubuntu-was8Node01/servers/server1|server.xml#Server_1183121908656)']
wsadmin>serverlist[0].split('(')[0]
'server1'
wsadmin>serverlist[1].split('(')[0]
'webserver1'

Hence you need a loop to fetch all servers present in the Cell.

How to get the state of WebSphere server with Jython


Here we go ... Simple script that will fetches the server state and print out that. You can call the serverState() function in a loop when you have the list of the servers.

def serverState(serverName):
 serverObj = AdminControl.completeObjectName('WebSphere:type=Server,name=' + serverName + ',*')
 if len(serverObj) > 0:
  serverStatus = AdminControl.getAttribute(serverObj, 'state')
 else:
  serverStatus = 'STOPPED'
 print "%15s %s" %(serverName, serverStatus)
serverState('server1')
serverState('webserver1')

You can execute the above script as following:

wsadmin>execfile('/home/vagrant/scripts/serverstat.py')
        server1 STARTED
     webserver1 STOPPED

Now lets use the ingredients which we collected in the above and construct the Jython script.

How to Stop the server using Jython?


The AdminControl have many options to control(stop/start) servers. Here we can use stopServer method. where it takes different number of arguments to stop the server at different scenarios.

AdminControl.stopServer(serverName, 'immediate')

Stops a server. If the "immediate flag" is set ton"immediate" then a stopImmediate is performed for the given server. Otherwise, a normal graceful stop is performed.

AdminControl.stopServer(serverName, nodeName)

This could be the real-time admin requirement, here we deal with node and its associated servers. This will stops a server in the specified node.

AdminControl.stopServer(serverName, nodeName, 'immediate')

Stops a server in the specified node. If the "immediate flag" is set to "immediate" then a stopImmediate is performed for the specified server. Otherwise, a normal stop is performed.

AdminControl.stopServer(serverName, nodeName, 'terminate')

Stops a server on the specified node. If the "terminate flag" is set to "terminate" then a terminate is performed for the specified server. Otherwise, a normal stop is performed. This operation can only be performed on a Managed Process. The node name is a required argument.

AdminControl.stopServer method in Jython




Lets stop the server from wsadmin Jython here it is WebSphere Network deployment environment:

wsadmin>AdminControl.stopServer('server1','immediate')
'WASX7264I: Stop completed for server "server1" on node "ubuntu-was8Node01"'

Unfortunately I cannot start this server1 because it is unmanaged server. If it was in a managed profile then I can do startServer function on AdminControl object.

With Managed environment see how it goes...

Stop Server on the Node with immediate flag

Start server using Jython wsadmin


Just like stop server have several options
startServer(serverName, wait)
Starts a server with scripting process is attached to a Node Agent server.
startServer(serverName, wait)
Starts a server by locating it in the configuration, scripting process is attached to a Node Agent server.
startServer(serverName, nodeName)
Starts a server in the specified node by locating it in the configuration scripting process is attached either to a Node Agent or Deployment Manager process.
 startServer(serverName, nodeName, wait)
Starts a server in the specified node and  "wait" time in seconds, and this will work when scripting process is attached either to a Node Agent or Deployment Manager process.

startServer in Jython wsadmin

AdminControl.startServer method in Jython

Hope you enjoyed this automation learnings.
Write your comments what you thing about this post? and also write your expectations.

1 comment:

  1. this command is throwing me error, we are using WAS 8.5
    serverObj = AdminControl.completeObjectName('WebSphere:type=Server,name=' + serverName + ',*')

    ReplyDelete

Containerization with Docker

Containerization with Docker
Learn modern microservices containers grow