shared project

This commit is contained in:
geek 2017-04-13 13:57:48 +09:00
commit 57beece37a
6 changed files with 398 additions and 0 deletions

22
crawler_jmx_java.iml Normal file
View File

@ -0,0 +1,22 @@
<?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" />
<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" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<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" 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" />
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" />
</component>
</module>

29
pom.xml Normal file
View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.loafle</groupId>
<artifactId>maven_parent_jar</artifactId>
<version>1.0.0-RELEASE</version>
</parent>
<groupId>com.loafle.overflow</groupId>
<artifactId>crawler_jmx</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>com.loafle.overflow.crawler_jmx</name>
<dependencies>
<dependency>
<groupId>com.loafle.overflow</groupId>
<artifactId>crawler</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -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<Object> result = new ArrayList<>();
@Override
public Object getInternal(Map<String, Object> map) throws Exception {
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);
}
};
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<String> 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<String, Object> environment = getAuthority();
jmxconn = JMXConnectorFactory.connect(new JMXServiceURL(this.jmxUrl), environment);
beanCon = jmxconn.getMBeanServerConnection();
}
try {
Set<ObjectInstance> mBeanNames = new HashSet();
List<ObjectName> objectNames = new LinkedList<ObjectName>();
objectNames.add(null);
// for (ObjectName name : objectNames) {
// Set<ObjectInstance> 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<String>(),
attr.getName(),
attr.getType(),
attr.getDescription(),
value
);
}
}
private void processBeanValue(String domain, LinkedList<String> 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<String>();
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<String> valueKeys = new TreeSet<String>(type.keySet());
LinkedList<String> extendedAttrKeys = new LinkedList<String>(attrKeys);
extendedAttrKeys.add(attrName);
for (Object valu : tds.values()) {
if (valu instanceof CompositeData) {
CompositeData composite = (CompositeData) valu;
for(String valueIdx : valueKeys) {
LinkedList<String> 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<String, Object> getAuthority() {
Map<String, Object> environment = new HashMap<String, Object>();
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<String> 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<String, Object> 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);
// }
}

0
src/main/resources/_ Normal file
View File

View File

@ -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();
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="3 seconds">
<contextName>jmx_crawler</contextName>
<!-- TRACE > DEBUG > INFO > WARN > ERROR -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n
</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
<logger name="com.loafle.overflow" level="ALL" />
</configuration>