wuchunfu commented on code in PR #83:
URL: https://github.com/apache/bigtop-manager/pull/83#discussion_r1798712537


##########
bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/proxy/PrometheusProxy.java:
##########
@@ -81,4 +82,262 @@ public JsonNode queryAgentsHealthyStatus() {
         }
         return agentsHealthyStatus;
     }
+    private JsonNode queryAgents(){
+        JsonNode result = query("agent_host_monitoring_cpu");
+        ObjectMapper objectMapper = new ObjectMapper();
+        if (result != null) {
+            JsonNode agentCpus = result.get("data").get("result");
+            if(agentCpus.isArray() && !agentCpus.isEmpty()){
+                Set<String> iPv4addrSet = new HashSet<>();
+                for(JsonNode agent:agentCpus){
+                    
iPv4addrSet.add(agent.get("metric").get("iPv4addr").asText());
+                }
+                ArrayNode iPv4addrArray = objectMapper.createArrayNode();
+                for (String value : iPv4addrSet.toArray(new String[0])) {
+                    iPv4addrArray.add(value);
+                }
+                return 
objectMapper.createObjectNode().set("iPv4addr",iPv4addrArray);// IPV4地址
+            }
+        }
+        return objectMapper.createObjectNode();
+    }
+    public JsonNode queryAgentsInfo() {
+        ObjectMapper objectMapper = new ObjectMapper();
+        ArrayNode agentsInfo = objectMapper.createArrayNode();
+        JsonNode agents = queryAgents().get("iPv4addr"); // 获取全主机
+
+        for(JsonNode agent:agents){
+            ObjectNode temp = objectMapper.createObjectNode();
+            JsonNode cpuResult = queryAgentCpu(agent.asText());
+            JsonNode memResult = queryAgentMemory(agent.asText());
+            JsonNode diskResult = queryAgentDisk(agent.asText());
+            // hostInfo
+            temp.put("hostname",cpuResult.get("hostname").asText());
+            temp.put("iPv4addr", cpuResult.get("iPv4addr").asText());
+            //temp.put("iPv6addr", cpuResult.get("iPv6addr").asText());
+            temp.put("cpuInfo",cpuResult.get("cpuInfo").asText().strip());
+            temp.put("time", cpuResult.get("time").asText());
+            temp.put("os", cpuResult.get("os").asText());
+            temp.put("architecture", cpuResult.get("architecture").asText());
+            temp.put("physical_cores", 
cpuResult.get("physical_cores").asText());
+            // MEM
+            temp.put("memIdle",memResult.get("memIdle").asLong());
+            temp.put("memTotal",memResult.get("memTotal").asLong());
+            // DISK
+            long totalSpace = 0;
+            long freeSpace = 0;
+            for(JsonNode disk:diskResult.get("diskInfo")){
+                if(Objects.equals(disk.get("diskUsage").asText(), 
"diskTotalSpace")){
+                    totalSpace += disk.get("diskValue").asLong();
+                }else if(Objects.equals(disk.get("diskUsage").asText(), 
"diskFreeSpace")){
+                    freeSpace += disk.get("diskValue").asLong();
+                }
+            }
+            temp.put("diskFreeSpace",freeSpace);
+            temp.put("diskTotalSpace",totalSpace);
+            agentsInfo.add(temp);
+        }
+
+        return agentsInfo;
+    }
+
+    public JsonNode queryAgentsInstStatus() {
+        ObjectMapper objectMapper = new ObjectMapper();
+        ArrayNode agentsInfo = objectMapper.createArrayNode();
+        JsonNode agents = queryAgents().get("iPv4addr"); // get all host
+
+        for(JsonNode agent:agents){
+            JsonNode cpuResult = queryAgentCpu(agent.asText());
+            JsonNode memResult = queryAgentMemory(agent.asText());
+            JsonNode diskResult = queryAgentDisk(agent.asText());
+            JsonNode diskIOResult = queryAgentDiskIO(agent.asText());
+            ObjectNode temp = objectMapper.createObjectNode();
+
+            // hostInfo
+            temp.put("hostname",cpuResult.get("hostname").asText());
+            temp.put("iPv4addr", cpuResult.get("iPv4addr").asText());
+            temp.put("cpuInfo",cpuResult.get("cpuInfo").asText().strip());
+            temp.put("time", cpuResult.get("time").asText());
+            temp.put("cpuLoadAvgMin_1", 
cpuResult.get("cpuLoadAvgMin_1").asDouble());
+            temp.put("cpuLoadAvgMin_5", 
cpuResult.get("cpuLoadAvgMin_5").asDouble());
+            temp.put("cpuLoadAvgMin_15", 
cpuResult.get("cpuLoadAvgMin_15").asDouble());
+            temp.put("cpuUsage", cpuResult.get("cpuUsage").asDouble());
+            
temp.put("fileTotalDescriptor",cpuResult.get("fileTotalDescriptor").asLong());
+            
temp.put("fileOpenDescriptor",cpuResult.get("fileOpenDescriptor").asLong());
+            // MEM
+            temp.put("memIdle",memResult.get("memIdle").asLong());
+            temp.put("memTotal",memResult.get("memTotal").asLong());
+            // DISK
+            long totalSpace = 0;
+            long freeSpace = 0;
+            long totalFileHandle = 0;
+            long freeFileHandle = 0;
+            for(JsonNode disk:diskResult.get("diskInfo")){
+                if(Objects.equals(disk.get("diskUsage").asText(), 
"diskTotalSpace")){
+                    totalSpace += disk.get("diskValue").asLong();
+                }else if(Objects.equals(disk.get("diskUsage").asText(), 
"diskFreeSpace")){
+                    freeSpace += disk.get("diskValue").asLong();
+                }else if (Objects.equals(disk.get("diskUsage").asText(), 
"diskTotalFileHandle")){
+                    totalFileHandle += disk.get("diskValue").asLong();
+                }else if (Objects.equals(disk.get("diskUsage").asText(), 
"diskFreeFileHandle")){
+                    freeFileHandle += disk.get("diskValue").asLong();
+                }
+            }
+            temp.put("diskFreeSpace",freeSpace);
+            temp.put("diskTotalSpace",totalSpace);
+            temp.put("diskFreeFileHandle",freeFileHandle);
+            temp.put("diskTotalFileHandle",totalFileHandle);
+            // DISK IO
+            temp.set("diskIO",diskIOResult.get("diskIO"));
+            agentsInfo.add(temp);
+        }
+
+        return agentsInfo;
+    }
+
+    public JsonNode query(String params){
+        Mono<JsonNode> body = webClient
+                .post()
+                .uri("/api/v1/query")
+                .contentType(MediaType.APPLICATION_FORM_URLENCODED)
+                .body(BodyInserters.fromFormData("query", params)
+                        .with("timeout", "10"))
+                .retrieve()
+                .bodyToMono(JsonNode.class);
+        JsonNode result = body.block();
+        if (result == null
+                || result.isEmpty()
+                || !"success".equals(result.get("status").asText("failure"))) {
+            return null;
+        }
+        return result;
+    }
+
+    public JsonNode queryAgentCpu(String iPv4addr){
+        String params = 
String.format("agent_host_monitoring_cpu{iPv4addr=\"%s\"}", iPv4addr);
+        JsonNode result = query(params);
+        ObjectMapper objectMapper = new ObjectMapper();
+        if (result != null) {
+            JsonNode agentCpus = result.get("data").get("result");
+            if(agentCpus.isArray() && !agentCpus.isEmpty()){
+                JsonNode agentCpuMetric = agentCpus.get(0).get("metric");
+                JsonNode agentCpuValue = agentCpus.get(0).get("value");
+                ObjectNode agentInfo = objectMapper.createObjectNode();
+                agentInfo.put("hostname", 
agentCpuMetric.get("hostname").asText());
+                
agentInfo.put("cpuInfo",agentCpuMetric.get("cpu_info").asText());
+                agentInfo.put("iPv4addr", 
agentCpuMetric.get("iPv4addr").asText());
+                //temp.put("iPv6addr", 
agentCpuMetric.get("iPv6addr").asText());
+                agentInfo.put("os", agentCpuMetric.get("os").asText());
+                agentInfo.put("architecture", 
agentCpuMetric.get("arch").asText());
+                agentInfo.put("physical_cores", 
agentCpuMetric.get("physical_cores").asText());
+                
agentInfo.put("fileOpenDescriptor",agentCpuMetric.get("fileOpenDescriptor").asLong());
+                
agentInfo.put("fileTotalDescriptor",agentCpuMetric.get("fileTotalDescriptor").asLong());
+                LocalDateTime instant = 
Instant.ofEpochSecond(agentCpuValue.get(0).asLong())
+                        .atZone(ZoneId.systemDefault())
+                        .toLocalDateTime();
+                agentInfo.put("time", instant.toString());
+                for (JsonNode agent : agentCpus) {
+                    
agentInfo.put(agent.get("metric").get("cpuUsage").asText(), 
agent.get("value").get(1).asDouble()); // cpu 指标值
+                }

Review Comment:
   @Cat-Drink Please change the comment information to English, thanks



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to