通过JMX获取weblogic的监控数据,包括JDBC,SESSION,SERVERLET,JVM等信息。主要用到weblogic自己的t3协议,所以要用到weblogic的jar包:wlfullclient.jar和wlclient.jar。这两个jar包怎么获取我专门在另外一篇文章中讲。下面贴一些获取监控指标的代码,做个备份只用吧。

1、对JDBC的监控,只取了最重要的监控指标:

package test;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable; import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context; public class PrintJdbc {
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service; static {
try {
service = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
}catch (MalformedObjectNameException e) {
throw new AssertionError(e.getMessage());
}
} public static void initConnection(String hostname, String portString,
String username, String password) throws IOException,
MalformedURLException {
String protocol = "t3";
Integer portInteger = Integer.valueOf(portString);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.runtime";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,
port, jndiroot + mserver);
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
"weblogic.management.remote");
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
} public static ObjectName getServerRuntimes() throws Exception {
return (ObjectName)connection.getAttribute(service,
"ServerRuntime");
} public ObjectName getJDBCServer() throws Exception {
ObjectName dc = getServerRuntimes();
ObjectName jdbcService = (ObjectName)connection.getAttribute(dc,
"JDBCServiceRuntime");
return jdbcService;
} public ObjectName[] getJDBCDataSourceRuntime() throws Exception {
ObjectName[] jdbcDataSourceRTMB = (ObjectName[]) connection.getAttribute(getJDBCServer(), "JDBCDataSourceRuntimeMBeans");
return jdbcDataSourceRTMB;
} public void printJdbc() throws Exception {
ObjectName[] objectList = getJDBCDataSourceRuntime();
if(objectList != null && objectList.length > 0){
for(ObjectName obj : objectList){
int connectionsTotalCount = (int) connection.getAttribute(obj, "ConnectionsTotalCount");
int activeConnectionsCurrentCount = (int)connection.getAttribute(obj, "ActiveConnectionsCurrentCount");
int activeConnectionsAverageCount = (int)connection.getAttribute(obj,"ActiveConnectionsAverageCount");
int failuresToReconnectCount = (int)connection.getAttribute(obj, "FailuresToReconnectCount");
String name = (String)connection.getAttribute(obj, "Name");
int waitingForConnectionCurrentCount = (int)connection.getAttribute(obj, "WaitingForConnectionCurrentCount");
long waitingForConnectionFailureTotal = (long)connection.getAttribute(obj, "WaitingForConnectionFailureTotal");
int waitSecondsHighCount = (int)connection.getAttribute(obj, "WaitSecondsHighCount");
int connectionDelayTime = (int)connection.getAttribute(obj, "ConnectionDelayTime");
int activeConnectionsHighCount = (int)connection.getAttribute(obj, "ActiveConnectionsHighCount");
int currCapacity = (int)connection.getAttribute(obj, "CurrCapacity");
System.out.println("currCapacity==" + currCapacity);
System.out.println("activeConnectionsHighCount==" + activeConnectionsHighCount);
System.out.println("connectionDelayTime==" + connectionDelayTime);
System.out.println("connectionsTotalCount==" + connectionsTotalCount);
System.out.println("activeConnectionsCurrentCount==" + activeConnectionsCurrentCount);
System.out.println("activeConnectionsAverageCount==" + activeConnectionsAverageCount);
System.out.println("failuresToReconnectCount==" + failuresToReconnectCount);
System.out.println("name==" + name);
System.out.println("waitingForConnectionCurrentCount==" +waitingForConnectionCurrentCount );
System.out.println("waitingForConnectionFailureTotal==" + waitingForConnectionFailureTotal);
System.out.println("waitSecondsHighCount==" + waitSecondsHighCount); }
}
}
public static void main(String[] args) throws Exception { String hostname = "10.111.131.50";
String portString = "7001";
String username = "weblogic";
String password = "weblogic123";
PrintJdbc s = new PrintJdbc();
initConnection(hostname, portString, username, password);
s.printJdbc();
connector.close();
} }

  2、有关thread的监控:

