Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

jython script for list servers

Posted on 2012-09-17
13
Medium Priority
?
4,626 Views
Last Modified: 2012-09-25
i have a  WAS7.0.0.21 ND on a linux machine, i got below  script from the was_root/scriptLibraries/servers/V70/AdminServerManagement.py ,  so when i run this python script i get no output and it wsadmin tool just brings back to prompt, please help me what i need to do in order to get list of servers, why this script is not working, what else i need  to provide in order it to work


#####################################################
      #--------------------------------------------------------------------
      # Set global constants
      #--------------------------------------------------------------------
      import sys
      import java
      import AdminUtilities

      # Setting up Global Variable within this script
      bundleName = "com.ibm.ws.scripting.resources.scriptLibraryMessage"
      resourceBundle = AdminUtilities.getResourceBundle(bundleName)

      ## Example 1 List available servers with given server type and node ##
      def listServers(serverType="APPLICATION_SERVER", nodeName="abe12", failonerror=AdminUtilities._BLANK_):
          if (failonerror==AdminUtilities._BLANK_):
            failonerror=AdminUtilities._FAIL_ON_ERROR_
          #endIf
          msgPrefix = "listServers("+`serverType`+", "+`nodeName`+", "+`failonerror`+"): "

          try:
            #--------------------------------------------------------------------
            # List servers
            #--------------------------------------------------------------------
            print "---------------------------------------------------------------"
            print " AdminServerManagement:  List servers"
            print " Optional parameters: "
            print "         Server type:    "+serverType
            print "         Node name:      "+nodeName
            print " Usage: AdminServerManagement.listServers(\""+serverType+"\", \""+nodeName+"\") "
            print " Return: List of the servers in the cell. The list is filtered based on the server type and node name parameters if they are provided."
            print "---------------------------------------------------------------"
            print " "
            print " "
            # Construct optional parameters
            optionalParamList = []

            if (len(serverType) > 0):
                optionalParamList = ['-serverType', serverType]

            if (len(nodeName) > 0):
                # check if node exists
                node = AdminConfig.getid("/Node:" +nodeName+"/")
                if (len(node) == 0):
                   raise AttributeError(AdminUtilities._formatNLS(resourceBundle, "WASL6040E", ["nodeName", nodeName]))
                #endIf
                optionalParamList = optionalParamList + ['-nodeName', nodeName]
            #endIf
            optionalParamList = optionalParamList + ['-nodeName', nodeName]
            #endIf

            # List servers with specified server type
            servers = AdminTask.listServers(optionalParamList)

            # Convert Jython string to list
            servers = AdminUtilities.convertToList(servers)

            # Loop through each server in server list
            newservers = []
            for aServer in servers:
                # Obtain server and node names
                sname = aServer[0:aServer.find("(")]
                nname = aServer[aServer.find("nodes/")+6:aServer.find("servers/")-1]
                # Retrieve the server config id
                sid = AdminConfig.getid("/Node:"+nname+"/Server:"+sname)
                if (newservers.count(sid) <= 0):
                    newservers.append(sid)
                #AdminUtilities.infoNotice("Server: " + sid)
            #endFor
            return newservers
          except:
            typ, val, tb = sys.exc_info()
            if (typ==SystemExit):  raise SystemExit,`val`
            if (failonerror != AdminUtilities._TRUE_):
                print "Exception: %s %s " % (sys.exc_type, sys.exc_value)
                val = "%s %s" % (sys.exc_type, sys.exc_value)
                raise "ScriptLibraryException: ", `val`
                return -1
            else:
                return AdminUtilities.fail(msgPrefix+AdminUtilities.getExceptionText(typ, val, tb), failonerror)
            #endIf
          #endTry
          AdminUtilities.infoNotice(AdminUtilities._OK_+msgPrefix)
      #endDef
0
Comment
Question by:wasman
  • 7
  • 6
