JMX remote monitoring JBoos/Tomcat

● Enable authentication for JBoss JMX remote access:

Find the file jmx-jboss-beans.xml under the directory %JBoss_Home%/server\default\deploy  , and at about lines 21-23 of the file, find

<!--UNCOMMENT THIS <property name="securityDomain">jmx-console</property> --> Remove the comment and modify it to:

<property name="securityDomain">jmx-console</property>

The default user name of JBoss is: admin, and the password is: admin. The user's access rights are: JBossAdmin, HttpInvoker.

If you want to modify the user name and password, you can find the file jmx-console-roles.properties in the directory %JBoss_Home%/server\default\conf\props  and modify it there.

If you want to modify the user's access rights, you can find the file jmx-console-users.properties  in the directory %JBoss_Home%/server\default\conf\props  and modify it there.

The default port for JBoss JMX remote access is: 1090. .If you want to modify the port, you can

Find the file bindings-jboss-beans.xml  under %JBoss_Home%/server\default\conf\bindingservice.beans\META-INF  , about line 244, you can modify it.

● Enable Tomcat JMX remote access authentication:

Because Tomcat is managed remotely by JMX through JDK, add username, password and access rights in JDK.

Locate the files jmxremote.password.template and jmxremote.access in the directory %JAVA_HOME%\jre\lib\management.

jmxremote.password.template is a template file that provides the format of how to create a username and password. At the end, you can see that the JDK provides two usernames and passwords, but they are commented out by "#",

Then change the directory to create a new  jmxremote.password  file, and add the user name and password to the new file, for example: admin admin.

The jmxremote.access  file configures user access rights, and adds admin readonly or admin readwrite at the end.

Then find the file catalina.bat in the directory %TOMCAT_HOME%\bin, edit the file, and add:


set JAVA_OPTS=%JAVA_OPTS% -Djava.rmi.server.hostname=192.168.100.126
set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.port="1090" // remote access port
set JAVA_OPTS=%JAVA_OPTS% - Dcom.sun.management.jmxremote.authenticate="true" // authentication
set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.ssl="false" //SSL verification

Under the window, jdk needs to be installed under the NTFS file system, select the file, right-click "Properties" -> Security, click "Advanced", remove "Inherit from parent....", select "Delete" in the pop-up window, This removes all access rights. Then select "Add" -> Advanced, "Find Now", select your user, such as administrator, click "OK", "OK". Come to the permissions window, check "Full Control", click "OK", OK.

● Monitor JBoss/Tomcat through jconsole

Open %JAVA_HOME%\bin\jconsole.exe, enter "192.168.0.88:1090" in "Remote Process", user name "admin", password "admin", you can monitor JBoss/Tomcat whose IP is "192.168.0.88" server.

● Obtain information in JBoss/Tomcat through Java programs

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();
                       }
              }

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327003861&siteId=291194637