package test;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable; import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context; public class PrintThread { private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service; static {
try {
service = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
}catch (MalformedObjectNameException e) {
throw new AssertionError(e.getMessage());
}
} public static void initConnection(String hostname, String portString,
String username, String password) throws IOException,
MalformedURLException {
String protocol = "t3";
Integer portInteger = Integer.valueOf(portString);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.runtime";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,
port, jndiroot + mserver);
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
"weblogic.management.remote");
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
} public static ObjectName getServerRuntimes() throws Exception {
return (ObjectName)connection.getAttribute(service,
"ServerRuntime");
} public void printInfo() throws Exception{
ObjectName objThreadPool = null;
ObjectName serverRT = getServerRuntimes(); objThreadPool = (ObjectName) connection.getAttribute(serverRT, "ThreadPoolRuntime");
double throughput = Double.parseDouble(String.valueOf(connection.getAttribute(
objThreadPool,"Throughput")));
System.out.println("throughput:" + throughput); int executeThreadTotalCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool, "ExecuteThreadTotalCount")));
System.out.println("executeThreadTotalCount:" + executeThreadTotalCount); int executeThreadIdleCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool, "ExecuteThreadIdleCount")));
System.out.println("executeThreadIdleCount:" + executeThreadIdleCount); int StandbyThreadCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool, "StandbyThreadCount")));
System.out.println("StandbyThreadCount:" + StandbyThreadCount); long timestamp = System.currentTimeMillis() / 1000;
String metricJson = "";
String jsonFormat = "{\"name\": \"weblogic_threadpool_metric_demo\", " +
"\"command\":\"weblogic_threadpool_metric\"," +
"\"type\": \"metric\"," +
"\"handlers\": [\"influxdb\"],"+
"\"output\": \"%s %.5f %d\\n%s %d %d\\n%s %d %d\\n\"," +
"\"status\": 0}";
metricJson = String.format(jsonFormat,
".weblogic.threadpool.throughput", (double)throughput, timestamp,
".weblogic.threadpool.executeThreadTotalCount", (int)executeThreadTotalCount, timestamp,
".weblogic.threadpool.executeThreadIdleCount", (int)executeThreadIdleCount, timestamp);
System.out.println("metricJson==" + metricJson); } public static void main(String args[]) throws Exception {
String hostname = "10.111.131.50";
String portString = "7001";
String username = "weblogic";
String password = "weblogic123";
PrintThread s = new PrintThread();
initConnection(hostname, portString, username, password);
s.printInfo();
connector.close();
}
}

  3、有关session的监控:

package test;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable; import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context; public class PrintServerState {
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service; static {
try {
service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
}catch (MalformedObjectNameException e) {
throw new AssertionError(e.getMessage());
}
} public static void initConnection(String hostname, String portString,
String username, String password) throws IOException,
MalformedURLException {
String protocol = "t3";
Integer portInteger = Integer.valueOf(portString);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.domainruntime";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,
port, jndiroot + mserver);
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
"weblogic.management.remote");
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
} public static ObjectName[] getServerRuntimes() throws Exception {
return (ObjectName[]) connection.getAttribute(service,
"ServerRuntimes");
} public static ObjectName[] getApplicationRuntimes(ObjectName serverRuntime) throws Exception {
return (ObjectName[])connection.getAttribute(serverRuntime, "ApplicationRuntimes");
} public static ObjectName[] getComponentRuntimes(ObjectName componentRutime) throws Exception {
return (ObjectName[]) connection.getAttribute(componentRutime, "ComponentRuntimes");
} public void printSessionState() throws Exception {
ObjectName[] serverRT = getServerRuntimes();
int length = (int) serverRT.length; for(int i = 0;i<length;i++){
ObjectName[] applicationRT = getApplicationRuntimes(serverRT[i]);
int length_app = (int)applicationRT.length; for(int y = 0;y<length_app;y++){
String applicationName = (String)connection.getAttribute(applicationRT[y], "ApplicationName"); if(applicationName.equals("helloworld")){ ObjectName[] componentRuntime = getComponentRuntimes(applicationRT[y]);
int length_component = (int)componentRuntime.length;
System.out.println("length_component==" + length_component);
for(int z = 0;z<length_component;z++){ String type = (String)connection.getAttribute(componentRuntime[z], "Type");
if(type.equals("WebAppComponentRuntime")){ int SessionsOpenedTotalCount = (int)connection.getAttribute(componentRuntime[z], "SessionsOpenedTotalCount");
System.out.println("SessionsOpenedTotalCount==" + SessionsOpenedTotalCount); int openSessionsHighCount = (int)connection.getAttribute(componentRuntime[z], "OpenSessionsHighCount");
System.out.println("openSessionsHighCount==" + openSessionsHighCount); int openSessionsCurrentCount = (int)connection.getAttribute(componentRuntime[z], "OpenSessionsCurrentCount");
System.out.println("openSessionsCurrentCount==" + openSessionsCurrentCount);
break;
} }
break;
}
}
}
} public void printNameAndState() throws Exception {
ObjectName[] serverRT = getServerRuntimes();
System.out.println("got server runtimes");
int length = (int) serverRT.length;
for (int i = 0; i < length; i++) { String name = (String) connection.getAttribute(serverRT[i],
"Name"); String state = (String) connection.getAttribute(serverRT[i],
"State"); long activationTime = (Long)connection.getAttribute(serverRT[i], "ActivationTime");
System.out.println("Server name: " + name);
System.out.println("Server state:" + state);
System.out.println("Server activationTime:" + activationTime); String weblogicVersion = (String) connection.getAttribute(serverRT[i], "WeblogicVersion");
System.out.println("Server weblogicVersion:" + weblogicVersion); String weblogicHome = (String) connection.getAttribute(serverRT[i], "WeblogicHome");
System.out.println("Server WeblogicHome:" +weblogicHome ); }
}
public static void main(String[] args) throws Exception {
String hostname = "10.111.131.50";
String portString = "7001";
String username = "weblogic";
String password = "weblogic123";
PrintServerState s = new PrintServerState();
initConnection(hostname, portString, username, password);
s.printNameAndState();
s.printSessionState();
connector.close();
}
}

  4、有关serverlet的监控:

