config 적용 테스트

This commit is contained in:
geek 2017-04-24 11:49:35 +09:00
parent d824601b33
commit cb1fd86aed
3 changed files with 119 additions and 34 deletions

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/../jmx_crawler/target/classes" />
<output-test url="file://$MODULE_DIR$/../jmx_crawler/target/test-classes" />
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
@ -13,6 +13,9 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.loafle.overflow:crawler:1.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.8" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.8" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />

View File

@ -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<String> whiteObjectNameList = new ArrayList<>();
private List<List<String>> keys = new ArrayList<>();
private List<Object> result = new ArrayList<>();
private Map<String, String> re = new HashMap<String, String>();
@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<String> attrKeys, String attrName, String attrType, String attrDescription, Object value) {
Map<String, String> l = new HashMap<String, String>();
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<com.loafle.overflow.crawler.config.Query> querys = item.getQueries();
for (Query q : querys) {
this.whiteObjectNameList.add(q.getQuery());
this.keys.add(q.getKeys());
}
}
this.doCrawler();
Map<String, String> result = new LinkedHashMap<>();
for (List<String> 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<String> 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<String>();
@ -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();

View File

@ -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<Item> ls = new ArrayList<>();
List<String> metrics = new ArrayList<>();
List<Query> queries = new ArrayList<>();
List<String> 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);
}
}