JMX客户端及对commons-pool的监控

举报
丁威 发表于 2021/05/21 22:48:49 2021/05/21
【摘要】     本文转自JMX客户端及对commons-pool的监控     本文对原文进行梳理,梳理出一条使用JMX客户端对commons-pool监控的简单使用指南。      生产线上,我们的服务基本都是在linux环境下部署的,当高并发的时候,机器的负载是比较高的,所以我们只能在linux机器上执行一些简单的命令行工具,如jps,jstack,jinfo等,像...

    本文转自JMX客户端及对commons-pool的监控

    本文对原文进行梳理,梳理出一条使用JMX客户端对commons-pool监控的简单使用指南。

    

生产线上,我们的服务基本都是在linux环境下部署的,当高并发的时候,机器的负载是比较高的,所以我们只能在linux机器上执行一些简单的命令行工具,如jps,jstack,jinfo等,像重量级的jconsole,jvisualvm这些首先需要桌面环境才能观看,服务器肯定是没有开启X client的,所以只能通过jmx在远程客户端观看,但这种情况下,势必会对本机造成一些不必要的资源占用,如网络带宽等。但是想mbean这些对象的监控目前又只能通过jconsole,jvisualvm这些jmx客户端访问,想要在本机以命令行的方式查看还是比较困难的。本文的目的就是介绍下怎么在linux服务器下以命令下的方式进行jmx监控。

