Link to home
Start Free TrialLog in
Avatar of WAS
WASFlag for United States of America

asked on

jython script for list servers

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
Avatar of HonorGod
HonorGod
Flag of United States of America image

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

Avatar of WAS

ASKER

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.
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

Avatar of WAS

ASKER

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
You shouldn't be modifying the routine.

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

ASKER

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
$
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

Avatar of WAS

ASKER

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.'

$
ASKER CERTIFIED SOLUTION
Avatar of HonorGod
HonorGod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WAS

ASKER

sure it helped, earlier i was trying without removing  special charactors, thank you HonorGod,
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
Avatar of WAS

ASKER

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
You are welcome.  If you have other questions, just ask.  We'll do what we can to help.