JMX 远程监控 JBoos/Tomcat
● 启用JBoss JMX远程访问的身份验证:
在目录%JBoss_Home%/server\default\deploy 下找到文件 jmx-jboss-beans.xml,在该文件的大约21-23行,找到
<!--UNCOMMENT THIS <property name="securityDomain">jmx-console</property> -->去掉注释,修改为:
<property name="securityDomain">jmx-console</property>
JBoss 默认的用户名为:admin,密码为:admin。用户的访问权限为:JBossAdmin,HttpInvoker。
如果要修改用户名和密码,可以在目录%JBoss_Home%/server\default\conf\props 下找到文件 jmx-console-roles.properties,在里面进行修改即可。
如果要修改用户的访问权限,可以在目录%JBoss_Home%/server\default\conf\props 下找到文件 jmx-console-users.properties ,在里面进行修改即可。
JBoss JMX 远程访问的默认端口为:1090。.如果要修改该端口,可以在目录
%JBoss_Home%/server\default\conf\bindingservice.beans\META-INF 下找到文件 bindings-jboss-beans.xml ,大约在244 行,进行修改即可。
● 启用Tomcat JMX 远程访问身份验证:
因为Tomcat 是通过JDK 进行JMX 远程管理的,所以要在JDK 中添加username,password 和 访问权限。
找到目录 %JAVA_HOME%\jre\lib\management 下的文件 jmxremote.password.template 和 jmxremote.access。
jmxremote.password.template 是一个模板文件,提供了如何创建用户名和密码的格式,在最后可以看到JDK 提供了两个用户名和密码,只不过是通过“#”注释掉了,
然后再改目录下新建一个 jmxremote.password 文件,在这个新建的文件里添加用户名和密码,例如:admin admin。
jmxremote.access 文件是配置用户访问权限的,在最后添加 admin readonly 或 admin readwrite。
再在目录%TOMCAT_HOME%\bin 下找到 文件 catalina.bat,编辑这个文件,在里面添加:
set JAVA_OPTS=%JAVA_OPTS% -Djava.rmi.server.hostname=192.168.100.126
set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.port="1090" // 远程访问端口
set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.authenticate="true" // 身份验证
set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.ssl="false" //SSL验证
在window下,现需要jdk装在NTFS文件系统下,选中文件,点右键“属性”-〉安全,点“高级”,去掉“从父项继承....”,弹出窗口中选“删除”,这样删除了所有访问权限。再选“添加”-〉高级,“立即查找”,选中你的用户,例administrator,点“确定",“确定"。来到权限窗口,勾选"完全控制",点"确定",OK了。
● 通过jconsole 监控 JBoss/Tomcat
打开%JAVA_HOME%\bin\jconsole.exe 在“远程进程”中输入“192.168.0.88:1090”,用户名“admin”,口令 “admin”,就可监控 IP 为“192.168.0.88” 的JBoss/Tomcat 服务器。
● 通过Java 程序获得JBoss/Tomcat 中的信息
package com.demo.jmx.jconsole;
import java.lang.management.MemoryUsage;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
public class JMXMonitor {
private static final String URL = "service:jmx:rmi:///jndi/rmi://127.0.0.1:1090/jmxrmi";
protected MBeanServerConnection mbsc;
private String path;
private Integer maxActiveSessions;
private Integer activeSessions;
private Integer sessionCounter;
private long maxMemory;
private long committedMemory;
private long usedMemory;
private String protocolPort;
private Integer maxThreads;
private Integer currentThreadCount;
private Integer currentThreadsBusy;
public JMXMonitor() throws Exception{
JMXServiceURL serviceURL = new JMXServiceURL(URL);
Map<String,String[]>map = new HashMap<String,String[]>();
String[] credentials = new String[] { "admin" , "admin" };
map.put("jmx.remote.credentials", credentials);
JMXConnector connector = JMXConnectorFactory.connect(serviceURL, map);
mbsc = connector.getMBeanServerConnection();
}
public Map<?,?>createSessionCounter()throws Exception{
String objectName = "jboss.web:type=Manager,path=/*,host=localhost"; //JBoss
//String objectName1 = "Catalina:type=Manager,context=/*,host=localhost"; //Tomcat
ObjectName managerObjName = new ObjectName(objectName);
Set<ObjectName> set=mbsc.queryNames(managerObjName, null);
Map<String,JMXMonitor> map = new HashMap<String,JMXMonitor>();
for (ObjectName obj:set){
if(objectName.indexOf(objectName)!= -1)
path = obj.getKeyProperty("path");
if(path.indexOf(objectName)!= -1)
path = obj.getKeyProperty("context");
ObjectName objname = new ObjectName(obj.getCanonicalName());
maxActiveSessions = (Integer)mbsc.getAttribute( objname, "maxActiveSessions");
activeSessions = (Integer)mbsc.getAttribute( objname, "activeSessions");
sessionCounter = (Integer)mbsc.getAttribute( objname, "sessionCounter");
map.put(path, this);
}
return map;
}
public void createHeapMemoryCounter() throws Exception{
String objectName = "java.lang:type=Memory";
ObjectName heapObjName = new ObjectName(objectName);
CompositeDataSupport cds = (CompositeDataSupport)mbsc.getAttribute(heapObjName, "HeapMemoryUsage");
MemoryUsage heapMemoryUsage = MemoryUsage.from(cds);
maxMemory = heapMemoryUsage.getMax();
committedMemory = heapMemoryUsage.getCommitted();
usedMemory = heapMemoryUsage.getUsed();
}
public Map<?,?>createThreadPoolCounter() throws Exception{
String objectName = "jboss.web:type=ThreadPool,name=*"; //JBoss
//String objectName = "Catalina:type=ThreadPool,name=*"; //Tomcat
ObjectName threadPoolObjName = new ObjectName(objectName);
Set<ObjectName> set=mbsc.queryNames(threadPoolObjName, null);
Map<String,JMXMonitor> map = new HashMap<String,JMXMonitor>();
for (ObjectName obj:set){
protocolPort = obj.getKeyProperty("name");
ObjectName objname=new ObjectName(obj.getCanonicalName());
maxThreads = (Integer)mbsc.getAttribute( objname, "maxThreads");
currentThreadCount = (Integer)mbsc.getAttribute( objname, "currentThreadCount");
currentThreadsBusy = (Integer)mbsc.getAttribute( objname, "currentThreadsBusy");
map.put(protocolPort, this);
}
return map;
}
public String getPath() {
return path;
}
public Integer getMaxActiveSessions() {
return maxActiveSessions;
}
public Integer getActiveSessions() {
return activeSessions;
}
public Integer getSessionCounter() {
return sessionCounter;
}
public long getMaxMemory() {
return maxMemory;
}
public long getCommittedMemory() {
return committedMemory;
}
public long getUsedMemory() {
return usedMemory;
}
public String getProtocolPort() {
return protocolPort;
}
public Integer getMaxThreads() {
return maxThreads;
}
public Integer getCurrentThreadCount() {
return currentThreadCount;
}
public Integer getCurrentThreadsBusy() {
return currentThreadsBusy;
}
}
测试类
package com.demo.jmx.jconsole;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class JMXTest {
public static void main(String[] args) throws Exception{
JMXMonitor monitor = new JMXMonitor();
Map<?,?>sessionMap = monitor.createSessionCounter();
Set<?>pathSet = sessionMap.keySet();
Iterator<?>it = pathSet.iterator();
while(it.hasNext()){
String path = (String)it.next();
System.out.println("path:" + path);
JMXMonitor seesionMonitor = (JMXMonitor)sessionMap.get(path);
System.out.println("activeSessions:" + seesionMonitor.getActiveSessions());
System.out.println("maxActiveSessions:" + seesionMonitor.getMaxActiveSessions());
System.out.println("activeSessions:" + seesionMonitor.getSessionCounter());
System.out.println();
}
System.out.println("#######################################################");
monitor.createHeapMemoryCounter();
long committedMemory = monitor.getCommittedMemory();
long maxMemory = monitor.getMaxMemory();
long usedMemory = monitor.getUsedMemory();
System.out.println("committedMemory:" + committedMemory);
System.out.println("maxMemory:" + maxMemory);
System.out.println("usedMemory:" + usedMemory);
System.out.println("#######################################################");
Map<?,?> threadPoolMap = monitor.createThreadPoolCounter();
Set<?> protocolPortSet = threadPoolMap.keySet();
Iterator<?>it1 = protocolPortSet.iterator();
while(it1.hasNext()){
String protocolPort = (String)it1.next();
System.out.println("protocol-Port:" + protocolPort);
JMXMonitor threadPoolMonitor = (JMXMonitor)threadPoolMap.get(protocolPort);
System.out.println("currentThreadCount:" + threadPoolMonitor.getCurrentThreadCount());
System.out.println("currentThreadsBusy:" + threadPoolMonitor.getCurrentThreadsBusy());
System.out.println("maxThreads:" + threadPoolMonitor.getMaxThreads());
System.out.println();
}
}
}