13 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 38407147
Q: What do you mean by the phrase "I got below  script from ..."?
  Did you copy the routine somewhere?
  I'll need to understand this in order to answer the "why didn't it work" question

- It is important to realize that the AdminServerManagement library is automatically provided to the wsadmin environment (unless, for some reason, you have removed the scriptLibraries directory).

  So, to "use" the routine, you only need call the method, using syntax similar to:
wsadmin>AdminServerManagement.listServers()
---------------------------------------------------------------
 AdminServerManagement:  List servers
 Optional parameters:
         Server type:
         Node name:
 Usage: AdminServerManagement.listServers("", "")
 Return: List of the servers in the cell. The list is filtered based on the server type and node name parameters if they are pro
vided.
---------------------------------------------------------------

Open in new window


By the way, the fact that the routine generates that big banner mean that I don't like it very much.

On the other hand, you don't indicate the kind of information that you want, or the format of how it should be displayed.

If, on the other hand, you only want to list the names of the application servers in your cell, you can do something simple like:

wsadmin>for node in AdminConfig.list( 'Node' ).splitlines() :
wsadmin>    nodeName = AdminConfig.showAttribute( node, 'name' )
wsadmin>    for server in AdminConfig.list( 'Server', node ).splitlines() :
wsadmin>        servName = AdminConfig.showAttribute( server, 'name' )
wsadmin>        print '  Node: %s\nServer: %s\n' % ( nodeName, servName )
wsadmin>

Open in new window

0
 

Author Comment

by:wasman
ID: 38407310
What do you mean by the phrase "I got below  script from ..."?
yes i planning to use the sample scripts that came with WAS installations,  just experimenting with them, (was_root/scriptLibraries/servers/V70/AdminServerManagement.py).

  Did you copy the routine somewhere?
what is routine, i didn't copied any thing except the script from the WAS7 sample scripts

i just want to make this script work for me, if you help me fix this script by telling me what is missing in it, it will be easier for me modify other sample scripts that came with WAS7,

please feel free to ask more question if you didn't understand my goal.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 38407345
Are you using a wsadmin interactive session, or do you specify a script file to be executed (i.e., by providing the "-f" command line option)?

Did you modify the script to provide this command parameter?

nodeName="abe12"

Where did that come from?

My AdminServerManagement.py has this as line 99:

def listServers(serverType="", nodeName="", failonerror=AdminUtilities._BLANK_):

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:wasman
ID: 38409971
i am providing the script with -f option and yes i modified this value nodeNAme="abc12" and also the serverType="APPLICATION_SERVER", initially they are left blank in example script, for me this line is -->       def listServers(serverType="APPLICATION_SERVER", nodeName="abe12", failonerror=AdminUtilities._BLANK_): is on line number 13
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 38410081
You shouldn't be modifying the routine.

You should be specifying values when you call the routine, as I showed above...
0
 

Author Comment

by:wasman
ID: 38410132
when don't provide anything also i get nothing, basically comes back to prompt

def listServers(serverType="", nodeName="", failonerror=AdminUtilities._BLANK_):


$ sudo -u webadmin ./wsadmin.sh -f ~/scripts/listservers.py
Realm/Cell Name: <default>
Username: xx31952
Password:                                                                                                                                                                WASX7209I: Connected to process "dmgr" on node abc12admgr using SOAP connector;  The type of process is: DeploymentManager
$
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 38410435
the way you should be calling it is:

$ sudo -u webadmin ./wsadmin.sh -c "AdminServerManagement.listServers()"

You have to realize though, that what you get back from this routine is an array / list of the configuration IDs of the configured application servers.

Which say "execute this command" (i.e., the command after the "-c")
The "-f", as you show, is used to execute a script.  If you want to, you could create a trivial script file to call this routine, and display more useful information from the results.  For example:

/scripts/listservers.py
for server in AdminServerManagement.listServers() :
    Type = AdminConfig.showAttribute( server, 'serverType' )
    name = AdminConfig.showAttribute( server, 'name' )
    print 'ServerType:', Type
    print 'ServerName:', name

