Tuesday, June 14, 2011

Using API to get WebSphere Application Server Information--get WAS Server information

Following the last post, after we connect to the server, we can get the server information

public static void getWASServerList() throws MalformedObjectNameException, InstanceNotFoundException, ReflectionException, ConnectorException,AttributeNotFoundException, MBeanException, ConfigServiceException{

ArrayList serverList=new ArrayList();
String queryString="WebSphere:type=Server,*";

long jvm_heapsize;
long jvm_usedmem;
long jvm_cpuusage;
ObjectName queryServerObj=new ObjectName(queryString);
Set s=adminClient.queryNames(queryServerObj,null);
Properties prop=new Properties();
Iterator itrServer=s.iterator();
ConfigService configService=new ConfigServiceProxy(adminClient);
Session session=new Session();
while (itrServer.hasNext()){
String serverStr=itrServer.next().toString();
ObjectName sobj=new ObjectName(serverStr);
List attrList=adminClient.getAttributes(sobj,new String[]{"name","serverVersion","state","processType","nodeName"});

//*********************************************************
//* this Command is used to get the server name, WAS server version, WAS server state, and server Process Type, and the node name server belongs to
//***********************************************************

String serverName=((Attribute)attrList.get(0)).getValue().toString();
String serverVersion=((Attribute)attrList.get(1)).getValue().toString();
String serverStatus=((Attribute)attrList.get(2)).getValue().toString();
String serverType=((Attribute)attrList.get(3)).getValue().toString();
String nodeName=((Attribute)attrList.get(4)).getValue().toString();


System.out.println("get Perf Information");
System.out.println("------------------");

//*************************************************
//Following codes are used to get WAS server dynamic heap size utilization information
//******************************************************

if (serverType.equals("DeploymentManager")){
jvm_heapsize=0;
jvm_usedmem=0;
jvm_cpuusage=0;
} else {
String PMIQueryString="WebSphere:type=Perf,process="+serverName+",node="+nodeName+",*";
ObjectName PMIObject=new ObjectName(PMIQueryString);
Set perf=adminClient.queryNames(PMIObject,null);
ObjectName perfObj=(ObjectName)perf.iterator().next();

String JVMQueryString="WebSphere:type=JVM,process="+serverName+",node="+nodeName+",*";
ObjectName JVMObject=new ObjectName(JVMQueryString);
Set jvm=adminClient.queryNames(JVMObject,null);
ObjectName jvmMBean=(ObjectName)jvm.iterator().next();
String[] signature = new String[] {"javax.management.ObjectName","java.lang.Boolean"};
Object[] params = new Object[] {jvmMBean, new Boolean(false)};
WSStats jvmStats = (WSStats) adminClient.invoke(perfObj, "getStatsObject", params, signature);
WSRangeStatistic jvmStatistic_Heapsize = (WSRangeStatistic)jvmStats.getStatistic(WSJVMStats.HeapSize);
WSCountStatistic jvmStatistic_UsedMem=(WSCountStatistic)jvmStats.getStatistic(WSJVMStats.UsedMemory);
WSCountStatistic jvmStatistic_CPUUsage=(WSCountStatistic)jvmStats.getStatistic(WSJVMStats.cpuUsage);

jvm_heapsize=jvmStatistic_Heapsize.getCurrent();
jvm_usedmem=jvmStatistic_UsedMem.getCount();
jvm_cpuusage=jvmStatistic_CPUUsage.getCount();
}

//*******************************************************************
//*Following codes to get the server static heap size config
//***************************************************************************

ObjectName[] servers=configService.resolve(session,"Server="+serverName);
Object attr=configService.getAttribute(session,servers[0],"processDefinitions");
int initHeapSizeidx=attr.toString().indexOf("initialHeapSize");
int maxHeapSizeidx=attr.toString().indexOf("maximumHeapSize");
int startInitHeapSizeidx=attr.toString().indexOf("=",initHeapSizeidx);
int startMaxHeapSizeidx=attr.toString().indexOf("=",maxHeapSizeidx);
int endInitHeapSizeidx=attr.toString().indexOf(",",initHeapSizeidx);
int endMaxHeapSizeidx=attr.toString().indexOf(",",maxHeapSizeidx);
String InitHeapSize=attr.toString().substring(startInitHeapSizeidx+2,endInitHeapSizeidx);
String MaxHeapSize=attr.toString().substring(startMaxHeapSizeidx+2,endMaxHeapSizeidx);


System.out.println(serverName+" "+serverStatus +" "+serverType +" "+InitHeapSize+" "+MaxHeapSize+" "+Float.toString((float)jvm_heapsize/1024)+" "+Float.toString((float)jvm_usedmem/1024)+" "+Long.toString(jvm_cpuusage));
}
}

No comments:

Post a Comment