From cb1fd86aedc9432458bac90de9f0ccc71b9a4463 Mon Sep 17 00:00:00 2001 From: geek Date: Mon, 24 Apr 2017 11:49:35 +0900 Subject: [PATCH] =?UTF-8?q?config=20=EC=A0=81=EC=9A=A9=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crawler_jmx_java.iml | 7 +- .../overflow/crawler/jmx/JmxCrawler.java | 84 ++++++++++++------- src/test/java/com/loafle/AppTest.java | 62 +++++++++++++- 3 files changed, 119 insertions(+), 34 deletions(-) diff --git a/crawler_jmx_java.iml b/crawler_jmx_java.iml index ab795b1..cc5f2fa 100644 --- a/crawler_jmx_java.iml +++ b/crawler_jmx_java.iml @@ -1,8 +1,8 @@ - - + + @@ -13,6 +13,9 @@ + + + diff --git a/src/main/java/com/loafle/overflow/crawler/jmx/JmxCrawler.java b/src/main/java/com/loafle/overflow/crawler/jmx/JmxCrawler.java index a6aba00..d1afe57 100644 --- a/src/main/java/com/loafle/overflow/crawler/jmx/JmxCrawler.java +++ b/src/main/java/com/loafle/overflow/crawler/jmx/JmxCrawler.java @@ -1,7 +1,8 @@ package com.loafle.overflow.crawler.jmx; import com.loafle.overflow.crawler.Crawler; -import com.loafle.overflow.crawler.config.Config; +import com.loafle.overflow.crawler.config.*; +import com.loafle.overflow.crawler.config.Query; import javax.management.*; import javax.management.openmbean.CompositeData; @@ -33,8 +34,10 @@ public class JmxCrawler extends Crawler{ private String password; private boolean ssl; private MBeanReceiver receiver; + private List whiteObjectNameList = new ArrayList<>(); + private List> keys = new ArrayList<>(); - private List result = new ArrayList<>(); + private Map re = new HashMap(); @Override public Object getInternal(Config config) throws Exception { @@ -42,26 +45,43 @@ public class JmxCrawler extends Crawler{ 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); + re.put(attrName, value.toString()); } }; -// 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(); +// "service:jmx:rmi:///jndi/rmi://192.168.1.103:9840/jmxrmi"; + String hostIp = config.getTarget().getConnection().getIp(); + String port = config.getTarget().getConnection().getPort(); + + this.jmxUrl = "service:jmx:rmi:///jndi/rmi://" + hostIp + ":" +port + "/jmxrmi"; + this.username = null; + this.password = null; + this.receiver = m; + + for (Item item : config.getItems()) { + List querys = item.getQueries(); + for (Query q : querys) { + this.whiteObjectNameList.add(q.getQuery()); + this.keys.add(q.getKeys()); + } + } + + + this.doCrawler(); + + Map result = new LinkedHashMap<>(); + + for (List ks : this.keys) { + for (String k : ks) { + for (String key : re.keySet()) { + if (k.equals(key)) { + result.put(key, re.get(key)); + } + } + } + } + + return result; } @@ -72,13 +92,13 @@ public class JmxCrawler extends Crawler{ 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 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 { @@ -112,11 +132,15 @@ public class JmxCrawler extends Crawler{ // crawlerBean(beanCon, name.getObjectName()); // } - ObjectName on = new ObjectName("Catalina:type=*,*"); +// ObjectName on = new ObjectName("Catalina:type=*,*"); // ObjectName on = new ObjectName("org.apache.cassandra.db:type=*,*"); - mBeanNames.addAll(beanCon.queryMBeans(on,null)); + ObjectName on = null; + for (String q : this.whiteObjectNameList) { + on = new ObjectName(q); + mBeanNames.addAll(beanCon.queryMBeans(on,null)); + } for (ObjectInstance name : mBeanNames) { crawlerBean(beanCon, name.getObjectName()); } @@ -174,13 +198,12 @@ public class JmxCrawler extends Crawler{ private void processBeanValue(String domain, LinkedList attrKeys, String attrName, String attrType, String attrDescription, Object value) { if (value == null) { - logCrawler("ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"+domain + attrName, "null"); + logCrawler("Value Null : "+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(); @@ -193,7 +216,6 @@ public class JmxCrawler extends Crawler{ 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(); diff --git a/src/test/java/com/loafle/AppTest.java b/src/test/java/com/loafle/AppTest.java index adfe5b6..403999f 100644 --- a/src/test/java/com/loafle/AppTest.java +++ b/src/test/java/com/loafle/AppTest.java @@ -2,9 +2,14 @@ package com.loafle; import static org.junit.Assert.*; +import com.loafle.overflow.crawler.Crawler; +import com.loafle.overflow.crawler.config.*; import com.loafle.overflow.crawler.jmx.JmxCrawler; import org.junit.Test; +import java.util.ArrayList; +import java.util.List; + public class AppTest { @Test public void testSum() { @@ -12,7 +17,7 @@ public class AppTest { } @Test - public void TestDoCrawler() { + public void TestDoCrawler() 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", @@ -24,5 +29,60 @@ public class AppTest { // ); // // jmxCrawler.doCrawler(); + + Config config = new Config(); + + config.setId("SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734"); + Connection cn = new Connection(); + cn.setIp("192.168.1.103"); + cn.setPort("9840"); + cn.setSsl(false); + cn.setPortType("tcp"); + + Target t = new Target(); + t.setConnection(cn); + + Schedule s = new Schedule(); + s.setInterval("10"); + + com.loafle.overflow.crawler.config.Crawler cc = new com.loafle.overflow.crawler.config.Crawler(); + cc.setName("jmx_crawler"); + cc.setContainer("java_crawler"); + + List ls = new ArrayList<>(); + List metrics = new ArrayList<>(); + List queries = new ArrayList<>(); + List keys = new ArrayList<>(); + keys.add("ProcessCpuLoad"); + keys.add("ProcessCpuTime"); + keys.add("SystemCpuLoad"); + + Query query = new Query(); + query.setKeys(keys); + query.setQuery("java.lang:type=OperatingSystem"); + + queries.add(query); + + metrics.add("cpu.system"); + metrics.add("cpu.user"); + metrics.add("cpu.system_children"); + metrics.add("cpu.user_children"); + + Item it = new Item(); + it.setMetrics(metrics); + it.setQueries(queries); + + ls.add(it); + + config.setTarget(t); + config.setSchedule(s); + config.setCrawler(cc); + config.setItems(ls); + + JmxCrawler cr = new JmxCrawler(); + + Object o = cr.getInternal(config); + + System.out.println("o = " + o); } } \ No newline at end of file