1.修改系统管理系统监控功能添加各个接口范围查询功能
This commit is contained in:
parent
67cad8b525
commit
fd102a9518
|
|
@ -0,0 +1,64 @@
|
||||||
|
package org.jeecg.common.constant.enums;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prometheus 范围查询条件枚举
|
||||||
|
*/
|
||||||
|
public enum PrometheusQueryTypeEnum {
|
||||||
|
|
||||||
|
FIVE_MIN("Last 5 minutes",(5*60),"15s"),
|
||||||
|
THIRTY_MIN("Last 30 minutes",(30*60),"15s"),
|
||||||
|
ONE_HOUR("Last 1 hour",(60*60),"15s"),
|
||||||
|
THREE_HOURS("Last 3 hours",(3*60*60),"15s"),
|
||||||
|
SIX_HOURS("Last 6 hours",(6*60*60),"15s"),
|
||||||
|
TWELVE_HOURS("Last 12 hours",(12*60*60),"1m0s"),
|
||||||
|
TWENTY_FOUR_HOURS("Last 24 hours",(24*60*60),"2m0s"),
|
||||||
|
TWO_DAYS("Last 2 days",(2*24*60*60),"5m0s"),
|
||||||
|
SEVEN_DAYS("Last 7 days",(7*24*60*60),"15m0s");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prometheus 范围查询条件
|
||||||
|
*/
|
||||||
|
private String conditions;
|
||||||
|
/**
|
||||||
|
* Prometheus 范围起始时间需要减的值(秒)
|
||||||
|
*/
|
||||||
|
private long lastSecond;
|
||||||
|
/**
|
||||||
|
* 间隔步长
|
||||||
|
*/
|
||||||
|
private String step;
|
||||||
|
|
||||||
|
PrometheusQueryTypeEnum(String conditions, Integer number, String step) {
|
||||||
|
this.conditions = conditions;
|
||||||
|
this.lastSecond = number;
|
||||||
|
this.step = step;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getConditions() {
|
||||||
|
return conditions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getLastSecond() {
|
||||||
|
return lastSecond;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStep() {
|
||||||
|
return step;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回对应枚举
|
||||||
|
* @param conditions
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static PrometheusQueryTypeEnum getQueryTypeEnum(String conditions) {
|
||||||
|
for (PrometheusQueryTypeEnum queryTypeEnum : PrometheusQueryTypeEnum.values()) {
|
||||||
|
if (queryTypeEnum.getConditions().equals(conditions)) {
|
||||||
|
return queryTypeEnum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -18,4 +18,9 @@ public class PrometheusServerProperties {
|
||||||
* node-exporter实例地址
|
* node-exporter实例地址
|
||||||
*/
|
*/
|
||||||
private String instance;
|
private String instance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监测的网卡名称
|
||||||
|
*/
|
||||||
|
private String networkCardName;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -142,7 +142,7 @@ public class SourceRebuildTask implements Serializable {
|
||||||
private Integer sourceStrength;
|
private Integer sourceStrength;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 耗时
|
* 耗时(分钟)
|
||||||
*/
|
*/
|
||||||
@Null(message = "耗时必须为空",groups = {InsertGroup.class, UpdateGroup.class})
|
@Null(message = "耗时必须为空",groups = {InsertGroup.class, UpdateGroup.class})
|
||||||
@TableField(value = "time_consuming")
|
@TableField(value = "time_consuming")
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,16 @@
|
||||||
package org.jeecg.modules.monitor.controller;
|
package org.jeecg.modules.monitor.controller;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.jeecg.common.api.vo.Result;
|
import org.jeecg.common.api.vo.Result;
|
||||||
import org.jeecg.common.aspect.annotation.AutoLog;
|
import org.jeecg.common.aspect.annotation.AutoLog;
|
||||||
import org.jeecg.modules.monitor.service.HostMonitorService;
|
import org.jeecg.modules.monitor.service.HostMonitorService;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@Validated
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("monitor")
|
@RequestMapping("monitor")
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
|
@ -15,30 +18,54 @@ public class ServiceMonitorController {
|
||||||
|
|
||||||
private final HostMonitorService hostMonitorService;
|
private final HostMonitorService hostMonitorService;
|
||||||
|
|
||||||
@AutoLog(value = "获取CPU信息")
|
@AutoLog(value = "查询当前时刻的CPU使用率")
|
||||||
@GetMapping("getCpuInfo")
|
@GetMapping("getCpuInfo")
|
||||||
public Result<?> getCpuInfo() {
|
public Result<?> getCpuInfo() {
|
||||||
return Result.OK(hostMonitorService.getCpuInfo());
|
return Result.OK(hostMonitorService.getCpuInfo());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@AutoLog(value = "查询过去指定时间范围内的CPU使用率")
|
||||||
|
@GetMapping("getCpuInfoList")
|
||||||
|
public Result<?> getCpuInfoList(@NotBlank(message = "查询条件不能为空") String conditions) {
|
||||||
|
return Result.OK(hostMonitorService.getCpuInfoList(conditions));
|
||||||
|
}
|
||||||
|
|
||||||
@AutoLog(value = "获取CPU核心数")
|
@AutoLog(value = "获取CPU核心数")
|
||||||
@GetMapping("getCpuCoreInfo")
|
@GetMapping("getCpuCoreInfo")
|
||||||
public Result<?> getCpuCoreInfo() {
|
public Result<?> getCpuCoreInfo() {
|
||||||
return Result.OK(hostMonitorService.getCpuCoreInfo());
|
return Result.OK(hostMonitorService.getCpuCoreInfo());
|
||||||
}
|
}
|
||||||
|
|
||||||
@AutoLog(value = "获取内存监测数据")
|
@AutoLog(value = "查询当前时刻的内存使用率")
|
||||||
@GetMapping("getMemoryInfo")
|
@GetMapping("getMemoryInfo")
|
||||||
public Result<?> getMemoryInfo() {
|
public Result<?> getMemoryInfo() {
|
||||||
return Result.OK(hostMonitorService.getMemoryInfo());
|
return Result.OK(hostMonitorService.getMemoryInfo());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@AutoLog(value = "获取总内存")
|
||||||
|
@GetMapping("getTotleMemoryInfo")
|
||||||
|
public Result<?> getTotleMemoryInfo() {
|
||||||
|
return Result.OK(hostMonitorService.getTotleMemoryInfo());
|
||||||
|
}
|
||||||
|
|
||||||
|
@AutoLog(value = "查询过去指定时间范围内的内存使用率")
|
||||||
|
@GetMapping("getMemoryInfoList")
|
||||||
|
public Result<?> getMemoryInfoList(@NotBlank(message = "查询条件不能为空") String conditions) {
|
||||||
|
return Result.OK(hostMonitorService.getMemoryInfoList(conditions));
|
||||||
|
}
|
||||||
|
|
||||||
@AutoLog(value = "获取网络带宽监测数据")
|
@AutoLog(value = "获取网络带宽监测数据")
|
||||||
@GetMapping("getNetworkInfo")
|
@GetMapping("getNetworkInfo")
|
||||||
public Result<?> getNetworkInfo() {
|
public Result<?> getNetworkInfo() {
|
||||||
return Result.OK(hostMonitorService.getNetworkInfo());
|
return Result.OK(hostMonitorService.getNetworkInfo());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@AutoLog(value = "获取网络带宽监测数据")
|
||||||
|
@GetMapping("getNetworkInfoList")
|
||||||
|
public Result<?> getNetworkInfoList(@NotBlank(message = "查询条件不能为空") String conditions) {
|
||||||
|
return Result.OK(hostMonitorService.getNetworkInfoList(conditions));
|
||||||
|
}
|
||||||
|
|
||||||
@AutoLog(value = "获取磁盘使用率")
|
@AutoLog(value = "获取磁盘使用率")
|
||||||
@GetMapping("getDiskInfo")
|
@GetMapping("getDiskInfo")
|
||||||
public Result<?> getDiskInfo() {
|
public Result<?> getDiskInfo() {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package org.jeecg.modules.monitor.service;
|
package org.jeecg.modules.monitor.service;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public interface HostMonitorService {
|
public interface HostMonitorService {
|
||||||
|
|
@ -10,6 +11,11 @@ public interface HostMonitorService {
|
||||||
*/
|
*/
|
||||||
Map<String,Object> getCpuInfo();
|
Map<String,Object> getCpuInfo();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取CPU信息列表
|
||||||
|
*/
|
||||||
|
List<Map<String,Object>> getCpuInfoList(String conditions);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取CPU核心数
|
* 获取CPU核心数
|
||||||
*/
|
*/
|
||||||
|
|
@ -20,19 +26,30 @@ public interface HostMonitorService {
|
||||||
*/
|
*/
|
||||||
Map<String,Object> getMemoryInfo();
|
Map<String,Object> getMemoryInfo();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取内存信息列表
|
||||||
|
*/
|
||||||
|
List<Map<String,Object>> getMemoryInfoList(String conditions);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取总内存
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Map<String, Object> getTotleMemoryInfo();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取网络信息
|
* 获取网络信息
|
||||||
*/
|
*/
|
||||||
Map<String,Object> getNetworkInfo();
|
Map<String,Object> getNetworkInfo();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取网络信息列表
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Map<String,List<Map<String,Object>>> getNetworkInfoList(String conditions);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取磁盘使用率
|
* 获取磁盘使用率
|
||||||
*/
|
*/
|
||||||
Map<String,Object> getDiskInfo();
|
Map<String,Object> getDiskInfo();
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取磁盘吞吐量
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
Map<String,Object> getDiskThroughputInfo();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,21 +4,18 @@ import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.jeecg.common.constant.enums.PrometheusQueryTypeEnum;
|
||||||
import org.jeecg.common.properties.PrometheusServerProperties;
|
import org.jeecg.common.properties.PrometheusServerProperties;
|
||||||
import org.jeecg.modules.monitor.service.HostMonitorService;
|
import org.jeecg.modules.monitor.service.HostMonitorService;
|
||||||
import org.jeecg.modules.monitor.vo.PrometheusResponse;
|
import org.jeecg.modules.monitor.vo.PrometheusResponse;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
import org.springframework.web.util.UriComponentsBuilder;
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Date;
|
import java.time.Instant;
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 基于Prometheus的主机监控
|
* 基于Prometheus的主机监控
|
||||||
|
|
@ -43,7 +40,7 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
//目标主机实例(node-exporter 的地址)
|
//目标主机实例(node-exporter 的地址)
|
||||||
String instance = serverProperties.getInstance();
|
String instance = serverProperties.getInstance();
|
||||||
//查询CPU利用率
|
//查询CPU利用率
|
||||||
String cpuQuery = "100 * (1 - avg(rate(node_cpu_seconds_total{mode=\"idle\", instance=\""+instance+"\"}[1m0s])))";
|
String cpuQuery = "100 * (1 - avg(rate(node_cpu_seconds_total{mode=\"idle\", instance=\""+instance+"\"}[15s])))";
|
||||||
PrometheusResponse response = webClient.get()
|
PrometheusResponse response = webClient.get()
|
||||||
.uri(buildUri(url,cpuQuery))
|
.uri(buildUri(url,cpuQuery))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
|
|
@ -57,7 +54,7 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
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, "yyyy-MM-dd HH:mm:ss"));
|
result.put("date", DateUtil.format(date, "MM/dd HH:mm:ss"));
|
||||||
result.put("usageRate", useRate);
|
result.put("usageRate", useRate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -67,6 +64,51 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取CPU信息列表
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<Map<String, Object>> getCpuInfoList(String conditions) {
|
||||||
|
List<Map<String, Object>> result = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
//Prometheus 服务器地址
|
||||||
|
String url = serverProperties.getServerUrl();
|
||||||
|
//目标主机实例(node-exporter 的地址)
|
||||||
|
String instance = serverProperties.getInstance();
|
||||||
|
//查询CPU利用率
|
||||||
|
String cpuQuery = "100 * (1 - avg(rate(node_cpu_seconds_total{mode=\"idle\", instance=\""+instance+"\"}[15s])))";
|
||||||
|
PrometheusQueryTypeEnum queryTypeEnum = PrometheusQueryTypeEnum.getQueryTypeEnum(conditions);
|
||||||
|
long end = Instant.now().getEpochSecond();
|
||||||
|
long start = end - queryTypeEnum.getLastSecond();
|
||||||
|
String step = queryTypeEnum.getStep();
|
||||||
|
PrometheusResponse response = webClient.get()
|
||||||
|
.uri(buildUri(url,cpuQuery,start,end,step))
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(PrometheusResponse.class)
|
||||||
|
.block();
|
||||||
|
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())) {
|
||||||
|
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();
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取CPU核心数
|
* 获取CPU核心数
|
||||||
*/
|
*/
|
||||||
|
|
@ -113,39 +155,39 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
//目标主机实例(node-exporter 的地址)
|
//目标主机实例(node-exporter 的地址)
|
||||||
String instance = serverProperties.getInstance();
|
String instance = serverProperties.getInstance();
|
||||||
//查询总内存
|
//查询总内存
|
||||||
String totalMemoryQuery = "node_memory_MemTotal_bytes{instance=\"" + instance + "\"}";
|
// String totalMemoryQuery = "node_memory_MemTotal_bytes{instance=\"" + instance + "\"}";
|
||||||
PrometheusResponse totalMemoryResponse = webClient.get()
|
// PrometheusResponse totalMemoryResponse = webClient.get()
|
||||||
.uri(buildUri(url,totalMemoryQuery))
|
// .uri(buildUri(url,totalMemoryQuery))
|
||||||
.retrieve()
|
// .retrieve()
|
||||||
.bodyToMono(PrometheusResponse.class)
|
// .bodyToMono(PrometheusResponse.class)
|
||||||
.block();
|
// .block();
|
||||||
if(Objects.nonNull(totalMemoryResponse) &&
|
// if(Objects.nonNull(totalMemoryResponse) &&
|
||||||
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);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
//剩余可用内存
|
//剩余可用内存
|
||||||
String availableMemoryQuery = "node_memory_MemAvailable_bytes{instance=\"" + instance + "\"}";
|
// String availableMemoryQuery = "node_memory_MemAvailable_bytes{instance=\"" + instance + "\"}";
|
||||||
PrometheusResponse availableMemoryResponse = webClient.get()
|
// PrometheusResponse availableMemoryResponse = webClient.get()
|
||||||
.uri(buildUri(url,availableMemoryQuery))
|
// .uri(buildUri(url,availableMemoryQuery))
|
||||||
.retrieve()
|
// .retrieve()
|
||||||
.bodyToMono(PrometheusResponse.class)
|
// .bodyToMono(PrometheusResponse.class)
|
||||||
.block();;
|
// .block();;
|
||||||
if(Objects.nonNull(availableMemoryResponse) &&
|
// if(Objects.nonNull(availableMemoryResponse) &&
|
||||||
Objects.nonNull(availableMemoryResponse.getData()) &&
|
// Objects.nonNull(availableMemoryResponse.getData()) &&
|
||||||
CollUtil.isNotEmpty(availableMemoryResponse.getData().getResult())
|
// CollUtil.isNotEmpty(availableMemoryResponse.getData().getResult())
|
||||||
) {
|
// ) {
|
||||||
PrometheusResponse.Result availableMemoryInfo = availableMemoryResponse.getData().getResult().get(0);
|
// PrometheusResponse.Result availableMemoryInfo = availableMemoryResponse.getData().getResult().get(0);
|
||||||
if(CollUtil.isNotEmpty(availableMemoryInfo.getValue())) {
|
// if(CollUtil.isNotEmpty(availableMemoryInfo.getValue())) {
|
||||||
Double availableMemory = BigDecimal.valueOf(availableMemoryInfo.getValue().get(1)/1024/1024/1024).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
// Double availableMemory = BigDecimal.valueOf(availableMemoryInfo.getValue().get(1)/1024/1024/1024).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
||||||
result.put("availableMemory",availableMemory);
|
// result.put("availableMemory",availableMemory);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
//使用率
|
//使用率
|
||||||
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()
|
||||||
|
|
@ -161,7 +203,7 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
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, "yyyy-MM-dd HH:mm:ss"));
|
result.put("date", DateUtil.format(date, "MM/dd HH:mm:ss"));
|
||||||
result.put("usageRate", usageRate);
|
result.put("usageRate", usageRate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -171,6 +213,85 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取内存信息列表
|
||||||
|
*
|
||||||
|
* @param conditions
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<Map<String,Object>> getMemoryInfoList(String conditions) {
|
||||||
|
List<Map<String, Object>> result = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
//Prometheus 服务器地址
|
||||||
|
String url = serverProperties.getServerUrl();
|
||||||
|
//目标主机实例(node-exporter 的地址)
|
||||||
|
String instance = serverProperties.getInstance();
|
||||||
|
//使用率
|
||||||
|
String usageRateQuery = "(1 - (node_memory_MemAvailable_bytes{instance=\""+instance+"\"} / node_memory_MemTotal_bytes{instance=\""+instance+"\"})) * 100";
|
||||||
|
PrometheusQueryTypeEnum queryTypeEnum = PrometheusQueryTypeEnum.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))
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(PrometheusResponse.class)
|
||||||
|
.block();
|
||||||
|
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())) {
|
||||||
|
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();
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取总内存
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> getTotleMemoryInfo() {
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
//Prometheus 服务器地址
|
||||||
|
String url = serverProperties.getServerUrl();
|
||||||
|
//目标主机实例(node-exporter 的地址)
|
||||||
|
String instance = serverProperties.getInstance();
|
||||||
|
//查询总内存
|
||||||
|
String totalMemoryQuery = "node_memory_MemTotal_bytes{instance=\"" + instance + "\"}";
|
||||||
|
PrometheusResponse totalMemoryResponse = webClient.get()
|
||||||
|
.uri(buildUri(url,totalMemoryQuery))
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(PrometheusResponse.class)
|
||||||
|
.block();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取网络信息
|
* 获取网络信息
|
||||||
*/
|
*/
|
||||||
|
|
@ -183,7 +304,7 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
//目标主机实例(node-exporter 的地址)
|
//目标主机实例(node-exporter 的地址)
|
||||||
String instance = serverProperties.getInstance();
|
String instance = serverProperties.getInstance();
|
||||||
//接收带宽 (Kbps)
|
//接收带宽 (Kbps)
|
||||||
String receiveKbpsQuery = "rate(node_network_receive_bytes_total{instance=\"" + instance + "\", device=\"ens32\"}[1m]) * 8 / 1000";
|
String receiveKbpsQuery = "rate(node_network_receive_bytes_total{instance=\"" + instance + "\", device=\""+serverProperties.getNetworkCardName()+"\"}[15s]) * 8 / 1000";
|
||||||
PrometheusResponse receiveKbpsResponse = webClient.get()
|
PrometheusResponse receiveKbpsResponse = webClient.get()
|
||||||
.uri(buildUri(url,receiveKbpsQuery))
|
.uri(buildUri(url,receiveKbpsQuery))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
|
|
@ -197,13 +318,13 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
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, "yyyy-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=\"ens32\"}[1m]) * 8 / 1000";
|
String transmitKbpsQuery = "rate(node_network_transmit_bytes_total{instance=\"" + instance + "\", device=\""+serverProperties.getNetworkCardName()+"\"}[15s]) * 8 / 1000";
|
||||||
PrometheusResponse transmitKbpsResponse = webClient.get()
|
PrometheusResponse transmitKbpsResponse = webClient.get()
|
||||||
.uri(buildUri(url,transmitKbpsQuery))
|
.uri(buildUri(url,transmitKbpsQuery))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
|
|
@ -217,7 +338,7 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
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, "yyyy-MM-dd HH:mm:ss"));
|
result.put("transmitDate", DateUtil.format(date, "MM/dd HH:mm:ss"));
|
||||||
result.put("transmitKbps", transmitKbps);
|
result.put("transmitKbps", transmitKbps);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -227,6 +348,83 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取网络信息列表
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Map<String, List<Map<String, Object>>> getNetworkInfoList(String conditions) {
|
||||||
|
Map<String,List<Map<String, Object>>> result = new HashMap<>();
|
||||||
|
try {
|
||||||
|
//Prometheus 服务器地址
|
||||||
|
String url = serverProperties.getServerUrl();
|
||||||
|
//目标主机实例(node-exporter 的地址)
|
||||||
|
String instance = serverProperties.getInstance();
|
||||||
|
//构建查询参数
|
||||||
|
PrometheusQueryTypeEnum queryTypeEnum = PrometheusQueryTypeEnum.getQueryTypeEnum(conditions);
|
||||||
|
long end = Instant.now().getEpochSecond();
|
||||||
|
long start = end - queryTypeEnum.getLastSecond();
|
||||||
|
String step = queryTypeEnum.getStep();
|
||||||
|
//接收带宽 (Kbps)
|
||||||
|
String receiveKbpsQuery = "rate(node_network_receive_bytes_total{instance=\"" + instance + "\", device=\""+serverProperties.getNetworkCardName()+"\"}[15s]) * 8 / 1000";
|
||||||
|
PrometheusResponse receiveKbpsResponse = webClient.get()
|
||||||
|
.uri(buildUri(url,receiveKbpsQuery,start,end,step))
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(PrometheusResponse.class)
|
||||||
|
.block();
|
||||||
|
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())) {
|
||||||
|
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();
|
||||||
|
pointDataMap.put("receiveDate", DateUtil.format(date, "MM/dd HH:mm:ss"));
|
||||||
|
pointDataMap.put("receiveKbps", useRate);
|
||||||
|
receiveDataList.add(pointDataMap);
|
||||||
|
}
|
||||||
|
result.put("receiveData", receiveDataList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//发送带宽 (Kbps)
|
||||||
|
String transmitKbpsQuery = "rate(node_network_transmit_bytes_total{instance=\"" + instance + "\", device=\""+serverProperties.getNetworkCardName()+"\"}[15s]) * 8 / 1000";
|
||||||
|
PrometheusResponse transmitKbpsResponse = webClient.get()
|
||||||
|
.uri(buildUri(url,transmitKbpsQuery,start,end,step))
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(PrometheusResponse.class)
|
||||||
|
.block();
|
||||||
|
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())) {
|
||||||
|
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();
|
||||||
|
pointDataMap.put("receiveDate", DateUtil.format(date, "MM/dd HH:mm:ss"));
|
||||||
|
pointDataMap.put("receiveKbps", useRate);
|
||||||
|
transmitDataList.add(pointDataMap);
|
||||||
|
}
|
||||||
|
result.put("transmitData", transmitDataList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch (Exception e){
|
||||||
|
log.error("获取网络信息错误,请检查Prometheus服务是否正常启动或Java请求参数是否正确,详细堆栈错误为:{}",e.getMessage());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取磁盘使用率
|
* 获取磁盘使用率
|
||||||
*/
|
*/
|
||||||
|
|
@ -261,18 +459,6 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取磁盘吞吐量
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Map<String, Object> getDiskThroughputInfo() {
|
|
||||||
//Expr: irate(node_disk_read_bytes_total{instance="192.168.186.143:9100",job="Prometheus服务器",device=~"[a-z]+|nvme[0-9]+n[0-9]+|mmcblk[0-9]+"}[1m0s])
|
|
||||||
//irate(node_disk_written_bytes_total{instance="192.168.186.143:9100",job="Prometheus服务器",device=~"[a-z]+|nvme[0-9]+n[0-9]+|mmcblk[0-9]+"}[1m0s])
|
|
||||||
return Map.of();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建URI
|
* 构建URI
|
||||||
* @param url
|
* @param url
|
||||||
|
|
@ -287,6 +473,28 @@ public class HostMonitorServiceImpl implements HostMonitorService {
|
||||||
return uri;
|
return uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建URI
|
||||||
|
* @param url
|
||||||
|
* @param query
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
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,
|
||||||
|
query,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
step
|
||||||
|
);
|
||||||
|
URI uri = UriComponentsBuilder.fromHttpUrl(uriAddr)
|
||||||
|
.build()
|
||||||
|
.toUri();
|
||||||
|
return uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
//&start=%d&end=%d&step=%s
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Date date = new Date(1758868629*1000L);
|
Date date = new Date(1758868629*1000L);
|
||||||
System.out.println(DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"));
|
System.out.println(DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"));
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ public class PrometheusResponse {
|
||||||
public static class Result {
|
public static class Result {
|
||||||
private Metric metric;
|
private Metric metric;
|
||||||
private List<Double> value; // 单值查询
|
private List<Double> value; // 单值查询
|
||||||
|
private List<List<Double>> values; // 范围查询
|
||||||
|
|
||||||
public Metric getMetric() {
|
public Metric getMetric() {
|
||||||
return metric;
|
return metric;
|
||||||
|
|
@ -57,11 +58,20 @@ public class PrometheusResponse {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<List<Double>> getValues() {
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValues(List<List<Double>> values) {
|
||||||
|
this.values = values;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Result{" +
|
return "Result{" +
|
||||||
"metric=" + metric +
|
"metric=" + metric +
|
||||||
", value=" + value +
|
", value=" + value +
|
||||||
|
", values=" + values +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,8 +30,10 @@
|
||||||
|
|
||||||
<select id="queryByUserId" parameterType="String">
|
<select id="queryByUserId" parameterType="String">
|
||||||
select sp.perms from sys_permission sp
|
select sp.perms from sys_permission sp
|
||||||
inner join sys_role_permission srp on sp.id = srp.permission_id
|
<if test="userId != null and userId != ''">
|
||||||
inner join sys_user_role sur on srp.role_id = sur.role_id
|
inner join sys_role_permission srp on sp.id = srp.permission_id
|
||||||
|
inner join sys_user_role sur on srp.role_id = sur.role_id
|
||||||
|
</if>
|
||||||
where sp.perms is not null
|
where sp.perms is not null
|
||||||
<if test="userId != null and userId != ''">
|
<if test="userId != null and userId != ''">
|
||||||
and sur.user_id = #{userId}
|
and sur.user_id = #{userId}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user