Compare commits

..

2 Commits

Author SHA1 Message Date
yangmengchuan 6a6095784b Merge remote-tracking branch 'origin/main' 2 days ago
yangmengchuan c0ee583eab 系统监控集成 2 days ago
  1. 1
      src/main/java/com/keyware/htey/HteyApplication.java
  2. 31
      src/main/java/com/keyware/htey/config/HertzServerConfig.java
  3. 37
      src/main/java/com/keyware/htey/controller/hertz/HertzController.java
  4. 17
      src/main/java/com/keyware/htey/entity/hertz/HertzInfo.java
  5. 17
      src/main/java/com/keyware/htey/entity/hertz/HertzMonitor.java
  6. 190
      src/main/java/com/keyware/htey/utli/HertzBeatClient.java
  7. 17
      src/main/java/com/keyware/htey/utli/HertzConstants.java
  8. 5
      src/main/resources/application.yml

@ -7,7 +7,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@SpringBootApplication @SpringBootApplication
@MapperScan({"com.keyware.htey.mybatis.itf"})
public class HteyApplication { public class HteyApplication {
public static void main(String[] args) { public static void main(String[] args) {

@ -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;
}

@ -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<String, Object> monitorData = hertzBeatClient.getMonitorData();
// 将 JSON 字符串转换为 Map
System.out.println("1");
return monitorData;
}
}

@ -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() {
}
}

@ -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;
}

@ -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<String, Object> getMonitorData() throws IOException {
String apiUrl = hertzServerConfig.getServer() + HertzConstants.MONITOR_DETAIL_API;
ResponseEntity<String> 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<String,Object> getMonitorDataMap(String responseData) throws IOException {
Map<String, Object> resultMap = new HashMap<>();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//将返回值处理为map集合
Map<String, Object> map = objectMapper.readValue(responseData, new TypeReference<Map<String, Object>>() {});
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<String> response = getStringResponseEntity(apiUrl, monitorId, metricName);
// 返回响应数据
if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {
try {
// 解析 JSON 数据
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> metricsData = objectMapper.readValue(response.getBody(), new TypeReference<Map<String, Object>>() {});
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<String, Object> metricsData) {
List<HertzInfo> list = new ArrayList<>();
Map<String, Object> data = (Map<String, Object>) metricsData.get("data");
if(data != null && data.size() != 0){
//获取到属性名
List<LinkedHashMap> fieldList = (ArrayList)data.get("fields");
if(!CollectionUtils.isEmpty(fieldList)){
List<LinkedHashMap> valueRowsMap = (ArrayList)data.get("valueRows");
if(!CollectionUtils.isEmpty(valueRowsMap) && valueRowsMap.size() == 1){
LinkedHashMap values = valueRowsMap.get(0);
if(values != null && values.size() == 2){
//获取到属性值
List<LinkedHashMap> 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<String> getStringResponseEntity(String apiUrl,String... args) {
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(hertzServerConfig.getUserName(), hertzServerConfig.getPassword());
// 发送 GET 请求
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(
apiUrl,
HttpMethod.GET,
entity,
String.class,
args
);
return response;
}
}

@ -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}";
}

@ -40,3 +40,8 @@ springdoc:
api-docs: api-docs:
path: /v3/api-docs # OpenAPI JSON 的访问路径 path: /v3/api-docs # OpenAPI JSON 的访问路径
enabled: true # 是否启用 OpenAPI JSON enabled: true # 是否启用 OpenAPI JSON
hertz:
server: http://172.16.36.140:1157
userName: admin
password: hertzbeat
monitorId: 467028128487936

Loading…
Cancel
Save