From 57beece37a0e5aca2f8c53c296c9f711e3d288be Mon Sep 17 00:00:00 2001 From: geek Date: Thu, 13 Apr 2017 13:57:48 +0900 Subject: [PATCH] shared project --- crawler_jmx_java.iml | 22 ++ pom.xml | 29 ++ .../overflow/crawler/jmx/JmxCrawler.java | 302 ++++++++++++++++++ src/main/resources/_ | 0 src/test/java/com/loafle/AppTest.java | 28 ++ src/test/resources/logback.xml | 17 + 6 files changed, 398 insertions(+) create mode 100644 crawler_jmx_java.iml create mode 100644 pom.xml create mode 100644 src/main/java/com/loafle/overflow/crawler/jmx/JmxCrawler.java create mode 100644 src/main/resources/_ create mode 100644 src/test/java/com/loafle/AppTest.java create mode 100644 src/test/resources/logback.xml diff --git a/crawler_jmx_java.iml b/crawler_jmx_java.iml new file mode 100644 index 0000000..ab795b1 --- /dev/null +++ b/crawler_jmx_java.iml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f92cc7e --- /dev/null +++ b/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + + com.loafle + maven_parent_jar + 1.0.0-RELEASE + + + com.loafle.overflow + crawler_jmx + jar + 1.0.0-SNAPSHOT + com.loafle.overflow.crawler_jmx + + + + com.loafle.overflow + crawler + 1.0.0-SNAPSHOT + + + + + + \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/crawler/jmx/JmxCrawler.java b/src/main/java/com/loafle/overflow/crawler/jmx/JmxCrawler.java new file mode 100644 index 0000000..71a7b78 --- /dev/null +++ b/src/main/java/com/loafle/overflow/crawler/jmx/JmxCrawler.java @@ -0,0 +1,302 @@ +package com.loafle.overflow.crawler.jmx; + +import com.loafle.overflow.crawler.Crawler; + +import javax.management.*; +import javax.management.openmbean.CompositeData; +import javax.management.openmbean.CompositeType; +import javax.management.openmbean.TabularData; +import javax.management.openmbean.TabularType; +import javax.management.remote.JMXConnector; +import javax.management.remote.JMXConnectorFactory; +import javax.management.remote.JMXServiceURL; +import javax.management.remote.rmi.RMIConnectorServer; + +import javax.naming.Context; +import javax.rmi.ssl.SslRMIClientSocketFactory; +import java.io.IOException; +import java.lang.management.ManagementFactory; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Created by geek on 2017-04-06. + */ + +public class JmxCrawler extends Crawler{ + private static final Logger logger = Logger.getLogger(JmxCrawler.class.getName()); + + private String jmxUrl; + private String username; + private String password; + private boolean ssl; + private MBeanReceiver receiver; + + private List result = new ArrayList<>(); + + @Override + public Object getInternal(Map map) throws Exception { + + MBeanReceiver m = new MBeanReceiver() { + @Override + public void recordBean(String domain, LinkedList attrKeys, String attrName, String attrType, String attrDescription, Object value) { + Map l = new HashMap(); + l.put(attrName, attrType+" "+value); + result.add(l); + } + }; + + String jmxUrl = (String) map.get("jmxUrl"); + String username = (String)map.get("username"); + String password = (String)map.get("password"); + Boolean isSSL = (Boolean)map.get("ssl"); + + JmxCrawler jmxCrawler = new JmxCrawler( + jmxUrl, + username, + password, + isSSL, + m + ); + + jmxCrawler.doCrawler(); + return result; + } + + public static interface MBeanReceiver { + void recordBean(String domain, LinkedList attrKeys, String attrName, String attrType, String attrDescription, Object value); + } + + public JmxCrawler() { + + } + public JmxCrawler(String jmxUrl, String username, String password, boolean ssl, MBeanReceiver receiver) { + this.jmxUrl = jmxUrl; + this.username = username; + this.password = password; + this.ssl = ssl; + this.receiver = receiver; + } + + // 커넥터 생성 + public void doCrawler() throws Exception { + MBeanServerConnection beanCon; + JMXConnector jmxconn = null; + + if (this.jmxUrl.isEmpty()) { + beanCon = ManagementFactory.getPlatformMBeanServer(); + } else { + Map environment = getAuthority(); + jmxconn = JMXConnectorFactory.connect(new JMXServiceURL(this.jmxUrl), environment); + beanCon = jmxconn.getMBeanServerConnection(); + } + + try { + Set mBeanNames = new HashSet(); + List objectNames = new LinkedList(); + objectNames.add(null); + +// for (ObjectName name : objectNames) { +// Set objectInstances = beanCon.queryMBeans(name, null); +// +// for (ObjectInstance oi : objectInstances ) { +// System.out.println("oi = " + oi.getClassName()); +// } +// mBeanNames.addAll(beanCon.queryMBeans(name, null)); +//// beanCon.queryMBeans(new ObjectName("Catalina:type=Engine"), null); +// } + +// for (ObjectInstance name : mBeanNames) { +// crawlerBean(beanCon, name.getObjectName()); +// } + + ObjectName on = new ObjectName("Catalina:type=*,*"); +// ObjectName on = new ObjectName("org.apache.cassandra.db:type=*,*"); + + mBeanNames.addAll(beanCon.queryMBeans(on,null)); + + for (ObjectInstance name : mBeanNames) { + crawlerBean(beanCon, name.getObjectName()); + } + }finally { + if (jmxconn != null) { + jmxconn.close(); + } + } + } + + // 수집 + private void crawlerBean(MBeanServerConnection beanCon, ObjectName mbeanName) { + MBeanInfo info; + + try { + info = beanCon.getMBeanInfo(mbeanName); + } catch (IOException e) { + logCrawler(mbeanName.toString(), "getMbeanInfo Fail: " + e); + return; + } catch (JMException e) { + logCrawler(mbeanName.toString(), "getMbeanInfo Fail: " + e); + return; + } + + MBeanAttributeInfo[] attrInfos = info.getAttributes(); + + for (int idx = 0; idx < attrInfos.length; ++idx) { + MBeanAttributeInfo attr = attrInfos[idx]; + + if (!attr.isReadable()) { + logCrawler(mbeanName, attr, "not readable"); + continue; + } + + Object value; + + try { + value = beanCon.getAttribute(mbeanName, attr.getName()); + } catch (Exception e) { + logCrawler(mbeanName, attr, "Fail: " + e); + continue; + } + + logCrawler(mbeanName, attr, "process"); + processBeanValue( + mbeanName.getDomain(), + new LinkedList(), + attr.getName(), + attr.getType(), + attr.getDescription(), + value + ); + } + } + + private void processBeanValue(String domain, LinkedList attrKeys, String attrName, String attrType, String attrDescription, Object value) { + if (value == null) { + logCrawler("ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"+domain + attrName, "null"); + } else if (value instanceof Number || value instanceof String || value instanceof Boolean) { + logCrawler(domain + attrName, value.toString()); + this.receiver.recordBean(domain, attrKeys, attrName, attrType, attrDescription,value); + // JSON 결과 생성 + } else if (value instanceof CompositeData) { + logCrawler("ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"+domain + attrName, "compositedata"); + CompositeData composite = (CompositeData)value; + CompositeType type = composite.getCompositeType(); + attrKeys = new LinkedList(); + attrKeys.add(attrName); + + for (String key : type.keySet()) { + String typ = type.getType(key).getTypeName(); + Object val = composite.get(key); + + processBeanValue(domain, attrKeys, key, typ, type.getDescription(), val); + } + } else if (value instanceof TabularData) { + logCrawler("ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"+domain + attrName, "tabulardata"); + TabularData tds = (TabularData)value; + TabularType tt = tds.getTabularType(); + + CompositeType type = tt.getRowType(); + Set valueKeys = new TreeSet(type.keySet()); + + LinkedList extendedAttrKeys = new LinkedList(attrKeys); + extendedAttrKeys.add(attrName); + + for (Object valu : tds.values()) { + if (valu instanceof CompositeData) { + CompositeData composite = (CompositeData) valu; + + for(String valueIdx : valueKeys) { + LinkedList attrNames = extendedAttrKeys; + String typ = type.getType(valueIdx).getTypeName(); + String name = valueIdx; + if (valueIdx.toLowerCase().equals("value")) { + // Skip appending 'value' to the name + attrNames = attrKeys; + name = attrName; + } + processBeanValue( + domain, + attrNames, + name, + typ, + type.getDescription(), + composite.get(valueIdx)); + } + } else { + logCrawler(domain, "not a correct tabulardata format"); + } + } + } else if (value.getClass().isArray()) { + logCrawler(domain, "arrays are unsupported"); + } else { + logCrawler(domain, attrType + "is not exported"); + } + } + + private Map getAuthority() { + + Map environment = new HashMap(); + + if (this.username != null && this.username.length() != 0 && this.password != null && this.password.length() != 0) { + String[] credent = new String[] {this.username, this.password}; + environment.put(JMXConnector.CREDENTIALS, credent); + } + + if (this.ssl) { + environment.put(Context.SECURITY_PROTOCOL, "ssl"); + SslRMIClientSocketFactory clientSocketFactory = new SslRMIClientSocketFactory(); + environment.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, clientSocketFactory); + environment.put("com.sun.jndi.rmi.factory.socket", clientSocketFactory); + } + + return environment; + } + + private static void logCrawler(String name, String msg) { + logger.log(Level.FINE, "crawler : '" + name + "': " + msg); + } + + private static void logCrawler(ObjectName mbeanName, MBeanAttributeInfo attr, String msg) { + logCrawler(mbeanName + "'_'" + attr.getName(), msg); + } + + private static class StdoutWriter implements MBeanReceiver { + public void recordBean( + String domain, + LinkedList attrKeys, + String attrName, + String attrType, + String attrDescription, + Object value) { + + System.out.println(domain + + " " + attrKeys + " " + + " " + attrName + " " + + " " + attrDescription + " " + + " " + value); + } + } + +// public static void main(String[] args) throws Exception { + +// JmxCrawler jmxCrawler = new JmxCrawler( +//// "service:jmx:rmi:///jndi/rmi://192.168.1.207:7199/jmxrmi", +// "service:jmx:rmi:///jndi/rmi://192.168.1.103:9840/jmxrmi", +//// "", +// "", +// "", +// false, +// new StdoutWriter() +// ); +// +// jmxCrawler.doCrawler(); + +// JmxCrawler d = new JmxCrawler(); +// Map m = new HashMap<>(); +// m.put("ip","service:jmx:rmi:///jndi/rmi://192.168.1.207:7199/jmxrmi"); +// +// Object r = d.getInternal(m); +// System.out.println("r = " + r); +// } +} diff --git a/src/main/resources/_ b/src/main/resources/_ new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/com/loafle/AppTest.java b/src/test/java/com/loafle/AppTest.java new file mode 100644 index 0000000..adfe5b6 --- /dev/null +++ b/src/test/java/com/loafle/AppTest.java @@ -0,0 +1,28 @@ + +package com.loafle; +import static org.junit.Assert.*; + +import com.loafle.overflow.crawler.jmx.JmxCrawler; +import org.junit.Test; + +public class AppTest { + @Test + public void testSum() { + assertEquals(1,1); + } + + @Test + public void TestDoCrawler() { +// JmxCrawler jmxCrawler = new JmxCrawler( +//// "service:jmx:rmi:///jndi/rmi://192.168.1.207:7199/jmxrmi", +// "service:jmx:rmi:///jndi/rmi://192.168.1.103:9840/jmxrmi", +//// "", +// "", +// "", +// false, +// new JmxCrawler.StdoutWriter() +// ); +// +// jmxCrawler.doCrawler(); + } +} \ No newline at end of file diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml new file mode 100644 index 0000000..a0a8208 --- /dev/null +++ b/src/test/resources/logback.xml @@ -0,0 +1,17 @@ + + + jmx_crawler + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n + + + + + + + + + \ No newline at end of file