标签:
一:Window7下配置方式。
1、配置catalina.bat 在第一行加入下面配置 注意下面这些配置要在一行,注意包含空格。
set JAVA_OPTS=-Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=192.168.10.126 -Dcom.sun.management.jmxremote.port=9008 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.password.file=../conf/jmxremote.password -Dcom.sun.management.jmxremote.access.file=../conf/jmxremote.access
-Djava.rmi.server.hostname=xxx.xxx.xxx.xxx 主机地址,内网就写内网地址,外网就写外网地址。
-Dcom.sun.management.jmxremote.port=xxx 端口号、配置完端口号之后需要将此端口开放出来,注意防火墙拦截。
-Dcom.sun.management.jmxremote.authenticate=
true
是否开启认证、
false
的时候可以不使用密码访问。
如果不开启认证,下面配置可以省略。
以下配置是开启认证时使用的access文件和password文件。
-Dcom.sun.management.jmxremote.password.file=../conf/jmxremote.password
-Dcom.sun.management.jmxremote.access.file=../conf/jmxremote.access
2、拷贝授权文件,这二个文件在 \jdk1.7.0_07\jre\lib\management 文件夹下有模板文件,拷贝一份.放到tomcat/conf文件夹下面。
jmxremote.access
monitorRole readonly
controlRole readwrite create javax.management.monitor.*,javax.management.timer.* unregister
jmxremote.password
monitorRole admin123
controlRole admin321
3、给密码文件授权,不然tomcat startup.bat闪退.
需要修改jmxremote.password文件的访问权限,只有运行Tomcat的用户才能拥有访问权限 :Windows的NTFS文件系统下,选中文件,点右键 -->“属性”-->“安全”--> 点“高级”--> 点“更改权限”--> 去掉“从父项继承....”--> 弹出窗口中选“删除”,这样就删除了所有访问权限。再选“添加”--> “高级”--> “立即查找”,选中你的用户(或用户组,如果选用户不行那就选用户组),例administrator,点“确定",“确定"。来到权限项目窗口,勾选“完全控制”,点“确定”,OK了。
4、重新启动Tomcat,在Windows命令行输入“ntstat -a”查看配置的端口号是否已打开,如果打开,说明上面的配置成功了。
二:Linxu下配置
1、配置startup.sh
export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9008 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true -Djava.rmi.server.hostname=136.16.19.82 -Dcom.sun.management.jmxremote.password.file=../conf/jmxremote.password -Dcom.sun.management.jmxremote.access.file=../conf/jmxremote.access"
把上面的配置加在 exec "$PRGDIR"/"$EXECUTABLE" start "$@" 这句话的前面。
2、制作授权文件和windows上面说的一样。
3、授权 chmod
600
jmx* 给文件授权,不然tomcat无法启动。
4、重新启动tomcat 在输入命令 netstat -an | grep 9008 查看端口是否被占用,如果占用,说明上面的配置成功了。
三、Java代码调用
1 package com; 2 3 import java.io.IOException; 4 import java.lang.management.ClassLoadingMXBean; 5 import java.lang.management.ManagementFactory; 6 import java.lang.management.MemoryMXBean; 7 import java.lang.management.MemoryUsage; 8 import java.lang.management.OperatingSystemMXBean; 9 import java.lang.management.ThreadMXBean; 10 import java.net.MalformedURLException; 11 import java.util.HashMap; 12 import java.util.Map; 13 import java.util.Timer; 14 import java.util.TimerTask; 15 import java.util.concurrent.TimeUnit; 16 17 import javax.management.AttributeNotFoundException; 18 import javax.management.InstanceNotFoundException; 19 import javax.management.MBeanException; 20 import javax.management.MBeanServerConnection; 21 import javax.management.ReflectionException; 22 import javax.management.remote.JMXConnector; 23 import javax.management.remote.JMXConnectorFactory; 24 import javax.management.remote.JMXServiceURL; 25 26 public class T { 27 28 //private final static String SERVICE_1 = "service:jmx:rmi:///jndi/rmi://xxx.xxx.xxx.xxx:9008/jmxrmi"; 29 private final static String SERVICE_1 = "service:jmx:rmi:///jndi/rmi://xxx.xxx.xxx.xxx:9008/jmxrmi"; 30 31 /** 32 * @param args 33 */ 34 public static void main(String[] args) { 35 36 // 10秒调用一次 37 Timer timer = new Timer(); 38 timer.schedule(new T().new MonitorTask(SERVICE_1), 0, 10000); 39 40 } 41 42 private class MonitorTask extends TimerTask { 43 44 private String service; 45 46 public MonitorTask(String service) { 47 this.service = service; 48 } 49 50 @Override 51 public void run() { 52 53 JMXmonitor(service); 54 } 55 56 } 57 58 private static void JMXmonitor(String service) { 59 JMXConnector jmxConnector = null; 60 61 try { 62 JMXServiceURL ServiceURL = new JMXServiceURL(service); 63 Map<String, String[]> environment = new HashMap<String, String[]>(); 64 // 用户名密码,在jmxremote.password文件中的密码 65 String[] credentials = new String[] {"controlRole", "admin321"}; 66 environment.put("jmx.remote.credentials", credentials); 67 jmxConnector = JMXConnectorFactory.connect(ServiceURL, environment); 68 69 MBeanServerConnection mBeanServerConnection = jmxConnector 70 .getMBeanServerConnection(); 71 72 // 获取MemoryMXBean 73 System.out.println("\nMemory"); 74 MemoryMXBean memoryMXBean = ManagementFactory 75 .newPlatformMXBeanProxy(mBeanServerConnection, 76 ManagementFactory.MEMORY_MXBEAN_NAME, 77 MemoryMXBean.class); 78 79 MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage(); 80 System.out.println("heapMemoryUsage :"); 81 System.out.println("committed = " 82 + convertKB(heapMemoryUsage.getCommitted())); 83 System.out 84 .println("init = " + convertKB(heapMemoryUsage.getInit())); 85 System.out.println("max = " + convertKB(heapMemoryUsage.getMax())); 86 System.out 87 .println("used = " + convertKB(heapMemoryUsage.getUsed())); 88 89 MemoryUsage nonHeapMemoryUsage = memoryMXBean 90 .getNonHeapMemoryUsage(); 91 System.out.println("\nnonHeapMemoryUsage :"); 92 System.out.println("committed = " 93 + convertKB(nonHeapMemoryUsage.getCommitted())); 94 System.out.println("init = " 95 + convertKB(nonHeapMemoryUsage.getInit())); 96 System.out.println("max = " 97 + convertKB(nonHeapMemoryUsage.getMax())); 98 System.out.println("used = " 99 + convertKB(nonHeapMemoryUsage.getUsed())); 100 101 // 获取 ThreadMXBean 102 System.out.println("\nThread"); 103 ThreadMXBean threadMXBean = ManagementFactory 104 .newPlatformMXBeanProxy(mBeanServerConnection, 105 ManagementFactory.THREAD_MXBEAN_NAME, 106 ThreadMXBean.class); 107 System.out 108 .println("ThreadCount = " + threadMXBean.getThreadCount()); 109 System.out.println("DaemonThreadCount = " 110 + threadMXBean.getDaemonThreadCount()); 111 System.out.println("PeakThreadCount = " 112 + threadMXBean.getPeakThreadCount()); 113 System.out.println("CurrentThreadCpuTime = " 114 + threadMXBean.getCurrentThreadCpuTime()); 115 System.out.println("CurrentThreadUserTime = " 116 + threadMXBean.getCurrentThreadUserTime()); 117 118 System.out.println("\nClassLoading"); 119 ClassLoadingMXBean classLoadingMXBean = ManagementFactory 120 .newPlatformMXBeanProxy(mBeanServerConnection, 121 ManagementFactory.CLASS_LOADING_MXBEAN_NAME, 122 ClassLoadingMXBean.class); 123 // 当前加载到Java虚拟机中类的数量 124 System.out.println("LoadedClassCount = " 125 + classLoadingMXBean.getLoadedClassCount()); 126 // Java 虚拟机开始执行到目前已经加载的类的总数。 127 System.out.println("TotalLoadedClassCount = " 128 + classLoadingMXBean.getTotalLoadedClassCount()); 129 // Java 虚拟机开始执行到目前已经卸载的类的总数。 130 System.out.println("UnloadedClassCount = " 131 + classLoadingMXBean.getUnloadedClassCount()); 132 133 System.out.println("\nCpu"); 134 OperatingSystemMXBean operatingSystemMXBean = ManagementFactory 135 .newPlatformMXBeanProxy(mBeanServerConnection, 136 ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, 137 OperatingSystemMXBean.class); 138 System.out.println("AvailableProcessors = " 139 + operatingSystemMXBean.getAvailableProcessors()); 140 double ratio = 0.0; 141 long start = System.currentTimeMillis(); 142 long startC; 143 try { 144 startC = (Long) mBeanServerConnection.getAttribute(operatingSystemMXBean.getObjectName(),"ProcessCpuTime"); 145 try { 146 TimeUnit.SECONDS.sleep(5); 147 } catch (InterruptedException e) { 148 e.printStackTrace(); 149 } 150 151 long end = System.currentTimeMillis(); 152 long endC = (Long) mBeanServerConnection.getAttribute(operatingSystemMXBean.getObjectName(),"ProcessCpuTime"); 153 154 int availableProcessors = operatingSystemMXBean 155 .getAvailableProcessors(); 156 ratio = (endC - startC) / 1000000.0 / (end - start) 157 / availableProcessors; 158 159 } catch (AttributeNotFoundException e1) { 160 // TODO Auto-generated catch block 161 e1.printStackTrace(); 162 } catch (InstanceNotFoundException e1) { 163 // TODO Auto-generated catch block 164 e1.printStackTrace(); 165 } catch (MBeanException e1) { 166 // TODO Auto-generated catch block 167 e1.printStackTrace(); 168 } catch (ReflectionException e1) { 169 // TODO Auto-generated catch block 170 e1.printStackTrace(); 171 } 172 173 System.out.println("CUP使用率" + round(ratio * 100) + "%"); 174 175 } catch (MalformedURLException e) { 176 e.printStackTrace(); 177 System.out.println("非法的ServiceURL"); 178 } catch (IOException e) { 179 e.printStackTrace(); 180 } finally { 181 try { 182 if (jmxConnector != null) { 183 jmxConnector.close(); 184 } 185 } catch (IOException e) { 186 e.printStackTrace(); 187 } 188 } 189 190 } 191 192 private static String convertKB(long src) { 193 194 if (src <= 0L) { 195 return "0KB"; 196 } 197 double conversrc = src / 1024 / 1024; 198 199 return round(conversrc) + "MB"; 200 } 201 202 private static float round(double src) { 203 return (float) (Math.round(src * 100)) / 100; 204 } 205 }
Tomcat配置JMX远程监控(Windown7 Linxu)
标签:
原文地址:http://www.cnblogs.com/daxin/p/5014203.html