Advertisement
Guest User

Untitled

a guest
May 13th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.lang.reflect.Array;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import javax.management.AttributeNotFoundException;
  7. import javax.management.InstanceNotFoundException;
  8. import javax.management.IntrospectionException;
  9. import javax.management.MBeanAttributeInfo;
  10. import javax.management.MBeanException;
  11. import javax.management.MBeanInfo;
  12. import javax.management.MBeanServerConnection;
  13. import javax.management.ObjectName;
  14. import javax.management.ReflectionException;
  15. import javax.management.openmbean.CompositeData;
  16. import javax.management.openmbean.CompositeType;
  17. import javax.management.openmbean.TabularData;
  18. import javax.management.remote.JMXConnector;
  19. import javax.management.remote.JMXConnectorFactory;
  20. import javax.management.remote.JMXServiceURL;
  21.  
  22.  
  23. public class JmxDump {
  24. static public MBeanServerConnection connect(String host, int port, String user, String password) throws IOException {
  25. String uri = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi";
  26. JMXServiceURL jmxserviceurl = new JMXServiceURL(uri);
  27. Map<String, Object> attributes = null;
  28. if(user != null && password != null ) {
  29. String[] credentials = new String[]{user, password};
  30. attributes = new HashMap<String, Object>();
  31. attributes.put("jmx.remote.credentials", credentials);
  32. }
  33. JMXConnector connector = JMXConnectorFactory.connect(jmxserviceurl, attributes);
  34. MBeanServerConnection connection = connector.getMBeanServerConnection();
  35. return connection;
  36. }
  37.  
  38. static public void dumpTd(String name, TabularData td, String prefix) {
  39. System.out.println(prefix + "javax.management.openmbean.TabularData[" + td.size() +"] " + name);
  40. }
  41.  
  42. static public void dumpCd(String name, CompositeData cd, String prefix) {
  43. System.out.println(prefix + "javax.management.openmbean.CompositeData " + name);
  44. if(cd != null) {
  45. CompositeType ct = cd.getCompositeType();
  46. for(Object key: ct.keySet()) {
  47. Object value = cd.get((String)key);
  48. dispatch(key.toString(), value.getClass().getName(), value, prefix + " ");
  49. }
  50. }
  51.  
  52. }
  53.  
  54. static public void dispatch(String name, String type, Object attr, String prefix) {
  55. if(type == null) {
  56. System.out.println(prefix + "null " + name);
  57. }
  58. else if(attr instanceof javax.management.openmbean.TabularData) {
  59. TabularData td = (TabularData) attr;
  60. dumpTd(name, td, prefix);
  61. }
  62. else if(attr instanceof javax.management.openmbean.CompositeData) {
  63. CompositeData cd = (CompositeData) attr;
  64. dumpCd(name, cd, prefix);
  65. }
  66. else if(attr.getClass().isArray()) {
  67. System.out.println(prefix + attr.getClass().getComponentType().getName() + "[" + Array.getLength(attr) + "]" + " " + name);
  68. }
  69. else {
  70. System.out.println(prefix + type + " " + name);
  71. }
  72.  
  73. }
  74.  
  75.  
  76. static public void dump(MBeanServerConnection mbean) throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException, IOException, IntrospectionException {
  77. for(Object nameObject: mbean.queryNames(null, null)) {
  78. ObjectName name = (ObjectName) nameObject;
  79. System.out.println(name);
  80. MBeanInfo info = mbean.getMBeanInfo(name);
  81. for(MBeanAttributeInfo attrInfo : info.getAttributes()) {
  82. Object attr;
  83. try {
  84. attr = mbean.getAttribute(name, attrInfo.getName());
  85. dispatch(attrInfo.getName(), attrInfo.getType(), attr, " ");
  86. } catch (Exception e) {
  87. String type = attrInfo.getType();
  88. if(type.startsWith("[L")) {
  89. type = type.substring(2, type.length() - 1 ) + "[]";
  90. }
  91. System.out.println(" " + type + " " + attrInfo.getName() +" (Unavailable)");
  92. }
  93. }
  94. }
  95.  
  96. }
  97. /**
  98. * @param args
  99. * @throws IOException
  100. * @throws ReflectionException
  101. * @throws MBeanException
  102. * @throws IntrospectionException
  103. * @throws InstanceNotFoundException
  104. * @throws AttributeNotFoundException
  105. */
  106. public static void main(String[] args) throws IOException, AttributeNotFoundException, InstanceNotFoundException, IntrospectionException, MBeanException, ReflectionException {
  107. String host = args[0];
  108. int port = 0;
  109. if(args.length == 2 || args.length == 4) {
  110. port = Integer.parseInt(args[1]);
  111. }
  112. int loginbase = 2;
  113. if(args.length == 3) {
  114. loginbase = 3;
  115. }
  116. String user = null;
  117. String password = null;
  118. if(args.length > 2) {
  119. user = args[loginbase];
  120. password = args[loginbase + 1];
  121. }
  122. MBeanServerConnection mbean = connect(host, port, user, password);
  123. dump(mbean);
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement