package com.monitor.util;
import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.exception.*;
import java.util.*;
import javax.management.*;
import com.ibm.websphere.pmi.*;
import com.ibm.websphere.pmi.stat.*;
public class PmiJmxUtil implements PmiConstants {
private AdminClient ac = null;
private ObjectName perfOName = null;
private ObjectName serverOName = null;
private ObjectName jvmOName = null;
private ObjectName ejbOName = null;
public static Map serverStatusMap = new HashMap();
private String host;
private String port;
// private static PmiJmxUtil instance;
// public PmiJmxUtil(){}
public PmiJmxUtil(String host,String port) {
this.host=host;
this.port=port;
init();
try {
getObjectNames();
} catch (Exception e) {
}
}
//初始化AdminClient
public void init() {
try {
String connector = AdminClient.CONNECTOR_TYPE_SOAP;
System.out.println("host:"+host+" info:创建AdminClient: host="+host+",port="+port);
ac = getAdminClient(host, port, connector);
serverStatusMap.put(host, "success");
} catch (Exception e) {
serverStatusMap.put(host, "exception");
System.out.println("host:"+host+" error:"+e.getMessage());
return;
}
}
//通过host,port,connector得到AdminClient
public final AdminClient getAdminClient(String hostStr, String portStr,
String connector) throws Exception {
System.out.println("host:"+host+" info:创建AdminClient中...");
long begin=System.currentTimeMillis();
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);
//props.put(AdminClient.CONNECTOR_SOAP_REQUEST_TIMEOUT, "6000");
//System.setProperty("was.install.root","E:/Program Files/IBM/SDP/runtimes/base_v61");
try {
ac = AdminClientFactory.createAdminClient(props);
} catch (Exception ex) {
System.out.println("host:"+host+" error:创建AdminClient异常:");
String msg ="系统无法创建 SOAP 连接器以连接到端口 "+port+" 上的主机 "+host;
throw new Exception(msg);
}
long end=System.currentTimeMillis();
System.out.println("host:"+host+" info:创建AdminClient耗时-->"+(end-begin)+"ms");
return ac;
}
public final void getObjectNames() throws Exception {
if(ac == null){
throw new Exception("host:"+host+" error:连接初始化失败");
}else{
try {
//得到一个ObjectNames的列表
javax.management.ObjectName on = new javax.management.ObjectName("WebSphere:*");
//得到服务器上所有的ObjectNames
Set objectNameSet = ac.queryNames(on, null);
//获取所需的ObjectNames
if (objectNameSet != null) {
Iterator i = objectNameSet.iterator();
while (i.hasNext()) {
on = (ObjectName) i.next();
String type = on.getKeyProperty("type");
if (type != null && type.equals("Perf")) {
//System.out.println("\nMBean: perf =" + on.toString());
perfOName = on;
//System.out.println(on.toString());
}
if (type != null && type.equals("Server")) {
//System.out.println("\nMBean: Server =" + on.toString());
serverOName = on;
}
if (type != null && type.equals("JVM")) {
//System.out.println("\nMBean: jvm =" + on.toString());
jvmOName = on;
}
if (type != null && type.equals("MessageDrivenBean")){
//System.out.println("\nMBean: MessageDrivenBean=" + on.toString());
ejbOName = on;
}
}
} else {
System.out.println("error: no object names found");
return;
}
//确认perfMBean不为空
if (perfOName == null) {
System.out.println("PerfMBean = NULL");
return;
}
} catch (Exception ex) {
new AdminException(ex).printStackTrace();
ex.printStackTrace();
}
}
}
public final com.ibm.websphere.pmi.stat.WSStats getJVMStats() throws Exception{
com.ibm.websphere.pmi.stat.WSStats jvmStats=null;
if(ac == null){
throw new Exception("host:"+host+" error:连接初始化失败");
}else{
try {
long start =System.currentTimeMillis();
String[] signature = new String[] { "javax.management.ObjectName","java.lang.Boolean" };
Object[] params = new Object[] { jvmOName, new Boolean(false) };
jvmStats = (WSStats)ac.invoke(perfOName, "getStatsObject",
params, signature);
long end =System.currentTimeMillis();
System.out.println("host:"+host+" info:成功获取JVMStats--"+(end-start)+"ms");
} catch (java.lang.IllegalArgumentException e){
String msg = "操作超时";
System.out.println("host:"+host+" error: "+msg);
}catch (com.ibm.websphere.management.exception.ConnectorException e) {
String msg = "系统无法进行 SOAP RPC 调用:invoke";
System.out.println("host:"+host+" error:"+msg);
}catch (Exception e) {
System.out.println("host:"+host+" error:获取JVMStats异常:");
e.printStackTrace();
}
}
return jvmStats;
}
public final com.ibm.websphere.pmi.stat.WSStats getEJBStats() throws Exception{
com.ibm.websphere.pmi.stat.WSStats ejbStats=null;
if(ac == null){
throw new Exception("host:"+host+" error:连接初始化失败");
}else{
try {
long start =System.currentTimeMillis();
String[] signature = new String[] { "javax.management.ObjectName","java.lang.Boolean" };
Object[] params = new Object[] { ejbOName, new Boolean(false) };
ejbStats = (WSStats)ac.invoke(perfOName, "getStatsObject",
params, signature);
long end =System.currentTimeMillis();
System.out.println("host:"+host+" info:成功获取EJBStats--"+(end-start)+"ms");
}catch (java.lang.IllegalArgumentException e){
String msg = "操作超时";
System.out.println("host:"+host+" error:"+msg);
}catch (com.ibm.websphere.management.exception.ConnectorException e) {
String msg = "系统无法进行 SOAP RPC 调用:invoke";
System.out.println("host:"+host+" error:"+msg);
}catch (Exception e) {
System.out.println("host:"+host+" error:获取EJBStats异常:");
e.printStackTrace();
}
}
return ejbStats;
}
public Map getJDBCStats(String[] providers) throws Exception{
Object[] params=null;
String[] signature=null;
com.ibm.websphere.pmi.stat.WSStats[] wsStats=null;
//List list = new ArrayList();
Map map = new HashMap();
if(ac == null){
throw new Exception("host:"+host+" error:连接初始化失败");
}else{
for(String provider : providers){
// 获取JDBCProvider
try {
System.out.println("host:"+host+" info:获取"+provider+"信息...");
long start = System.currentTimeMillis();
signature = new String[] {
"[Lcom.ibm.websphere.pmi.stat.StatDescriptor;",
"java.lang.Boolean" };
StatDescriptor jdbcProvider = new StatDescriptor(
new String[] { WSJDBCConnectionPoolStats.NAME, "Oracle JDBC Driver",provider});
params = new Object[] {
new StatDescriptor[] { jdbcProvider },
new Boolean(true) };
wsStats = (com.ibm.websphere.pmi.stat.WSStats[]) ac.invoke(
perfOName, "getStatsArray", params, signature);
long end = System.currentTimeMillis();
if(wsStats[0] == null){
map.put(provider, null);
throw new NullPointerException();
}else{
System.out.println("host:"+host+" info:"+provider+"信息获取成功 --"+(end-start)+"ms");
}
map.put(provider, wsStats);
} catch (NullPointerException e) {
String msg = "数据源"+provider+"不存在";
System.out.println("host:"+host+" error:"+msg);
} catch (java.lang.IllegalArgumentException e){
String msg = "操作超时";
System.out.println("host:"+host+" error:"+msg);
}catch (com.ibm.websphere.management.exception.ConnectorException e) {
String msg = "系统无法进行 SOAP RPC 调用:invoke";
System.out.println("host:"+host+" error: "+msg);
}catch (Exception e) {
e.printStackTrace();
}
}
}
return map;
}
}
PS:com.ibm.ws.admin.client_6.1.0.jar
JMX接口监测(一)
最新推荐文章于 2022-02-26 12:41:18 发布