`

JMX监控Tomcat

 
阅读更多

环境:Tomcat5.5、jdk6
1、先配Tomcat的启动语句,window下tomcat的bin/catalina.bat(linux为catalina.sh),在头上注释部分(.bat为rem、.sh为#)后面加上set JAVA_OPTS=-Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true
(linux为JAVA_OPTS=-Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true)
2、修改jmx远程访问授权。默认为JAVA_HOME/jre/lib/management下jmxremote.access、jmxremote.password(缺省系统提供了个模版jmxremote.password.template改下名就成)
注意:linux下需要该权限,chmod 600 jmxremote.access, chmod 600 jmxremote.password
window下特麻烦,现需要jdk装在NTFS文件系统下,选中文件,点右键“属性”-〉安全,点“高级”,去掉“从父项继承....”,弹出窗口中选“删除”,这样删除了所有访问权限。再选“添加”-〉高级,“立即查找”,选中你的用户,例administrator,点“确定",“确定"。来到权限窗口,勾选"完全控制",点"确定",OK了。
3、测试JMX。启动tomcat,在window“命令行窗口”中输入netstat -an看下8999端口打开没有。若没有,则前面没配对。若已打开,则可在另一台机器的“命令行窗口”中输入jconsole,打开jdk自带的jmx客户端。选远程连接,录入tomcat所在机器的IP,端口例192.168.10.10:8999,帐号、密码在jmxremote.password中,如帐号controlRole,密码R&D(缺省monitorRole只能读,controlRole能读写,jmxremote.access中可配置)。点“连接”。看到图就行了。
4、关于数据。Mbean属性页中给出了相应的数据,Catalina中是tomcat的,java.lang是jvm的。对于加粗的黑体属性值,需双击一下才可看内容
5、关于编程。

public class JMXTest {   
  
    /**  
     * @param args  
     */  
    public static void main(String[] args) {   
        try {   
  
           String jmxURL = "service:jmx:rmi:///jndi/rmi://192.168.10.93:8999/jmxrmi";//tomcat jmx url   
           JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);   
              
           Map map = new HashMap();   
           String[] credentials = new String[] { "monitorRole" , "QED" };   
           map.put("jmx.remote.credentials", credentials);   
           JMXConnector connector = JMXConnectorFactory.connect(serviceURL, map);   
           MBeanServerConnection  mbsc = connector.getMBeanServerConnection();   
              
           //端口最好是动态取得   
           ObjectName threadObjName = new ObjectName("Catalina:type=ThreadPool,name=http-8080");   
           MBeanInfo mbInfo = mbsc.getMBeanInfo(threadObjName);   
              
           String attrName = "currentThreadCount";//tomcat的线程数对应的属性值   
           MBeanAttributeInfo[] mbAttributes = mbInfo.getAttributes();   
           System.out.println("currentThreadCount:"+mbsc.getAttribute(threadObjName, attrName));   
              
           //heap   
           for(int j=0;j <mbsc.getDomains().length;j++){    
               System.out.println("###########"+mbsc.getDomains()[j]);    
           }    
           Set MBeanset = mbsc.queryMBeans(null, null);   
           System.out.println("MBeanset.size() : " + MBeanset.size());   
           Iterator MBeansetIterator = MBeanset.iterator();   
           while (MBeansetIterator.hasNext()) {    
               ObjectInstance objectInstance = (ObjectInstance)MBeansetIterator.next();   
               ObjectName objectName = objectInstance.getObjectName();   
               String canonicalName = objectName.getCanonicalName();   
               System.out.println("canonicalName : " + canonicalName);    
               if (canonicalName.equals("Catalina:host=localhost,type=Cluster"))      {     
                   // Get details of cluster MBeans   
                   System.out.println("Cluster MBeans Details:");   
                   System.out.println("=========================================");    
                   //getMBeansDetails(canonicalName);   
                  String canonicalKeyPropList = objectName.getCanonicalKeyPropertyListString();   
              }   
           }   
           //------------------------- system ----------------------   
           ObjectName runtimeObjName = new ObjectName("java.lang:type=Runtime");   
           System.out.println("厂商:"+ (String)mbsc.getAttribute(runtimeObjName, "VmVendor"));   
           System.out.println("程序:"+ (String)mbsc.getAttribute(runtimeObjName, "VmName"));   
           System.out.println("版本:"+ (String)mbsc.getAttribute(runtimeObjName, "VmVersion"));   
           Date starttime=new Date((Long)mbsc.getAttribute(runtimeObjName, "StartTime"));   
           SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
           System.out.println("启动时间:"+df.format(starttime));   
              
           Long timespan=(Long)mbsc.getAttribute(runtimeObjName, "Uptime");   
           System.out.println("连续工作时间:"+JMXTest.formatTimeSpan(timespan));   
           //------------------------ JVM -------------------------   
           //堆使用率   
           ObjectName heapObjName = new ObjectName("java.lang:type=Memory");   
           MemoryUsage heapMemoryUsage =  MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(heapObjName, "HeapMemoryUsage"));   
           long maxMemory = heapMemoryUsage.getMax();//堆最大   
           long commitMemory = heapMemoryUsage.getCommitted();//堆当前分配   
           long usedMemory = heapMemoryUsage.getUsed();   
           System.out.println("heap:"+(double)usedMemory*100/commitMemory+"%");//堆使用率   
              
           MemoryUsage nonheapMemoryUsage =  MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(heapObjName, "NonHeapMemoryUsage"));              
           long noncommitMemory = nonheapMemoryUsage.getCommitted();   
           long nonusedMemory = heapMemoryUsage.getUsed();   
           System.out.println("nonheap:"+(double)nonusedMemory*100/noncommitMemory+"%");   
              
           ObjectName permObjName = new ObjectName("java.lang:type=MemoryPool,name=Perm Gen");   
           MemoryUsage permGenUsage =  MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(permObjName, "Usage"));              
           long committed = permGenUsage.getCommitted();//持久堆大小   
           long used = heapMemoryUsage.getUsed();//   
           System.out.println("perm gen:"+(double)used*100/committed+"%");//持久堆使用率   
              
           //-------------------- Session ---------------    
           ObjectName managerObjName = new ObjectName("Catalina:type=Manager,*");   
           Set<ObjectName> s=mbsc.queryNames(managerObjName, null);   
           for (ObjectName obj:s){   
               System.out.println("应用名:"+obj.getKeyProperty("path"));   
               ObjectName objname=new ObjectName(obj.getCanonicalName());   
               System.out.println("最大会话数:"+ mbsc.getAttribute( objname, "maxActiveSessions"));   
               System.out.println("会话数:"+ mbsc.getAttribute( objname, "activeSessions"));   
               System.out.println("活动会话数:"+ mbsc.getAttribute( objname, "sessionCounter"));   
           }   
              
           //----------------- Thread Pool ----------------   
           ObjectName threadpoolObjName = new ObjectName("Catalina:type=ThreadPool,*");   
           Set<ObjectName> s2=mbsc.queryNames(threadpoolObjName, null);   
           for (ObjectName obj:s2){   
               System.out.println("端口名:"+obj.getKeyProperty("name"));   
               ObjectName objname=new ObjectName(obj.getCanonicalName());   
               System.out.println("最大线程数:"+ mbsc.getAttribute( objname, "maxThreads"));   
               System.out.println("当前线程数:"+ mbsc.getAttribute( objname, "currentThreadCount"));   
               System.out.println("繁忙线程数:"+ mbsc.getAttribute( objname, "currentThreadsBusy"));   
           }   
                  
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
  
    public static String formatTimeSpan(long span){   
        long minseconds = span % 1000;   
           
        span = span /1000;   
        long seconds = span % 60;   
           
        span = span / 60;   
        long mins = span % 60;   
           
        span = span / 60;   
        long hours = span % 24;   
  
        span = span / 24;   
        long days = span;   
        return (new Formatter()).format("%1$d天 %2$02d:%3$02d:%4$02d.%5$03d", days,hours,mins,seconds,minseconds).toString();   
    }   
}  
public class JMXTest { 

/** 
  * @param args 
  */ 
public static void main(String[] args) { 
  try { 

     String jmxURL = "service:jmx:rmi:///jndi/rmi://192.168.10.93:8999/jmxrmi";//tomcat jmx url 
           JMXServiceURL serviceURL = new JMXServiceURL(jmxURL); 
           
           Map map = new HashMap(); 
           String[] credentials = new String[] { "monitorRole" , "QED" }; 
           map.put("jmx.remote.credentials", credentials); 
           JMXConnector connector = JMXConnectorFactory.connect(serviceURL, map); 
           MBeanServerConnection  mbsc = connector.getMBeanServerConnection(); 
           
           //端口最好是动态取得 
           ObjectName threadObjName = new ObjectName("Catalina:type=ThreadPool,name=http-8080"); 
           MBeanInfo mbInfo = mbsc.getMBeanInfo(threadObjName); 
           
           String attrName = "currentThreadCount";//tomcat的线程数对应的属性值 
           MBeanAttributeInfo[] mbAttributes = mbInfo.getAttributes(); 
           System.out.println("currentThreadCount:"+mbsc.getAttribute(threadObjName, attrName)); 
           
           //heap 
           for(int j=0;j <mbsc.getDomains().length;j++){ 
            System.out.println("###########"+mbsc.getDomains()[j]); 
           } 
           Set MBeanset = mbsc.queryMBeans(null, null); 
           System.out.println("MBeanset.size() : " + MBeanset.size()); 
           Iterator MBeansetIterator = MBeanset.iterator(); 
           while (MBeansetIterator.hasNext()) { 
            ObjectInstance objectInstance = (ObjectInstance)MBeansetIterator.next(); 
            ObjectName objectName = objectInstance.getObjectName(); 
            String canonicalName = objectName.getCanonicalName(); 
            System.out.println("canonicalName : " + canonicalName); 
            if (canonicalName.equals("Catalina:host=localhost,type=Cluster"))      {  
             // Get details of cluster MBeans 
             System.out.println("Cluster MBeans Details:"); 
             System.out.println("========================================="); 
             //getMBeansDetails(canonicalName); 
               String canonicalKeyPropList = objectName.getCanonicalKeyPropertyListString(); 
           } 
           } 
           //------------------------- system ---------------------- 
           ObjectName runtimeObjName = new ObjectName("java.lang:type=Runtime"); 
           System.out.println("厂商:"+ (String)mbsc.getAttribute(runtimeObjName, "VmVendor")); 
           System.out.println("程序:"+ (String)mbsc.getAttribute(runtimeObjName, "VmName")); 
           System.out.println("版本:"+ (String)mbsc.getAttribute(runtimeObjName, "VmVersion")); 
           Date starttime=new Date((Long)mbsc.getAttribute(runtimeObjName, "StartTime")); 
           SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
           System.out.println("启动时间:"+df.format(starttime)); 
           
           Long timespan=(Long)mbsc.getAttribute(runtimeObjName, "Uptime"); 
           System.out.println("连续工作时间:"+JMXTest.formatTimeSpan(timespan)); 
           //------------------------ JVM ------------------------- 
           //堆使用率 
           ObjectName heapObjName = new ObjectName("java.lang:type=Memory"); 
           MemoryUsage heapMemoryUsage =  MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(heapObjName, "HeapMemoryUsage")); 
           long maxMemory = heapMemoryUsage.getMax();//堆最大 
           long commitMemory = heapMemoryUsage.getCommitted();//堆当前分配 
           long usedMemory = heapMemoryUsage.getUsed(); 
           System.out.println("heap:"+(double)usedMemory*100/commitMemory+"%");//堆使用率 
           
           MemoryUsage nonheapMemoryUsage =  MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(heapObjName, "NonHeapMemoryUsage"));           
           long noncommitMemory = nonheapMemoryUsage.getCommitted(); 
           long nonusedMemory = heapMemoryUsage.getUsed(); 
           System.out.println("nonheap:"+(double)nonusedMemory*100/noncommitMemory+"%"); 
           
           ObjectName permObjName = new ObjectName("java.lang:type=MemoryPool,name=Perm Gen");
           MemoryUsage permGenUsage =  MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(permObjName, "Usage"));           
           long committed = permGenUsage.getCommitted();//持久堆大小 
           long used = heapMemoryUsage.getUsed();// 
           System.out.println("perm gen:"+(double)used*100/committed+"%");//持久堆使用率 
           
           //-------------------- Session --------------- 
           ObjectName managerObjName = new ObjectName("Catalina:type=Manager,*"); 
           Set<ObjectName> s=mbsc.queryNames(managerObjName, null); 
           for (ObjectName obj:s){ 
            System.out.println("应用名:"+obj.getKeyProperty("path")); 
            ObjectName objname=new ObjectName(obj.getCanonicalName()); 
            System.out.println("最大会话数:"+ mbsc.getAttribute( objname, "maxActiveSessions")); 
            System.out.println("会话数:"+ mbsc.getAttribute( objname, "activeSessions")); 
            System.out.println("活动会话数:"+ mbsc.getAttribute( objname, "sessionCounter")); 
           } 
           
           //----------------- Thread Pool ---------------- 
           ObjectName threadpoolObjName = new ObjectName("Catalina:type=ThreadPool,*"); 
           Set<ObjectName> s2=mbsc.queryNames(threadpoolObjName, null); 
           for (ObjectName obj:s2){ 
            System.out.println("端口名:"+obj.getKeyProperty("name")); 
            ObjectName objname=new ObjectName(obj.getCanonicalName()); 
            System.out.println("最大线程数:"+ mbsc.getAttribute( objname, "maxThreads")); 
            System.out.println("当前线程数:"+ mbsc.getAttribute( objname, "currentThreadCount")); 
            System.out.println("繁忙线程数:"+ mbsc.getAttribute( objname, "currentThreadsBusy")); 
           } 
            
  } catch (Exception e) { 
            e.printStackTrace(); 
        } 
} 

public static String formatTimeSpan(long span){ 
  long minseconds = span % 1000; 
  
  span = span /1000; 
  long seconds = span % 60; 
  
  span = span / 60; 
  long mins = span % 60; 
  
  span = span / 60; 
  long hours = span % 24; 

  span = span / 24; 
  long days = span; 
  return (new Formatter()).format("%1$d天 %2$02d:%3$02d:%4$02d.%5$03d", days,hours,mins,seconds,minseconds).toString(); 
} 
} 
 


转自 diy8187  http://blog.csdn.net/diy8187/article/details/4369137

 

分享到:
评论

相关推荐

    jmx 监控 tomcat

    下面我们将详细介绍如何设置和使用JMX来监控Tomcat。 1. **开启JMX支持** 在Tomcat的`conf/server.xml`文件中,找到`&lt;Server&gt;`标签,在其中添加一个`Service`,并配置一个`Connector`来启用RMI(Remote Method ...

    jmx监控tomcat测试包

    catalina-jmx-remote.jar放到tomcat/lib目录下 如果是windows版本,编辑TOMCAT_HOME/bin/catalina.bat,在开头加入下面几行: set CATALINA_OPTS=%CATALINA_OPTS% -Djava.rmi.server.hostname=JMX_HOST set CATALINA...

    zabbix 监控 tomcat

    本文将介绍如何使用 Zabbix 通过 JMX 监控 Tomcat,帮助初学的朋友快速上手。 监控 Tomcat 的必要性 Tomcat 作为一个流行的 Java Web 服务器,广泛应用于各种 Web 应用程序中。然而,随着应用程序的复杂度增加,...

    v5developer#maven-framework-project#使用JMX监控Tomcat1

    ① 先修改Tomcat的启动脚本,windows下为bin/catalina.bat(linux下为catalina.sh),添加以下内容,8999是jmxre

    jmx监控weblogic,tomcat,websphere源码

    在本项目中,"jmx监控weblogic,tomcat,websphere源码"涉及了使用JMX来监控三个主流的Java应用服务器:WebLogic、Tomcat和WebSphere。这些服务器都是企业级应用部署的常见选择,对它们的监控对于确保系统性能、稳定性...

    Monitoring Apache Tomcat with JMX.pdf

    有多种工具可以用来通过JMX监控Tomcat,包括: 1. jconsole:JDK自带的轻量级监控工具,提供GUI界面来查看JVM和应用的运行状况。 2. VisualVM:更强大的JDK工具,包含内存分析、线程分析和CPU剖析等功能。 3. 应用级...

    jmx_tomcat_jboss

    总之,“jmx_tomcat_jboss”是关于利用JMX技术来管理和监控Tomcat和JBoss服务器,提高应用性能和稳定性的重要实践。理解并掌握JMX在这些服务器中的应用,对于提升Java应用的运维效率和质量至关重要。

    基于Spring+JMX+Tomcat实现资源动态管理

    JMX与Spring的结合使得开发者能够轻松地创建和管理自定义的管理接口,而Tomcat作为服务器,通过支持JMX提供了丰富的监控和管理点。这种方式有助于实时优化性能,处理异常情况,以及在不重启服务的情况下进行配置更新...

    zabbix监控tomcat模版及jar包

    本篇文章将详细介绍如何使用Zabbix监控Tomcat,特别是通过JMX(Java Management Extensions)来监控Tomcat,并涉及到相关的jar包和Zabbix模板。 首先,让我们了解一下`cmdline-jmx`和`catalina-jmx`这两个标签所指...

    catalina-jmx-remote.jar

    catalina jmx remote jmx 监控 tomcat 资源

    Zabbix监控Tomcat详细指标(附模板)的资料

    Zabbix监控Tomcat详细指标(附模板)的资料 cmdline-jmxclient-0.10.3 catalina-jmx-remote

    jmx 实现远程连接tomcat

    jmx 实现远程连接tomcat jmx(Java Management Extensions)是一种Java技术,用于监控和管理Java应用程序。...通过以上步骤,我们可以成功实现jmx远程连接tomcat,实现在远程监控tomcat的应用程序。

    Zabbix通过JMX监控java中间件.docx

    2. 开启 JMX,监控 Tomcat Zabbix 通过 JMX 监控 Java 中间件可以帮助管理员实时监控 Java 应用程序的性能指标,快速检测和解决问题,提高系统的可靠性和稳定性。 知识点: * Zabbix 通过 JMX 监控 Java 中间件的...

    Zabbix监控tomcat所需模板

    2. **Template JMX Tomcat.xml**:此文件可能包含了更具体的JMX监控配置,可能针对Tomcat的特定组件或者提供了额外的监控指标,如Web应用程序的运行情况、Garbage Collector的状态等。 3. **Tomcat.xml**:这个文件...

    Zabbix通过JMX方式监控java中间件

    - 配置Tomcat或其他Java应用的相关JMX监控。 通过以上步骤,可以成功实现Zabbix通过JMX方式对Java中间件的监控。这种方式不仅可以提高监控的灵活性和精确度,还能帮助运维人员更高效地管理复杂的Java应用环境。

    zabbix监控tomcat所需的cmdline-jmxclient-0.10.3.jar和zbx自定义模版

    本文将深入探讨如何使用Zabbix监控Tomcat应用服务器,特别关注"cmdline-jmxclient-0.10.3.jar"工具以及相关的自定义模板。 首先,`cmdline-jmxclient-0.10.3.jar`是一个Java管理扩展(JMX)客户端,它允许通过...

    JMX、MXBean学习

    例如,你可以通过JMX监控Tomcat服务器的线程池状态,或者调整数据库连接池的参数。 8. **自定义MBean**:根据需求,开发者可以创建自定义的MBean来管理特定的应用逻辑或资源。 9. **异常处理**:MBean的操作可能会...

    LambdaProbe监控Tomcat 和Jconsole 监控Tomcat

    标题中的“LambdaProbe监控Tomcat”和“Jconsole监控Tomcat”是指两种不同的方式来监控Java应用程序服务器,特别是Apache Tomcat。这两种工具都是为了帮助开发者和运维人员了解Tomcat的运行状态,诊断性能问题,以及...

    通过Tomcat开启JMX监控的方法图解

    环境准备我们这里就不直接演示了,直接配置tomcat的jmx  1、进入到tomcat的bin目录下  # cd /opt/tomcat/apache-tomcat-8.0.48/bin/  2、编辑配置文件  # vim catalina.sh  3、在下图中上面添加参数    参数...

Global site tag (gtag.js) - Google Analytics