Open in new window

0
 

Author Comment

by:wasman
ID: 38428874
that trivial script is working that was excellent, but can use the usage given in /opt/app/WebSphere7.0.0.21/AppServer/scriptLibraries/servers/V70/AdminServerManagement.py script which is

 Usage: AdminServerManagement.listServers(\""+serverType+"\", \""+nodeName+"\") "

so i used it now i get below error, can you please correct me with this

$ sudo -u webadmin ./wsadmin.sh -c " AdminServerManagement.listServers(\""+serverType+"\", \""+nodeName+"\") "
Realm/Cell Name: <default>
Username: webadmin
Password:                                                                                                                                                               WASX7209I: Connected to process "dmgr" on node lseth321admgr using SOAP connector;  The type of process is: DeploymentManager
---------------------------------------------------------------
 AdminServerManagement:  List servers
 Optional parameters:
         Server type:    +serverType+
         Node name:      +nodeName+
 Usage: AdminServerManagement.listServers("+serverType+", "+nodeName+")
 Return: List of the servers in the cell. The list is filtered based on the server type and node name parameters if they are provided.
---------------------------------------------------------------


Exception: exceptions.AttributeError WASL6040E: The nodeName:+nodeName+ specified argument does not exist.
WASX7015E: Exception running command: "AdminServerManagement.listServers("+serverType+", "+nodeName+")"; exception information:
 com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "<input>", line 1, in ?
  File "/opt/app/WebSphere7.0.0.21/AppServer/scriptLibraries/servers/V70/AdminServerManagement.py", line 159, in listServers
ScriptLibraryException: : 'exceptions.AttributeError WASL6040E: The nodeName:+nodeName+ specified argument does not exist.'

$
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 2000 total points
ID: 38429496
The problem is related to how you are invoking wsadmin, and providing the information to be used.

If we ignore the sudo stuff, the call to wsadmin looks like this:

./wsadmin.sh -c " AdminServerManagement.listServers(\""+serverType+"\", \""+nodeName+"\") "

Where is the operating system supposed to get the value of "serverType" and "nodeName"?

What you need to do is provide these values in the call.  For example:

./wsadmin.sh -c "AdminServerManagement.listServers('APPLICATION_SERVER', 'myNode01' )"

that is, if you want to list the configuration IDs for all of the APPLICATION_SERVER objects configured for the node named "myNode01".

If  you want to list the configuration IDs for all of the servers, of every type, in your cell, you can use a call something like:

./wsadmin.sh -c "AdminServerManagement.listServers( '', '' )"

Note how empty strings had to be specified, AND that since we used double quotes to start and stop the "command" to be executed by wsadmin, that we used an open, close single quoted string for each of the two parameters.

The first parameter, i.e., serverType should be something like:

DEPLOYMENT_MANAGER
NODE_AGENT
APPLICATION_SERVER

and the second parameter must be either an empty string (meaning all nodes in the cell), or the name of an existing node in your cell.

Does that help?
0
 

Author Closing Comment

by:wasman
ID: 38429788
sure it helped, earlier i was trying without removing  special charactors, thank you HonorGod,
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 38429856
You are very welcome.

Thanks for the grade & points.

Good luck & have a great day.

If you are going to be doing much WebSphere Application Server administration using Jython, you may want to take a look at this book:

From IBM Press (publisher): http://www.ibmpressbooks.com/bookstore/product.asp?isbn=0137009526
From Amazon: http://www.amazon.com/dp/0137009526
0
 

Author Comment

by:wasman
ID: 38430375
yeah i have that book, but didn't had enough time to learn jython.... i rarely get work on jython...on top i am not good at programming .... trying logic and syntaxs are my hard part, but thanks for advice
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 38432230
You are welcome.  If you have other questions, just ask.  We'll do what we can to help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question