package test;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable; import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context; public class PrintServerlet { private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;
private final String applicationName_app="helloworld";
private final String serverletName1="index.jsp";
private final String serverletName2="helloworld.jsp"; static {
try {
service = new ObjectName(
"com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
}catch (MalformedObjectNameException e) {
throw new AssertionError(e.getMessage());
}
} public static void initConnection(String hostname, String portString,
String username, String password) throws IOException,
MalformedURLException {
String protocol = "t3";
Integer portInteger = Integer.valueOf(portString);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.domainruntime";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,
port, jndiroot + mserver);
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
"weblogic.management.remote");
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
} public static ObjectName[] getServerRuntimes() throws Exception {
return (ObjectName[]) connection.getAttribute(service,
"ServerRuntimes");
} public String getApplicationName(ObjectName objectName) throws Exception {
return (String)connection.getAttribute(objectName, "Name");
} public String getServletName(ObjectName objectName) throws Exception {
return (String)connection.getAttribute(objectName,"Name");
} public void getServletData() throws Exception {
ObjectName[] serverRT = getServerRuntimes();
int length = (int) serverRT.length;
for (int i = 0; i < length; i++) {
ObjectName[] appRT =
(ObjectName[]) connection.getAttribute(serverRT[i],
"ApplicationRuntimes");
int appLength = (int) appRT.length;
for (int x = 0; x < appLength; x++) {
String applicationName = getApplicationName(appRT[x]);
if(applicationName_app.equals(applicationName)){
System.out.println("Application name: " + applicationName);
ObjectName[] compRT =
(ObjectName[]) connection.getAttribute(appRT[x],
"ComponentRuntimes");
int compLength = (int) compRT.length; for (int y = 0; y < compLength; y++) {
printValue("Component name",(String)connection.getAttribute(compRT[y], "Name"));
String componentType =
(String) connection.getAttribute(compRT[y], "Type");
System.out.println(componentType.toString());
if (componentType.toString().equals("WebAppComponentRuntime")){
ObjectName[] servletRTs = (ObjectName[])
connection.getAttribute(compRT[y], "Servlets");
int servletLength = (int) servletRTs.length;
for (int z = 0; z < servletLength; z++) {
String servletName = getServletName(servletRTs[z]);
if (servletName.contains(serverletName1) || servletName.contains(serverletName2)){
printValue("Servlet name",servletName); printValue("Servlet context path" ,
(String)connection.getAttribute(servletRTs[z],
"ContextPath"));
printValue("Invocation Total Count" ,
(Object)connection.getAttribute(servletRTs[z],
"InvocationTotalCount"));
printValue("ExecutionTimeHigh",
(Object)connection.getAttribute(servletRTs[z],
"ExecutionTimeHigh"));
printValue("ExecutionTimeTotal",(Object)connection.getAttribute(servletRTs[z],
"ExecutionTimeTotal"));
printValue("ExecutionTimeAverage",(Object)connection.getAttribute(servletRTs[z],
"ExecutionTimeAverage")); } }
}
}
}
}
}
} private void printValue(String key,Object value){
System.out.println(" " + key + " : " + value);
}
public static void main(String[] args) throws Exception {
String hostname = "10.111.131.50";
String portString = "7001";
String username = "weblogic";
String password = "weblogic123";
PrintServerlet s = new PrintServerlet();
initConnection(hostname, portString, username, password);
s.getServletData();
connector.close();
}
}

  