1、代码

   
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Properties;
  9. import java.util.Set;
  10. import javax.management.MBeanAttributeInfo;
  11. import javax.management.MBeanInfo;
  12. import javax.management.MBeanServerConnection;
  13. import javax.management.ObjectName;
  14. import javax.management.remote.JMXConnector;
  15. import javax.management.remote.JMXConnectorFactory;
  16. import javax.management.remote.JMXServiceURL;
  17. import com.sun.tools.attach.VirtualMachine;
  18. import com.sun.tools.attach.VirtualMachineDescriptor;
  19. public class JmxC {
  20. public static void main(String[] args) {
  21. if (args.length > 0) {
  22. if (args[0].equals("-l")) {
  23. JmxC c = new JmxC();
  24. c.m1();
  25. } else if (args[0].equals("-p")) {
  26. int id = Integer.valueOf(args[1]);
  27. System.out.println("pid:" + id);
  28. JmxC c = new JmxC();
  29. if (args.length > 2 && args[2].equals("-n")) {
  30. String name = args[3];
  31. System.out.println("args name:" + name);
  32. if (args.length > 4 && args[4].equals("-a")) {
  33. String atts = args[5];
  34. System.out.println("args atts:" + name);
  35. c.processMbean(c.getConnectStrById(id), name, atts);
  36. } else {
  37. c.processMbean(c.getConnectStrById(id), name, null);
  38. }
  39. } else {
  40. c.processMbean(c.getConnectStrById(id));
  41. }
  42. }
  43. }
  44. }
  45. public void processMbean(String connectorAddress, String name, String att) {
  46. if (null == connectorAddress)
  47. return;
  48. JMXConnector connector = null;
  49. try {
  50. System.out.println("conn:" + connectorAddress);
  51. JMXServiceURL url = new JMXServiceURL(connectorAddress);
  52. connector = JMXConnectorFactory.connect(url);
  53. MBeanServerConnection mbeanConn = connector
  54. .getMBeanServerConnection();
  55. Set<ObjectName> beanSet = mbeanConn.queryNames(null, null);
  56. System.out.println("beanSet num:" + beanSet.size());
  57. ObjectName n = new ObjectName(name);
  58. MBeanInfo info = mbeanConn.getMBeanInfo(n);
  59. if (null != info) {
  60. if (null != att && !att.isEmpty()) {
  61. String[] as = att.split(",");
  62. List<String> al = new LinkedList<String>();
  63. System.out.print(" time " + "\t");
  64. for (String a : as) {
  65. if (null != a && !a.isEmpty()) {
  66. al.add(a);
  67. System.out.print(a + "\t");
  68. }
  69. }
  70. System.out.println();
  71. SimpleDateFormat dateformat1 = new SimpleDateFormat(
  72. "HH:mm:ss");
  73. while (true) {
  74. System.out.print(dateformat1.format(new Date()) + "\t");
  75. for (String a : al) {
  76. Object value = null;
  77. if (!a.contains("-")) {
  78. value = mbeanConn.getAttribute(n, a);
  79. } else {
  80. String[] at = a.split("-");
  81. value = (long) mbeanConn.getAttribute(n, at[0])
  82. - (long) mbeanConn.getAttribute(n,
  83. at[1]);
  84. }
  85. System.out.print(value + "\t");
  86. }
  87. System.out.println();
  88. Thread.sleep(1000);
  89. }
  90. } else {
  91. MBeanAttributeInfo[] atts = info.getAttributes();
  92. for (MBeanAttributeInfo attr : atts) {
  93. Object value = mbeanConn
  94. .getAttribute(n, attr.getName());
  95. System.out.println(attr.getName() + "->" + value);
  96. }
  97. }
  98. } else {
  99. System.err.println("info is null");
  100. }
  101. } catch (Exception e) {
  102. // TODO Auto-generated catch block
  103. e.printStackTrace();
  104. } finally {
  105. try {
  106. if (connector != null)
  107. connector.close();
  108. // break;
  109. } catch (IOException e) {
  110. // TODO Auto-generated catch block
  111. e.printStackTrace();
  112. }
  113. }
  114. // ...
  115. }
  116. public void processMbean(String connectorAddress) {
  117. if (null == connectorAddress)
  118. return;
  119. JMXConnector connector = null;
  120. try {
  121. System.out.println("conn:" + connectorAddress);
  122. JMXServiceURL url = new JMXServiceURL(connectorAddress);
  123. connector = JMXConnectorFactory.connect(url);
  124. MBeanServerConnection mbeanConn = connector
  125. .getMBeanServerConnection();
  126. Set<ObjectName> beanSet = mbeanConn.queryNames(null, null);
  127. System.out.println("beanSet num:" + beanSet.size());
  128. for (ObjectName name : beanSet) {
  129. if (name.getDomain().equals("org.apache.commons.pool2")) {
  130. System.out.println("name:" + name);
  131. // ObjectInstance instance =
  132. // mbeanConn.getObjectInstance(name);
  133. MBeanInfo info = mbeanConn.getMBeanInfo(name);
  134. MBeanAttributeInfo[] atts = info.getAttributes();
  135. for (MBeanAttributeInfo attr : atts) {
  136. Object value = mbeanConn.getAttribute(name,
  137. attr.getName());
  138. System.out.println(attr.getName() + "->" + value);
  139. }
  140. System.out.println();
  141. System.out.println();
  142. System.out.println();
  143. }
  144. }
  145. } catch (Exception e) {
  146. // TODO Auto-generated catch block
  147. e.printStackTrace();
  148. } finally {
  149. try {
  150. if (connector != null)
  151. connector.close();
  152. // break;
  153. } catch (IOException e) {
  154. // TODO Auto-generated catch block
  155. e.printStackTrace();
  156. }
  157. }
  158. // ...
  159. }
  160. public String getConnectStrById(int pid) {
  161. List<VirtualMachineDescriptor> vms = VirtualMachine.list();
  162. int i = 0;
  163. for (VirtualMachineDescriptor desc : vms) {
  164. if (!desc.id().equals("" + pid)) {
  165. continue;
  166. }
  167. VirtualMachine vm;
  168. try {
  169. System.out.println("desc:" + desc);
  170. System.out.println("process id:" + desc.id());
  171. vm = VirtualMachine.attach(desc);
  172. } catch (Exception e) {
  173. e.printStackTrace();
  174. continue;
  175. }
  176. try {
  177. Properties props = vm.getAgentProperties();
  178. System.out.println("args:" + props.get("sun.jvm.args"));
  179. for (Map.Entry<Object, Object> entry : props.entrySet()) {
  180. // System.out.println(entry.getKey() + "->" +
  181. // entry.getValue());
  182. }
  183. String connectorAddress = props
  184. .getProperty("com.sun.management.jmxremote.localConnectorAddress");
  185. if (connectorAddress == null) {
  186. System.out
  187. .println("connectorAddress is null,and continue search");
  188. props = vm.getSystemProperties();
  189. String home = props.getProperty("java.home");
  190. // Normally in ${java.home}/jre/lib/management-agent.jar but
  191. // might
  192. // be in ${java.home}/lib in build environments.
  193. String agent = home + File.separator + "jre"
  194. + File.separator + "lib" + File.separator
  195. + "management-agent.jar";
  196. File f = new File(agent);
  197. if (!f.exists()) {
  198. agent = home + File.separator + "lib" + File.separator
  199. + "management-agent.jar";
  200. f = new File(agent);
  201. if (!f.exists()) {
  202. throw new IOException("Management agent not found");
  203. }
  204. }
  205. agent = f.getCanonicalPath();
  206. vm.loadAgent(agent, "com.sun.management.jmxremote");
  207. props = vm.getAgentProperties();
  208. connectorAddress = props
  209. .getProperty("com.sun.management.jmxremote.localConnectorAddress");
  210. if (connectorAddress == null) {
  211. System.out.println("connectorAddress is null");
  212. }
  213. }
  214. return connectorAddress;
  215. } catch (Exception e) {
  216. e.printStackTrace();
  217. }
  218. }
  219. System.out.println("return null");
  220. return null;
  221. }
  222. public void m1() {
  223. List<VirtualMachineDescriptor> vms = VirtualMachine.list();
  224. int i = 0;
  225. for (VirtualMachineDescriptor desc : vms) {
  226. System.out.println();
  227. System.out.println();
  228. System.out.println("------" + i++ + "----------------");
  229. VirtualMachine vm;
  230. try {
  231. System.out.println("desc:" + desc);
  232. System.out.println("process id:" + desc.id());
  233. vm = VirtualMachine.attach(desc);
  234. } catch (Exception e) {
  235. e.printStackTrace();
  236. continue;
  237. }
  238. JMXConnector connector = null;
  239. try {
  240. Properties props = vm.getAgentProperties();
  241. System.out.println(props.get("sun.jvm.args"));
  242. for (Map.Entry<Object, Object> entry : props.entrySet()) {
  243. // System.out.println(entry.getKey() + "->" +
  244. // entry.getValue());
  245. }
  246. String connectorAddress = props
  247. .getProperty("com.sun.management.jmxremote.localConnectorAddress");
  248. if (connectorAddress == null) {
  249. System.out
  250. .println("connectorAddress is null,and continue search");
  251. props = vm.getSystemProperties();
  252. String home = props.getProperty("java.home");
  253. // Normally in ${java.home}/jre/lib/management-agent.jar but
  254. // might
  255. // be in ${java.home}/lib in build environments.
  256. String agent = home + File.separator + "jre"
  257. + File.separator + "lib" + File.separator
  258. + "management-agent.jar";
  259. File f = new File(agent);
  260. if (!f.exists()) {
  261. agent = home + File.separator + "lib" + File.separator
  262. + "management-agent.jar";
  263. f = new File(agent);
  264. if (!f.exists()) {
  265. throw new IOException("Management agent not found");
  266. }
  267. }
  268. agent = f.getCanonicalPath();
  269. vm.loadAgent(agent, "com.sun.management.jmxremote");
  270. props = vm.getAgentProperties();
  271. connectorAddress = props
  272. .getProperty("com.sun.management.jmxremote.localConnectorAddress");
  273. if (connectorAddress == null) {
  274. System.out.println("connectorAddress is null");
  275. continue;
  276. }
  277. }
  278. System.out.println("conn:" + connectorAddress);
  279. JMXServiceURL url = new JMXServiceURL(connectorAddress);
  280. connector = JMXConnectorFactory.connect(url);
  281. MBeanServerConnection mbeanConn = connector
  282. .getMBeanServerConnection();
  283. Set<ObjectName> beanSet = mbeanConn.queryNames(null, null);
  284. System.out.println("beanSet num:" + beanSet.size());
  285. // ...
  286. } catch (Exception e) {
  287. e.printStackTrace();
  288. } finally {
  289. try {
  290. if (connector != null)
  291. connector.close();
  292. // break;
  293. } catch (IOException e) {
  294. // TODO Auto-generated catch block
  295. e.printStackTrace();
  296. }
  297. }
  298. }
  299. }
  300. }
