From 161cbe0f27e6bcf31312752011f6cf944dd5dca7 Mon Sep 17 00:00:00 2001 From: snoop Date: Tue, 25 Apr 2017 18:41:28 +0900 Subject: [PATCH] config process --- .../overflow/crawler/wmi/WMICrawlerOS.java | 7 +- .../crawler/wmi/WMICrawlerWindows.java | 142 ++++++++++++++---- .../overflow/crawler/wmi/WMICrawlerTest.java | 30 +++- src/test/resources/config/example.json | 48 +++--- 4 files changed, 178 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerOS.java b/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerOS.java index 25689b5..fc84c3d 100644 --- a/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerOS.java +++ b/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerOS.java @@ -28,5 +28,10 @@ public abstract class WMICrawlerOS { return WMICrawlerOS.wmiCrawlerOS; } - public abstract Object processWMI(Config config) throws Exception; + public Object processWMI(Config config) throws Exception { + + + + return null; + } } diff --git a/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerWindows.java b/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerWindows.java index e155c04..d243c90 100644 --- a/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerWindows.java +++ b/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerWindows.java @@ -7,6 +7,8 @@ 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 com.loafle.overflow.crawler.result.OFResultSet; +import com.loafle.overflow.crawler.result.OFResultSetCol; import java.util.ArrayList; import java.util.HashMap; @@ -25,54 +27,140 @@ public class WMICrawlerWindows extends WMICrawlerOS { Item cItem = null; Query cQuery = null; - int idxMetric = 0; +// int idxMetric = 0; - Map resultMap = new HashMap<>(); + List retlist = new ArrayList<>(); ActiveXComponent wmiconnect = null; + + OFResultSet ofResultSet = null; for(int cIndexI = 0 ; cIndexI < config.getItems().size() ; ++cIndexI) { cItem = config.getItems().get(cIndexI); - for( int indexJ = 0; indexJ < cItem.getQueries().size(); ++indexJ) { + cQuery = cItem.getQuery(); + query = (String)cQuery.getQueryInfo().get("query"); - cQuery = cItem.getQueries().get(indexJ); - query =cQuery.getQuery(); + wmiconnect = connectServer(config); - wmiconnect = connectServer(config); + Variant vQuery = new Variant(query); - Variant vQuery = new Variant(query); + Variant vCollection = wmiconnect.invoke("ExecQuery", vQuery); - Variant vCollection = wmiconnect.invoke("ExecQuery", vQuery); + wmiconnect.safeRelease(); + vQuery.safeRelease(); - wmiconnect.safeRelease(); - vQuery.safeRelease(); - - Dispatch dConnection = vCollection.toDispatch(); - vCollection.safeRelease(); + Dispatch dConnection = vCollection.toDispatch(); + vCollection.safeRelease(); - int count = getCount(dConnection); +// int count = getCount(dConnection); - EnumVariant enumVariant = new EnumVariant(dConnection); - dConnection.safeRelease(); + EnumVariant enumVariant = new EnumVariant(dConnection); + dConnection.safeRelease(); - if( count <= 1 ) { - getSingleValue(enumVariant, cQuery, cItem, idxMetric, resultMap); +// if( count <= 1 ) { +// getSingleValue(enumVariant, cQuery, cItem, idxMetric, resultMap); +// } +// else { +// getMultiValue(enumVariant, cQuery, cItem, idxMetric, resultMap); +// } + + ofResultSet = getResultSet(enumVariant, cItem); + + enumVariant.safeRelease(); +// idxMetric += cQuery.getKeys().size(); + + retlist.add(ofResultSet); + } + + return retlist; + } + + protected OFResultSet getResultSet(EnumVariant enumVariant, Item citem) { + + OFResultSet ofResultSet = new OFResultSetCol(citem); + + Variant vItem = null; + Dispatch item = null; + + Variant vValue = null; + + String value = null; + + List row = null; + + List columns = (List)citem.getQuery().getQueryInfo().get("arrayColumns"); + + while (enumVariant.hasMoreElements()) { + + row = new ArrayList<>(); + + vItem = enumVariant.nextElement(); + item = vItem.toDispatch(); + + for( int indexI = 0; indexI < citem.getQuery().getKeys().size(); ++indexI) { + vValue = Dispatch.call(item, citem.getQuery().getKeys().get(indexI)); + value = vValue.toString(); + vValue.safeRelease(); + row.add(value); + } + + if (columns != null) { + for( int indexI = 0 ; indexI < columns.size(); ++indexI) { + vValue = Dispatch.call(item, columns.get(indexI)); + value = vValue.toString(); + vValue.safeRelease(); + row.add(value); } - else { - getMultiValue(enumVariant, cQuery, cItem, idxMetric, resultMap); - } - - - enumVariant.safeRelease(); - idxMetric += cQuery.getKeys().size(); - } + vItem.safeRelease(); + item.safeRelease(); + + ofResultSet.addRow(row); } - return resultMap; + + + return ofResultSet; + } + + protected void getMultiValue(EnumVariant enumVariant,Query cQuery, Map resultMap) { + + Variant vItem = null; + Dispatch item = null; + + String indexColumn = (String)cQuery.getQueryInfo().get("indexColumn"); + + Variant vValue = null; + Variant vColumn = null; + String value = null; + String columnStr = null; + + + + while (enumVariant.hasMoreElements()) { + + vItem = enumVariant.nextElement(); + item = vItem.toDispatch(); + + vColumn = Dispatch.call(item, indexColumn); + columnStr = vColumn.toString(); + vColumn.safeRelease(); + + for( int indexI = 0; indexI < cQuery.getKeys().size(); ++indexI) { + + vValue = Dispatch.call(item, cQuery.getKeys().get(indexI)); + value = vValue.toString(); + + vValue.safeRelease(); + } + + vItem.safeRelease(); + item.safeRelease(); + } + } protected void getMultiValue(EnumVariant enumVariant, Query cQuery, Item cItem, int idxMetric, Map resultMap) { diff --git a/src/test/java/com/loafle/overflow/crawler/wmi/WMICrawlerTest.java b/src/test/java/com/loafle/overflow/crawler/wmi/WMICrawlerTest.java index 0b6ece3..9f7ec89 100644 --- a/src/test/java/com/loafle/overflow/crawler/wmi/WMICrawlerTest.java +++ b/src/test/java/com/loafle/overflow/crawler/wmi/WMICrawlerTest.java @@ -2,6 +2,7 @@ package com.loafle.overflow.crawler.wmi; import com.fasterxml.jackson.databind.ObjectMapper; import com.loafle.overflow.crawler.config.Config; +import com.loafle.overflow.crawler.result.OFResultSet; import com.sun.xml.internal.ws.api.ResourceLoader; import org.junit.After; import org.junit.Before; @@ -135,12 +136,19 @@ public class WMICrawlerTest { WMICrawler wmiCrawler = new WMICrawler(); - Map resultMap = (Map)wmiCrawler.getInternal(c); - + List resultList = (List)wmiCrawler.getInternal(c); + for(int indexI = 0 ; indexI < resultList.size(); ++indexI) { + Map resultMap = resultList.get(indexI).getData(); + System.out.println("============================================"); for( String key : resultMap.keySet() ){ System.out.println( String.format("key : %s ||| value : %s", key, resultMap.get(key)) ); } + System.out.println("============================================"); + } + + + } @@ -178,4 +186,22 @@ public class WMICrawlerTest { } + @Test + public void testReplace() { + + + String aaa = "cpu[$0].usage.system"; + + String bbb = "$"; + String ccc = bbb + String.valueOf(0); + + String ddd = aaa.replace(ccc, "asdfasdf"); + + + System.out.println(ddd); + + + + } + } \ No newline at end of file diff --git a/src/test/resources/config/example.json b/src/test/resources/config/example.json index 0b804ac..d0e7ef3 100644 --- a/src/test/resources/config/example.json +++ b/src/test/resources/config/example.json @@ -19,33 +19,43 @@ "container":"network_crawler" }, "items" : [ + { + "metrics": [ + "cpu[$0].usage.system", + "cpu[$0].usage.idle", + "cpu[$0].usage.user" + ], + "query": + { + "queryInfo": { + "query":"select PercentProcessorTime, PercentIdleTime, PercentUserTime,Name from Win32_PerfFormattedData_PerfOS_Processor", + "parseDirection" : "col", + "arrayColumns":["Name"] + }, + "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 where Name!='_Total'", - "keys":["PercentProcessorTime","PercentIdleTime", "PercentUserTime"] - } - ] + "query": + { + "queryInfo": { + "query":"select PercentProcessorTime, PercentIdleTime, PercentUserTime,Name from Win32_PerfFormattedData_PerfOS_Processor where Name='_Total'", + "parseDirection" : "col" + + }, + "keys":["PercentProcessorTime","PercentIdleTime", "PercentUserTime"] + } + } + ] } \ No newline at end of file