Compare commits
No commits in common. '6a6095784b88fe048f359f878eb7249bd939cc35' and 'bd3c599bf4cfb328f75d6230c73090116cdc2aca' have entirely different histories.
6a6095784b
...
bd3c599bf4
@ -1,31 +0,0 @@ |
||||
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; |
||||
} |
@ -1,37 +0,0 @@ |
||||
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; |
||||
} |
||||
} |
@ -1,17 +0,0 @@ |
||||
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() { |
||||
} |
||||
} |
@ -1,17 +0,0 @@ |
||||
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; |
||||
} |
@ -1,190 +0,0 @@ |
||||
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; |
||||
} |
||||
|
||||
|
||||
} |
@ -1,17 +0,0 @@ |
||||
package com.keyware.htey.utli; |
||||
|
||||
|
||||
/** |
||||
* 监控服务通用常量 |
||||
*/ |
||||
public interface HertzConstants { |
||||
/** |
||||
* 获取指定指标数据 |
||||
*/ |
||||
String MONITOR_GET_MERICS = "/api/monitor/{monitorId}/metrics/{metricName}"; |
||||
/** |
||||
* 获取监控详情接口 |
||||
*/ |
||||
String MONITOR_DETAIL_API = "/api/monitor/{monitorId}"; |
||||
|
||||
} |
Loading…
Reference in new issue