将上述代码编译,如果使用javac命令编译出错的话,建议先拷贝到eclipse,然后直接拿到编译后的.class文件。

2、使用上述工具来监控commons-pool
1)首先我们将编译后的class文件存放在


然后我们先cmd进入到 jedis目录:

2.1 先执行java  cn/uce/uop/jmx/JmxC -l


解决办法:

将%JAVA_HOME%/jre/bin/attach.dll 拷贝到 %JAVA_HOME%/bin/目录下

再执行上述命令:


会列出当前的java进程,我们选择我们需要监控的进程,执行如下命令

2.2 javacn/uce/uop/jmx/JmxC -p 13260


  
  1. pid:13260
  2. desc:sun.tools.attach.WindowsAttachProvider@4617c264: 13260 cn.uce.uop.Test
  3. process id:13260
  4. args:-Dfile.encoding=UTF-8
  5. conn:service:jmx:rmi://127.0.0.1/stub/rO0ABXNyAC5qYXZheC5tYW5hZ2VtZW50LnJlbW90ZS5ybWkuUk1JU2VydmVySW1wbF9TdHViAAAAAAAAAAICAAB4cgAaamF2YS5ybWkuc2VydmVyLlJlbW90ZVN0dWLp/tzJi+FlGgIAAHhyABxqYXZhLnJtaS5zZXJ2ZXIuUmVtb3RlT2JqZWN002G0kQxhMx4DAAB4cHc2AAtVbmljYXN0UmVmMgAACzEwLjIwMS4yLjYzAADh5wsYyZlaKbJNg8lsRAAAAWC1Eqi1gAEAeA==
  6. beanSet num:20
  7. name:org.apache.commons.pool2:type=GenericObjectPool,name=pool
  8. NumActive->30
  9. NumIdle->0
  10. NumWaiters->1
  11. TimeBetweenEvictionRunsMillis->30000
  12. Fairness->false
  13. Lifo->true
  14. Closed->false
  15. MaxTotal->30
  16. MaxIdle->5
  17. MinIdle->0
  18. TestOnCreate->false
  19. TestOnReturn->false
  20. TestWhileIdle->true
  21. NumTestsPerEvictionRun->-1
  22. MinEvictableIdleTimeMillis->60000
  23. TestOnBorrow->false
  24. BlockWhenExhausted->true
  25. MaxWaitMillis->-1
  26. DestroyedByEvictorCount->0
  27. MeanIdleTimeMillis->0
  28. CreatedCount->30
  29. DestroyedCount->0
  30. CreationStackTrace->java.lang.Exception
  31. at org.apache.commons.pool2.impl.BaseGenericObjectPool.<init>(BaseGenericObjectPool.java:139)
  32. at org.apache.commons.pool2.impl.GenericObjectPool.<init>(GenericObjectPool.java:107)
  33. at redis.clients.util.Pool.initPool(Pool.java:44)
  34. at redis.clients.util.Pool.<init>(Pool.java:23)
  35. at redis.clients.jedis.JedisPool.<init>(JedisPool.java:185)
  36. at redis.clients.jedis.JedisPool.<init>(JedisPool.java:162)
  37. at redis.clients.jedis.JedisPool.<init>(JedisPool.java:109)
  38. at cn.uce.uop.Test.main(Test.java:19)
  39. BorrowedCount->38
  40. ReturnedCount->8
  41. MeanActiveTimeMillis->0
  42. DestroyedByBorrowValidationCount->0
  43. RemoveAbandonedOnMaintenance->false
  44. MaxBorrowWaitTimeMillis->41
  45. LogAbandoned->false
  46. AbandonedConfig->false
  47. RemoveAbandonedOnBorrow->false
  48. RemoveAbandonedTimeout->2147483647
  49. MeanBorrowWaitTimeMillis->1
上述会列出通过jmx能监控的MBean属性


下一步我们就可以对重点参数进行动态监控

2.3 java  cn/uce/uop/jmx/JmxC -p 13260 -n org.apache.commons.pool2:type=GenericObjectPool,name=pool  -a NumActive,NumIdle,NumWaiters,BorrowedCount,ReturnedCount,BorrowedCount-ReturnedCount,CreatedCount,DestroyedCount,DestroyedByEvictorCount

注意,-n 后面的值都来源于2,2那个命令的输出




文章来源: blog.csdn.net,作者:中间件兴趣圈,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/prestigeding/article/details/78950758

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。