Below is some Jython code to show how to use log4j and also get the status of a Websphere server (JVM) running on a WASND node.

showServerStatus.py – code

#Required for Log4j

from org.apache.log4j import Logger
from org.apache.log4j import PropertyConfigurator #By removing are lettnig Log4j find the log4.properties file.

global logger

#----------------------------------------------------------------
# Show Server Status a server
#----------------------------------------------------------------
def showServerStatus(cellName, nodeName, serverName):
    logger.debug("ENTER: showServerStatus")
    beanId = AdminControl.completeObjectName("node=" + nodeName + ",process=" + serverName + ",name=" + serverName + ",j2eeType=J2EEServer,*")
    #beanId = AdminControl.completeObjectName("process=" + serverName + ",name=" + serverName + ",j2eeType=J2EEServer,*")
    logger.debug("beanId = " + beanId)


    serverId = AdminConfig.getid("/Node:" + nodeName + "/Server:" + serverName + "/")
    logger.debug("serverId = " + serverId)
    if (serverId == "") :
        logger.error( "server[" + serverName + "] on node[" + nodeName + "] not found in cell [" + cellName+ "] configuration. Check cellName, nodeName, serverName and try again!")
        return "Unknown!"
    #endIf 
    
    if (beanId == "") :
        state = "Stopped"
        logger.debug("beanId is blank, so we assume stopped")
        pid = "N/A"
    else:
        logger.debug("beanId = " + beanId)
        state = AdminControl.getAttribute(beanId, "state")
        pid = AdminControl.getAttribute(beanId, "pid")

    beanId = AdminControl.completeObjectName("J2EEServer=" + serverName + ",name=JVM,*")

    if (beanId == ""):
        heapSize = 0
        freeMem = 0
        maxMem = 0
    else:
        logger.debug("beanId = " + beanId)
        heapSize = long(AdminControl.getAttribute(beanId, "heapSize")) / 1024 / 1024
        freeMem = long(AdminControl.getAttribute(beanId, "freeMemory")) / 1024 / 1024
        maxMem = long(AdminControl.getAttribute(beanId, "maxMemory")) / 1024 / 1024
    
    print "[%s] %s : %s: pid=%s, heap=%dMB, free=%dMB, max=%dMB" % (nodeName, serverName, state, pid, heapSize, freeMem, maxMem)
     
    logger.debug("state=" + state)
    logger.debug("EXIT: showServerStatus")
    return state

#endDef

#########################################################
################# MAIN ENTRY POINT ######################
#########################################################

#Setup log4j It is the responsbility of the calling module to import log4j.
PropertyConfigurator.configure('/var/stever/examples/log4j.properties') #By removing this line we let Log4j find the config in the classpath
logger = Logger.getLogger("Show Server Example")
#use the config data in the properties file
logger.debug("Log4j successfully setup") 

#Below is an example of how to show server status using the showServerStatus function above.

cellName = AdminControl.getCell()
nodeName = "batrounNode02" #Change to the node on which the sever is located
serverName = "Server02" #The srever you wish to get the status for

returnVal = showServerStatus(cellName, nodeName, serverName)





Sample shell script to test: showServerStatus.sh

#-----------showServerStatus.sh-----------
#!/bin/sh
WAS_DMGR_HOME="/opt/IBM/WebSphere/AppServer/profiles/Dmgr01/"
${WAS_DMGR_HOME}bin/wsadmin.sh -password wasadmin -username wasadmin -f /var/stever/examples/showServerStatus.py
echo "Exit status=" $?

Sample output form log4j when a an error is found for example a blank node name is found

Sample output from log4j when a server is located and status can established

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply