feat(运维监控模块):新增获取集群状态数据接口

调用Prometheus接口查询Pod运行状态,并将相关状态翻译为中文后返回
This commit is contained in:
OTTO 2025-05-21 19:50:13 +08:00
parent 39d43e3d68
commit ad405c912e
6 changed files with 41 additions and 3 deletions

View File

@ -15,6 +15,8 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
@RestController
@RequestMapping("dashboard")
@Api(tags = "微服务大屏")

View File

@ -7,6 +7,7 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.HashMap;
import java.util.List;
@ -43,6 +44,9 @@ public class K8sClusterStatus {
@ApiModelProperty(value = "今日微服务重启次数")
private Integer microserviceRestartCountToday;
@ApiModelProperty(value = "微服务Pod运行状态Key代表状态Value代表数量")
private HashMap<String, Integer> podStatus;
public void setNodeCount(PrometheusResVo prometheusResVo) {
this.nodeCount = Integer.parseInt(getPromStringValue(prometheusResVo));
}
@ -83,6 +87,30 @@ public class K8sClusterStatus {
this.microserviceRestartCountToday = Integer.parseInt(getPromStringValue(prometheusResVo));
}
public void setPodStatus(PrometheusResVo prometheusResVo) {
HashMap<String, Integer> podStatus = new HashMap<>();
if (prometheusResVo != null && "success".equals(prometheusResVo.getStatus())) {
List<PrometheusResultVo> resultList = prometheusResVo.getData().getResult();
for (PrometheusResultVo result : resultList) {
Integer value=Integer.parseInt(result.getValue()[1]);
switch (result.getMetric().getString("phase")){
case "Running":
podStatus.put("运行中", value);
break;
case "Pending":
podStatus.put("等待中", value);
break;
case "Succeeded":
podStatus.put("运行已完成", value);
break;
case "Failed":
podStatus.put("运行失败", value);
}
}
}
this.podStatus = podStatus;
}
private String getPromStringValue(PrometheusResVo prometheusResVo) {
String value = "";
if (prometheusResVo != null && "success".equals(prometheusResVo.getStatus())) {

View File

@ -4,6 +4,7 @@ import com.microservices.mon.k8s.domain.K8sClusterStatus;
import com.microservices.mon.k8s.domain.K8sClusterNode;
import com.microservices.mon.k8s.domain.K8sNamespace;
import java.util.HashMap;
import java.util.List;
public interface IK8sDashboardService {

View File

@ -18,6 +18,7 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

View File

@ -103,8 +103,11 @@ public class MonCommonAsyncServiceImpl extends CommonService implements IMonComm
List<K8sNamespace> k8sNamespaceList = k8sDashboardService.getNamespaceDetailList();
k8sClusterStatus.setMicroserviceApplicationCount(k8sNamespaceList);
List<String> namespaceNameList = k8sNamespaceList.stream().map(K8sNamespace::getName).collect(Collectors.toList());
JSONObject microserviceRestartCountTodayJson = remoteGatewayService.getPrometheusQueryRes(PromQLConstant.MICROSERVICE_RESTART_COUNT_TODAY(namespaceNameList), SecurityContextHolder.getUserKey());
JSONObject microserviceRestartCountTodayJson = remoteGatewayService.getPrometheusQueryRes(PromQLConstant.MICROSERVICE_RESTART_COUNT_TODAY(cluster_id,namespaceNameList), SecurityContextHolder.getUserKey());
k8sClusterStatus.setMicroserviceRestartCountToday(microserviceRestartCountTodayJson.toJavaObject(PrometheusResVo.class));
JSONObject podStatusJson = remoteGatewayService.getPrometheusQueryRes(PromQLConstant.POD_STATUS(cluster_id,namespaceNameList), SecurityContextHolder.getUserKey());
k8sClusterStatus.setPodStatus(podStatusJson.toJavaObject(PrometheusResVo.class));
} finally {
countDownLatch.countDown();
}

View File

@ -30,7 +30,10 @@ public class PromQLConstant {
public static final String AVERAGE_REQUEST_PROCESSING_TIME_TODAY = "sum(rate(nginx_http_response_time_seconds_sum[1d]))*1000";
public static final String REQUEST_SUCCESS_RATE_TODAY = "(sum(nginx_http_response_count_total{status=~\"2.*|3.*\"})/sum(nginx_http_response_count_total))*100";
public static String MICROSERVICE_RESTART_COUNT_TODAY(List<String> namespaceNameList) {
return String.format("sum(changes(kube_pod_container_status_restarts_total{namespace=~\"%s\"}[1d]))", String.join("|", namespaceNameList));
public static String MICROSERVICE_RESTART_COUNT_TODAY(String clusterId,List<String> namespaceNameList) {
return String.format("sum(changes(kube_pod_container_status_restarts_total{cluster=~\"%s\",namespace=~\"%s\"}[1d]))",clusterId, String.join("|", namespaceNameList));
}
public static String POD_STATUS(String clusterId,List<String> namespaceNameList) {
return String.format("sum(kube_pod_status_phase{cluster=~\"%s\",namespace=~\"%s\", phase!=\"Unknown\"}) by (phase)",clusterId, String.join("|", namespaceNameList));
}
}