格式化代码
This commit is contained in:
parent
3a1c57b161
commit
70852d88b8
|
|
@ -41,9 +41,12 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
//目标主机实例(node-exporter 的地址)
|
//目标主机实例(node-exporter 的地址)
|
||||||
String instance = this.getInstance(ip);
|
String instance = this.getInstance(ip);
|
||||||
//查询CPU利用率
|
//查询CPU利用率
|
||||||
PrometheusHostQueryTypeEnum queryTypeEnum = PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions);
|
PrometheusHostQueryTypeEnum queryTypeEnum =
|
||||||
|
PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions);
|
||||||
String exprTime = queryTypeEnum.getExprTime();
|
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()
|
PrometheusResponse response = webClient.get()
|
||||||
.uri(buildUri(url, cpuQuery))
|
.uri(buildUri(url, cpuQuery))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
|
|
@ -56,13 +59,16 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
PrometheusResponse.Result cpuInfo = response.getData().getResult().get(0);
|
PrometheusResponse.Result cpuInfo = response.getData().getResult().get(0);
|
||||||
if (CollUtil.isNotEmpty(cpuInfo.getValue())) {
|
if (CollUtil.isNotEmpty(cpuInfo.getValue())) {
|
||||||
Date date = new Date(cpuInfo.getValue().get(0).longValue() * 1000);
|
Date date = new Date(cpuInfo.getValue().get(0).longValue() * 1000);
|
||||||
Double useRate = BigDecimal.valueOf(cpuInfo.getValue().get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
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("date", DateUtil.format(date, "MM/dd HH:mm:ss"));
|
||||||
result.put("usageRate", useRate);
|
result.put("usageRate", useRate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("获取CPU信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage());
|
log.error(
|
||||||
|
"获取CPU信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",
|
||||||
|
e.getMessage());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -79,12 +85,15 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
//目标主机实例(node-exporter 的地址)
|
//目标主机实例(node-exporter 的地址)
|
||||||
String instance = this.getInstance(ip);
|
String instance = this.getInstance(ip);
|
||||||
//查询CPU利用率
|
//查询CPU利用率
|
||||||
PrometheusHostQueryTypeEnum queryTypeEnum = PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions);
|
PrometheusHostQueryTypeEnum queryTypeEnum =
|
||||||
|
PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions);
|
||||||
long end = Instant.now().getEpochSecond();
|
long end = Instant.now().getEpochSecond();
|
||||||
long start = end - queryTypeEnum.getLastSecond();
|
long start = end - queryTypeEnum.getLastSecond();
|
||||||
String step = queryTypeEnum.getStep();
|
String step = queryTypeEnum.getStep();
|
||||||
String exprTime = queryTypeEnum.getExprTime();
|
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()
|
PrometheusResponse response = webClient.get()
|
||||||
.uri(buildUri(url, cpuQuery, start, end, step))
|
.uri(buildUri(url, cpuQuery, start, end, step))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
|
|
@ -100,7 +109,8 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
for (List<Double> pointData : pointDatas) {
|
for (List<Double> pointData : pointDatas) {
|
||||||
Map<String, Object> pointDataMap = new HashMap<>();
|
Map<String, Object> pointDataMap = new HashMap<>();
|
||||||
Date date = new Date(pointData.get(0).longValue() * 1000);
|
Date date = new Date(pointData.get(0).longValue() * 1000);
|
||||||
Double useRate = BigDecimal.valueOf(pointData.get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
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("date", DateUtil.format(date, "MM/dd HH:mm:ss"));
|
||||||
pointDataMap.put("usageRate", useRate);
|
pointDataMap.put("usageRate", useRate);
|
||||||
result.add(pointDataMap);
|
result.add(pointDataMap);
|
||||||
|
|
@ -108,7 +118,9 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("获取CPU信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage());
|
log.error(
|
||||||
|
"获取CPU信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",
|
||||||
|
e.getMessage());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -125,7 +137,8 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
//目标主机实例(node-exporter 的地址)
|
//目标主机实例(node-exporter 的地址)
|
||||||
String instance = this.getInstance(ip);
|
String instance = this.getInstance(ip);
|
||||||
//查询CPU核数
|
//查询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()
|
PrometheusResponse response = webClient.get()
|
||||||
.uri(buildUri(url, cpuCoreQuery))
|
.uri(buildUri(url, cpuCoreQuery))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
|
|
@ -142,7 +155,9 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("获取CPU核心数错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage());
|
log.error(
|
||||||
|
"获取CPU核心数错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",
|
||||||
|
e.getMessage());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -193,7 +208,8 @@ 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()
|
PrometheusResponse usageRateResponse = webClient.get()
|
||||||
.uri(buildUri(url, usageRateQuery))
|
.uri(buildUri(url, usageRateQuery))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
|
|
@ -203,16 +219,20 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
Objects.nonNull(usageRateResponse.getData()) &&
|
Objects.nonNull(usageRateResponse.getData()) &&
|
||||||
CollUtil.isNotEmpty(usageRateResponse.getData().getResult())
|
CollUtil.isNotEmpty(usageRateResponse.getData().getResult())
|
||||||
) {
|
) {
|
||||||
PrometheusResponse.Result usageRateInfo = usageRateResponse.getData().getResult().get(0);
|
PrometheusResponse.Result usageRateInfo =
|
||||||
|
usageRateResponse.getData().getResult().get(0);
|
||||||
if (CollUtil.isNotEmpty(usageRateInfo.getValue())) {
|
if (CollUtil.isNotEmpty(usageRateInfo.getValue())) {
|
||||||
Date date = new Date(usageRateInfo.getValue().get(0).longValue() * 1000);
|
Date date = new Date(usageRateInfo.getValue().get(0).longValue() * 1000);
|
||||||
Double usageRate = BigDecimal.valueOf(usageRateInfo.getValue().get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
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("date", DateUtil.format(date, "MM/dd HH:mm:ss"));
|
||||||
result.put("usageRate", usageRate);
|
result.put("usageRate", usageRate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("获取内存信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage());
|
log.error(
|
||||||
|
"获取内存信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",
|
||||||
|
e.getMessage());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -231,8 +251,10 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
//目标主机实例(node-exporter 的地址)
|
//目标主机实例(node-exporter 的地址)
|
||||||
String instance = this.getInstance(ip);
|
String instance = this.getInstance(ip);
|
||||||
//使用率
|
//使用率
|
||||||
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 +
|
||||||
PrometheusHostQueryTypeEnum queryTypeEnum = PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions);
|
"\"} / node_memory_MemTotal_bytes{instance=\"" + instance + "\"})) * 100";
|
||||||
|
PrometheusHostQueryTypeEnum queryTypeEnum =
|
||||||
|
PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions);
|
||||||
long end = Instant.now().getEpochSecond();
|
long end = Instant.now().getEpochSecond();
|
||||||
long start = end - queryTypeEnum.getLastSecond();
|
long start = end - queryTypeEnum.getLastSecond();
|
||||||
String step = queryTypeEnum.getStep();
|
String step = queryTypeEnum.getStep();
|
||||||
|
|
@ -251,7 +273,8 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
for (List<Double> pointData : pointDatas) {
|
for (List<Double> pointData : pointDatas) {
|
||||||
Map<String, Object> pointDataMap = new HashMap<>();
|
Map<String, Object> pointDataMap = new HashMap<>();
|
||||||
Date date = new Date(pointData.get(0).longValue() * 1000);
|
Date date = new Date(pointData.get(0).longValue() * 1000);
|
||||||
Double useRate = BigDecimal.valueOf(pointData.get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
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("date", DateUtil.format(date, "MM/dd HH:mm:ss"));
|
||||||
pointDataMap.put("usageRate", useRate);
|
pointDataMap.put("usageRate", useRate);
|
||||||
result.add(pointDataMap);
|
result.add(pointDataMap);
|
||||||
|
|
@ -259,7 +282,9 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("获取CPU信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage());
|
log.error(
|
||||||
|
"获取CPU信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",
|
||||||
|
e.getMessage());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -287,9 +312,12 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
Objects.nonNull(totalMemoryResponse.getData()) &&
|
Objects.nonNull(totalMemoryResponse.getData()) &&
|
||||||
CollUtil.isNotEmpty(totalMemoryResponse.getData().getResult())
|
CollUtil.isNotEmpty(totalMemoryResponse.getData().getResult())
|
||||||
) {
|
) {
|
||||||
PrometheusResponse.Result totalMemoryInfo = totalMemoryResponse.getData().getResult().get(0);
|
PrometheusResponse.Result totalMemoryInfo =
|
||||||
|
totalMemoryResponse.getData().getResult().get(0);
|
||||||
if (CollUtil.isNotEmpty(totalMemoryInfo.getValue())) {
|
if (CollUtil.isNotEmpty(totalMemoryInfo.getValue())) {
|
||||||
Double totalMemory = BigDecimal.valueOf(totalMemoryInfo.getValue().get(1)/1024/1024/1024).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
Double totalMemory =
|
||||||
|
BigDecimal.valueOf(totalMemoryInfo.getValue().get(1) / 1024 / 1024 / 1024)
|
||||||
|
.setScale(2, RoundingMode.HALF_UP).doubleValue();
|
||||||
result.put("totalMemory", totalMemory);
|
result.put("totalMemory", totalMemory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -307,10 +335,14 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
String url = serverProperties.getServerUrl();
|
String url = serverProperties.getServerUrl();
|
||||||
//目标主机实例(node-exporter 的地址)
|
//目标主机实例(node-exporter 的地址)
|
||||||
String instance = this.getInstance(ip);
|
String instance = this.getInstance(ip);
|
||||||
PrometheusHostQueryTypeEnum queryTypeEnum = PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions);
|
PrometheusHostQueryTypeEnum queryTypeEnum =
|
||||||
|
PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions);
|
||||||
String exprTime = queryTypeEnum.getExprTime();
|
String exprTime = queryTypeEnum.getExprTime();
|
||||||
//接收带宽 (Kbps)
|
//接收带宽 (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()
|
PrometheusResponse receiveKbpsResponse = webClient.get()
|
||||||
.uri(buildUri(url, receiveKbpsQuery))
|
.uri(buildUri(url, receiveKbpsQuery))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
|
|
@ -320,17 +352,22 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
Objects.nonNull(receiveKbpsResponse.getData()) &&
|
Objects.nonNull(receiveKbpsResponse.getData()) &&
|
||||||
CollUtil.isNotEmpty(receiveKbpsResponse.getData().getResult())
|
CollUtil.isNotEmpty(receiveKbpsResponse.getData().getResult())
|
||||||
) {
|
) {
|
||||||
PrometheusResponse.Result receiveKbpsInfo = receiveKbpsResponse.getData().getResult().get(0);
|
PrometheusResponse.Result receiveKbpsInfo =
|
||||||
|
receiveKbpsResponse.getData().getResult().get(0);
|
||||||
if (CollUtil.isNotEmpty(receiveKbpsInfo.getValue())) {
|
if (CollUtil.isNotEmpty(receiveKbpsInfo.getValue())) {
|
||||||
Date date = new Date(receiveKbpsInfo.getValue().get(0).longValue() * 1000);
|
Date date = new Date(receiveKbpsInfo.getValue().get(0).longValue() * 1000);
|
||||||
Double receiveKbps = BigDecimal.valueOf(receiveKbpsInfo.getValue().get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
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("receiveDate", DateUtil.format(date, "MM/dd HH:mm:ss"));
|
||||||
result.put("receiveKbps", receiveKbps);
|
result.put("receiveKbps", receiveKbps);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//发送带宽 (Kbps)
|
//发送带宽 (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()
|
PrometheusResponse transmitKbpsResponse = webClient.get()
|
||||||
.uri(buildUri(url, transmitKbpsQuery))
|
.uri(buildUri(url, transmitKbpsQuery))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
|
|
@ -340,16 +377,20 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
Objects.nonNull(transmitKbpsResponse.getData()) &&
|
Objects.nonNull(transmitKbpsResponse.getData()) &&
|
||||||
CollUtil.isNotEmpty(transmitKbpsResponse.getData().getResult())
|
CollUtil.isNotEmpty(transmitKbpsResponse.getData().getResult())
|
||||||
) {
|
) {
|
||||||
PrometheusResponse.Result transmitKbpsInfo = transmitKbpsResponse.getData().getResult().get(0);
|
PrometheusResponse.Result transmitKbpsInfo =
|
||||||
|
transmitKbpsResponse.getData().getResult().get(0);
|
||||||
if (CollUtil.isNotEmpty(transmitKbpsInfo.getValue())) {
|
if (CollUtil.isNotEmpty(transmitKbpsInfo.getValue())) {
|
||||||
Date date = new Date(transmitKbpsInfo.getValue().get(0).longValue() * 1000);
|
Date date = new Date(transmitKbpsInfo.getValue().get(0).longValue() * 1000);
|
||||||
Double transmitKbps = BigDecimal.valueOf(transmitKbpsInfo.getValue().get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
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("transmitDate", DateUtil.format(date, "MM/dd HH:mm:ss"));
|
||||||
result.put("transmitKbps", transmitKbps);
|
result.put("transmitKbps", transmitKbps);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("获取网络信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage());
|
log.error(
|
||||||
|
"获取网络信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",
|
||||||
|
e.getMessage());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -368,13 +409,17 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
//目标主机实例(node-exporter 的地址)
|
//目标主机实例(node-exporter 的地址)
|
||||||
String instance = this.getInstance(ip);
|
String instance = this.getInstance(ip);
|
||||||
//构建查询参数
|
//构建查询参数
|
||||||
PrometheusHostQueryTypeEnum queryTypeEnum = PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions);
|
PrometheusHostQueryTypeEnum queryTypeEnum =
|
||||||
|
PrometheusHostQueryTypeEnum.getQueryTypeEnum(conditions);
|
||||||
long end = Instant.now().getEpochSecond();
|
long end = Instant.now().getEpochSecond();
|
||||||
long start = end - queryTypeEnum.getLastSecond();
|
long start = end - queryTypeEnum.getLastSecond();
|
||||||
String step = queryTypeEnum.getStep();
|
String step = queryTypeEnum.getStep();
|
||||||
String exprTime = queryTypeEnum.getExprTime();
|
String exprTime = queryTypeEnum.getExprTime();
|
||||||
//接收带宽 (Kbps)
|
//接收带宽 (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()
|
PrometheusResponse receiveKbpsResponse = webClient.get()
|
||||||
.uri(buildUri(url, receiveKbpsQuery, start, end, step))
|
.uri(buildUri(url, receiveKbpsQuery, start, end, step))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
|
|
@ -384,14 +429,16 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
Objects.nonNull(receiveKbpsResponse.getData()) &&
|
Objects.nonNull(receiveKbpsResponse.getData()) &&
|
||||||
CollUtil.isNotEmpty(receiveKbpsResponse.getData().getResult())
|
CollUtil.isNotEmpty(receiveKbpsResponse.getData().getResult())
|
||||||
) {
|
) {
|
||||||
PrometheusResponse.Result receiveKbpsInfo = receiveKbpsResponse.getData().getResult().get(0);
|
PrometheusResponse.Result receiveKbpsInfo =
|
||||||
|
receiveKbpsResponse.getData().getResult().get(0);
|
||||||
if (CollUtil.isNotEmpty(receiveKbpsInfo.getValues())) {
|
if (CollUtil.isNotEmpty(receiveKbpsInfo.getValues())) {
|
||||||
List<Map<String, Object>> receiveDataList = new ArrayList<>();
|
List<Map<String, Object>> receiveDataList = new ArrayList<>();
|
||||||
List<List<Double>> pointDatas = receiveKbpsInfo.getValues();
|
List<List<Double>> pointDatas = receiveKbpsInfo.getValues();
|
||||||
for (List<Double> pointData : pointDatas) {
|
for (List<Double> pointData : pointDatas) {
|
||||||
Map<String, Object> pointDataMap = new HashMap<>();
|
Map<String, Object> pointDataMap = new HashMap<>();
|
||||||
Date date = new Date(pointData.get(0).longValue() * 1000);
|
Date date = new Date(pointData.get(0).longValue() * 1000);
|
||||||
Double useRate = BigDecimal.valueOf(pointData.get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
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("receiveDate", DateUtil.format(date, "MM/dd HH:mm:ss"));
|
||||||
pointDataMap.put("receiveKbps", useRate);
|
pointDataMap.put("receiveKbps", useRate);
|
||||||
receiveDataList.add(pointDataMap);
|
receiveDataList.add(pointDataMap);
|
||||||
|
|
@ -401,7 +448,10 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
}
|
}
|
||||||
|
|
||||||
//发送带宽 (Kbps)
|
//发送带宽 (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()
|
PrometheusResponse transmitKbpsResponse = webClient.get()
|
||||||
.uri(buildUri(url, transmitKbpsQuery, start, end, step))
|
.uri(buildUri(url, transmitKbpsQuery, start, end, step))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
|
|
@ -411,14 +461,16 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
Objects.nonNull(transmitKbpsResponse.getData()) &&
|
Objects.nonNull(transmitKbpsResponse.getData()) &&
|
||||||
CollUtil.isNotEmpty(transmitKbpsResponse.getData().getResult())
|
CollUtil.isNotEmpty(transmitKbpsResponse.getData().getResult())
|
||||||
) {
|
) {
|
||||||
PrometheusResponse.Result transmitKbpsInfo = transmitKbpsResponse.getData().getResult().get(0);
|
PrometheusResponse.Result transmitKbpsInfo =
|
||||||
|
transmitKbpsResponse.getData().getResult().get(0);
|
||||||
if (CollUtil.isNotEmpty(transmitKbpsInfo.getValues())) {
|
if (CollUtil.isNotEmpty(transmitKbpsInfo.getValues())) {
|
||||||
List<Map<String, Object>> transmitDataList = new ArrayList<>();
|
List<Map<String, Object>> transmitDataList = new ArrayList<>();
|
||||||
List<List<Double>> pointDatas = transmitKbpsInfo.getValues();
|
List<List<Double>> pointDatas = transmitKbpsInfo.getValues();
|
||||||
for (List<Double> pointData : pointDatas) {
|
for (List<Double> pointData : pointDatas) {
|
||||||
Map<String, Object> pointDataMap = new HashMap<>();
|
Map<String, Object> pointDataMap = new HashMap<>();
|
||||||
Date date = new Date(pointData.get(0).longValue() * 1000);
|
Date date = new Date(pointData.get(0).longValue() * 1000);
|
||||||
Double useRate = BigDecimal.valueOf(pointData.get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
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("receiveDate", DateUtil.format(date, "MM/dd HH:mm:ss"));
|
||||||
pointDataMap.put("receiveKbps", useRate);
|
pointDataMap.put("receiveKbps", useRate);
|
||||||
transmitDataList.add(pointDataMap);
|
transmitDataList.add(pointDataMap);
|
||||||
|
|
@ -427,7 +479,9 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("获取网络信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage());
|
log.error(
|
||||||
|
"获取网络信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",
|
||||||
|
e.getMessage());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -444,7 +498,10 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
//目标主机实例(node-exporter 的地址)
|
//目标主机实例(node-exporter 的地址)
|
||||||
String instance = this.getInstance(ip);
|
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()
|
PrometheusResponse diskUsageResponse = webClient.get()
|
||||||
.uri(buildUri(url, diskUsageQuery))
|
.uri(buildUri(url, diskUsageQuery))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
|
|
@ -454,14 +511,18 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
Objects.nonNull(diskUsageResponse.getData()) &&
|
Objects.nonNull(diskUsageResponse.getData()) &&
|
||||||
CollUtil.isNotEmpty(diskUsageResponse.getData().getResult())
|
CollUtil.isNotEmpty(diskUsageResponse.getData().getResult())
|
||||||
) {
|
) {
|
||||||
for (PrometheusResponse.Result responseData :diskUsageResponse.getData().getResult()){
|
for (PrometheusResponse.Result responseData : diskUsageResponse.getData()
|
||||||
|
.getResult()) {
|
||||||
String mountpoint = responseData.getMetric().getMountpoint();
|
String mountpoint = responseData.getMetric().getMountpoint();
|
||||||
Double usageRate = BigDecimal.valueOf(responseData.getValue().get(1)).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
Double usageRate = BigDecimal.valueOf(responseData.getValue().get(1))
|
||||||
|
.setScale(2, RoundingMode.HALF_UP).doubleValue();
|
||||||
result.put(mountpoint, usageRate);
|
result.put(mountpoint, usageRate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("获取磁盘使用率错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage());
|
log.error(
|
||||||
|
"获取磁盘使用率错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",
|
||||||
|
e.getMessage());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -478,12 +539,14 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取服务器实例
|
* 获取服务器实例
|
||||||
|
*
|
||||||
* @param ip
|
* @param ip
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private String getInstance(String ip) {
|
private String getInstance(String ip) {
|
||||||
if (StrUtil.isBlank(ip)) {
|
if (StrUtil.isBlank(ip)) {
|
||||||
Map.Entry<String, Integer> first = serverProperties.getInstances().entrySet().stream().findFirst().get();
|
Map.Entry<String, Integer> first =
|
||||||
|
serverProperties.getInstances().entrySet().stream().findFirst().get();
|
||||||
return first.getKey() + ":" + first.getValue();
|
return first.getKey() + ":" + first.getValue();
|
||||||
}
|
}
|
||||||
if (!serverProperties.getInstances().containsKey(ip)) {
|
if (!serverProperties.getInstances().containsKey(ip)) {
|
||||||
|
|
@ -496,6 +559,7 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建URI
|
* 构建URI
|
||||||
|
*
|
||||||
* @param url
|
* @param url
|
||||||
* @param query
|
* @param query
|
||||||
* @return
|
* @return
|
||||||
|
|
@ -510,6 +574,7 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建URI
|
* 构建URI
|
||||||
|
*
|
||||||
* @param url
|
* @param url
|
||||||
* @param query
|
* @param query
|
||||||
* @return
|
* @return
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user