From c0ee583eab1687039fd4cc5792653d8c66cb73c9 Mon Sep 17 00:00:00 2001 From: yangmengchuan Date: Fri, 17 Jan 2025 11:18:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E7=9B=91=E6=8E=A7=E9=9B=86?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/keyware/htey/HteyApplication.java | 1 - .../htey/config/HertzServerConfig.java | 31 +++ .../controller/hertz/HertzController.java | 37 ++++ .../keyware/htey/entity/hertz/HertzInfo.java | 17 ++ .../htey/entity/hertz/HertzMonitor.java | 17 ++ .../keyware/htey/utli/HertzBeatClient.java | 190 ++++++++++++++++++ .../com/keyware/htey/utli/HertzConstants.java | 17 ++ src/main/resources/application.yml | 5 + 8 files changed, 314 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/keyware/htey/config/HertzServerConfig.java create mode 100644 src/main/java/com/keyware/htey/controller/hertz/HertzController.java create mode 100644 src/main/java/com/keyware/htey/entity/hertz/HertzInfo.java create mode 100644 src/main/java/com/keyware/htey/entity/hertz/HertzMonitor.java create mode 100644 src/main/java/com/keyware/htey/utli/HertzBeatClient.java create mode 100644 src/main/java/com/keyware/htey/utli/HertzConstants.java diff --git a/src/main/java/com/keyware/htey/HteyApplication.java b/src/main/java/com/keyware/htey/HteyApplication.java index e524805..90929bf 100644 --- a/src/main/java/com/keyware/htey/HteyApplication.java +++ b/src/main/java/com/keyware/htey/HteyApplication.java @@ -7,7 +7,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication -@MapperScan({"com.keyware.htey.mybatis.itf"}) public class HteyApplication { public static void main(String[] args) { diff --git a/src/main/java/com/keyware/htey/config/HertzServerConfig.java b/src/main/java/com/keyware/htey/config/HertzServerConfig.java new file mode 100644 index 0000000..90aeecd --- /dev/null +++ b/src/main/java/com/keyware/htey/config/HertzServerConfig.java @@ -0,0 +1,31 @@ +package com.keyware.htey.config; + + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * 监控三方服务配置 + */ +@Data +@Configuration +@ConfigurationProperties(prefix = "hertz") +public class HertzServerConfig { + /** + * 服务地址 + */ + private String server; + /** + * 服务账号 + */ + private String userName; + /** + * 服务密码 + */ + private String password; + /** + * 监控系统id + */ + private String monitorId; +} diff --git a/src/main/java/com/keyware/htey/controller/hertz/HertzController.java b/src/main/java/com/keyware/htey/controller/hertz/HertzController.java new file mode 100644 index 0000000..8e5113c --- /dev/null +++ b/src/main/java/com/keyware/htey/controller/hertz/HertzController.java @@ -0,0 +1,37 @@ +package com.keyware.htey.controller.hertz; + +import com.keyware.htey.config.HertzServerConfig; +import com.keyware.htey.utli.HertzBeatClient; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.io.IOException; +import java.util.Map; + +@Controller +@RequestMapping("/hertz") +@Slf4j +@Tag(name = "hertz API", description = "系统监视接口") +public class HertzController { + @Autowired + private HertzBeatClient hertzBeatClient; + @Autowired + private HertzServerConfig hertzServerConfig; + @GetMapping("/selectByMonitorId") + @ResponseBody + @Operation(summary = "系统监视", description = "系统监视") + @ApiResponse(responseCode = "200", description = "系统监视") + public Object selectAll() throws IOException { + Map monitorData = hertzBeatClient.getMonitorData(); + // 将 JSON 字符串转换为 Map + System.out.println("1"); + return monitorData; + } +} diff --git a/src/main/java/com/keyware/htey/entity/hertz/HertzInfo.java b/src/main/java/com/keyware/htey/entity/hertz/HertzInfo.java new file mode 100644 index 0000000..5b73e7c --- /dev/null +++ b/src/main/java/com/keyware/htey/entity/hertz/HertzInfo.java @@ -0,0 +1,17 @@ +package com.keyware.htey.entity.hertz; + +import lombok.Data; + +@Data +public class HertzInfo { + private String name; + private String value; + + public HertzInfo(String name, String value) { + this.name = name; + this.value = value; + } + + public HertzInfo() { + } +} diff --git a/src/main/java/com/keyware/htey/entity/hertz/HertzMonitor.java b/src/main/java/com/keyware/htey/entity/hertz/HertzMonitor.java new file mode 100644 index 0000000..96f4234 --- /dev/null +++ b/src/main/java/com/keyware/htey/entity/hertz/HertzMonitor.java @@ -0,0 +1,17 @@ +package com.keyware.htey.entity.hertz; + +import lombok.Data; + +@Data +public class HertzMonitor { + //系统监视id + private String id; + //系统监视名称 + private String name; + //系统监视ip + private String host; + //系统监视周期 + private String intervals; + //系统类型 + private String app; +} diff --git a/src/main/java/com/keyware/htey/utli/HertzBeatClient.java b/src/main/java/com/keyware/htey/utli/HertzBeatClient.java new file mode 100644 index 0000000..2759492 --- /dev/null +++ b/src/main/java/com/keyware/htey/utli/HertzBeatClient.java @@ -0,0 +1,190 @@ +package com.keyware.htey.utli; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.keyware.htey.config.HertzServerConfig; +import com.keyware.htey.entity.hertz.HertzInfo; +import com.keyware.htey.entity.hertz.HertzMonitor; +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.util.CollectionUtils; +import org.springframework.web.client.RestTemplate; + +import java.io.IOException; +import java.util.*; + +/** + * @author yangmengchuan + * @date 2025/1/16 + * @description 系统监控工具类 + */ +@Data +@Configuration +public class HertzBeatClient { + @Autowired + private RestTemplate restTemplate; + @Autowired + private HertzServerConfig hertzServerConfig; + + + /** + * 获取监控数据 + * + * @return 监控数据的 JSON 字符串 + */ + public Map getMonitorData() throws IOException { + String apiUrl = hertzServerConfig.getServer() + HertzConstants.MONITOR_DETAIL_API; + ResponseEntity response = getStringResponseEntity(apiUrl, hertzServerConfig.getMonitorId()); + // 返回响应数据 + if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) { + return getMonitorDataMap(response.getBody()); + } else { + throw new RuntimeException("Failed to fetch monitor data: " + response.getStatusCode()); + } + } + + /** + * 对返回值进行处理 + */ + public Map getMonitorDataMap(String responseData) throws IOException { + Map resultMap = new HashMap<>(); + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + //将返回值处理为map集合 + Map map = objectMapper.readValue(responseData, new TypeReference>() {}); + LinkedHashMap dataMap = (LinkedHashMap) map.get("data"); + //获取监控的基本信息 + LinkedHashMap monitorMap = (LinkedHashMap) dataMap.get("monitor"); + HertzMonitor hertzMonitor = objectMapper.convertValue(monitorMap, HertzMonitor.class); + resultMap.put("监控任务信息",hertzMonitor); + //获取指标名 + List metricsList = (ArrayList)dataMap.get("metrics"); + if(!CollectionUtils.isEmpty(metricsList)){ + for (Object metrics : metricsList) { + getMetricsData(hertzServerConfig.getMonitorId(), metrics.toString(),resultMap,hertzMonitor.getApp()); + } + } + return resultMap; + } + + /** + * 获取指定指标的具体数据 + * @param monitorId + * @param metricName + * @param resultMap + * @param appType + * @return + */ + public void getMetricsData(String monitorId, String metricName,Map resultMap,String appType) { + String apiUrl = hertzServerConfig.getServer() + HertzConstants.MONITOR_GET_MERICS; + ResponseEntity response = getStringResponseEntity(apiUrl, monitorId, metricName); + // 返回响应数据 + if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) { + try { + // 解析 JSON 数据 + ObjectMapper objectMapper = new ObjectMapper(); + Map metricsData = objectMapper.readValue(response.getBody(), new TypeReference>() {}); + String mapKey = getMapKey(metricName); + resultMap.put(mapKey,setHertzBasic(metricsData)); + } catch (Exception e) { + throw new RuntimeException("Failed to parse response: " + e.getMessage()); + } + } else { + throw new RuntimeException("Failed to fetch metric data: " + response.getStatusCode()); + } + } + + /** + * 获取指标名称 + * @param metricName + * @return + */ + private String getMapKey(String metricName) { + switch (metricName){ + case "basic": + return "系统基本信息"; + case "cpu": + return "CPU信息"; + case "memory": + return "内存信息"; + case "disk": + return "磁盘信息"; + case "interface": + return "网卡信息"; + case "disk_free": + return "文件系统"; + case "top_cpu_process": + return "Top10 cpu进程"; + case "top_mem_process": + return "Top10 内存进程"; + default: + return null; + } + } + + /** + * 对系统基本信息进行设置 + * @param metricsData + * @return + */ + private List setHertzBasic(Map metricsData) { + List list = new ArrayList<>(); + Map data = (Map) metricsData.get("data"); + if(data != null && data.size() != 0){ + //获取到属性名 + List fieldList = (ArrayList)data.get("fields"); + if(!CollectionUtils.isEmpty(fieldList)){ + List valueRowsMap = (ArrayList)data.get("valueRows"); + if(!CollectionUtils.isEmpty(valueRowsMap) && valueRowsMap.size() == 1){ + LinkedHashMap values = valueRowsMap.get(0); + if(values != null && values.size() == 2){ + //获取到属性值 + List valueRows = (ArrayList)values.get("values"); + if(!CollectionUtils.isEmpty(values) && valueRows.size() == fieldList.size()){ + for (int i = 0; i < fieldList.size(); i++) { + LinkedHashMap fieldMap = fieldList.get(i); + LinkedHashMap valueMap = valueRows.get(i); + //获取到属性key + String keyName = (String)fieldMap.get("name"); + //获取到属性value + String keyValue = (String)valueMap.get("origin"); + list.add(new HertzInfo(keyName,keyValue)); + } + } + } + } + } + } + return list; + } + + /** + * 发送get请求 + * @param apiUrl + * @param args + * @return + */ + private ResponseEntity getStringResponseEntity(String apiUrl,String... args) { + // 设置请求头 + HttpHeaders headers = new HttpHeaders(); + headers.setBasicAuth(hertzServerConfig.getUserName(), hertzServerConfig.getPassword()); + // 发送 GET 请求 + HttpEntity entity = new HttpEntity<>(headers); + ResponseEntity response = restTemplate.exchange( + apiUrl, + HttpMethod.GET, + entity, + String.class, + args + ); + return response; + } + + +} diff --git a/src/main/java/com/keyware/htey/utli/HertzConstants.java b/src/main/java/com/keyware/htey/utli/HertzConstants.java new file mode 100644 index 0000000..e7133d4 --- /dev/null +++ b/src/main/java/com/keyware/htey/utli/HertzConstants.java @@ -0,0 +1,17 @@ +package com.keyware.htey.utli; + + +/** + * 监控服务通用常量 + */ +public interface HertzConstants { + /** + * 获取指定指标数据 + */ + String MONITOR_GET_MERICS = "/api/monitor/{monitorId}/metrics/{metricName}"; + /** + * 获取监控详情接口 + */ + String MONITOR_DETAIL_API = "/api/monitor/{monitorId}"; + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index dcdf10e..e20e18f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -40,3 +40,8 @@ springdoc: api-docs: path: /v3/api-docs # OpenAPI JSON 的访问路径 enabled: true # 是否启用 OpenAPI JSON +hertz: + server: http://172.16.36.140:1157 + userName: admin + password: hertzbeat + monitorId: 467028128487936