config ing
This commit is contained in:
parent
670993e924
commit
8f3da472c4
2
pom.xml
2
pom.xml
|
@ -20,7 +20,7 @@
|
|||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.loafle.overflow</groupId>
|
||||
<artifactId>crawler_java</artifactId>
|
||||
<artifactId>crawler</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.loafle.overflow.crawler.wmi;
|
||||
|
||||
import com.loafle.overflow.crawler.Crawler;
|
||||
import com.loafle.overflow.crawler.config.Config;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -10,9 +11,9 @@ import java.util.Map;
|
|||
*/
|
||||
public class WMICrawler extends Crawler {
|
||||
|
||||
public Object getInternal(Map<String, Object> map) throws Exception {
|
||||
return WMICrawlerOS.getInstance().processWMI(map);
|
||||
@Override
|
||||
public Object getInternal(Config config) throws Exception {
|
||||
|
||||
return WMICrawlerOS.getInstance().processWMI(config);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.loafle.overflow.crawler.wmi;
|
||||
|
||||
import com.loafle.overflow.crawler.config.Config;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
|
@ -12,11 +14,18 @@ public class WMICrawlerLinux extends WMICrawlerOS {
|
|||
private final String DELIMITER = "||";
|
||||
private final String DELIMITER_SPLIT = "\\|\\|";
|
||||
|
||||
public Object processWMI(Map<String, Object> params) throws Exception {
|
||||
return processCommand(params);
|
||||
public Object processWMI(Config config) throws Exception {
|
||||
return processCommand(config);
|
||||
}
|
||||
|
||||
public Object processCommand(Map<String, Object> params) throws Exception {
|
||||
public Object processCommand(Config config) throws Exception {
|
||||
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("id", "administrator");
|
||||
params.put("pw", "!@#$qwer1234");
|
||||
params.put("nameSpace", "root/cimv2");
|
||||
params.put("query", "select * from Win32_OperatingSystem");
|
||||
params.put("ip", "192.168.1.1");
|
||||
|
||||
String id = (String)params.get("id");
|
||||
String pw = (String)params.get("pw");
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.loafle.overflow.crawler.wmi;
|
||||
|
||||
import com.loafle.overflow.crawler.config.Config;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
|
@ -26,5 +28,5 @@ public abstract class WMICrawlerOS {
|
|||
return WMICrawlerOS.wmiCrawlerOS;
|
||||
}
|
||||
|
||||
public abstract Object processWMI(Map<String, Object> params) throws Exception;
|
||||
public abstract Object processWMI(Config config) throws Exception;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,9 @@ import com.jacob.activeX.ActiveXComponent;
|
|||
import com.jacob.com.Dispatch;
|
||||
import com.jacob.com.EnumVariant;
|
||||
import com.jacob.com.Variant;
|
||||
import com.loafle.overflow.crawler.config.Config;
|
||||
import com.loafle.overflow.crawler.config.Item;
|
||||
import com.loafle.overflow.crawler.config.Query;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
@ -15,82 +18,145 @@ import java.util.Map;
|
|||
*/
|
||||
public class WMICrawlerWindows extends WMICrawlerOS {
|
||||
|
||||
public Object processWMI(Map<String, Object> params) throws Exception {
|
||||
public Object processWMI(Config config) throws Exception {
|
||||
|
||||
String id = (String)params.get("id");
|
||||
String pw = (String)params.get("pw");
|
||||
String nameSpace = (String)params.get("nameSpace");
|
||||
String query = (String)params.get("query");
|
||||
String ip = (String)params.get("ip");
|
||||
String query = "";
|
||||
|
||||
ActiveXComponent wmi = null;
|
||||
wmi = new ActiveXComponent("WbemScripting.SWbemLocator");
|
||||
Item cItem = null;
|
||||
Query cQuery = null;
|
||||
|
||||
Variant vIp = new Variant(ip);
|
||||
Variant vNs = new Variant(nameSpace);
|
||||
Variant vId = new Variant(id);
|
||||
Variant vPw = new Variant(pw);
|
||||
int idxMetric = 0;
|
||||
|
||||
Variant conRet = wmi.invoke("ConnectServer", vIp, vNs, vId, vPw);
|
||||
Map<String, String> resultMap = new HashMap<>();
|
||||
ActiveXComponent wmiconnect = null;
|
||||
for(int cIndexI = 0 ; cIndexI < config.getItems().size() ; ++cIndexI) {
|
||||
|
||||
vIp.safeRelease();
|
||||
vNs.safeRelease();
|
||||
vId.safeRelease();
|
||||
vPw.safeRelease();
|
||||
cItem = config.getItems().get(cIndexI);
|
||||
|
||||
Dispatch disConret = conRet.toDispatch();
|
||||
for( int indexJ = 0; indexJ < cItem.getQueries().size(); ++indexJ) {
|
||||
|
||||
ActiveXComponent wmiconnect = new ActiveXComponent(disConret);
|
||||
disConret.safeRelease();
|
||||
cQuery = cItem.getQueries().get(indexJ);
|
||||
query =cQuery.getQuery();
|
||||
|
||||
Variant vQuery = new Variant(query);
|
||||
wmiconnect = connectServer(config);
|
||||
|
||||
Variant vCollection = wmiconnect.invoke("ExecQuery", vQuery);
|
||||
Variant vQuery = new Variant(query);
|
||||
|
||||
wmiconnect.safeRelease();
|
||||
vQuery.safeRelease();
|
||||
Variant vCollection = wmiconnect.invoke("ExecQuery", vQuery);
|
||||
|
||||
EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
|
||||
wmiconnect.safeRelease();
|
||||
vQuery.safeRelease();
|
||||
|
||||
wmi.safeRelease();
|
||||
conRet.safeRelease();
|
||||
vCollection.safeRelease();
|
||||
Dispatch dConnection = vCollection.toDispatch();
|
||||
vCollection.safeRelease();
|
||||
|
||||
|
||||
int count = getCount(dConnection);
|
||||
|
||||
EnumVariant enumVariant = new EnumVariant(dConnection);
|
||||
dConnection.safeRelease();
|
||||
|
||||
if( count <= 1 ) {
|
||||
getSingleValue(enumVariant, cQuery, cItem, idxMetric, resultMap);
|
||||
}
|
||||
else {
|
||||
getMultiValue(enumVariant, cQuery, cItem, idxMetric, resultMap);
|
||||
}
|
||||
|
||||
|
||||
enumVariant.safeRelease();
|
||||
idxMetric += cQuery.getKeys().size();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
protected void getMultiValue(EnumVariant enumVariant, Query cQuery, Item cItem, int idxMetric, Map<String, String> resultMap) {
|
||||
|
||||
Variant vItem = null;
|
||||
Dispatch item = null;
|
||||
|
||||
List<String> columns = null;
|
||||
Map<String, String> resultMap = null;
|
||||
List<Map<String, String>> resultMapList = new ArrayList<Map<String, String>>();
|
||||
|
||||
int keyIdx = 0;
|
||||
while (enumVariant.hasMoreElements()) {
|
||||
|
||||
vItem = enumVariant.nextElement();
|
||||
item = vItem.toDispatch();
|
||||
|
||||
if (columns == null) {
|
||||
columns = getColumns(item);
|
||||
}
|
||||
|
||||
resultMap = new HashMap<String, String>();
|
||||
String name = null;
|
||||
String value = null;
|
||||
Variant vValue = null;
|
||||
|
||||
for(int indexI = 0 ; indexI < columns.size(); ++indexI) {
|
||||
name = columns.get(indexI);
|
||||
for(int indexI = 0 ; indexI < cQuery.getKeys().size(); ++indexI) {
|
||||
name = cQuery.getKeys().get(indexI);
|
||||
vValue = Dispatch.call(item, name);
|
||||
value = vValue.toString();
|
||||
resultMap.put(name, value);
|
||||
String keyStr = convertArrayKey(cItem.getMetrics().get(indexI),keyIdx );
|
||||
resultMap.put(keyStr, value);
|
||||
vValue.safeRelease();
|
||||
}
|
||||
|
||||
resultMapList.add(resultMap);
|
||||
vItem.safeRelease();
|
||||
item.safeRelease();
|
||||
++idxMetric;
|
||||
++keyIdx;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected String convertArrayKey(String key, int index) {
|
||||
int idx = key.indexOf("[");
|
||||
String ret = "";
|
||||
|
||||
if(idx <= 0) {
|
||||
idx = key.indexOf(".");
|
||||
ret += key.substring(0, idx);
|
||||
ret += "[";
|
||||
ret += String.valueOf(index);
|
||||
ret += "]";
|
||||
ret += key.substring(idx +1);
|
||||
}else {
|
||||
|
||||
ret += key.substring(0, idx +1);
|
||||
ret += String.valueOf(index);
|
||||
ret += key.substring(idx +1);
|
||||
}
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected void getSingleValue(EnumVariant enumVariant, Query cQuery, Item cItem, int idxMetric, Map<String, String> resultMap) {
|
||||
|
||||
Variant vItem = null;
|
||||
Dispatch item = null;
|
||||
|
||||
while (enumVariant.hasMoreElements()) {
|
||||
|
||||
vItem = enumVariant.nextElement();
|
||||
item = vItem.toDispatch();
|
||||
|
||||
String name = null;
|
||||
String value = null;
|
||||
Variant vValue = null;
|
||||
|
||||
for(int indexI = 0 ; indexI < cQuery.getKeys().size(); ++indexI) {
|
||||
name = cQuery.getKeys().get(indexI);
|
||||
vValue = Dispatch.call(item, name);
|
||||
value = vValue.toString();
|
||||
resultMap.put(cItem.getMetrics().get(idxMetric++), value);
|
||||
vValue.safeRelease();
|
||||
}
|
||||
|
||||
vItem.safeRelease();
|
||||
item.safeRelease();
|
||||
}
|
||||
return resultMapList;
|
||||
|
||||
}
|
||||
|
||||
protected List<String> getColumns(Dispatch item) {
|
||||
|
@ -134,4 +200,51 @@ public class WMICrawlerWindows extends WMICrawlerOS {
|
|||
}
|
||||
|
||||
|
||||
protected int getCount(Dispatch d) {
|
||||
|
||||
Variant c = Dispatch.call(d, "Count");
|
||||
|
||||
int retInt = Integer.valueOf(c.toString());
|
||||
|
||||
c.safeRelease();
|
||||
|
||||
return retInt;
|
||||
}
|
||||
|
||||
protected ActiveXComponent connectServer(Config config) {
|
||||
|
||||
String id = (String)config.getTarget().getAuth().get("id");
|
||||
String pw = (String)config.getTarget().getAuth().get("pw");
|
||||
String nameSpace = "";
|
||||
// String query = (String)config.get.get("query");
|
||||
|
||||
String ip = config.getTarget().getConnection().getIp();
|
||||
|
||||
ActiveXComponent wmi = null;
|
||||
wmi = new ActiveXComponent("WbemScripting.SWbemLocator");
|
||||
|
||||
Variant vIp = new Variant(ip);
|
||||
Variant vNs = new Variant(nameSpace);
|
||||
Variant vId = new Variant(id);
|
||||
Variant vPw = new Variant(pw);
|
||||
|
||||
Variant conRet = wmi.invoke("ConnectServer", vIp, vNs, vId, vPw);
|
||||
|
||||
vIp.safeRelease();
|
||||
vNs.safeRelease();
|
||||
vId.safeRelease();
|
||||
vPw.safeRelease();
|
||||
|
||||
Dispatch disConret = conRet.toDispatch();
|
||||
|
||||
ActiveXComponent wmiconnect = new ActiveXComponent(disConret);
|
||||
disConret.safeRelease();
|
||||
|
||||
wmi.safeRelease();
|
||||
conRet.safeRelease();
|
||||
|
||||
return wmiconnect;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,10 +1,16 @@
|
|||
package com.loafle.overflow.crawler.wmi;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.loafle.overflow.crawler.config.Config;
|
||||
import com.sun.xml.internal.ws.api.ResourceLoader;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -58,21 +64,31 @@ public class WMICrawlerTest {
|
|||
// String query = "select * from Win32_OperatingSystem";
|
||||
// String ip = "192.168.1.1";
|
||||
|
||||
map.put("id", "administrator");
|
||||
map.put("pw", "!@#$qwer1234");
|
||||
map.put("nameSpace", "root/cimv2");
|
||||
map.put("query", "select * from Win32_OperatingSystem");
|
||||
map.put("ip", "192.168.1.1");
|
||||
// map.put("id", "administrator");
|
||||
// map.put("pw", "!@#$qwer1234");
|
||||
// map.put("nameSpace", "root/cimv2");
|
||||
// map.put("query", "select * from Win32_OperatingSystem");
|
||||
// map.put("ip", "192.168.1.1");
|
||||
|
||||
Config config = new Config();
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
|
||||
String s = classLoader.getResource("config").getFile();
|
||||
System.out.println(s);
|
||||
|
||||
// Config c = mapper.readValue(new File(classLoader.getResource("config").getFile()),Config.class);
|
||||
|
||||
|
||||
List<Map<String, String>> resultList = (List<Map<String, String>>)wmiCrawler.getInternal(map);
|
||||
|
||||
|
||||
for( Map<String,String> rMap : resultList) {
|
||||
for( String key : rMap.keySet() ){
|
||||
System.out.println( String.format("key : %s ||| value : %s", key, rMap.get(key)) );
|
||||
}
|
||||
}
|
||||
// List<Map<String, String>> resultList = (List<Map<String, String>>)wmiCrawler.getInternal(map);
|
||||
//
|
||||
//
|
||||
// for( Map<String,String> rMap : resultList) {
|
||||
// for( String key : rMap.keySet() ){
|
||||
// System.out.println( String.format("key : %s ||| value : %s", key, rMap.get(key)) );
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
@ -83,4 +99,83 @@ public class WMICrawlerTest {
|
|||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfig() throws IOException {
|
||||
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
String p = ResourceLoader.class.getClass().getResource("/config/example.json").getPath();
|
||||
|
||||
System.out.println(p);
|
||||
|
||||
|
||||
Config c = mapper.readValue(new File(p),Config.class);
|
||||
|
||||
System.out.println(c);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWMIJson() throws Exception {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String p = ResourceLoader.class.getClass().getResource("/config/example.json").getPath();
|
||||
Config c = mapper.readValue(new File(p),Config.class);
|
||||
|
||||
// Map<String, Object> map = new HashMap<>();
|
||||
// map.put("id", "administrator");
|
||||
// map.put("pw", "!@#$qwer1234");
|
||||
|
||||
// c.getTarget().setAuth(map);
|
||||
|
||||
c.getTarget().getAuth().put("id", "administrator");
|
||||
c.getTarget().getAuth().put("pw", "!@#$qwer1234");
|
||||
|
||||
|
||||
WMICrawler wmiCrawler = new WMICrawler();
|
||||
|
||||
|
||||
Map<String, String> resultMap = (Map<String, String>)wmiCrawler.getInternal(c);
|
||||
|
||||
|
||||
for( String key : resultMap.keySet() ){
|
||||
System.out.println( String.format("key : %s ||| value : %s", key, resultMap.get(key)) );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInt() {
|
||||
|
||||
Integer a = 1;
|
||||
|
||||
aaa(a);
|
||||
|
||||
System.out.println(a);
|
||||
|
||||
}
|
||||
|
||||
public void aaa(Integer a) {
|
||||
++a;
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testString() {
|
||||
|
||||
String str = "cpu[].usage.system";
|
||||
|
||||
int idx = str.indexOf("[");
|
||||
int arrIdx = 0;
|
||||
|
||||
String ret = "";
|
||||
ret += str.substring(0, idx +1);
|
||||
ret += String.valueOf(arrIdx);
|
||||
ret += str.substring(idx +1);
|
||||
|
||||
System.out.println(ret);
|
||||
|
||||
}
|
||||
|
||||
}
|
65
src/test/resources/config/example.json
Normal file
65
src/test/resources/config/example.json
Normal file
|
@ -0,0 +1,65 @@
|
|||
{
|
||||
"id" : "SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734",
|
||||
"target" : {
|
||||
"connection" : {
|
||||
"ip" : "192.168.1.1",
|
||||
"port" : "135",
|
||||
"ssl" : false,
|
||||
"portType" : "tcp"
|
||||
},
|
||||
"auth" : {
|
||||
|
||||
}
|
||||
},
|
||||
"schedule" : {
|
||||
"interval" : "10"
|
||||
},
|
||||
"crawler" : {
|
||||
"name":"redis_protocol_crawler",
|
||||
"container":"network_crawler"
|
||||
},
|
||||
"items" : [
|
||||
{
|
||||
"metrics": [
|
||||
"cpu.usage.system",
|
||||
"cpu.usage.idle",
|
||||
"cpu.usage.user"
|
||||
],
|
||||
"queries": [
|
||||
{
|
||||
"query":"select PercentProcessorTime, PercentIdleTime, PercentUserTime from Win32_PerfFormattedData_PerfOS_Processor where Name='_Total'",
|
||||
"keys":["PercentProcessorTime","PercentIdleTime", "PercentUserTime"]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"metrics": [
|
||||
"cpu[].usage.system",
|
||||
"cpu[].usage.idle",
|
||||
"cpu[].usage.user"
|
||||
],
|
||||
"queries": [
|
||||
{
|
||||
"query":"select PercentProcessorTime, PercentIdleTime, PercentUserTime from Win32_PerfFormattedData_PerfOS_Processor where Name!='_Total'",
|
||||
"keys":["PercentProcessorTime","PercentIdleTime", "PercentUserTime"]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
,{
|
||||
"metrics": [
|
||||
"cpu.usage.system",
|
||||
"cpu.usage.idle",
|
||||
"cpu.usage.user"
|
||||
],
|
||||
"queries": [
|
||||
{
|
||||
"query":"select PercentProcessorTime, PercentIdleTime, PercentUserTime from Win32_PerfFormattedData_PerfOS_Processor",
|
||||
"keys":["PercentProcessorTime","PercentIdleTime", "PercentUserTime"]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
]
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user