WebSphere Application Server & Liberty

WebSphere Application Server & Liberty

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Hi Team, I wrote a script to stop the cluster in WebSphere Application Server

    Posted Mon December 14, 2015 08:33 PM

    Hi Team,
    I wrote a script to stop the cluster in WebSphere Application Server. While running the script i'm getting the below error. Could you please check and advise.

    WASX7017E: Exception received while running file "stopCluster.py"; exception information: com.ibm.bsf.BSFException: exception from Jython:
    Traceback (innermost last):
    File "<string>", line 71, in ?
    NameError: arg1

    SCRIPT:

    # This program may be used, executed, copied, modified and distributed
    # without royalty for the purpose of developing, using, marketing, or distribution
    #
    #-----------------------------------------------------------------
    # Global stopCluster
    #-----------------------------------------------------------------
    #
    # The purpose of this example is to demonstrate the invocation
    # of stopping a cluster passing args through a shell script.
    #
    # This script can be included in the wsadmin command invocation like this:
    #
    # ./wsadmin -lang jython -f stopCluster.py clusterName cell
    #
    # Simply create a shell script with the following
    # #!/bin/sh
    # # Change the directory to your WAS_HOME/bin
    # cd /opt/websphere/bin
    # # issue the wsadmin command with the two arguments
    # # make sure your path to stopCluster.py is correct
    # # in the example below it is located in WAS_HOME/bin
    # # clusterName below = your cluster name found via the admin console
    # # cell below = your cell name found via the admin console
    # ./wsadmin.sh -lang jython stopCluster.py clusterName cell
    #
    # The script expects one parameter:
    # arg1 - cluster Name
    # arg2 - cell Name
    #
    #-----------------------------------------------------------------
    #
    import sys
    import string
    import random

    def x(arg1,cel):


    #--------------------------------------------------------------
    # set up globals
    #--------------------------------------------------------------
    global AdminControl
    global AdminConfig
    global AdminTask
    global AdminApp
    global Help

    #---------------------------------------------------------
    # First, list the existing cluster(s)
    #---------------------------------------------------------
    cluster = AdminConfig.list('ServerCluster')
    print "----------------------------------------------------------"
    print "Clusters found: "
    print cluster
    print "----------------------------------------------------------"


    #---------------------------------------------------------
    # Second, get the cell information
    #---------------------------------------------------------
    printcellname = AdminConfig.list('Cell')
    print "----------------------------------------------------------"
    print "Cell(s): "
    print printcellname
    print "----------------------------------------------------------"


    #---------------------------------------------------------
    # Here is the cluster we'll be dealing with...
    #---------------------------------------------------------
    work=arg1
    print "----------------------------------------------------------"
    print "The cluster we are going to perform action STOP on:"
    print work
    print "----------------------------------------------------------"

    #---------------------------------------------------------
    # Here is the cell we'll be dealing with...
    #---------------------------------------------------------
    cwork = cel
    print "----------------------------------------------------------"
    print "The cell we are working with:"
    print cwork
    print "----------------------------------------------------------"

    #---------------------------------------------------------
    # Creating the stopcluster variable
    #---------------------------------------------------------
    stopcluster = AdminControl.completeObjectName('cell='+cwork +',type=Cluster,name='+ work +',*')
    #---------------------------------------------------------
    print "----------------------------------------------------------"
    print "Successfully completed the object name and the cluster that will stop is:"
    print stopcluster
    print "----------------------------------------------------------"

    #---------------------------------------------------------
    # A very simple stop command
    # to run start cluster simply change below command to
    # AdminControl.invoke(stopcluster, 'start')
    #---------------------------------------------------------
    print "----------------------------------------------------------"
    print "A simple stop command....."
    print AdminControl.invoke(stopcluster, 'stop')
    message = "Tail the app server logs to confirm they have stopped"
    print message
    print "----------------------------------------------------------"


    #-----------------------------------------------------------------
    # Main
    #-----------------------------------------------------------------
    if len(sys.argv) != 2:
    print "x: this script requires 2 parameters:"
    print " cluster name, and cell name"
    print ""
    print "e.g.: wsadmin -lang jython -f stopCluster.py REMCluster u060rem11Network"
    else:
    x(sys.argv[0], sys.argv[1])


    Thanks a lot in advance.



  • 2.  RE: Hi Team, I wrote a script to stop the cluster in WebSphere Application Server

    Posted Mon December 14, 2015 08:36 PM

    Hi Srikanth,

    I don't see an #endIf at the end of your program. Please check if it is missing.

    Also, please post line number 71 where it is throwing an error.

    Regards,



  • 3.  RE: Hi Team, I wrote a script to stop the cluster in WebSphere Application Server

    Posted Mon December 14, 2015 08:39 PM

    Hi,

    Please take indentation of python code also under consideration. A wrong indentation leads to errors in python.

    Regards
    Tousi



  • 4.  RE: Hi Team, I wrote a script to stop the cluster in WebSphere Application Server

    Posted Mon December 21, 2015 12:09 PM

    Hi Srikath,

    First and foremost, you did a great job - the script is very clean and easy to follow.  A few suggestions:

    1) Your comments say the script only expects one parameter when it actually expects two: clusterName and cellName

    2) You check to make sure the two parameters have been specified in the run command, but you don't check that the correct parameters have been entered before issuing the stop.  Name errors most often mean the script can't find the arguement entered - this is usually due to typos.  For example you could add a function to check the arguments like this -

    def argcheck():

        if (len(AdminControl.getid("/ServerCluster:"+arg1+"") == []):

                print " Cluster name entered not found, please try again..."

                exit(1) #make sure you define and keep record of your error codes so you can refer to them if an error is thrown - an exit code 0 means the script executed successfully - any number assigned to the exit code means there was an issue

        else:

               print " Cluster name" + arg1 + " found..."

        if (AdminControl.getCell() != arg2):

            print " Cell name entered not found, please try again..."

            exit(2)

        else:

            print " Cell name found..."

    You can also add logic that sends an alert letting you know when an error is encountered, so you know right away if someone ran your script and is having issues. 

    Lastly, there an easier way to do this in which you can start/stop a cluster using the name of an application installed on the cluster.  This method is most effective if you have isolated your applications to a 1:1 ratio - 1 application per cluster.  If you have multiple applications installed on the same cluster, this may not be the best method to use.

    Hope this helps - Happy Scripting!!!

    Danielle



  • 5.  RE: Hi Team, I wrote a script to stop the cluster in WebSphere Application Server

    Posted Fri April 08, 2016 09:45 AM

    Your problem is a likely indentation - using the right indentation is a required in python or Jython. Please google  "indentation in python or Jython"