格式化代码

This commit is contained in:
duwenyuan 2026-07-08 15:20:05 +08:00
parent 3a1c57b161
commit 70852d88b8

View File

@ -33,82 +33,94 @@ public class HostMonitorServiceImpl implements HostMonitorService {
* 获取CPU信息
*/
@Override
public Map<String,Object> getCpuInfo(String ip,String conditions) {
Map<String,Object> result = new HashMap<>();
public Map<String, Object> getCpuInfo(String ip, String conditions) {
Map<String, Object> 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<Map<String, Object>> getCpuInfoList(String ip,String conditions) {
List<Map<String, Object>> result = new ArrayList<>();
public List<Map<String, Object>> getCpuInfoList(String ip, String conditions) {
List<Map<String, Object>> 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<List<Double>> pointDatas = cpuInfoList.getValues();
for(List<Double> pointData : pointDatas) {
Map<String,Object> 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<Double> pointData : pointDatas) {
Map<String, Object> 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<String,Object> getCpuCoreInfo(String ip) {
Map<String,Object> result = new HashMap<>();
public Map<String, Object> getCpuCoreInfo(String ip) {
Map<String, Object> 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<String,Object> getMemoryInfo(String ip) {
Map<String,Object> result = new HashMap<>();
public Map<String, Object> getMemoryInfo(String ip) {
Map<String, Object> 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<Map<String,Object>> getMemoryInfoList(String ip,String conditions) {
List<Map<String, Object>> result = new ArrayList<>();
public List<Map<String, Object>> getMemoryInfoList(String ip, String conditions) {
List<Map<String, Object>> 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<List<Double>> pointDatas = usageRateList.getValues();
for(List<Double> pointData : pointDatas) {
Map<String,Object> 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<Double> pointData : pointDatas) {
Map<String, Object> 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<String,Object> getNetworkInfo(String ip,String conditions) {
Map<String,Object> result = new HashMap<>();
public Map<String, Object> getNetworkInfo(String ip, String conditions) {
Map<String, Object> 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<String, List<Map<String, Object>>> getNetworkInfoList(String ip,String conditions) {
Map<String,List<Map<String, Object>>> result = new HashMap<>();
public Map<String, List<Map<String, Object>>> getNetworkInfoList(String ip, String conditions) {
Map<String, List<Map<String, Object>>> 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<Map<String, Object>> receiveDataList = new ArrayList<>();
List<List<Double>> pointDatas = receiveKbpsInfo.getValues();
for(List<Double> pointData : pointDatas) {
Map<String,Object> 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<Double> pointData : pointDatas) {
Map<String, Object> 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<Map<String, Object>> transmitDataList = new ArrayList<>();
List<List<Double>> pointDatas = transmitKbpsInfo.getValues();
for(List<Double> pointData : pointDatas) {
Map<String,Object> 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<Double> pointData : pointDatas) {
Map<String, Object> 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<String,Object> getDiskInfo(String ip) {
Map<String,Object> result = new HashMap<>();
public Map<String, Object> getDiskInfo(String ip) {
Map<String, Object> 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<String, Integer> first = serverProperties.getInstances().entrySet().stream().findFirst().get();
return first.getKey()+":"+first.getValue();
if (StrUtil.isBlank(ip)) {
Map.Entry<String, Integer> 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,