There are many instances when it is necessary to find members (servers) that constitute a cluster. For example, to script starting and stopping an application, we need to know names of servers and nodes where the application is installed. Or we may want to restart all members of a cluster after some configuration change.

As always, this is easy to do when you know what you’re doing. A query-like activity in WAS environment usually heavily relies on AdminConfig object. Our example is no exception:


import sys

"""
List application servers that belong to a particular cluster
Cluster name is passed as a parameter to the script
"""
cluster_name=sys.argv[0]

# Get configID of the cluster
cluster_conf_id = AdminConfig.getid("/ServerCluster:"+cluster_name )
if not cluster_conf_id:
    raise "Cluster %s does not exist!" % cluster_name

# Get confids of the cluster members
member_conf_ids = AdminConfig.showAttribute(cluster_conf_id, "members")
# AdminConfig returns the list in [], get rid of the brackets
member_conf_ids = member_conf_ids[1:-1]
print "Cluster %s has following members:" % cluster_name  
# split by space
for member_conf_id in member_conf_ids.split():
    # Obtain server name and node name
    member_name=AdminConfig.showAttribute(member_conf_id, "memberName")
    node_name=AdminConfig.showAttribute(member_conf_id, "nodeName")
    print node_name+"/"+member_name


Now we can do a lot of useful things with node_name and member_name variables; for example, we can try to get an MBean of this server and check its state. Or we can attempt to restart it. I will cover this in one of the future posts.

This post is part of the series on WebSphere Application Server administration. Please subscribe to this blog if you’d like to receive updates.

One thought on “WebSphere Administration: Finding Cluster Members

  1. Your call to AdminConfig.getid() is not well formed. You are missing the trailing/closing ‘/’. That is why I much prefer to use string formatting to build the string to be passed to the WSAS scripting API. For example:

    cluster_conf_id = AdminConfig.getid(“/ServerCluster:%s/” % cluster_name )

    Be careful though, if the user happens to pass an empty string as the cluster name, e.g.,

    wsadmin -scriptName clusterInfo.py “”

    Then the call will be:

    cluster_conf_id = AdminConfig.getid(“/ServerCluster:/” )

    which could result in a multi-line respose.

    Next, you should be aware that splitting configuration IDs using whitespace, i.e.,

    member_conf_ids.split()

    will only work if none of the configuration IDs contain an embedded space.

    This is “legal” as far as the WebSphere Application Server product is concerned. We can leave the discussion about whether this is, or is not a best/bad practice to some other time.

    I just wanted to bring it to your attention, since I have been bitten by this little “feature”… ;-)

    Let me know if you want to see the code I use to handle this little feature.

Comments are closed.