This commit is contained in:
crusader 2018-04-24 20:51:41 +09:00
parent 0eda0683de
commit 2b4aeea3e6
7 changed files with 317 additions and 306 deletions

View File

@ -179,12 +179,6 @@
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>com.loafle.overflow</groupId>
<artifactId>crawler</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency> <dependency>
<groupId>com.loafle.overflow</groupId> <groupId>com.loafle.overflow</groupId>
<artifactId>commons-java</artifactId> <artifactId>commons-java</artifactId>

View File

@ -1,12 +1,12 @@
package com.loafle.overflow.central.module.generator.service; package com.loafle.overflow.central.module.generator.service;
import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.crawler.config.Crawler;
import com.loafle.overflow.crawler.config.Keys;
import com.loafle.overflow.model.meta.MetaCrawler; import com.loafle.overflow.model.meta.MetaCrawler;
import com.loafle.overflow.model.meta.MetaSensorItemKey; import com.loafle.overflow.model.meta.MetaSensorItemKey;
import com.loafle.overflow.model.meta.type.MetaCrawlerEnum; import com.loafle.overflow.model.meta.type.MetaCrawlerEnum;
import com.loafle.overflow.model.sensor.SensorItem; import com.loafle.overflow.model.sensor.SensorItem;
import com.loafle.overflow.model.sensorconfig.Crawler;
import com.loafle.overflow.model.sensorconfig.Keys;
import com.loafle.overflow.service.central.meta.MetaSensorItemKeyService; import com.loafle.overflow.service.central.meta.MetaSensorItemKeyService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;

View File

