diff --git a/src/main/java/com/loafle/overflow/module/sensor/service/SensorService.java b/src/main/java/com/loafle/overflow/module/sensor/service/SensorService.java index fd5971a..b3c3c98 100644 --- a/src/main/java/com/loafle/overflow/module/sensor/service/SensorService.java +++ b/src/main/java/com/loafle/overflow/module/sensor/service/SensorService.java @@ -19,6 +19,7 @@ import com.loafle.overflow.module.sensor.model.Sensor; import com.loafle.overflow.module.sensor.model.SensorItem; import com.loafle.overflow.module.target.model.Target; import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; @@ -281,52 +282,92 @@ public class SensorService { } private void processWMI(List sensorItems, Map keyMap, Sensor dbSensor, Config config) throws IOException { - List keysList = new ArrayList<>(); - - Map> metricMap = new HashMap<>(); + Map> metricMap = new HashMap<>(); MetaSensorItemKey itemKey = null; - Keys keys = null; + for(SensorItem sItem : sensorItems) { - itemKey = keyMap.get(sItem.getItem().getId()); - if(metricMap.containsKey(itemKey.getFroms()) == false) { metricMap.put(itemKey.getFroms(), new ArrayList<>()); } - keys = new Keys(); - keys.setKey(itemKey.getKey()); - keys.setMetric(itemKey.getItem().getKey()); - - metricMap.get(itemKey.getFroms()).add(keys); + metricMap.get(itemKey.getFroms()).add(itemKey); } List itemList = new ArrayList<>(); Item item = null; QueryInfo queryInfo = null; MappingInfo mappingInfo = null; - String query = null; + StringBuffer stringBuffer = new StringBuffer(); Map extendMap = new HashMap<>(); extendMap.put("nameSpace", "root/cimv2"); extendMap.put("wmicPath", "/home/snoop/temp/wmic"); - for(Map.Entry> elems : metricMap.entrySet()) { + + List keysList = null; + MetaSensorItemKey tempItemKey = null; + Keys keys = null; + for(Map.Entry> elems : metricMap.entrySet()) { + keysList = new ArrayList<>(); item = new Item(); stringBuffer.setLength(0); stringBuffer.append("SELECT "); for(int indexI = 0; indexI < elems.getValue().size(); ++indexI) { - stringBuffer.append(elems.getValue().get(indexI).getKey()); + + 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(keys); } - stringBuffer.append(" FROM "); - stringBuffer.append(elems.getKey()); + + List arrayColumns = null; + + String json = tempItemKey.getOption(); + if(json != null && json.length() > 0) { + HashMap optionMap; + optionMap = new ObjectMapper().readValue(json, new TypeReference>() {}); + + Object obj = null; + obj = optionMap.get("appends"); + if(obj != null) { + List appendsList = (List)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)obj; + } + + } + else { + stringBuffer.append(" FROM "); + stringBuffer.append(elems.getKey()); + } queryInfo = new QueryInfo(); queryInfo.setQuery(stringBuffer.toString()); @@ -334,13 +375,11 @@ public class SensorService { mappingInfo = new MappingInfo(); mappingInfo.setParseDirection("col"); - List arrayColumns = new ArrayList<>(); - arrayColumns.add("Caption"); mappingInfo.setArrayColumns(arrayColumns); item.setMappingInfo(mappingInfo); item.setQueryInfo(queryInfo); - item.setKeys(elems.getValue()); + item.setKeys(keysList); itemList.add(item);