Skip to main content

Development

Jump start WSADMIN scripting using Jython

Wsadmin is a command line interface used to connect to and administer WAS products that use the Admin Service for its own administration, like the WebSphere Application Server. The connection to Admin Service can be established remotely or locally in which case Admin Service runs on the same server. All the examples provided are in Jython as it’s the most commonly used language when using wsadmin tool. A local connection is established in the following command.

./wsadmin.sh –lang jythlon

<wsadmin>

Every wsadmin commands begins with an Object. There are five major wsadmin objects together which, using JMX, interacts with product and its elements.

  • AdminConfig.

The AdminConfig object is used to create or edit configurational elements such as changing the heap memory parameters of a server or creating a new JDBC DataSource. Pretty much every configuration that you can do using a web console can be done using AdminConfig.

  • AdminControl.

This object is used to change the state and behaviour of a running element such as an Application Server. For example, AdminControl object can be used to stop or restart a server, but one cannot start anything using AdminControl since there is no running object of it. This is achieved by AdminConfig.

  • AdminApp.

This object is specifically used to interact with the applications that the product hosts. It can be used to install, uninstall and modify any applications.

  • AdminTask.

AdminTask provides a lot of alternatives to administer both configuration and running objects. Frequently performed tasks can be achieved using AdminTask with only a few line of code.

  • Help

Perhaps the most important of them all. Help object provides you with all the information regarding all wsadmin objects, which can be used to create the scripts.

************************************************************************************************************

The following jython code will stop a given Application Server when executed. We will be using AdminControl object since we are going to interact with a running object. The server name is passed as a command line parameter when executing. To be able to reuse a script, save it to a file with extenstion .py and use the following command to execute it.

 

./wsadmin.sh –lang jyton –f <absolute-path-of-file-name> <server-name>

  1. import sys;
  2. serverName = sys.argv[0];
  3. node = AdminConfig.getNode();
  4. serverObj = AdminControl.completeObjectName(‘type=Server,process=’+serverName+’,*’);
  5. state = AdminControl.getAttribute(serverObj,’state’);
  6. if state == “STARTED”:
  7. AdminControl.stopServer(serverName,node);

Line 1 imports the python package sys to read command line arguments. Line2 reads the first command line argument, which is the server name, and stores it to a String. Line 3 gets the name of current node using AdminConfig object. Line 4 gets the object reference of the running server. This object can be then used to invoke operations and get or set its attribtues. Line 5 gets the current value for attribute state. Line 7 stops the server if the state is STARTED.

Seems pretty simple if one knows all the operations and attributes of each wsadmin object and the running JMX objects. This is where the Help object comes in handy. Using the Help wsadmin object one can list all the functions of any wsadmin object.

for x in Help.AdminControl().split(‘\n’):

print x;

The above command will list all the functions the AdminControl object supports and its description. A few listed below.

completeObjectName

                Return a String version of an object name given a

                template name

getNode         returns the node name of the connected server

getObjectInstance

                Given String version of ObjectName, returns

                ObjectInstance object that match.

getPort         returns String representation of port in use

getType         returns String representation of connection type in use

help           Show help information

invoke_jmx     Given ObjectName, name of method, array of parameters and

                signature, invoke method on MBean specified

invoke         Invoke a method on the specified MBean

 

Likewise for all wsadmin objects, the attributes and operations that can be invoked on various other configuration and running elements can be listed using the Help command, here serverObj is a string representation of the running object, in this case an application server.

for x in Help.operations(serverObj).split(‘\n’):

print x;

Lists out various operations that can be invoked on the object. A few mentioned below

java.lang.String getPid()

java.lang.String getCellName()

java.lang.String getCellShortName()

[Ljava.lang.String; getDeployedObjects()

[Ljava.lang.String; getJavaVMs()

java.lang.String getNodeName()

java.lang.String getNodeShortName()

java.lang.String getProcessType()

[Ljava.lang.String; getResources()

To execute any operation we use AdminControl.invoke(<objectName>,<opertionName>);

 

The following command lists out all attributes of the object.

for x in Help.attributes(serverObj).split(‘\n’):

print x;

pid                                         java.lang.String               

cellName                             java.lang.String               

cellShortName                   java.lang.String               

deployedObjects               [Ljava.lang.String;            

javaVMs                             [Ljava.lang.String;            

nodeName                       java.lang.String               

nodeShortName             java.lang.String              

 

To retrieve and set value for any attribute, use the following commands respectively.

AdminControl.getAttribute(<Object-name,<attribute-name>);

AdminControl.setAttribute(<Object-name>,<attribute-name>,<value-name>);

I have a written a collection of scripts that consists of many basic and routine administrative tasks. The scripts are downloadable via the following link

 

https://drive.google.com/open?id=0Bz66giOvR_NmMFNteXVqWDY3cms

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Shankar Menon

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram