ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzatz_5.4.0.1/51/admin/prfdevjmxsamp.htm

778 lines
28 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Example: JMX administrative client that retrieves PMI data</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h5><a name="prfdevjmxsamp"></a>Example: JMX administrative client that retrieves PMI data</h5>
<p>This example code uses the JMX interface to get PMI data from PerfMBean and individual MBeans that support the getStats method.</p>
<p><strong>Note:</strong> Read the <a href="codex.htm">Code example disclaimer</a> for important legal information.</p>
<pre>
package com.ibm.websphere.pmi;
import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.exception.ConnectorException;
import com.ibm.websphere.management.exception.InvalidAdminClientTypeException;
import com.ibm.websphere.management.exception.*;
import java.util.*;
import javax.management.*;
import com.ibm.websphere.pmi.*;
import com.ibm.websphere.pmi.client.*;
import com.ibm.websphere.pmi.stat.*;
/**
* Sample code to use AdminClient API directly to get PMI data from PerfMBean
* and individual MBeans which support getStats method.
*/
public class PmiJmxTest implements PmiConstants
{
private AdminClient ac = null;
private ObjectName perfOName = null;
private ObjectName serverOName = null;
private ObjectName wlmOName = null;
private ObjectName jvmOName = null;
private ObjectName orbtpOName = null;
private boolean failed = false;
private PmiModuleConfig[] configs = null;
/**
* Creates a new test object
* (Need a default constructor for the testing framework)
*/
public PmiJmxTest()
{
}
/**
* @param args[0] host
* @param args[1] port, optional, default is 8880
* @param args[2] connectorType, optional, default is SOAP connector
*
*/
public static void main(String[] args)
{
PmiJmxTest instance = new PmiJmxTest();
// parse arguments and create AdminClient object
instance.init(args);
// navigate all the MBean ObjectNames and cache those we are interested
instance.getObjectNames();
// set level, get data, display data
instance.doTest();
// test for EJB data
instance.testEJB();
// how to use JSR77 getStats method for individual MBean other than PerfMBean
instance.testJSR77Stats();
}
/**
* parse args and getAdminClient
*/
public void init(String[] args)
{
try
{
String host = null;
String port = &quot;8880&quot;;
String connector = &quot;SOAP&quot;;
if(args.length &lt; 1)
{
System.err.println(&quot;ERROR: Usage: PmiJmxTest &lt;host&gt; [&lt;port&gt;] [&lt;connector&gt;]&quot;);
System.exit(2);
}
else
{
host = args[0];
if(args.length &gt; 1)
port = args[1];
if(args.length &gt; 2)
connector = args[2];
}
if(host == null)
{
host = &quot;localhost&quot;;
}
if(port == null)
{
port = &quot;8880&quot;;
}
if(connector == null)
{
connector = AdminClient.CONNECTOR_TYPE_SOAP;
}
System.out.println(&quot;host=&quot; + host + &quot; , port=&quot; + port + &quot;, connector=&quot; + connector);
//----------------------------------------------------------------------------
// Get the ac object for the AppServer
//----------------------------------------------------------------------------
System.out.println(&quot;main: create the adminclient&quot;);
ac = getAdminClient(host, port, connector);
}
catch(Exception ex)
{
failed = true;
new AdminException(ex).printStackTrace();
ex.printStackTrace();
}
}
/**
* get AdminClient using the given host, port, and connector
*/
public AdminClient getAdminClient(String hostStr, String portStr, String connector)
{
System.out.println(&quot;getAdminClient: host=&quot; + hostStr + &quot; , portStr=&quot; + portStr);
AdminClient ac = null;
java.util.Properties props = new java.util.Properties();
props.put(AdminClient.CONNECTOR_TYPE, connector);
props.put(AdminClient.CONNECTOR_HOST, hostStr);
props.put(AdminClient.CONNECTOR_PORT, portStr);
try
{
ac = AdminClientFactory.createAdminClient(props);
}
catch(Exception ex)
{
failed = true;
new AdminException(ex).printStackTrace();
System.out.println(&quot;getAdminClient: exception&quot;);
}
return ac;
}
/**
* get all the ObjectNames.
*/
public void getObjectNames()
{
try
{
//----------------------------------------------------------------------------
// Get a list of object names
//----------------------------------------------------------------------------
javax.management.ObjectName on = new javax.management.ObjectName(&quot;WebSphere:*&quot;);
//----------------------------------------------------------------------------
// get all objectnames for this server
//----------------------------------------------------------------------------
Set objectNameSet= ac.queryNames(on, null);
//----------------------------------------------------------------------------
// get the object names that we care about: Perf, Server, JVM, WLM
// (only applicable in ND)
//----------------------------------------------------------------------------
if(objectNameSet != null)
{
Iterator i = objectNameSet.iterator();
while(i.hasNext())
{
on = (ObjectName)i.next();
String type = on.getKeyProperty(&quot;type&quot;);
// uncomment it if you want to print the ObjectName for each MBean
// System.out.println(&quot;\n\n&quot; + on.toString());
// find the MBeans we are interested
if(type != null &amp;&amp; type.equals(&quot;Perf&quot;))
{
System.out.println(&quot;\nMBean: perf =&quot; + on.toString());
perfOName = on;
}
if(type != null &amp;&amp; type.equals(&quot;Server&quot;))
{
System.out.println(&quot;\nMBean: Server =&quot; + on.toString());
serverOName = on;
}
if(type != null &amp;&amp; type.equals(&quot;JVM&quot;))
{
System.out.println(&quot;\nMBean: jvm =&quot; + on.toString());
jvmOName = on;
}
if(type != null &amp;&amp; type.equals(&quot;WLMAppServer&quot;))
{
System.out.println(&quot;\nmain: WLM =&quot; + on.toString());
wlmOName = on;
}
if(type != null &amp;&amp; type.equals(&quot;ThreadPool&quot;))
{
String name = on.getKeyProperty(&quot;name&quot;);
if(name.equals(&quot;ORB.thread.pool&quot;))
System.out.println(&quot;\nMBean: ORB ThreadPool =&quot; + on.toString());
orbtpOName = on;
}
}
}
else
{
System.err.println(&quot;main: ERROR: no object names found&quot;);
System.exit(2);
}
// You must have Perf MBean in order to get PMI data.
if(perfOName == null)
{
System.err.println(&quot;main: cannot get PerfMBean. Make sure PMI is enabled&quot;);
System.exit(3);
}
}
catch(Exception ex)
{
failed = true;
new AdminException(ex).printStackTrace();
ex.printStackTrace();
}
}
/**
* Some sample code to set level, get data, and display data.
*/
public void doTest()
{
try
{
// first get all the configs - used to set static info for Stats
// Note: server only returns the value and time info.
// No description, unit, etc is returned with PMI data to reduce
// communication cost.
// You have to call setConfig to bind the static info and Stats data later.
configs = (PmiModuleConfig[])ac.invoke(perfOName, &quot;getConfigs&quot;, null, null);
// print out all the PMI modules and matching mbean types
for(int i=0; i&lt;configs.length;i++)
System.out.println(&quot;config: moduleName=&quot; + configs[i].getShortName()
+ &quot;, mbeanType=&quot; + configs[i].getMbeanType());
// set the instrumentation level for the server
setInstrumentationLevel(serverOName, null, PmiConstants.LEVEL_HIGH);
// example to use StatDescriptor.
// Note WLM module is only available in ND.
StatDescriptor sd = new StatDescriptor(new String[]{&quot;wlmModule.server&quot;});
setInstrumentationLevel(wlmOName, sd, PmiConstants.LEVEL_HIGH);
// example to getInstrumentationLevel
MBeanLevelSpec[] mlss = getInstrumentationLevel(wlmOName, sd, true);
// you can call getLevel(), getObjectName(), getStatDescriptor() on mlss[i]
// get data for the server
Stats stats = getStatsObject(serverOName, true);
System.out.println(stats.toString());
// get data for WLM server submodule
stats = getStatsObject(wlmOName, sd, true);
if(stats == null)
System.out.println(&quot;Cannot get Stats for WLM data&quot;);
else
System.out.println(stats.toString());
// get data for JVM MBean
stats = getStatsObject(jvmOName, true);
processStats(stats);
// get data for multiple MBeans
ObjectName[] onames = new ObjectName[]{orbtpOName, jvmOName};
Object[] params = new Object[]{onames, new Boolean(true)};
String[] signature = new String[]{&quot;[Ljavax.management.ObjectName;&quot;,
&quot;java.lang.Boolean&quot;};
Stats[] statsArray = (Stats[])ac.invoke(perfOName, &quot;getStatsArray&quot;,
params, signature);
// you can call toString or processStats on statsArray[i]
if(!failed)
System.out.println(&quot;All tests passed&quot;);
else
System.out.println(&quot;Some tests failed&quot;);
}
catch(Exception ex)
{
new AdminException(ex).printStackTrace();
ex.printStackTrace();
}
}
/**
* Sample code to get level
*/
protected MBeanLevelSpec[] getInstrumentationLevel(ObjectName on, StatDescriptor sd,
boolean recursive)
{
if(sd == null)
return getInstrumentationLevel(on, recursive);
System.out.println(&quot;\ntest getInstrumentationLevel\n&quot;);
try
{
Object[] params = new Object[2];
params[0] = new MBeanStatDescriptor(on, sd);
params[1] = new Boolean(recursive);
String[] signature= new String[]{ &quot;com.ibm.websphere.pmi.stat.MBeanStatDescriptor&quot;,
&quot;java.lang.Boolean&quot;};
MBeanLevelSpec[] mlss = (MBeanLevelSpec[])ac.invoke(perfOName,
&quot;getInstrumentationLevel&quot;, params, signature);
return mlss;
}
catch(Exception e)
{
new AdminException(e).printStackTrace();
System.out.println(&quot;getInstrumentationLevel: Exception Thrown&quot;);
return null;
}
}
/**
* Sample code to get level
*/
protected MBeanLevelSpec[] getInstrumentationLevel(ObjectName on, boolean recursive)
{
if(on == null)
return null;
System.out.println(&quot;\ntest getInstrumentationLevel\n&quot;);
try
{
Object[] params = new Object[]{on, new Boolean(recursive)};
String[] signature= new String[]{ &quot;javax.management.ObjectName&quot;,
&quot;java.lang.Boolean&quot;};
MBeanLevelSpec[] mlss = (MBeanLevelSpec[])ac.invoke(perfOName,
&quot;getInstrumentationLevel&quot;, params, signature);
return mlss;
}
catch(Exception e)
{
new AdminException(e).printStackTrace();
failed = true;
System.out.println(&quot;getInstrumentationLevel: Exception Thrown&quot;);
return null;
}
}
/**
* Sample code to set level
*/
protected void setInstrumentationLevel(ObjectName on, StatDescriptor sd, int level)
{
System.out.println(&quot;\ntest setInstrumentationLevel\n&quot;);
try
{
Object[] params = new Object[2];
String[] signature = null;
MBeanLevelSpec[] mlss = null;
params[0] = new MBeanLevelSpec(on, sd, level);
params[1] = new Boolean(true);
signature= new String[]{ &quot;com.ibm.websphere.pmi.stat.MBeanLevelSpec&quot;,
&quot;java.lang.Boolean&quot;};
ac.invoke(perfOName, &quot;setInstrumentationLevel&quot;, params, signature);
}
catch(Exception e)
{
failed = true;
new AdminException(e).printStackTrace();
System.out.println(&quot;setInstrumentationLevel: FAILED: Exception Thrown&quot;);
}
}
/**
* Sample code to get a Stats object
*/
public Stats getStatsObject(ObjectName on, StatDescriptor sd, boolean recursive)
{
if(sd == null)
return getStatsObject(on, recursive);
System.out.println(&quot;\ntest getStatsObject\n&quot;);
try
{
Object[] params = new Object[2];
params[0] = new MBeanStatDescriptor(on, sd); // construct MBeanStatDescriptor
params[1] = new Boolean(recursive);
String[] signature = new String[] {
&quot;com.ibm.websphere.pmi.stat.MBeanStatDescriptor&quot;, &quot;java.lang.Boolean&quot;};
Stats stats = (Stats)ac.invoke(perfOName, &quot;getStatsObject&quot;, params, signature);
if(stats == null) return null;
// find the PmiModuleConfig and bind it with the data
String type = on.getKeyProperty(&quot;type&quot;);
if(type.equals(MBeanTypeList.SERVER_MBEAN))
setServerConfig(stats);
else
stats.setConfig(PmiClient.findConfig(configs, on));
return stats;
}
catch(Exception e)
{
failed = true;
new AdminException(e).printStackTrace();
System.out.println(&quot;getStatsObject: Exception Thrown&quot;);
return null;
}
}
/**
* Sample code to get a Stats object
*/
public Stats getStatsObject(ObjectName on, boolean recursive)
{
if(on == null)
return null;
System.out.println(&quot;\ntest getStatsObject\n&quot;);
try
{
Object[] params = new Object[]{on, new Boolean(recursive)};
String[] signature = new String[] { &quot;javax.management.ObjectName&quot;,
&quot;java.lang.Boolean&quot;};
Stats stats = (Stats)ac.invoke(perfOName, &quot;getStatsObject&quot;, params,
signature);
// find the PmiModuleConfig and bind it with the data
String type = on.getKeyProperty(&quot;type&quot;);
if(type.equals(MBeanTypeList.SERVER_MBEAN))
setServerConfig(stats);
else
stats.setConfig(PmiClient.findConfig(configs, on));
return stats;
}
catch(Exception e)
{
failed = true;
new AdminException(e).printStackTrace();
System.out.println(&quot;getStatsObject: Exception Thrown&quot;);
return null;
}
}
/**
* Sample code to navigate and get the data value from the Stats object.
*/
private void processStats(Stats stat)
{
processStats(stat, &quot;&quot;);
}
/**
* Sample code to navigate and get the data value from the Stats and Statistic object.
*/
private void processStats(Stats stat, String indent)
{
if(stat == null) return;
System.out.println(&quot;\n\n&quot;);
// get name of the Stats
String name = stat.getName();
System.out.println(indent + &quot;stats name=&quot; + name);
// list data names
String[] dataNames = stat.getStatisticNames();
for(int i=0; i&lt;dataNames.length;i++)
System.out.println(indent + &quot; &quot; + &quot;data name=&quot; + dataNames[i]);
System.out.println(&quot;&quot;);
// list all datas
com.ibm.websphere.management.statistics.Statistic[] allData = stat.getStatistics();
// cast it to be PMI's Statistic type so that we can have get more
// Also show how to do translation.
Statistic[] dataMembers = (Statistic[])allData;
if(dataMembers != null)
{
for(int i=0; i&lt;dataMembers.length;i++)
{
System.out.print(indent + &quot; &quot; + &quot;data name=&quot; +
PmiClient.getNLSValue(dataMembers[i].getName())
+ &quot;, description=&quot; +
PmiClient.getNLSValue(dataMembers[i].getDescription())
+ &quot;, startTime=&quot; + dataMembers[i].getStartTime()
+ &quot;, lastSampleTime=&quot; + dataMembers[i].getLastSampleTime());
if(dataMembers[i].getDataInfo().getType() == TYPE_LONG)
{
System.out.println(&quot;, count=&quot; +
((CountStatisticImpl)dataMembers[i]).getCount());
}
else if(dataMembers[i].getDataInfo().getType() == TYPE_STAT)
{
TimeStatisticImpl data = (TimeStatisticImpl)dataMembers[i];
System.out.println(&quot;, count=&quot; + data.getCount()
+ &quot;, total=&quot; + data.getTotal()
+ &quot;, mean=&quot; + data.getMean()
+ &quot;, min=&quot; + data.getMin()
+ &quot;, max=&quot; + data.getMax());
}
else if(dataMembers[i].getDataInfo().getType() == TYPE_LOAD)
{
RangeStatisticImpl data = (RangeStatisticImpl)dataMembers[i];
System.out.println(&quot;, current=&quot; + data.getCurrent()
+ &quot;, integral=&quot; + data.getIntegral()
+ &quot;, avg=&quot; + data.getMean()
+ &quot;, lowWaterMark=&quot; + data.getLowWaterMark()
+ &quot;, highWaterMark=&quot; + data.getHighWaterMark());
}
}
}
// recursively for sub-stats
Stats[] substats = (Stats[])stat.getSubStats();
if(substats == null || substats.length == 0)
return;
for(int i=0; i&lt;substats.length; i++)
{
processStats(substats[i], indent + &quot; &quot;);
}
}
/**
* The Stats object returned from server does not have static config info.
* You have to set it on client side.
*/
public void setServerConfig(Stats stats)
{
if(stats == null) return;
if(stats.getType() != TYPE_SERVER) return;
PmiModuleConfig config = null;
Stats[] statList = stats.getSubStats();
if(statList == null || statList.length == 0)
return;
Stats oneStat = null;
for(int i=0; i&lt;statList.length; i++)
{
oneStat = statList[i];
if(oneStat == null) continue;
config = PmiClient.findConfig(configs, oneStat.getName());
if(config != null)
oneStat.setConfig(config);
else
System.out.println(&quot;Error: get null config for &quot; + oneStat.getName());
}
}
/**
* sample code to show how to get a specific MBeanStatDescriptor
*/
public MBeanStatDescriptor getStatDescriptor(ObjectName oName, String name)
{
try
{
Object[] params = new Object[]{serverOName};
String[] signature= new String[]{&quot;javax.management.ObjectName&quot;};
MBeanStatDescriptor[] msds = (MBeanStatDescriptor[])ac.invoke(perfOName,
&quot;listStatMembers&quot;, params, signature);
if(msds == null)
return null;
for(int i=0; i&lt;msds.length; i++)
{
if(msds[i].getName().equals(name))
return msds[i];
}
return null;
}
catch(Exception e)
{
new AdminException(e).printStackTrace();
System.out.println(&quot;listStatMembers: Exception Thrown&quot;);
return null;
}
}
/**
* sample code to show you how to navigate MBeanStatDescriptor via listStatMembers
*/
public MBeanStatDescriptor[] listStatMembers(ObjectName mName)
{
if(mName == null)
return null;
try
{
Object[] params = new Object[]{mName};
String[] signature= new String[]{&quot;javax.management.ObjectName&quot;};
MBeanStatDescriptor[] msds = (MBeanStatDescriptor[])ac.invoke(perfOName,
&quot;listStatMembers&quot;, params, signature);
if(msds == null)
return null;
for(int i=0; i&lt;msds.length; i++)
{
if(msds[i].getName().equals(mName))
return msds;
}
return null;
}
catch(Exception e)
{
new AdminException(e).printStackTrace();
System.out.println(&quot;listStatMembers: Exception Thrown&quot;);
return null;
}
}
public MBeanStatDescriptor[] listStatMembers(MBeanStatDescriptor mName)
{
if(mName == null)
return null;
try
{
Object[] params = new Object[]{mName};
String[] signature= new String[]{&quot;com.ibm.websphere.pmi.stat.MBeanStatDescriptor&quot;};
MBeanStatDescriptor[] msds = (MBeanStatDescriptor[])ac.invoke(perfOName,
&quot;listStatMembers&quot;, params, signature);
if(msds == null)
return null;
for(int i=0; i&lt;msds.length; i++)
{
MBeanStatDescriptor[] msds2 = listStatMembers(msds[i]);
// you may recursively call listStatMembers until find the one you want
}
return msds;
}
catch(Exception e)
{
new AdminException(e).printStackTrace();
System.out.println(&quot;listStatMembers: Exception Thrown&quot;);
return null;
}
}
/**
* sample code to get PMI data from beanModule
*/
public void testEJB()
{
// This is the MBeanStatDescriptor for Enterprise EJB
MBeanStatDescriptor beanMsd = getStatDescriptor(serverOName, PmiConstants.BEAN_MODULE);
if(beanMsd == null)
System.out.println(&quot;Error: cannot find beanModule&quot;);
// get the Stats for module level only since recursive is false
Stats stats = getStatsObject(beanMsd.getObjectName(), beanMsd.getStatDescriptor(),
false); // pass true if you wannt data from individual beans
// find the avg method RT
TimeStatisticImpl rt = (TimeStatisticImpl)stats.getStatistic(EJBStatsImpl.METHOD_RT);
System.out.println(&quot;rt is &quot; + rt.getMean());
try
{
java.lang.Thread.sleep(5000);
}
catch(Exception ex)
{
ex.printStackTrace();
}
// get the Stats again
Stats stats2 = getStatsObject(beanMsd.getObjectName(), beanMsd.getStatDescriptor(),
false); // pass true if you wannt data from individual beans
// find the avg method RT
TimeStatisticImpl rt2 = (TimeStatisticImpl)stats2.getStatistic(EJBStatsImpl.METHOD_RT);
System.out.println(&quot;rt2 is &quot; + rt2.getMean());
// calculate the difference between this time and last time.
TimeStatisticImpl deltaRt = (TimeStatisticImpl)rt2.delta(rt);
System.out.println(&quot;deltaRt is &quot; + rt.getMean());
}
/**
* Sample code to show how to call getStats on StatisticProvider MBean directly.
*/
public void testJSR77Stats()
{
// first, find the MBean ObjectName you are interested.
// Refer method getObjectNames for sample code.
// assume we want to call getStats on JVM MBean to get statistics
try
{
com.ibm.websphere.management.statistics.JVMStats stats =
(com.ibm.websphere.management.statistics.JVMStats)ac.invoke(jvmOName,
&quot;getStats&quot;, null, null);
System.out.println(&quot;\n get data from JVM MBean&quot;);
if(stats == null)
{
System.out.println(&quot;WARNING: getStats on JVM MBean returns null&quot;);
}
else
{
// first, link with the static info if you care
((Stats)stats).setConfig(PmiClient.findConfig(configs, jvmOName));
// print out all the data if you want
//System.out.println(stats.toString());
// navigate and get the data in the stats object
processStats((Stats)stats);
// call JSR77 methods on JVMStats to get the related data
com.ibm.websphere.management.statistics.CountStatistic upTime =
stats.getUpTime();
com.ibm.websphere.management.statistics.BoundedRangeStatistic heapSize =
stats.getHeapSize();
if(upTime != null)
System.out.println(&quot;\nJVM up time is &quot; + upTime.getCount());
if(heapSize != null)
System.out.println(&quot;\nheapSize is &quot; + heapSize.getCurrent());
}
}
catch(Exception ex)
{
ex.printStackTrace();
new AdminException(ex).printStackTrace();
}
}
}
</pre>
</body>
</html>