Search This Blog

Wednesday, August 25, 2010

Get Options for command line in WLST script

Problem Statement

Required a Python script for check if the WebLogic admin server is 'Running' or Not recursively
To execute this you have 2 options
1. Executing forever
2. timeout, interval as aruguments

If it isn't keep checking forever or as long as is passed on the command- line with the -t parameter. The wait between checks can be modified with the -i parameter on the command line.

There could be your environemnt also need such options for a Python script. This script will be a example for such requirements.

Script Logic
Hey Smart WLAs what do you think the solution for the above problem statement?? Got any idea? I know you guys are very intelligents!! hope you got idea about getting options at command-line. Yes, it is possible for our WLST Script too, getopt is a python capability which allows us to accept the command line arguments with options -t, -i. Of-course my buddy struggle to find this clues on the Google almost took 2 days.
  1. To read the command line values as dictionary and split as key, value pair and compare the key with desired options then performing the script according to user choice.
  2. If the user failed to enter the options at commandline exception handling with usage funcation.
  3. WLST connect to admin server regular function.
  4. Using sleep method for stopping the WLST execution for some time interval. Repeating this process till given timeout or run this above steps for forever.


#!/usr/bin/python
# Author : Raghunath
# Save Script as : checkAdmin.py 

import time
import getopt
import sys

# ========= connecting to Admin server ==================
def connectAdmin():
   r=1
   try:
        # Update the following line as per your environment
        connect(url='t3://AdminHost:AdminPort')
        print "*** Connected sucessesfully ***"
        r=0
        sys.exit(r)
    except:
        return r
 
def usage():
    print "Usage:"
    print "checkAdmin.py [-t timeout] [-i interval]"
    print "exit with 0 if find Admin Server"
    print "exit with 1 if do not find Admin Server"
    print "if no timeout is given, look for Admin Server until it is found"


# ===== Default settings ===================
Timeout = 0 
Interval = 25000
forever = 1

#====== Main program ===============================
try:
    opts, args = getopt.getopt( sys.argv[1:], "t:i", ["Timeout","Interval"] )
except getopt.GetoptError, err:
    print str(err)
    usage()
    sys.exit(2)

#===== Handling get options  ===============
for opt, arg in opts:
    if opt == "-t":
        Timeout = arg
        forever = 0
    elif opt == "-i":
        Interval = arg

while (forever == 1) or  (Timeout > 0):
     if connectsave() == 1:
         print 'Now, Sleeping  15 sec *************'
         java.lang.Thread.sleep( Interval )
         print 'Waking up after 15 sec ...'
     
      
print 'done'



To run the above script you can make a small shell script as follows:
# checkAdmin.sh

. $WL_HOME/server/bin/setWLSEnv.sh
java weblogic.WLST checkAdmin.py "$@"

Run this shell script as :
$ checkAdmin.sh -t 100 -i 20

or you call directly python script as follows
$ java weblogic.WLST checkAdmin.py -t 100 -i 20

Note that indentation is must for every Python script, please double check before you run the script.
Write back for any issues ariases when you execute the script in your environment.
Referneces:

# email functionality base on http://docs.python.org/library/email-examples.html

8 comments:

  1. Hi

    great scripts.
    I have a question about WLST:
    I tried to assign the following:
    status=state('ms1',Server),but it always return None to status.
    If I run this manually, it usually returns the following:
    Current state of 'ms1' : SHUTDOWN

    any idea why?
    thanks!

    Dianyuan

    ReplyDelete
  2. Hi, Very useful website, I am very new to weblogic, learning more thing from this website.
    can you please guide me with WLST command to find the Thread Pool Threads with Hogger Thread status, Module status, Application.
    thanks,
    Sam

    ReplyDelete
  3. Hey Sam,
    Thanks for your comment.
    Threadpool related script, It is already posted as content 16. Please go thru this link : http://wlstbyexamples.blogspot.in/2009/06/self-tuned-thread-pool-count.html

    Please let me know if you have any further queries.

    ReplyDelete
  4. Hi Pavan,

    Thanks for your quick response....

    I am interested in finding the Hogging threads information not the count "Tuning Thread Pool Threads " which thread had stuck or Hogger. I can take thread dump but i am looking for a WSLD command or is there any other way to find it.
    Sorry i am new to Weblogic if my Q look stupid please guide me.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Hi Pavan,

    Thanks for your quick response....

    I am interested in finding the Hogging threads information not the count "Tuning Thread Pool Threads " which thread had stuck or Hogger. I can take thread dump but i am looking for a WSLD command or is there any other way to find it.
    Sorry i am new to Weblogic if my Q look stupid please guide me.

    ReplyDelete
  7. I ran "checkAdmin.py" and got the below error:
    Problem invoking WLST - Traceback (innermost last):
    File "/tmp/checkAdmin.py", line 53, in ?
    NameError: connectsave

    Could anyone help?

    ReplyDelete
    Replies
    1. I am getting the same error as Larry Xing in connectsave. Can anyone help?

      Delete

Please write your comment here

Popular Posts