@ -2,10 +2,7 @@ package com.loafle.overflow.central.module.generator.service;
import com.loafle.overflow.central.commons.utils.StringConvertor; import com.loafle.overflow.central.commons.utils.StringConvertor;
import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.crawler.config.Config; import com.loafle.overflow.core.type.PortType;
import com.loafle.overflow.crawler.config.Connection;
import com.loafle.overflow.crawler.config.Crawler;
import com.loafle.overflow.crawler.config.Schedule;
import com.loafle.overflow.model.auth.AuthCrawler; import com.loafle.overflow.model.auth.AuthCrawler;
import com.loafle.overflow.model.infra.Infra; import com.loafle.overflow.model.infra.Infra;
import com.loafle.overflow.model.infra.InfraHost; import com.loafle.overflow.model.infra.InfraHost;
@ -13,6 +10,11 @@ import com.loafle.overflow.model.meta.MetaSensorItemKey;
import com.loafle.overflow.model.meta.type.MetaCrawlerEnum; import com.loafle.overflow.model.meta.type.MetaCrawlerEnum;
import com.loafle.overflow.model.sensor.Sensor; import com.loafle.overflow.model.sensor.Sensor;
import com.loafle.overflow.model.sensor.SensorItem; import com.loafle.overflow.model.sensor.SensorItem;
import com.loafle.overflow.model.sensorconfig.Connection;
import com.loafle.overflow.model.sensorconfig.Crawler;
import com.loafle.overflow.model.sensorconfig.Schedule;
import com.loafle.overflow.model.sensorconfig.SensorConfig;
import com.loafle.overflow.model.sensorconfig.Target;
import com.loafle.overflow.service.central.auth.AuthCrawlerService; import com.loafle.overflow.service.central.auth.AuthCrawlerService;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
@ -43,36 +45,36 @@ public class InfraHostGenerator {
InfraHost infraHost = (InfraHost) infra; InfraHost infraHost = (InfraHost) infra;
Config config = new Config(); SensorConfig sensorConfig = new SensorConfig();
config.setId(String.valueOf(dbSensor.getId())); sensorConfig.setConfigID(String.valueOf(dbSensor.getId()));
com.loafle.overflow.crawler.config.Target target = this.createTarget(infraHost, dbSensor); Target target = this.createTarget(infraHost, dbSensor);
if (target == null) { if (target == null) {
return null; return null;
} }
config.setTarget(target); sensorConfig.setTarget(target);
// FIXME: Interval // FIXME: Interval
Schedule schedule = new Schedule(); Schedule schedule = new Schedule();
schedule.setInterval("5"); schedule.setInterval("5");
config.setSchedule(schedule); sensorConfig.setSchedule(schedule);
Crawler crawler = this.generateUtil.getCrawler(dbSensor.getCrawler()); Crawler crawler = this.generateUtil.getCrawler(dbSensor.getCrawler());
config.setCrawler(crawler); sensorConfig.setCrawler(crawler);
Map<Integer, MetaSensorItemKey> keyMap = this.generateUtil.initMappingMap(dbSensor.getCrawler()); Map<Integer, MetaSensorItemKey> keyMap = this.generateUtil.initMappingMap(dbSensor.getCrawler());
if (dbSensor.getCrawler().getId() == MetaCrawlerEnum.WMI_CRAWLER.getValue()) { if (dbSensor.getCrawler().getId() == MetaCrawlerEnum.WMI_CRAWLER.getValue()) {
this.infraHostWMIGenerator.process(sensorItems, keyMap, dbSensor, config); this.infraHostWMIGenerator.process(sensorItems, keyMap, dbSensor, sensorConfig);
} }
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(config); return objectMapper.writeValueAsString(sensorConfig);
} }
private com.loafle.overflow.crawler.config.Target createTarget(InfraHost infraHost, Sensor dbSensor) private Target createTarget(InfraHost infraHost, Sensor dbSensor)
throws Exception { throws Exception {
AuthCrawler authCrawler = this.authCrawlerService.readAuth(dbSensor.getCrawler(), dbSensor.getTarget()); AuthCrawler authCrawler = this.authCrawlerService.readAuth(dbSensor.getCrawler(), dbSensor.getTarget());
@ -81,7 +83,7 @@ public class InfraHostGenerator {
return null; return null;
} }
com.loafle.overflow.crawler.config.Target target = new com.loafle.overflow.crawler.config.Target(); Target target = new Target();
Connection connection = new Connection(); Connection connection = new Connection();
connection.setIp(StringConvertor.intToIp(infraHost.getIp())); connection.setIp(StringConvertor.intToIp(infraHost.getIp()));
@ -91,8 +93,8 @@ public class InfraHostGenerator {
}); });
if (dbSensor.getCrawler().getId() == MetaCrawlerEnum.WMI_CRAWLER.getValue()) { if (dbSensor.getCrawler().getId() == MetaCrawlerEnum.WMI_CRAWLER.getValue()) {
connection.setPort("135"); connection.setPort(135);
connection.setPortType("tcp"); connection.setPortType(PortType.TCP);
connection.setSsl(false); connection.setSsl(false);
target.setConnection(connection); target.setConnection(connection);

View File

@ -1,9 +1,13 @@
package com.loafle.overflow.central.module.generator.service; package com.loafle.overflow.central.module.generator.service;
import com.loafle.overflow.crawler.config.*;
import com.loafle.overflow.model.meta.MetaSensorItemKey; import com.loafle.overflow.model.meta.MetaSensorItemKey;
import com.loafle.overflow.model.sensor.Sensor; import com.loafle.overflow.model.sensor.Sensor;
import com.loafle.overflow.model.sensor.SensorItem; import com.loafle.overflow.model.sensor.SensorItem;
import com.loafle.overflow.model.sensorconfig.Item;
import com.loafle.overflow.model.sensorconfig.Keys;
import com.loafle.overflow.model.sensorconfig.MappingInfo;
import com.loafle.overflow.model.sensorconfig.QueryInfo;
import com.loafle.overflow.model.sensorconfig.SensorConfig;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference; import org.codehaus.jackson.type.TypeReference;
@ -22,116 +26,115 @@ import java.util.Map;
@Service("InfraHostWMIGenerator") @Service("InfraHostWMIGenerator")
public class InfraHostWMIGenerator { public class InfraHostWMIGenerator {
@Autowired @Autowired
private GenerateUtil generateUtil; private GenerateUtil generateUtil;
public void process(List<SensorItem> sensorItems, Map<Integer, MetaSensorItemKey> keyMap, Sensor dbSensor, Config config) throws Exception { public void process(List<SensorItem> sensorItems, Map<Integer, MetaSensorItemKey> keyMap, Sensor dbSensor,
SensorConfig sensorConfig) throws Exception {
Map<String, List<MetaSensorItemKey>> metricMap = null; Map<String, List<MetaSensorItemKey>> metricMap = null;
metricMap = this.generateUtil.sortItems(sensorItems, keyMap); metricMap = this.generateUtil.sortItems(sensorItems, keyMap);
// MetaSensorItemKey itemKey = null; // MetaSensorItemKey itemKey = null;
// //
// for(SensorItem sItem : sensorItems) { // for(SensorItem sItem : sensorItems) {
// itemKey = keyMap.get(sItem.getItem().getId()); // itemKey = keyMap.get(sItem.getItem().getId());
// if(metricMap.containsKey(itemKey.getFroms()) == false) { // if(metricMap.containsKey(itemKey.getFroms()) == false) {
// metricMap.put(itemKey.getFroms(), new ArrayList<>()); // metricMap.put(itemKey.getFroms(), new ArrayList<>());
// } // }
// metricMap.get(itemKey.getFroms()).add(itemKey); // metricMap.get(itemKey.getFroms()).add(itemKey);
// } // }
List<Item> itemList = new ArrayList<>(); List<Item> itemList = new ArrayList<>();
Item item = null; Item item = null;
QueryInfo queryInfo = null; QueryInfo queryInfo = null;
MappingInfo mappingInfo = null; MappingInfo mappingInfo = null;
StringBuffer stringBuffer = new StringBuffer(); StringBuffer stringBuffer = new StringBuffer();
Map<String, Object> extendMap = new HashMap<>(); Map<String, Object> extendMap = new HashMap<>();
extendMap.put("nameSpace", "root/cimv2"); extendMap.put("nameSpace", "root/cimv2");
extendMap.put("wmicPath", "/home/snoop/temp/wmic"); extendMap.put("wmicPath", "/home/snoop/temp/wmic");
List<Keys> keysList = null;
MetaSensorItemKey tempItemKey = null;
Keys keys = null;
for (Map.Entry<String, List<MetaSensorItemKey>> elems : metricMap.entrySet()) {
keysList = new ArrayList<>();
item = new Item();
stringBuffer.setLength(0);
List<Keys> keysList = null; stringBuffer.append("SELECT ");
MetaSensorItemKey tempItemKey = null;
Keys keys = null;
for(Map.Entry<String, List<MetaSensorItemKey>> elems : metricMap.entrySet()) {
keysList = new ArrayList<>();
item = new Item();
stringBuffer.setLength(0);
stringBuffer.append("SELECT "); for (int indexI = 0; indexI < elems.getValue().size(); ++indexI) {
for(int indexI = 0; indexI < elems.getValue().size(); ++indexI) {
tempItemKey = elems.getValue().get(indexI);
stringBuffer.append(tempItemKey.getKey());
if(indexI + 1 < elems.getValue().size()) {
stringBuffer.append(", ");
}
// keys = new Keys();
// keys.setKey(tempItemKey.getKey());
// keys.setMetric(tempItemKey.getItem().getKey());
keysList.add(this.generateUtil.createKeys(tempItemKey));
}
List<String> arrayColumns = null;
String json = tempItemKey.getOption();
if(json != null && json.length() > 0) {
HashMap<String,Object> optionMap;
optionMap = new ObjectMapper().readValue(json, new TypeReference<HashMap<String,Object>>() {});
Object obj = null;
obj = optionMap.get("appends");
if(obj != null) {
List<String> appendsList = (List<String>)obj;
for(String append : appendsList) {
stringBuffer.append(", ");
stringBuffer.append(append);
}
}
stringBuffer.append(" FROM ");
stringBuffer.append(elems.getKey());
obj = optionMap.get("where");
if(obj != null) {
String where = (String)obj;
stringBuffer.append(" WHERE ");
stringBuffer.append(where);
}
obj = optionMap.get("arrayColumns");
if(obj != null) {
arrayColumns = (List<String>)obj;
}
}
else {
stringBuffer.append(" FROM ");
stringBuffer.append(elems.getKey());
}
queryInfo = new QueryInfo();
queryInfo.setQuery(stringBuffer.toString());
queryInfo.setExtend(extendMap);
mappingInfo = new MappingInfo();
mappingInfo.setParseDirection("col");
mappingInfo.setArrayColumns(arrayColumns);
item.setMappingInfo(mappingInfo);
item.setQueryInfo(queryInfo);
item.setKeys(keysList);
itemList.add(item);
tempItemKey = elems.getValue().get(indexI);
stringBuffer.append(tempItemKey.getKey());
if (indexI + 1 < elems.getValue().size()) {
stringBuffer.append(", ");
} }
config.setItems(itemList); // keys = new Keys();
// keys.setKey(tempItemKey.getKey());
// keys.setMetric(tempItemKey.getItem().getKey());
keysList.add(this.generateUtil.createKeys(tempItemKey));
}
List<String> arrayColumns = null;
String json = tempItemKey.getOption();
if (json != null && json.length() > 0) {
HashMap<String, Object> optionMap;
optionMap = new ObjectMapper().readValue(json, new TypeReference<HashMap<String, Object>>() {
});
Object obj = null;
obj = optionMap.get("appends");
if (obj != null) {
List<String> appendsList = (List<String>) obj;
for (String append : appendsList) {
stringBuffer.append(", ");
stringBuffer.append(append);
}
}
stringBuffer.append(" FROM ");
stringBuffer.append(elems.getKey());
obj = optionMap.get("where");
if (obj != null) {
String where = (String) obj;
stringBuffer.append(" WHERE ");
stringBuffer.append(where);
}
obj = optionMap.get("arrayColumns");
if (obj != null) {
arrayColumns = (List<String>) obj;
}
} else {
stringBuffer.append(" FROM ");
stringBuffer.append(elems.getKey());
}
queryInfo = new QueryInfo();
queryInfo.setQuery(stringBuffer.toString());
queryInfo.setExtend(extendMap);
mappingInfo = new MappingInfo();
mappingInfo.setParseDirection("col");
mappingInfo.setArrayColumns(arrayColumns);
item.setMappingInfo(mappingInfo);
item.setQueryInfo(queryInfo);
item.setKeys(keysList);
itemList.add(item);
} }
sensorConfig.setItems(itemList);
}
} }

View File

@ -1,7 +1,7 @@
package com.loafle.overflow.central.module.generator.service; package com.loafle.overflow.central.module.generator.service;
import com.loafle.overflow.central.commons.utils.StringConvertor; import com.loafle.overflow.central.commons.utils.StringConvertor;
import com.loafle.overflow.crawler.config.*; import com.loafle.overflow.core.type.PortType;
import com.loafle.overflow.model.auth.AuthCrawler; import com.loafle.overflow.model.auth.AuthCrawler;
import com.loafle.overflow.model.infra.Infra; import com.loafle.overflow.model.infra.Infra;
import com.loafle.overflow.model.infra.InfraService; import com.loafle.overflow.model.infra.InfraService;
@ -9,6 +9,11 @@ import com.loafle.overflow.model.meta.MetaSensorItemKey;
import com.loafle.overflow.model.meta.type.MetaCrawlerEnum; import com.loafle.overflow.model.meta.type.MetaCrawlerEnum;
import com.loafle.overflow.model.sensor.Sensor; import com.loafle.overflow.model.sensor.Sensor;
import com.loafle.overflow.model.sensor.SensorItem; import com.loafle.overflow.model.sensor.SensorItem;
import com.loafle.overflow.model.sensorconfig.Connection;
import com.loafle.overflow.model.sensorconfig.Crawler;
import com.loafle.overflow.model.sensorconfig.Schedule;
import com.loafle.overflow.model.sensorconfig.SensorConfig;
import com.loafle.overflow.model.sensorconfig.Target;
import com.loafle.overflow.service.central.auth.AuthCrawlerService; import com.loafle.overflow.service.central.auth.AuthCrawlerService;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
@ -42,8 +47,8 @@ public class InfraServiceGenerator {
public String process(Sensor dbSensor, List<SensorItem> sensorItems, Infra infra) throws Exception { public String process(Sensor dbSensor, List<SensorItem> sensorItems, Infra infra) throws Exception {
com.loafle.overflow.model.infra.InfraService infraService = (com.loafle.overflow.model.infra.InfraService)infra; com.loafle.overflow.model.infra.InfraService infraService = (com.loafle.overflow.model.infra.InfraService)infra;
Config config = new Config(); SensorConfig sensorConfig = new SensorConfig();
config.setId(String.valueOf(dbSensor.getId())); sensorConfig.setConfigID(String.valueOf(dbSensor.getId()));
Target target = this.createTarget(infraService, dbSensor); Target target = this.createTarget(infraService, dbSensor);
@ -52,26 +57,26 @@ public class InfraServiceGenerator {
} }
config.setTarget(target); sensorConfig.setTarget(target);
// FIXME: Interval // FIXME: Interval
Schedule schedule = new Schedule(); Schedule schedule = new Schedule();
schedule.setInterval("5"); schedule.setInterval("5");
config.setSchedule(schedule); sensorConfig.setSchedule(schedule);
Crawler crawler = this.generateUtil.getCrawler(dbSensor.getCrawler()); Crawler crawler = this.generateUtil.getCrawler(dbSensor.getCrawler());
config.setCrawler(crawler); sensorConfig.setCrawler(crawler);
Map<Integer, MetaSensorItemKey> keyMap = this.generateUtil.initMappingMap(dbSensor.getCrawler()); Map<Integer, MetaSensorItemKey> keyMap = this.generateUtil.initMappingMap(dbSensor.getCrawler());
if(dbSensor.getCrawler().getId() == MetaCrawlerEnum.MYSQL_CRAWLER.getValue()) { if(dbSensor.getCrawler().getId() == MetaCrawlerEnum.MYSQL_CRAWLER.getValue()) {
this.infraServiceMysqlGenerator.process(sensorItems, keyMap, dbSensor, config); this.infraServiceMysqlGenerator.process(sensorItems, keyMap, dbSensor, sensorConfig);
} else if (dbSensor.getCrawler().getId() == MetaCrawlerEnum.JMX_CRAWLER.getValue()) { } else if (dbSensor.getCrawler().getId() == MetaCrawlerEnum.JMX_CRAWLER.getValue()) {
this.infraServiceJMXGenerator.process(sensorItems, keyMap, dbSensor, config); this.infraServiceJMXGenerator.process(sensorItems, keyMap, dbSensor, sensorConfig);
} }
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(config); return objectMapper.writeValueAsString(sensorConfig);
} }
private Target createTarget(InfraService infraService, Sensor sensor) throws Exception { private Target createTarget(InfraService infraService, Sensor sensor) throws Exception {
@ -82,11 +87,11 @@ public class InfraServiceGenerator {
return null; return null;
} }
Target target = new com.loafle.overflow.crawler.config.Target(); Target target = new Target();
Connection connection = new Connection(); Connection connection = new Connection();
connection.setIp(StringConvertor.intToIp(infraService.getHost().getIp())); connection.setIp(StringConvertor.intToIp(infraService.getHost().getIp()));
connection.setPort(String.valueOf(infraService.getPort())); connection.setPort(infraService.getPort());
connection.setPortType(infraService.getPortType()); connection.setPortType(PortType.valueOf(infraService.getPortType()));
connection.setSsl(infraService.isTlsType()); connection.setSsl(infraService.isTlsType());
target.setConnection(connection); target.setConnection(connection);
@ -103,7 +108,7 @@ public class InfraServiceGenerator {
} else if (sensor.getCrawler().getId() == MetaCrawlerEnum.JMX_CRAWLER.getValue()) { } else if (sensor.getCrawler().getId() == MetaCrawlerEnum.JMX_CRAWLER.getValue()) {
auth.put("id", optionMap.get("ID")); // FIXME: Auth Info auth.put("id", optionMap.get("ID")); // FIXME: Auth Info
auth.put("pw", optionMap.get("PassWord")); // FIXME: Auth Info auth.put("pw", optionMap.get("PassWord")); // FIXME: Auth Info
connection.setPort("9840"); connection.setPort(9840);
} }
target.setAuth(auth); target.setAuth(auth);

View File

@ -1,9 +1,13 @@
package com.loafle.overflow.central.module.generator.service; package com.loafle.overflow.central.module.generator.service;
import com.loafle.overflow.crawler.config.*;
import com.loafle.overflow.model.meta.MetaSensorItemKey; import com.loafle.overflow.model.meta.MetaSensorItemKey;
import com.loafle.overflow.model.sensor.Sensor; import com.loafle.overflow.model.sensor.Sensor;
import com.loafle.overflow.model.sensor.SensorItem; import com.loafle.overflow.model.sensor.SensorItem;
import com.loafle.overflow.model.sensorconfig.Item;
import com.loafle.overflow.model.sensorconfig.Keys;
import com.loafle.overflow.model.sensorconfig.MappingInfo;
import com.loafle.overflow.model.sensorconfig.QueryInfo;
import com.loafle.overflow.model.sensorconfig.SensorConfig;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference; import org.codehaus.jackson.type.TypeReference;
@ -21,81 +25,82 @@ import java.util.Map;
@Service("InfraServiceJMXGenerator") @Service("InfraServiceJMXGenerator")
public class InfraServiceJMXGenerator { public class InfraServiceJMXGenerator {
@Autowired @Autowired
private GenerateUtil generateUtil; private GenerateUtil generateUtil;
public void process(List<SensorItem> sensorItems, Map<Integer, MetaSensorItemKey> keyMap, Sensor dbSensor, Config config) throws Exception { public void process(List<SensorItem> sensorItems, Map<Integer, MetaSensorItemKey> keyMap, Sensor dbSensor,
Map<String, List<MetaSensorItemKey>> metricMap = null; SensorConfig sensorConfig) throws Exception {
metricMap = this.generateUtil.sortItems(sensorItems, keyMap); Map<String, List<MetaSensorItemKey>> metricMap = null;
metricMap = this.generateUtil.sortItems(sensorItems, keyMap);
List<Item> itemList = new ArrayList<>(); List<Item> itemList = new ArrayList<>();
Item item = null; Item item = null;
QueryInfo queryInfo = null; QueryInfo queryInfo = null;
MappingInfo mappingInfo = null; MappingInfo mappingInfo = null;
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
Map<String, Object> extendMap = new HashMap<>(); Map<String, Object> extendMap = new HashMap<>();
List<Keys> keysList = null; List<Keys> keysList = null;
MetaSensorItemKey tempItemKey = null; MetaSensorItemKey tempItemKey = null;
Keys keys = null; Keys keys = null;
for (Map.Entry<String, List<MetaSensorItemKey>> elms : metricMap.entrySet()) { for (Map.Entry<String, List<MetaSensorItemKey>> elms : metricMap.entrySet()) {
keysList = new ArrayList<>(); keysList = new ArrayList<>();
item = new Item(); item = new Item();
buffer.setLength(0); buffer.setLength(0);
buffer.append(elms.getKey()); buffer.append(elms.getKey());
for (int idx = 0; idx < elms.getValue().size(); idx++) { for (int idx = 0; idx < elms.getValue().size(); idx++) {
tempItemKey = elms.getValue().get(idx); tempItemKey = elms.getValue().get(idx);
buffer.append(tempItemKey.getKey()); buffer.append(tempItemKey.getKey());
keys = new Keys(); keys = new Keys();
keys.setKey(tempItemKey.getKey()); keys.setKey(tempItemKey.getKey());
keys.setMetric(tempItemKey.getItem().getKey()); keys.setMetric(tempItemKey.getItem().getKey());
keysList.add(this.generateUtil.createKeys(tempItemKey)); keysList.add(this.generateUtil.createKeys(tempItemKey));
} }
String json = tempItemKey.getOption(); String json = tempItemKey.getOption();
List<String> aliases = null; List<String> aliases = null;
List<String> arrayCol = null; List<String> arrayCol = null;
if (json != null && json.length() > 0) { if (json != null && json.length() > 0) {
HashMap<String, Object> optionMap; HashMap<String, Object> optionMap;
optionMap = new ObjectMapper().readValue(json, new TypeReference<HashMap<String,Object>>() {}); optionMap = new ObjectMapper().readValue(json, new TypeReference<HashMap<String, Object>>() {
});
Object obj = null; Object obj = null;
obj = optionMap.get("aliases"); obj = optionMap.get("aliases");
if (obj != null) { if (obj != null) {
aliases = (List<String>)obj; aliases = (List<String>) obj;
}
obj = optionMap.get("arrayColumns");
if (obj != null) {
arrayCol = (List<String>)obj;
}
}
queryInfo = new QueryInfo();
queryInfo.setQuery(tempItemKey.getFroms());
queryInfo.setExtend(extendMap);
extendMap.put("aliases", aliases);
mappingInfo = new MappingInfo();
mappingInfo.setArrayColumns(arrayCol);
item.setMappingInfo(mappingInfo);
item.setQueryInfo(queryInfo);
item.setKeys(keysList);
itemList.add(item);
} }
config.setItems(itemList);
}
obj = optionMap.get("arrayColumns");
if (obj != null) {
arrayCol = (List<String>) obj;
}
}
queryInfo = new QueryInfo();
queryInfo.setQuery(tempItemKey.getFroms());
queryInfo.setExtend(extendMap);
extendMap.put("aliases", aliases);
mappingInfo = new MappingInfo();
mappingInfo.setArrayColumns(arrayCol);
item.setMappingInfo(mappingInfo);
item.setQueryInfo(queryInfo);
item.setKeys(keysList);
itemList.add(item);
}
sensorConfig.setItems(itemList);
}
} }

View File

@ -1,10 +1,14 @@
package com.loafle.overflow.central.module.generator.service; package com.loafle.overflow.central.module.generator.service;
import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.crawler.config.*;
import com.loafle.overflow.model.meta.MetaSensorItemKey; import com.loafle.overflow.model.meta.MetaSensorItemKey;
import com.loafle.overflow.model.sensor.Sensor; import com.loafle.overflow.model.sensor.Sensor;
import com.loafle.overflow.model.sensor.SensorItem; import com.loafle.overflow.model.sensor.SensorItem;
import com.loafle.overflow.model.sensorconfig.Item;
import com.loafle.overflow.model.sensorconfig.Keys;
import com.loafle.overflow.model.sensorconfig.MappingInfo;
import com.loafle.overflow.model.sensorconfig.QueryInfo;
import com.loafle.overflow.model.sensorconfig.SensorConfig;
import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.JsonMappingException;
@ -25,132 +29,130 @@ import java.util.Map;
@Service("InfraServiceMysqlGenerator") @Service("InfraServiceMysqlGenerator")
public class InfraServiceMysqlGenerator { public class InfraServiceMysqlGenerator {
@Autowired @Autowired
private GenerateUtil generateUtil; private GenerateUtil generateUtil;
public void process(List<SensorItem> sensorItems, Map<Integer, MetaSensorItemKey> keyMap, Sensor dbSensor, Config config) throws Exception { public void process(List<SensorItem> sensorItems, Map<Integer, MetaSensorItemKey> keyMap, Sensor dbSensor,
SensorConfig sensorConfig) throws Exception {
// List<Keys> keysList = new ArrayList<>();
// for(SensorItem sItem : sensorItems) {
// keys = new Keys();
// keys.setMetric(sItem.getItem().getKey());
// keys.setKey(KeyMap.get(sItem.getItem().getId()).getKey());
// keysList.add(keys);
// }
// item.setKeys(keysList);
Map<String, List<MetaSensorItemKey>> metricMap = null;
metricMap = this.generateUtil.sortItems(sensorItems, keyMap);
// List<Keys> keysList = new ArrayList<>(); List<Item> itemList = new ArrayList<>();
// for(SensorItem sItem : sensorItems) {
// keys = new Keys();
// keys.setMetric(sItem.getItem().getKey());
// keys.setKey(KeyMap.get(sItem.getItem().getId()).getKey());
// keysList.add(keys);
// }
// item.setKeys(keysList);
Map<String, List<MetaSensorItemKey>> metricMap = null;
metricMap = this.generateUtil.sortItems(sensorItems, keyMap);
List<Item> itemList = new ArrayList<>(); QueryInfo queryInfo = null;
MappingInfo mappingInfo = null;
QueryInfo queryInfo = null; List<Keys> keysList = null;
MappingInfo mappingInfo = null; MetaSensorItemKey tempItemKey = null;
Keys keys = null;
Item item = null;
StringBuffer stringBuffer = new StringBuffer();
for (Map.Entry<String, List<MetaSensorItemKey>> elems : metricMap.entrySet()) {
List<Keys> keysList = null; keysList = new ArrayList<>();
MetaSensorItemKey tempItemKey = null; item = new Item();
Keys keys = null; stringBuffer.setLength(0);
Item item = null;
StringBuffer stringBuffer = new StringBuffer();
for(Map.Entry<String, List<MetaSensorItemKey>> elems : metricMap.entrySet()) {
keysList = new ArrayList<>(); stringBuffer.append(elems.getKey());
item = new Item();
stringBuffer.setLength(0);
stringBuffer.append(elems.getKey()); stringBuffer.append(" where ");
for (int indexI = 0; indexI < elems.getValue().size(); ++indexI) {
tempItemKey = elems.getValue().get(indexI);
stringBuffer.append(" where "); stringBuffer.append("variable_name = '");
for(int indexI = 0; indexI < elems.getValue().size(); ++indexI) { stringBuffer.append(tempItemKey.getKey());
tempItemKey = elems.getValue().get(indexI); stringBuffer.append("'");
stringBuffer.append("variable_name = '"); if (indexI + 1 < elems.getValue().size()) {
stringBuffer.append(tempItemKey.getKey()); stringBuffer.append(" or ");
stringBuffer.append("'"); }
keysList.add(this.generateUtil.createKeys(tempItemKey));
}
if(indexI + 1 < elems.getValue().size()) { List<String> keyColumns = null;
stringBuffer.append(" or "); String valueColumn = null;
}
keysList.add(this.generateUtil.createKeys(tempItemKey));
}
String json = tempItemKey.getOption();
if (json != null && json.length() > 0) {
HashMap<String, Object> optionMap;
optionMap = new ObjectMapper().readValue(json, new TypeReference<HashMap<String, Object>>() {
});
Object obj = null;
List<String> keyColumns = null; obj = optionMap.get("valueColumn");
String valueColumn = null; if (obj != null) {
valueColumn = (String) obj;
String json = tempItemKey.getOption();
if(json != null && json.length() > 0) {
HashMap<String,Object> optionMap;
optionMap = new ObjectMapper().readValue(json, new TypeReference<HashMap<String,Object>>() {});
Object obj = null;
obj = optionMap.get("valueColumn");
if(obj != null) {
valueColumn = (String)obj;
}
obj = optionMap.get("keyColumns");
if(obj != null) {
keyColumns = (List<String>)obj;
}
}
queryInfo = new QueryInfo();
queryInfo.setQuery(stringBuffer.toString());
mappingInfo = new MappingInfo();
mappingInfo.setParseDirection("row");
mappingInfo.setValueColumn(valueColumn);
mappingInfo.setKeyColumns(keyColumns);
item.setMappingInfo(mappingInfo);
item.setQueryInfo(queryInfo);
item.setKeys(keysList);
itemList.add(item);
} }
config.setItems(itemList); obj = optionMap.get("keyColumns");
if (obj != null) {
keyColumns = (List<String>) obj;
}
}
queryInfo = new QueryInfo();
queryInfo.setQuery(stringBuffer.toString());
mappingInfo = new MappingInfo();
mappingInfo.setParseDirection("row");
mappingInfo.setValueColumn(valueColumn);
mappingInfo.setKeyColumns(keyColumns);
item.setMappingInfo(mappingInfo);
item.setQueryInfo(queryInfo);
item.setKeys(keysList);
itemList.add(item);
// ObjectMapper objectMapper = new ObjectMapper();
//
// return objectMapper.writeValueAsString(config);
} }
// public void setQueryAndMapping(MetaCrawler metaCrawler, List<Keys> keysList, QueryInfo queryInfo, MappingInfo mappingInfo) { sensorConfig.setItems(itemList);
//
// switch (metaCrawler.getId()) { // ObjectMapper objectMapper = new ObjectMapper();
// case 11: // mysql //
// { // return objectMapper.writeValueAsString(config);
// String query = "show status where "; }
//// queryInfo.setQuery("show status where ");
// // public void setQueryAndMapping(MetaCrawler metaCrawler, List<Keys> keysList, QueryInfo queryInfo, MappingInfo mappingInfo) {
// Keys keys = null; //
// for(int indexI = 0 ; indexI < keysList.size(); ++indexI) { // switch (metaCrawler.getId()) {
// keys = keysList.get(indexI); // case 11: // mysql
// query += "variable_name = '"; // {
// query += keys.getKey(); // String query = "show status where ";
// query += "'"; //// queryInfo.setQuery("show status where ");
// if(indexI + 1 < keysList.size()) { //
// query += " or "; // Keys keys = null;
// } // for(int indexI = 0 ; indexI < keysList.size(); ++indexI) {
// } // keys = keysList.get(indexI);
// // query += "variable_name = '";
// queryInfo.setQuery(query); // query += keys.getKey();
// // query += "'";
// mappingInfo.setParseDirection("row"); // if(indexI + 1 < keysList.size()) {
// mappingInfo.setValueColumn("Value"); // query += " or ";
// // }
// List<String> keyColumns = new ArrayList<>(); // }
// keyColumns.add("Variable_name"); //
// // queryInfo.setQuery(query);
// mappingInfo.setKeyColumns(keyColumns); //
// } // mappingInfo.setParseDirection("row");
// break; // mappingInfo.setValueColumn("Value");
// } //
// // List<String> keyColumns = new ArrayList<>();
// } // keyColumns.add("Variable_name");
//
// mappingInfo.setKeyColumns(keyColumns);
// }
// break;
// }
//
// }
} }