当前位置: > Linux服务器 > Tomcat >

JMX监控Tomcat

时间:2017-08-05 22:20来源:linux.it.net.cn 作者:IT

环境: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、用jconsole连接远程linux服务时, IP地址和port都输入正确的情况下,仍然是连接失败 
vi /etc/hosts,将hostname对应的IP改为真实IP

 

4、测试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中可配置)。点“连接”。看到图就行了。

 

5、关于数据。Mbean属性页中给出了相应的数据,Catalina中是tomcat的,Java.lang是jvm的。对于加粗的黑体属性值,需双击一下才可看内容

5、关于编程。

 

 
  1. public class JMXTest {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         try {  
  8.   
  9.            String jmxURL = "service:jmx:rmi:///jndi/rmi://192.168.10.93:8999/jmxrmi";//tomcat jmx url  
  10.            JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);  
  11.              
  12.            Map map = new HashMap();  
  13.            String[] credentials = new String[] { "monitorRole" , "QED" };  
  14.            map.put("jmx.remote.credentials", credentials);  
  15.            JMXConnector connector = JMXConnectorFactory.connect(serviceURL, map);  
  16.            MBeanServerConnection  mbsc = connector.getMBeanServerConnection();  
  17.              
  18.            //端口最好是动态取得  
  19.            ObjectName threadObjName = new ObjectName("Catalina:type=ThreadPool,name=http-8080");  
  20.            MBeanInfo mbInfo = mbsc.getMBeanInfo(threadObjName);  
  21.              
  22.            String attrName = "currentThreadCount";//tomcat的线程数对应的属性值  
  23.            MBeanAttributeInfo[] mbAttributes = mbInfo.getAttributes();  
  24.            System.out.println("currentThreadCount:"+mbsc.getAttribute(threadObjName, attrName));  
  25.              
  26.            //heap  
  27.            for(int j=0;j <mbsc.getDomains().length;j++){   
  28.                System.out.println("###########"+mbsc.getDomains()[j]);   
  29.            }   
  30.            Set MBeanset = mbsc.queryMBeans(null, null);  
  31.            System.out.println("MBeanset.size() : " + MBeanset.size());  
  32.            Iterator MBeansetIterator = MBeanset.iterator();  
  33.            while (MBeansetIterator.hasNext()) {   
  34.                ObjectInstance objectInstance = (ObjectInstance)MBeansetIterator.next();  
  35.                ObjectName objectName = objectInstance.getObjectName();  
  36.                String canonicalName = objectName.getCanonicalName();  
  37.                System.out.println("canonicalName : " + canonicalName);   
  38.                if (canonicalName.equals("Catalina:host=localhost,type=Cluster"))      {    
  39.                    // Get details of cluster MBeans  
  40.                    System.out.println("Cluster MBeans Details:");  
  41.                    System.out.println("=========================================");   
  42.                    //getMBeansDetails(canonicalName);  
  43.                   String canonicalKeyPropList = objectName.getCanonicalKeyPropertyListString();  
  44.               }  
  45.            }  
  46.            //------------------------- system ----------------------  
  47.            ObjectName runtimeObjName = new ObjectName("java.lang:type=Runtime");  
  48.            System.out.println("厂商:"+ (String)mbsc.getAttribute(runtimeObjName, "VmVendor"));  
  49.            System.out.println("程序:"+ (String)mbsc.getAttribute(runtimeObjName, "VmName"));  
  50.            System.out.println("版本:"+ (String)mbsc.getAttribute(runtimeObjName, "VmVersion"));  
  51.            Date starttime=new Date((Long)mbsc.getAttribute(runtimeObjName, "StartTime"));  
  52.            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  53.            System.out.println("启动时间:"+df.format(starttime));  
  54.              
  55.            Long timespan=(Long)mbsc.getAttribute(runtimeObjName, "Uptime");  
  56.            System.out.println("连续工作时间:"+JMXTest.formatTimeSpan(timespan));  
  57.            //------------------------ JVM -------------------------  
  58.            //堆使用率  
  59.            ObjectName heapObjName = new ObjectName("java.lang:type=Memory");  
  60.            MemoryUsage heapMemoryUsage =  MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(heapObjName, "HeapMemoryUsage"));  
  61.            long maxMemory = heapMemoryUsage.getMax();//堆最大  
  62.            long commitMemory = heapMemoryUsage.getCommitted();//堆当前分配  
  63.            long usedMemory = heapMemoryUsage.getUsed();  
  64.            System.out.println("heap:"+(double)usedMemory*100/commitMemory+"%");//堆使用率  
  65.              
  66.            MemoryUsage nonheapMemoryUsage =  MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(heapObjName, "NonHeapMemoryUsage"));             
  67.            long noncommitMemory = nonheapMemoryUsage.getCommitted();  
  68.            long nonusedMemory = heapMemoryUsage.getUsed();  
  69.            System.out.println("nonheap:"+(double)nonusedMemory*100/noncommitMemory+"%");  
  70.              
  71.            ObjectName permObjName = new ObjectName("java.lang:type=MemoryPool,name=Perm Gen");  
  72.            MemoryUsage permGenUsage =  MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(permObjName, "Usage"));             
  73.            long committed = permGenUsage.getCommitted();//持久堆大小  
  74.            long used = heapMemoryUsage.getUsed();//  
  75.            System.out.println("perm gen:"+(double)used*100/committed+"%");//持久堆使用率  
  76.              
  77.            //-------------------- Session ---------------   
  78.            ObjectName managerObjName = new ObjectName("Catalina:type=Manager,*");  
  79.            Set<ObjectName> s=mbsc.queryNames(managerObjName, null);  
  80.            for (ObjectName obj:s){  
  81.                System.out.println("应用名:"+obj.getKeyProperty("path"));  
  82.                ObjectName objname=new ObjectName(obj.getCanonicalName());  
  83.                System.out.println("最大会话数:"+ mbsc.getAttribute( objname, "maxActiveSessions"));  
  84.                System.out.println("会话数:"+ mbsc.getAttribute( objname, "activeSessions"));  
  85.                System.out.println("活动会话数:"+ mbsc.getAttribute( objname, "sessionCounter"));  
  86.            }  
  87.              
  88.            //----------------- Thread Pool ----------------  
  89.            ObjectName threadpoolObjName = new ObjectName("Catalina:type=ThreadPool,*");  
  90.            Set<ObjectName> s2=mbsc.queryNames(threadpoolObjName, null);  
  91.            for (ObjectName obj:s2){  
  92.                System.out.println("端口名:"+obj.getKeyProperty("name"));  
  93.                ObjectName objname=new ObjectName(obj.getCanonicalName());  
  94.                System.out.println("最大线程数:"+ mbsc.getAttribute( objname, "maxThreads"));  
  95.                System.out.println("当前线程数:"+ mbsc.getAttribute( objname, "currentThreadCount"));  
  96.                System.out.println("繁忙线程数:"+ mbsc.getAttribute( objname, "currentThreadsBusy"));  
  97.            }  
  98.                  
  99.         } catch (Exception e) {  
  100.             e.printStackTrace();  
  101.         }  
  102.     }  
  103.   
  104.     public static String formatTimeSpan(long span){  
  105.         long minseconds = span % 1000;  
  106.           
  107.         span = span /1000;  
  108.         long seconds = span % 60;  
  109.           
  110.         span = span / 60;  
  111.         long mins = span % 60;  
  112.           
  113.         span = span / 60;  
  114.         long hours = span % 24;  
  115.   
  116.         span = span / 24;  
  117.         long days = span;  
  118.         return (new Formatter()).format("%1$d天 %2$02d:%3$02d:%4$02d.%5$03d", days,hours,mins,seconds,minseconds).toString();  
  119.     }  
  120. }  

 

 




(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容