通过JMX获取weblogic的监控指标的更多相关文章

  1. Weblogic常用监控指标以及监控工具小结

    https://blog.csdn.net/hualusiyu/article/details/39583549

  2. Weblogic常用监控指标

    http://blog.csdn.net/konglongaa/article/details/53897562

  3. Hadoop记录- zookeeper 监控指标

    目前zookeeper获取监控指标已知的有两种方式: 1.通过zookeeper自带的 four letter words command 获取各种各样的监控指标 2.通过JMX Client连接zo ...

  4. 探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据

    上一篇博文介绍了如何连接Windows Azure: http://www.cnblogs.com/teld/p/5113063.html 本篇我们继续上次的示例代码,获取虚拟机的监控定义和监控数据. ...

  5. [博客迁移]探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据

    上一篇博文介绍了如何连接Windows Azure: http://www.cnblogs.com/teld/p/5113063.html 本篇我们继续上次的示例代码,获取虚拟机的监控定义和监控数据. ...

  6. 【转载】apache kafka系列之-监控指标

    原文地址:http://blog.csdn.net/lizhitao/article/details/24581907 1.监控目标 1.当系统可能或处于亚健康状态时及时提醒,预防故障发生 2.报警提 ...

  7. apache kafka系列之-监控指标

    apache kafka中国社区QQ群:162272557 1.监控目标 1.当系统可能或处于亚健康状态时及时提醒,预防故障发生 2.报警提示 a.短信方式 b.邮件 2.监控内容 2.1 机器监控 ...

  8. 关于kafka生产者相关监控指标的理解(未解决)

    关于生产者相关的监控指标含义的理解,希望大神帮忙进行确定下.     这边找了官网,看了网上各样的资料,但都无法帮我理解监控项目相关含义.     相关的监控项目是从jconsole获取的,并接入到了 ...

  9. Hadoop记录-Hadoop集群重要监控指标

    通用监控指标 对于每个RPC服务应该监控 RpcProcessingTimeAvgTime(PRC处理的平均时间) 通常hdfs在异常任务突发大量访问时,这个参数会突然变得很大,导致其他用户访问hdf ...

随机推荐

  1. selinue引起的ssh连接错误

    在客户端执行ssh依然报错: Permission denied (publickey,gssapi-keyex,gssapi-with-mic). 在这个页面不小心看到了原因: http://ser ...

  2. 矩阵快速幂计算hdu1575

    矩阵快速幂计算和整数快速幂计算相同.在计算A^7时,7的二进制为111,从而A^7=A^(1+2+4)=A*A^2*A^4.而A^2可以由A*A得到,A^4可以由A^2*A^2得到.计算两个n阶方阵的 ...

  3. 第十八章 Python批量管理主机(paramiko、fabric与pexpect)

    这个人的文章不错:http://lizhenliang.blog.51cto.com/all/7876557 转载:http://lizhenliang.blog.51cto.com/7876557/ ...

  4. DotnetBrowser入门教程-(3)启动与使用简单的WebSocket服务

    websocket是个很好的通信协议,基本可以贯穿支持html5的所有设备.dotnetbrowser内置了对websocket服务端与客户端的支持.请看例子: 1.新建桌面项目,基于.net 4.0 ...

  5. JAVA使用外部字体将文字生成图片,并使用FontMetrics居中文字

    需求: 1.用户输入文字,根据外部字体,将文字生成图片 2.输出的文字需要居中在图片中显示 遇到的问题: 1.如何导入外部字体?使用Java的Font类,所有的字体都是系统安装过的 2.每次用户输入的 ...

  6. android图片素材參考

    hpi:通常是大图像素是:480x800   (640*960)宽比长大致为0.6左右      一般240dpi.    小图的像素依据实际来. xhdi:一般大图像素是: 640x1136 (72 ...

  7. 应用设置Setting的实现

    有非常多应用都在iOS设置中有相关的设置.例如以下图:     通过这个设置能够方便的相应用的一些主要的设置进行更改. 要完整的实现这个设置功能,有下面几方面问题须要解决: 1)设置的编写(实现设置的 ...

  8. HTML5移动开发实战必备知识——本地存储(2)

    了解了一些主要的本地存储使用方法和思想后.我们来系统的介绍一下本地存储. 本地存储分为三大类:localStorage/sessionStorage/本地数据库 localStorage和sessio ...

  9. 重读金典------高质量C编程指南(林锐)-------第二章 程序的板式

    2.1 空行 规则1:在每个类声明之后,每个函数定义结束之后加空行. 规则2:在某个函数体内,相关的不加空行,不相关的加空行. // 空行 void Function1(-) { - } // 空行 ...

  10. c# out ref

    out 关键字会导致参数通过引用来传递.这与 ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化.若要使用 out 参数,方法定义和调用方法都必须显式使用 out 关键字.例如 ...