From 70852d88b8b04aa9852e7d9e9f092a48f4c1dc77 Mon Sep 17 00:00:00 2001 From: duwenyuan <15600000461@163.com> Date: Wed, 8 Jul 2026 15:20:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/HostMonitorServiceImpl.java | 325 +++++++++++------- 1 file changed, 195 insertions(+), 130 deletions(-) diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/monitor/service/impl/HostMonitorServiceImpl.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/monitor/service/impl/HostMonitorServiceImpl.java index beecfa5..6bcdbbe 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/monitor/service/impl/HostMonitorServiceImpl.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/monitor/service/impl/HostMonitorServiceImpl.java @@ -33,82 +33,94 @@ public class HostMonitorServiceImpl implements HostMonitorService { * 获取CPU信息 */ @Override - public Map getCpuInfo(String ip,String conditions) { - Map result = new HashMap<>(); + public Map getCpuInfo(String ip, String conditions) { + Map result = new HashMap<>(); try { //Prometheus 服务器地址 String url = serverProperties.getServerUrl(); //目标主机实例(node-exporter 的地址) String instance = this.getInstance(ip); //查询CPU利用率 - PrometheusHostQueryTypeEnum queryTypeEnum = PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions); + PrometheusHostQueryTypeEnum queryTypeEnum = + PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions); String exprTime = queryTypeEnum.getExprTime(); - String cpuQuery = "100 * (1 - avg(rate(node_cpu_seconds_total{mode=\"idle\", instance=\""+instance+"\"}["+exprTime+"])))"; + String cpuQuery = + "100 * (1 - avg(rate(node_cpu_seconds_total{mode=\"idle\", instance=\"" + + instance + "\"}[" + exprTime + "])))"; PrometheusResponse response = webClient.get() - .uri(buildUri(url,cpuQuery)) + .uri(buildUri(url, cpuQuery)) .retrieve() .bodyToMono(PrometheusResponse.class) .block(); - if(Objects.nonNull(response) && + if (Objects.nonNull(response) && Objects.nonNull(response.getData()) && CollUtil.isNotEmpty(response.getData().getResult()) ) { - PrometheusResponse.Result cpuInfo = response.getData().getResult().get(0); - if(CollUtil.isNotEmpty(cpuInfo.getValue())) { - Date date = new Date(cpuInfo.getValue().get(0).longValue()*1000); - Double useRate = BigDecimal.valueOf(cpuInfo.getValue().get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue(); + PrometheusResponse.Result cpuInfo = response.getData().getResult().get(0); + if (CollUtil.isNotEmpty(cpuInfo.getValue())) { + Date date = new Date(cpuInfo.getValue().get(0).longValue() * 1000); + Double useRate = BigDecimal.valueOf(cpuInfo.getValue().get(1)) + .setScale(2, RoundingMode.HALF_UP).doubleValue(); result.put("date", DateUtil.format(date, "MM/dd HH:mm:ss")); result.put("usageRate", useRate); } } - }catch (Exception e){ - log.error("获取CPU信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage()); + } catch (Exception e) { + log.error( + "获取CPU信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}", + e.getMessage()); } - return result; + return result; } /** * 获取CPU信息列表 */ @Override - public List> getCpuInfoList(String ip,String conditions) { - List> result = new ArrayList<>(); + public List> getCpuInfoList(String ip, String conditions) { + List> result = new ArrayList<>(); try { //Prometheus 服务器地址 String url = serverProperties.getServerUrl(); //目标主机实例(node-exporter 的地址) String instance = this.getInstance(ip); //查询CPU利用率 - PrometheusHostQueryTypeEnum queryTypeEnum = PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions); + PrometheusHostQueryTypeEnum queryTypeEnum = + PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions); long end = Instant.now().getEpochSecond(); long start = end - queryTypeEnum.getLastSecond(); String step = queryTypeEnum.getStep(); String exprTime = queryTypeEnum.getExprTime(); - String cpuQuery = "100 * (1 - avg(rate(node_cpu_seconds_total{mode=\"idle\", instance=\""+instance+"\"}["+exprTime+"])))"; + String cpuQuery = + "100 * (1 - avg(rate(node_cpu_seconds_total{mode=\"idle\", instance=\"" + + instance + "\"}[" + exprTime + "])))"; PrometheusResponse response = webClient.get() - .uri(buildUri(url,cpuQuery,start,end,step)) + .uri(buildUri(url, cpuQuery, start, end, step)) .retrieve() .bodyToMono(PrometheusResponse.class) .block(); - if(Objects.nonNull(response) && + if (Objects.nonNull(response) && Objects.nonNull(response.getData()) && CollUtil.isNotEmpty(response.getData().getResult()) ) { - PrometheusResponse.Result cpuInfoList = response.getData().getResult().get(0); - if(CollUtil.isNotEmpty(cpuInfoList.getValues())) { + PrometheusResponse.Result cpuInfoList = response.getData().getResult().get(0); + if (CollUtil.isNotEmpty(cpuInfoList.getValues())) { List> pointDatas = cpuInfoList.getValues(); - for(List pointData : pointDatas) { - Map pointDataMap = new HashMap<>(); - Date date = new Date(pointData.get(0).longValue()*1000); - Double useRate = BigDecimal.valueOf(pointData.get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue(); + for (List pointData : pointDatas) { + Map pointDataMap = new HashMap<>(); + Date date = new Date(pointData.get(0).longValue() * 1000); + Double useRate = BigDecimal.valueOf(pointData.get(1)) + .setScale(2, RoundingMode.HALF_UP).doubleValue(); pointDataMap.put("date", DateUtil.format(date, "MM/dd HH:mm:ss")); pointDataMap.put("usageRate", useRate); result.add(pointDataMap); } } } - }catch (Exception e){ - log.error("获取CPU信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage()); + } catch (Exception e) { + log.error( + "获取CPU信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}", + e.getMessage()); } return result; } @@ -117,42 +129,45 @@ public class HostMonitorServiceImpl implements HostMonitorService { * 获取CPU核心数 */ @Override - public Map getCpuCoreInfo(String ip) { - Map result = new HashMap<>(); + public Map getCpuCoreInfo(String ip) { + Map result = new HashMap<>(); try { //Prometheus 服务器地址 String url = serverProperties.getServerUrl(); //目标主机实例(node-exporter 的地址) String instance = this.getInstance(ip); //查询CPU核数 - String cpuCoreQuery = "count(count by (cpu) (node_cpu_seconds_total{instance=\"" + instance + "\"}))"; + String cpuCoreQuery = + "count(count by (cpu) (node_cpu_seconds_total{instance=\"" + instance + "\"}))"; PrometheusResponse response = webClient.get() - .uri(buildUri(url,cpuCoreQuery)) + .uri(buildUri(url, cpuCoreQuery)) .retrieve() .bodyToMono(PrometheusResponse.class) .block(); - if(Objects.nonNull(response) && + if (Objects.nonNull(response) && Objects.nonNull(response.getData()) && CollUtil.isNotEmpty(response.getData().getResult()) ) { - PrometheusResponse.Result cpuInfo = response.getData().getResult().get(0); - if(CollUtil.isNotEmpty(cpuInfo.getValue())) { + PrometheusResponse.Result cpuInfo = response.getData().getResult().get(0); + if (CollUtil.isNotEmpty(cpuInfo.getValue())) { Integer cpuCores = cpuInfo.getValue().get(1).intValue(); result.put("cpuCores", cpuCores); } } - }catch (Exception e){ - log.error("获取CPU核心数错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage()); + } catch (Exception e) { + log.error( + "获取CPU核心数错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}", + e.getMessage()); } - return result; + return result; } /** * 获取内存信息 */ @Override - public Map getMemoryInfo(String ip) { - Map result = new HashMap<>(); + public Map getMemoryInfo(String ip) { + Map result = new HashMap<>(); try { //Prometheus 服务器地址 String url = serverProperties.getServerUrl(); @@ -193,28 +208,33 @@ public class HostMonitorServiceImpl implements HostMonitorService { // } // } //使用率 - String usageRateQuery = "(1 - (node_memory_MemAvailable_bytes{instance=\""+instance+"\"} / node_memory_MemTotal_bytes{instance=\""+instance+"\"})) * 100"; + String usageRateQuery = "(1 - (node_memory_MemAvailable_bytes{instance=\"" + instance + + "\"} / node_memory_MemTotal_bytes{instance=\"" + instance + "\"})) * 100"; PrometheusResponse usageRateResponse = webClient.get() - .uri(buildUri(url,usageRateQuery)) + .uri(buildUri(url, usageRateQuery)) .retrieve() .bodyToMono(PrometheusResponse.class) .block(); - if(Objects.nonNull(usageRateResponse) && + if (Objects.nonNull(usageRateResponse) && Objects.nonNull(usageRateResponse.getData()) && CollUtil.isNotEmpty(usageRateResponse.getData().getResult()) ) { - PrometheusResponse.Result usageRateInfo = usageRateResponse.getData().getResult().get(0); - if(CollUtil.isNotEmpty(usageRateInfo.getValue())) { - Date date = new Date(usageRateInfo.getValue().get(0).longValue()*1000); - Double usageRate = BigDecimal.valueOf(usageRateInfo.getValue().get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue(); + PrometheusResponse.Result usageRateInfo = + usageRateResponse.getData().getResult().get(0); + if (CollUtil.isNotEmpty(usageRateInfo.getValue())) { + Date date = new Date(usageRateInfo.getValue().get(0).longValue() * 1000); + Double usageRate = BigDecimal.valueOf(usageRateInfo.getValue().get(1)) + .setScale(2, RoundingMode.HALF_UP).doubleValue(); result.put("date", DateUtil.format(date, "MM/dd HH:mm:ss")); result.put("usageRate", usageRate); } } - }catch (Exception e){ - log.error("获取内存信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage()); + } catch (Exception e) { + log.error( + "获取内存信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}", + e.getMessage()); } - return result; + return result; } /** @@ -223,43 +243,48 @@ public class HostMonitorServiceImpl implements HostMonitorService { * @param conditions */ @Override - public List> getMemoryInfoList(String ip,String conditions) { - List> result = new ArrayList<>(); + public List> getMemoryInfoList(String ip, String conditions) { + List> result = new ArrayList<>(); try { //Prometheus 服务器地址 String url = serverProperties.getServerUrl(); //目标主机实例(node-exporter 的地址) String instance = this.getInstance(ip); //使用率 - String usageRateQuery = "(1 - (node_memory_MemAvailable_bytes{instance=\""+instance+"\"} / node_memory_MemTotal_bytes{instance=\""+instance+"\"})) * 100"; - PrometheusHostQueryTypeEnum queryTypeEnum = PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions); + String usageRateQuery = "(1 - (node_memory_MemAvailable_bytes{instance=\"" + instance + + "\"} / node_memory_MemTotal_bytes{instance=\"" + instance + "\"})) * 100"; + PrometheusHostQueryTypeEnum queryTypeEnum = + PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions); long end = Instant.now().getEpochSecond(); long start = end - queryTypeEnum.getLastSecond(); String step = queryTypeEnum.getStep(); PrometheusResponse response = webClient.get() - .uri(buildUri(url,usageRateQuery,start,end,step)) + .uri(buildUri(url, usageRateQuery, start, end, step)) .retrieve() .bodyToMono(PrometheusResponse.class) .block(); - if(Objects.nonNull(response) && + if (Objects.nonNull(response) && Objects.nonNull(response.getData()) && CollUtil.isNotEmpty(response.getData().getResult()) ) { - PrometheusResponse.Result usageRateList = response.getData().getResult().get(0); - if(CollUtil.isNotEmpty(usageRateList.getValues())) { + PrometheusResponse.Result usageRateList = response.getData().getResult().get(0); + if (CollUtil.isNotEmpty(usageRateList.getValues())) { List> pointDatas = usageRateList.getValues(); - for(List pointData : pointDatas) { - Map pointDataMap = new HashMap<>(); - Date date = new Date(pointData.get(0).longValue()*1000); - Double useRate = BigDecimal.valueOf(pointData.get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue(); + for (List pointData : pointDatas) { + Map pointDataMap = new HashMap<>(); + Date date = new Date(pointData.get(0).longValue() * 1000); + Double useRate = BigDecimal.valueOf(pointData.get(1)) + .setScale(2, RoundingMode.HALF_UP).doubleValue(); pointDataMap.put("date", DateUtil.format(date, "MM/dd HH:mm:ss")); pointDataMap.put("usageRate", useRate); result.add(pointDataMap); } } } - }catch (Exception e){ - log.error("获取CPU信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage()); + } catch (Exception e) { + log.error( + "获取CPU信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}", + e.getMessage()); } return result; } @@ -279,18 +304,21 @@ public class HostMonitorServiceImpl implements HostMonitorService { //查询总内存 String totalMemoryQuery = "node_memory_MemTotal_bytes{instance=\"" + instance + "\"}"; PrometheusResponse totalMemoryResponse = webClient.get() - .uri(buildUri(url,totalMemoryQuery)) + .uri(buildUri(url, totalMemoryQuery)) .retrieve() .bodyToMono(PrometheusResponse.class) .block(); - if(Objects.nonNull(totalMemoryResponse) && + if (Objects.nonNull(totalMemoryResponse) && Objects.nonNull(totalMemoryResponse.getData()) && CollUtil.isNotEmpty(totalMemoryResponse.getData().getResult()) ) { - PrometheusResponse.Result totalMemoryInfo = totalMemoryResponse.getData().getResult().get(0); - if(CollUtil.isNotEmpty(totalMemoryInfo.getValue())) { - Double totalMemory = BigDecimal.valueOf(totalMemoryInfo.getValue().get(1)/1024/1024/1024).setScale(2, RoundingMode.HALF_UP).doubleValue(); - result.put("totalMemory",totalMemory); + PrometheusResponse.Result totalMemoryInfo = + totalMemoryResponse.getData().getResult().get(0); + if (CollUtil.isNotEmpty(totalMemoryInfo.getValue())) { + Double totalMemory = + BigDecimal.valueOf(totalMemoryInfo.getValue().get(1) / 1024 / 1024 / 1024) + .setScale(2, RoundingMode.HALF_UP).doubleValue(); + result.put("totalMemory", totalMemory); } } return result; @@ -300,58 +328,71 @@ public class HostMonitorServiceImpl implements HostMonitorService { * 获取网络信息 */ @Override - public Map getNetworkInfo(String ip,String conditions) { - Map result = new HashMap<>(); + public Map getNetworkInfo(String ip, String conditions) { + Map result = new HashMap<>(); try { //Prometheus 服务器地址 String url = serverProperties.getServerUrl(); //目标主机实例(node-exporter 的地址) String instance = this.getInstance(ip); - PrometheusHostQueryTypeEnum queryTypeEnum = PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions); + PrometheusHostQueryTypeEnum queryTypeEnum = + PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions); String exprTime = queryTypeEnum.getExprTime(); //接收带宽 (Kbps) - String receiveKbpsQuery = "rate(node_network_receive_bytes_total{instance=\"" + instance + "\", device=\""+serverProperties.getNetworkCardName()+"\"}["+exprTime+"]) * 8 / 1000"; + String receiveKbpsQuery = + "rate(node_network_receive_bytes_total{instance=\"" + instance + + "\", device=\"" + serverProperties.getNetworkCardName() + "\"}[" + + exprTime + "]) * 8 / 1000"; PrometheusResponse receiveKbpsResponse = webClient.get() - .uri(buildUri(url,receiveKbpsQuery)) + .uri(buildUri(url, receiveKbpsQuery)) .retrieve() .bodyToMono(PrometheusResponse.class) .block(); - if(Objects.nonNull(receiveKbpsResponse) && + if (Objects.nonNull(receiveKbpsResponse) && Objects.nonNull(receiveKbpsResponse.getData()) && CollUtil.isNotEmpty(receiveKbpsResponse.getData().getResult()) ) { - PrometheusResponse.Result receiveKbpsInfo = receiveKbpsResponse.getData().getResult().get(0); - if(CollUtil.isNotEmpty(receiveKbpsInfo.getValue())) { - Date date = new Date(receiveKbpsInfo.getValue().get(0).longValue()*1000); - Double receiveKbps = BigDecimal.valueOf(receiveKbpsInfo.getValue().get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue(); + PrometheusResponse.Result receiveKbpsInfo = + receiveKbpsResponse.getData().getResult().get(0); + if (CollUtil.isNotEmpty(receiveKbpsInfo.getValue())) { + Date date = new Date(receiveKbpsInfo.getValue().get(0).longValue() * 1000); + Double receiveKbps = BigDecimal.valueOf(receiveKbpsInfo.getValue().get(1)) + .setScale(2, RoundingMode.HALF_UP).doubleValue(); result.put("receiveDate", DateUtil.format(date, "MM/dd HH:mm:ss")); result.put("receiveKbps", receiveKbps); } } //发送带宽 (Kbps) - String transmitKbpsQuery = "rate(node_network_transmit_bytes_total{instance=\"" + instance + "\", device=\""+serverProperties.getNetworkCardName()+"\"}["+exprTime+"]) * 8 / 1000"; + String transmitKbpsQuery = + "rate(node_network_transmit_bytes_total{instance=\"" + instance + + "\", device=\"" + serverProperties.getNetworkCardName() + "\"}[" + + exprTime + "]) * 8 / 1000"; PrometheusResponse transmitKbpsResponse = webClient.get() - .uri(buildUri(url,transmitKbpsQuery)) + .uri(buildUri(url, transmitKbpsQuery)) .retrieve() .bodyToMono(PrometheusResponse.class) .block(); - if(Objects.nonNull(transmitKbpsResponse) && + if (Objects.nonNull(transmitKbpsResponse) && Objects.nonNull(transmitKbpsResponse.getData()) && CollUtil.isNotEmpty(transmitKbpsResponse.getData().getResult()) ) { - PrometheusResponse.Result transmitKbpsInfo = transmitKbpsResponse.getData().getResult().get(0); - if(CollUtil.isNotEmpty(transmitKbpsInfo.getValue())) { - Date date = new Date(transmitKbpsInfo.getValue().get(0).longValue()*1000); - Double transmitKbps = BigDecimal.valueOf(transmitKbpsInfo.getValue().get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue(); + PrometheusResponse.Result transmitKbpsInfo = + transmitKbpsResponse.getData().getResult().get(0); + if (CollUtil.isNotEmpty(transmitKbpsInfo.getValue())) { + Date date = new Date(transmitKbpsInfo.getValue().get(0).longValue() * 1000); + Double transmitKbps = BigDecimal.valueOf(transmitKbpsInfo.getValue().get(1)) + .setScale(2, RoundingMode.HALF_UP).doubleValue(); result.put("transmitDate", DateUtil.format(date, "MM/dd HH:mm:ss")); result.put("transmitKbps", transmitKbps); } } - }catch (Exception e){ - log.error("获取网络信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage()); + } catch (Exception e) { + log.error( + "获取网络信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}", + e.getMessage()); } - return result; + return result; } /** @@ -360,38 +401,44 @@ public class HostMonitorServiceImpl implements HostMonitorService { * @return */ @Override - public Map>> getNetworkInfoList(String ip,String conditions) { - Map>> result = new HashMap<>(); + public Map>> getNetworkInfoList(String ip, String conditions) { + Map>> result = new HashMap<>(); try { //Prometheus 服务器地址 String url = serverProperties.getServerUrl(); //目标主机实例(node-exporter 的地址) String instance = this.getInstance(ip); //构建查询参数 - PrometheusHostQueryTypeEnum queryTypeEnum = PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions); + PrometheusHostQueryTypeEnum queryTypeEnum = + PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions); long end = Instant.now().getEpochSecond(); long start = end - queryTypeEnum.getLastSecond(); String step = queryTypeEnum.getStep(); String exprTime = queryTypeEnum.getExprTime(); //接收带宽 (Kbps) - String receiveKbpsQuery = "rate(node_network_receive_bytes_total{instance=\"" + instance + "\", device=\""+serverProperties.getNetworkCardName()+"\"}["+exprTime+"]) * 8 / 1000"; + String receiveKbpsQuery = + "rate(node_network_receive_bytes_total{instance=\"" + instance + + "\", device=\"" + serverProperties.getNetworkCardName() + "\"}[" + + exprTime + "]) * 8 / 1000"; PrometheusResponse receiveKbpsResponse = webClient.get() - .uri(buildUri(url,receiveKbpsQuery,start,end,step)) + .uri(buildUri(url, receiveKbpsQuery, start, end, step)) .retrieve() .bodyToMono(PrometheusResponse.class) .block(); - if(Objects.nonNull(receiveKbpsResponse) && + if (Objects.nonNull(receiveKbpsResponse) && Objects.nonNull(receiveKbpsResponse.getData()) && CollUtil.isNotEmpty(receiveKbpsResponse.getData().getResult()) ) { - PrometheusResponse.Result receiveKbpsInfo = receiveKbpsResponse.getData().getResult().get(0); - if(CollUtil.isNotEmpty(receiveKbpsInfo.getValues())) { + PrometheusResponse.Result receiveKbpsInfo = + receiveKbpsResponse.getData().getResult().get(0); + if (CollUtil.isNotEmpty(receiveKbpsInfo.getValues())) { List> receiveDataList = new ArrayList<>(); List> pointDatas = receiveKbpsInfo.getValues(); - for(List pointData : pointDatas) { - Map pointDataMap = new HashMap<>(); - Date date = new Date(pointData.get(0).longValue()*1000); - Double useRate = BigDecimal.valueOf(pointData.get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue(); + for (List pointData : pointDatas) { + Map pointDataMap = new HashMap<>(); + Date date = new Date(pointData.get(0).longValue() * 1000); + Double useRate = BigDecimal.valueOf(pointData.get(1)) + .setScale(2, RoundingMode.HALF_UP).doubleValue(); pointDataMap.put("receiveDate", DateUtil.format(date, "MM/dd HH:mm:ss")); pointDataMap.put("receiveKbps", useRate); receiveDataList.add(pointDataMap); @@ -401,24 +448,29 @@ public class HostMonitorServiceImpl implements HostMonitorService { } //发送带宽 (Kbps) - String transmitKbpsQuery = "rate(node_network_transmit_bytes_total{instance=\"" + instance + "\", device=\""+serverProperties.getNetworkCardName()+"\"}["+exprTime+"]) * 8 / 1000"; + String transmitKbpsQuery = + "rate(node_network_transmit_bytes_total{instance=\"" + instance + + "\", device=\"" + serverProperties.getNetworkCardName() + "\"}[" + + exprTime + "]) * 8 / 1000"; PrometheusResponse transmitKbpsResponse = webClient.get() - .uri(buildUri(url,transmitKbpsQuery,start,end,step)) + .uri(buildUri(url, transmitKbpsQuery, start, end, step)) .retrieve() .bodyToMono(PrometheusResponse.class) .block(); - if(Objects.nonNull(transmitKbpsResponse) && + if (Objects.nonNull(transmitKbpsResponse) && Objects.nonNull(transmitKbpsResponse.getData()) && CollUtil.isNotEmpty(transmitKbpsResponse.getData().getResult()) ) { - PrometheusResponse.Result transmitKbpsInfo = transmitKbpsResponse.getData().getResult().get(0); - if(CollUtil.isNotEmpty(transmitKbpsInfo.getValues())) { + PrometheusResponse.Result transmitKbpsInfo = + transmitKbpsResponse.getData().getResult().get(0); + if (CollUtil.isNotEmpty(transmitKbpsInfo.getValues())) { List> transmitDataList = new ArrayList<>(); List> pointDatas = transmitKbpsInfo.getValues(); - for(List pointData : pointDatas) { - Map pointDataMap = new HashMap<>(); - Date date = new Date(pointData.get(0).longValue()*1000); - Double useRate = BigDecimal.valueOf(pointData.get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue(); + for (List pointData : pointDatas) { + Map pointDataMap = new HashMap<>(); + Date date = new Date(pointData.get(0).longValue() * 1000); + Double useRate = BigDecimal.valueOf(pointData.get(1)) + .setScale(2, RoundingMode.HALF_UP).doubleValue(); pointDataMap.put("receiveDate", DateUtil.format(date, "MM/dd HH:mm:ss")); pointDataMap.put("receiveKbps", useRate); transmitDataList.add(pointDataMap); @@ -426,42 +478,51 @@ public class HostMonitorServiceImpl implements HostMonitorService { result.put("transmitData", transmitDataList); } } - }catch (Exception e){ - log.error("获取网络信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage()); + } catch (Exception e) { + log.error( + "获取网络信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}", + e.getMessage()); } - return result; + return result; } /** * 获取磁盘使用率 */ @Override - public Map getDiskInfo(String ip) { - Map result = new HashMap<>(); + public Map getDiskInfo(String ip) { + Map result = new HashMap<>(); try { //Prometheus 服务器地址 String url = serverProperties.getServerUrl(); //目标主机实例(node-exporter 的地址) String instance = this.getInstance(ip); //磁盘使用率 - String diskUsageQuery = "((node_filesystem_size_bytes{instance=\""+instance+"\", device!~\"rootfs\"} - node_filesystem_avail_bytes{instance=\""+instance+"\", device!~\"rootfs\"}) / node_filesystem_size_bytes{instance=\""+instance+"\", device!~\"rootfs\"}) * 100"; + String diskUsageQuery = "((node_filesystem_size_bytes{instance=\"" + instance + + "\", device!~\"rootfs\"} - node_filesystem_avail_bytes{instance=\"" + instance + + "\", device!~\"rootfs\"}) / node_filesystem_size_bytes{instance=\"" + instance + + "\", device!~\"rootfs\"}) * 100"; PrometheusResponse diskUsageResponse = webClient.get() - .uri(buildUri(url,diskUsageQuery)) + .uri(buildUri(url, diskUsageQuery)) .retrieve() .bodyToMono(PrometheusResponse.class) .block(); - if(Objects.nonNull(diskUsageResponse) && + if (Objects.nonNull(diskUsageResponse) && Objects.nonNull(diskUsageResponse.getData()) && CollUtil.isNotEmpty(diskUsageResponse.getData().getResult()) ) { - for (PrometheusResponse.Result responseData :diskUsageResponse.getData().getResult()){ + for (PrometheusResponse.Result responseData : diskUsageResponse.getData() + .getResult()) { String mountpoint = responseData.getMetric().getMountpoint(); - Double usageRate = BigDecimal.valueOf(responseData.getValue().get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue(); - result.put(mountpoint,usageRate); + Double usageRate = BigDecimal.valueOf(responseData.getValue().get(1)) + .setScale(2, RoundingMode.HALF_UP).doubleValue(); + result.put(mountpoint, usageRate); } } - }catch (Exception e){ - log.error("获取磁盘使用率错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage()); + } catch (Exception e) { + log.error( + "获取磁盘使用率错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}", + e.getMessage()); } return result; } @@ -478,29 +539,32 @@ public class HostMonitorServiceImpl implements HostMonitorService { /** * 获取服务器实例 + * * @param ip * @return */ private String getInstance(String ip) { - if(StrUtil.isBlank(ip)){ - Map.Entry first = serverProperties.getInstances().entrySet().stream().findFirst().get(); - return first.getKey()+":"+first.getValue(); + if (StrUtil.isBlank(ip)) { + Map.Entry first = + serverProperties.getInstances().entrySet().stream().findFirst().get(); + return first.getKey() + ":" + first.getValue(); } - if (!serverProperties.getInstances().containsKey(ip)){ + if (!serverProperties.getInstances().containsKey(ip)) { throw new RuntimeException("此ip不在服务器列表中,请检查监控ip列表配置"); } Integer port = serverProperties.getInstances().get(ip); //return "192.168.186.143"+":"+port; - return ip+":"+port; + return ip + ":" + port; } /** * 构建URI + * * @param url * @param query * @return */ - private URI buildUri(String url,String query){ + private URI buildUri(String url, String query) { URI uri = UriComponentsBuilder.fromHttpUrl(url + "/api/v1/query") .queryParam("query", query) .build() @@ -510,11 +574,12 @@ public class HostMonitorServiceImpl implements HostMonitorService { /** * 构建URI + * * @param url * @param query * @return */ - private URI buildUri(String url,String query,Long start,Long end,String step){ + private URI buildUri(String url, String query, Long start, Long end, String step) { String uriAddr = String.format( "%s/api/v1/query_range?query=%s&start=%d&end=%d&step=%s", url,