parent
64d88e366c
commit
970bc7818a
|
@ -86,7 +86,6 @@ import java.lang.annotation.Target;
|
||||||
*/
|
*/
|
||||||
@Target(ElementType.FIELD)
|
@Target(ElementType.FIELD)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@JsonSerialize(using = AutoExternStringJsonProcess.class)
|
|
||||||
public @interface AutoExternString {
|
public @interface AutoExternString {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -73,6 +73,8 @@ public class AutoExternStringJsonProcess extends JsonSerializer<Number> implemen
|
||||||
*/
|
*/
|
||||||
private Class<? extends Enum<?>> enumClass = null;
|
private Class<? extends Enum<?>> enumClass = null;
|
||||||
|
|
||||||
|
private boolean enableFormat = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认构造方法。
|
* 默认构造方法。
|
||||||
*/
|
*/
|
||||||
|
@ -92,12 +94,14 @@ public class AutoExternStringJsonProcess extends JsonSerializer<Number> implemen
|
||||||
String suffix,
|
String suffix,
|
||||||
UtilsFormatType formatType,
|
UtilsFormatType formatType,
|
||||||
String units,
|
String units,
|
||||||
Class<? extends Enum<?>> classType) {
|
Class<? extends Enum<?>> classType,
|
||||||
|
boolean enableFormat) {
|
||||||
this.prefix = prefix;
|
this.prefix = prefix;
|
||||||
this.suffix = suffix;
|
this.suffix = suffix;
|
||||||
this.formatType = formatType;
|
this.formatType = formatType;
|
||||||
this.enumClass = classType;
|
this.enumClass = classType;
|
||||||
this.units = units;
|
this.units = units;
|
||||||
|
this.enableFormat = enableFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -115,6 +119,7 @@ public class AutoExternStringJsonProcess extends JsonSerializer<Number> implemen
|
||||||
public void serialize(Number value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
public void serialize(Number value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
||||||
Long numericValue = null;
|
Long numericValue = null;
|
||||||
|
|
||||||
|
|
||||||
// 将字段值转换为 Long 类型
|
// 将字段值转换为 Long 类型
|
||||||
if (value instanceof Long) {
|
if (value instanceof Long) {
|
||||||
numericValue = (Long) value;
|
numericValue = (Long) value;
|
||||||
|
@ -126,6 +131,7 @@ public class AutoExternStringJsonProcess extends JsonSerializer<Number> implemen
|
||||||
// 写入原始字段值
|
// 写入原始字段值
|
||||||
jsonGenerator.writeNumber(numericValue);
|
jsonGenerator.writeNumber(numericValue);
|
||||||
|
|
||||||
|
if (enableFormat) {
|
||||||
// 生成额外字段名
|
// 生成额外字段名
|
||||||
String filedName = prefix + jsonGenerator.getOutputContext().getCurrentName() + suffix;
|
String filedName = prefix + jsonGenerator.getOutputContext().getCurrentName() + suffix;
|
||||||
jsonGenerator.writeFieldName(filedName);
|
jsonGenerator.writeFieldName(filedName);
|
||||||
|
@ -158,6 +164,7 @@ public class AutoExternStringJsonProcess extends JsonSerializer<Number> implemen
|
||||||
} else {
|
} else {
|
||||||
jsonGenerator.writeString(numericValue.toString());
|
jsonGenerator.writeString(numericValue.toString());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// 如果值为空或不支持,则使用默认序列化方式
|
// 如果值为空或不支持,则使用默认序列化方式
|
||||||
serializerProvider.defaultSerializeValue(value, jsonGenerator);
|
serializerProvider.defaultSerializeValue(value, jsonGenerator);
|
||||||
|
@ -183,7 +190,7 @@ public class AutoExternStringJsonProcess extends JsonSerializer<Number> implemen
|
||||||
if (annotation != null) {
|
if (annotation != null) {
|
||||||
// 根据注解配置创建新的序列化器
|
// 根据注解配置创建新的序列化器
|
||||||
return new AutoExternStringJsonProcess(annotation.prefix(), annotation.suffix(), annotation.format(), annotation.units(),
|
return new AutoExternStringJsonProcess(annotation.prefix(), annotation.suffix(), annotation.format(), annotation.units(),
|
||||||
annotation.enumClass());
|
annotation.enumClass(), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 如果字段不存在注解,返回默认实例
|
// 如果字段不存在注解,返回默认实例
|
||||||
|
|
|
@ -2,7 +2,10 @@ package com.cmhi.magent.controller;
|
||||||
|
|
||||||
import com.cmhi.magent.annotation.EncryptionProtocol;
|
import com.cmhi.magent.annotation.EncryptionProtocol;
|
||||||
import com.cmhi.magent.annotation.OperationLogAnnotation;
|
import com.cmhi.magent.annotation.OperationLogAnnotation;
|
||||||
|
import com.cmhi.magent.pojo.vo.GetFileStoreInfoResp;
|
||||||
import com.cmhi.magent.pojo.vo.GetHwInfoResp;
|
import com.cmhi.magent.pojo.vo.GetHwInfoResp;
|
||||||
|
import com.cmhi.magent.pojo.vo.GetMemoryInfoResp;
|
||||||
|
import com.cmhi.magent.pojo.vo.GetNetworkInfoResp;
|
||||||
import com.cmhi.magent.pojo.vo.GetOsInfoResp;
|
import com.cmhi.magent.pojo.vo.GetOsInfoResp;
|
||||||
import com.cmhi.magent.pojo.vo.GetProcessorInfoResp;
|
import com.cmhi.magent.pojo.vo.GetProcessorInfoResp;
|
||||||
import com.cmhi.magent.pojo.vo.ProtocolResp;
|
import com.cmhi.magent.pojo.vo.ProtocolResp;
|
||||||
|
@ -100,4 +103,52 @@ public class SystemInfoApi {
|
||||||
public ProtocolResp<GetHwInfoResp> getHwInfo() {
|
public ProtocolResp<GetHwInfoResp> getHwInfo() {
|
||||||
return ProtocolResp.result(GetHwInfoResp.builder().hwInfo(systemInfoService.getHardwareInfo()).build());
|
return ProtocolResp.result(GetHwInfoResp.builder().hwInfo(systemInfoService.getHardwareInfo()).build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取网络信息。
|
||||||
|
*
|
||||||
|
* <p>该接口返回系统网络相关信息。返回数据通过加密协议进行保护,并记录操作日志。</p>
|
||||||
|
*
|
||||||
|
* @return 封装了网络信息的响应对象。
|
||||||
|
*/
|
||||||
|
@GetMapping("/networkInfo")
|
||||||
|
@EncryptionProtocol
|
||||||
|
@OperationLogAnnotation(OperationModule = "SYSLOG_MOD_SYSINFO",
|
||||||
|
OperationType = "SYSLOG_TYPE_READ",
|
||||||
|
OperationDesc = "SYSLOG_DESC_GET_HW_INFO")
|
||||||
|
public ProtocolResp<GetNetworkInfoResp> getNetworkInfo() {
|
||||||
|
return ProtocolResp.result(GetNetworkInfoResp.builder().networkInfo(systemInfoService.getNetworkInfo()).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取内存信息。
|
||||||
|
*
|
||||||
|
* <p>该接口返回系统内存相关信息。返回数据通过加密协议进行保护,并记录操作日志。</p>
|
||||||
|
*
|
||||||
|
* @return 封装了网络信息的响应对象。
|
||||||
|
*/
|
||||||
|
@GetMapping("/memoryInfo")
|
||||||
|
@EncryptionProtocol
|
||||||
|
@OperationLogAnnotation(OperationModule = "SYSLOG_MOD_SYSINFO",
|
||||||
|
OperationType = "SYSLOG_TYPE_READ",
|
||||||
|
OperationDesc = "SYSLOG_DESC_GET_HW_INFO")
|
||||||
|
public ProtocolResp<GetMemoryInfoResp> geMemoryInfo() {
|
||||||
|
return ProtocolResp.result(GetMemoryInfoResp.builder().memoryInfo(systemInfoService.getMemoryInfo()).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件存储信息。
|
||||||
|
*
|
||||||
|
* <p>该接口返回系统文件系统以及磁盘相关信息。返回数据通过加密协议进行保护,并记录操作日志。</p>
|
||||||
|
*
|
||||||
|
* @return 封装了文件存储信息的响应对象。
|
||||||
|
*/
|
||||||
|
@GetMapping("/fileStoreInfo")
|
||||||
|
@EncryptionProtocol
|
||||||
|
@OperationLogAnnotation(OperationModule = "SYSLOG_MOD_SYSINFO",
|
||||||
|
OperationType = "SYSLOG_TYPE_READ",
|
||||||
|
OperationDesc = "SYSLOG_DESC_GET_HW_INFO")
|
||||||
|
public ProtocolResp<GetFileStoreInfoResp> geFileStoreInfo() {
|
||||||
|
return ProtocolResp.result(GetFileStoreInfoResp.builder().fileStoreInfo(systemInfoService.getFileStoreInfo()).build());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,145 +5,55 @@ import com.cmhi.magent.common.UtilsFormatType;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 网络信息类。
|
* NetworkInfo 类表示系统的网络信息,包括网络接口、主机名、域名、DNS 服务器和网关信息。
|
||||||
* <p>
|
* <p>
|
||||||
* 此类用于封装网络的详细信息,包括网络名称、网络类型、网络状态、网络速度等。
|
* 该类通过 Lombok 的 {@code @Data} 注解自动生成 getter/setter 方法。
|
||||||
* 主要用于监控系统网络的状态,例如网络连接的速度和稳定性。
|
* 同时集成了 Swagger 3 的注释以支持接口文档的生成。
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>
|
|
||||||
* 依赖于 Lombok 的 {@code @Data} 注解,自动生成以下方法:
|
|
||||||
* </p>
|
|
||||||
* <ul>
|
|
||||||
* <li>Getter 和 Setter 方法</li>
|
|
||||||
* <li>{@code toString()} 方法</li>
|
|
||||||
* <li>{@code equals()} 和 {@code hashCode()} 方法</li>
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* 此类还集成了自定义注解 {@link AutoExternString},用于在 JSON 序列化时对字段进行额外处理。
|
|
||||||
* 例如,自动将网络速度值格式化为更易识别的单位(如 Mbps、Kbps)。
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* <p><b>使用示例:</b></p>
|
|
||||||
* <pre>
|
|
||||||
* NetworkInfo networkInfo = new NetworkInfo();
|
|
||||||
* networkInfo.setNetworkName("Wi-Fi");
|
|
||||||
* networkInfo.setNetworkType("Wireless");
|
|
||||||
* networkInfo.setNetworkSpeed(1024L); // 设置网络速度为 1 Mbps
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
* <p><b>JSON 输出示例:</b></p>
|
|
||||||
* <pre>
|
|
||||||
* {
|
|
||||||
* "networkName": "Wi-Fi",
|
|
||||||
* "networkType": "Wireless",
|
|
||||||
* "networkSpeed": 1024,
|
|
||||||
* "networkSpeedStr": "1 Mbps"
|
|
||||||
* }
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
* <p>注意:{@code @AutoExternString} 注解会自动根据字段值转换为适合的单位,并生成额外的格式化字符串。</p>
|
|
||||||
*
|
|
||||||
* <p>例如,值 {@code 1024L}(字节/秒)在 JSON 中将自动格式化为 {@code "1 Mbps"}。</p>
|
|
||||||
*
|
|
||||||
* @author huangxin@cmhi.chinamobile.com
|
* @author huangxin@cmhi.chinamobile.com
|
||||||
* @version 1.0.0
|
* * @version 1.0.0
|
||||||
* @since 2025-01-13
|
* * @since 2025-01-13
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class NetworkInfo {
|
public class NetworkInfo {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 网络名称。
|
* 系统中所有的网络接口信息。
|
||||||
* <p>
|
|
||||||
* 该字段记录网络的名称,通常用于标识不同的网络连接,如 Wi-Fi、以太网等。
|
|
||||||
* 在 JSON 序列化时,该字段直接输出原始值。
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* <p><b>示例值:</b></p>
|
|
||||||
* <ul>
|
|
||||||
* <li>{@code "Wi-Fi"}。</li>
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* <p><b>JSON 输出示例:</b></p>
|
|
||||||
* <pre>
|
|
||||||
* {
|
|
||||||
* "networkName": "Wi-Fi"
|
|
||||||
* }
|
|
||||||
* </pre>
|
|
||||||
*/
|
*/
|
||||||
@Schema(description = "网络名称。", example = "Wi-Fi")
|
@Schema(description = "系统中所有的网络接口信息")
|
||||||
private String networkName;
|
private List<IfInterfaceInfo> ifNetwork;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 网络类型。
|
* 系统的主机名。
|
||||||
* <p>
|
|
||||||
* 该字段记录网络的类型,如无线网络(Wireless)、有线网络(Wired)等。
|
|
||||||
* 在 JSON 序列化时,该字段直接输出原始值。
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* <p><b>示例值:</b></p>
|
|
||||||
* <ul>
|
|
||||||
* <li>{@code "Wireless"}。</li>
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* <p><b>JSON 输出示例:</b></p>
|
|
||||||
* <pre>
|
|
||||||
* {
|
|
||||||
* "networkType": "Wireless"
|
|
||||||
* }
|
|
||||||
* </pre>
|
|
||||||
*/
|
*/
|
||||||
@Schema(description = "网络类型。", example = "Wireless")
|
@Schema(description = "系统的主机名")
|
||||||
private String networkType;
|
private String hostName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 网络状态。
|
* 系统所在的域名。
|
||||||
* <p>
|
|
||||||
* 该字段记录网络的当前状态,如已连接(Connected)、断开连接(Disconnected)等。
|
|
||||||
* 在 JSON 序列化时,该字段直接输出原始值。
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* <p><b>示例值:</b></p>
|
|
||||||
* <ul>
|
|
||||||
* <li>{@code "Connected"}。</li>
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* <p><b>JSON 输出示例:</b></p>
|
|
||||||
* <pre>
|
|
||||||
* {
|
|
||||||
* "networkStatus": "Connected"
|
|
||||||
* }
|
|
||||||
* </pre>
|
|
||||||
*/
|
*/
|
||||||
@Schema(description = "网络状态。", example = "Connected")
|
@Schema(description = "系统所在的域名")
|
||||||
private String networkStatus;
|
private String domainName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 网络速度(单位:字节/秒)。
|
* 系统配置的 DNS 服务器地址列表。
|
||||||
* <p>
|
|
||||||
* 该字段记录网络的当前速度,通常用于表示网络连接的带宽或数据传输速率。
|
|
||||||
* 在 JSON 序列化时,通过 {@link AutoExternString} 注解生成一个格式化后的额外字段。
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* <p><b>示例值:</b></p>
|
|
||||||
* <ul>
|
|
||||||
* <li>{@code 1024L}(表示 1 Mbps 网络速度)。</li>
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* <p><b>JSON 输出示例:</b></p>
|
|
||||||
* <pre>
|
|
||||||
* {
|
|
||||||
* "networkSpeed": 1024,
|
|
||||||
* "networkSpeedStr": "1 Mbps"
|
|
||||||
* }
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
* @see AutoExternString
|
|
||||||
*/
|
*/
|
||||||
@Schema(description = "网络速度(单位:字节/秒)。", example = "1024")
|
@Schema(description = "系统配置的 DNS 服务器地址列表")
|
||||||
@AutoExternString(format = UtilsFormatType.FORMAT_BYTES)
|
private String[] dnsServers;
|
||||||
private Long networkSpeed;
|
|
||||||
|
/**
|
||||||
|
* 系统的 IPv4 默认网关地址。
|
||||||
|
*/
|
||||||
|
@Schema(description = "系统的 IPv4 默认网关地址")
|
||||||
|
private String ipv4DefaultGateway;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统的 IPv6 默认网关地址。
|
||||||
|
*/
|
||||||
|
@Schema(description = "系统的 IPv6 默认网关地址")
|
||||||
|
private String ipv6DefaultGateway;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.cmhi.magent.pojo.po;
|
package com.cmhi.magent.pojo.po;
|
||||||
|
|
||||||
|
import com.cmhi.magent.annotation.AutoExternString;
|
||||||
|
import com.cmhi.magent.common.UtilsFormatType;
|
||||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
@ -116,6 +118,7 @@ public class ProcessorInfo {
|
||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
@Schema(description = "处理器的标称主频(单位:Hz)。", example = "3800000000")
|
@Schema(description = "处理器的标称主频(单位:Hz)。", example = "3800000000")
|
||||||
|
@AutoExternString(format = UtilsFormatType.FORMAT_UNITS, units = "Hz")
|
||||||
private Long cpuVendorFreq;
|
private Long cpuVendorFreq;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
package com.cmhi.magent.pojo.vo;
|
||||||
|
|
||||||
|
import com.cmhi.magent.pojo.po.BaseRespStatus;
|
||||||
|
import com.cmhi.magent.pojo.po.FileStoreInfo;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件存储信息响应类 {@code GetFileStoreInfoResp}。
|
||||||
|
* <p>
|
||||||
|
* 用于封装文件存储信息的响应结果,继承自 {@link BaseRespStatus},包含状态码、消息以及文件存储的详细信息。
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 核心功能:
|
||||||
|
* <ul>
|
||||||
|
* <li>继承基础响应状态类,统一包含状态码和消息。</li>
|
||||||
|
* <li>封装文件存储相关的详细信息,通过 {@code fileStoreInfo} 字段返回。</li>
|
||||||
|
* </ul>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 使用场景:
|
||||||
|
* <ul>
|
||||||
|
* <li>用于 RESTful API 的文件存储信息查询接口。</li>
|
||||||
|
* <li>适用于系统监控模块,获取文件存储的详细配置信息,例如总存储空间、可用存储空间、文件系统类型等。</li>
|
||||||
|
* </ul>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author huangxin@cmhi.chinamobile.com
|
||||||
|
* @version 1.0.0
|
||||||
|
* @since 2025-01-07
|
||||||
|
*/
|
||||||
|
@Schema(name = "文件存储信息", description = "包含文件存储的详细信息,如总存储空间、可用存储空间、文件系统类型等。")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@JsonPropertyOrder({"fileStoreInfo", "status", "message"})
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class GetFileStoreInfoResp extends BaseRespStatus {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件存储信息内容。
|
||||||
|
* <p>
|
||||||
|
* 包含文件存储的详细信息,例如总存储空间、可用存储空间、文件系统类型等。
|
||||||
|
* 类型为 {@link FileStoreInfo}。
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
private FileStoreInfo fileStoreInfo;
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
package com.cmhi.magent.pojo.vo;
|
||||||
|
|
||||||
|
import com.cmhi.magent.pojo.po.BaseRespStatus;
|
||||||
|
import com.cmhi.magent.pojo.po.MemoryInfo;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 内存信息响应类 {@code GetMemoryInfoResp}。
|
||||||
|
* <p>
|
||||||
|
* 用于封装内存信息的响应结果,继承自 {@link BaseRespStatus},包含状态码、消息以及内存的详细信息。
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 核心功能:
|
||||||
|
* <ul>
|
||||||
|
* <li>继承基础响应状态类,统一包含状态码和消息。</li>
|
||||||
|
* <li>封装内存相关的详细信息,通过 {@code memoryInfo} 字段返回。</li>
|
||||||
|
* </ul>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 使用场景:
|
||||||
|
* <ul>
|
||||||
|
* <li>用于 RESTful API 的内存信息查询接口。</li>
|
||||||
|
* <li>适用于系统监控模块,获取内存的详细配置信息,例如总内存、可用内存、内存使用率等。</li>
|
||||||
|
* </ul>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author huangxin@cmhi.chinamobile.com
|
||||||
|
* @version 1.0.0
|
||||||
|
* @since 2025-01-07
|
||||||
|
*/
|
||||||
|
@Schema(name = "内存信息", description = "包含内存的详细信息,如总内存、可用内存、内存使用率等。")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@JsonPropertyOrder({"memoryInfo", "status", "message"})
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class GetMemoryInfoResp extends BaseRespStatus {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 内存信息内容。
|
||||||
|
* <p>
|
||||||
|
* 包含内存的详细信息,例如总内存、可用内存、内存使用率等。
|
||||||
|
* 类型为 {@link MemoryInfo}。
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
private MemoryInfo memoryInfo;
|
||||||
|
}
|
|
@ -0,0 +1,74 @@
|
||||||
|
package com.cmhi.magent.pojo.vo;
|
||||||
|
|
||||||
|
import com.cmhi.magent.pojo.po.BaseRespStatus;
|
||||||
|
import com.cmhi.magent.pojo.po.NetworkInfo;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 网络信息响应类 {@code GetNetworkInfoResp}。
|
||||||
|
* <p>
|
||||||
|
* 用于封装网络信息的响应结果,继承自 {@link BaseRespStatus},包含状态码、消息以及网络的详细信息。
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 核心功能:
|
||||||
|
* <ul>
|
||||||
|
* <li>继承基础响应状态类,统一包含状态码和消息。</li>
|
||||||
|
* <li>封装网络相关的详细信息,通过 {@code networkInfo} 字段返回。</li>
|
||||||
|
* </ul>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 使用场景:
|
||||||
|
* <ul>
|
||||||
|
* <li>用于 RESTful API 的网络信息查询接口。</li>
|
||||||
|
* <li>适用于系统监控模块,获取网络的详细配置信息,例如IP地址、MAC地址、网络接口等。</li>
|
||||||
|
* </ul>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* JSON 输出示例:
|
||||||
|
* <pre>
|
||||||
|
* {
|
||||||
|
* "status": "SUCCESS",
|
||||||
|
* "message": "Network information retrieved successfully",
|
||||||
|
* "networkInfo": {
|
||||||
|
* "ipAddress": "192.168.1.100",
|
||||||
|
* "macAddress": "00:11:22:33:44:55",
|
||||||
|
* "networkInterfaces": [
|
||||||
|
* "eth0",
|
||||||
|
* "wlan0"
|
||||||
|
* ]
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author huangxin@cmhi.chinamobile.com
|
||||||
|
* @version 1.0.0
|
||||||
|
* @since 2025-01-07
|
||||||
|
*/
|
||||||
|
@Schema(name = "网络信息", description = "包含网络的详细信息,如IP地址、MAC地址、网络接口等。")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@JsonPropertyOrder({"networkInfo", "status", "message"})
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class GetNetworkInfoResp extends BaseRespStatus {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 网络信息内容。
|
||||||
|
* <p>
|
||||||
|
* 包含网络的详细信息,例如IP地址、MAC地址、网络接口等。
|
||||||
|
* 类型为 {@link NetworkInfo}。
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
private NetworkInfo networkInfo;
|
||||||
|
}
|
|
@ -1,6 +1,9 @@
|
||||||
package com.cmhi.magent.service;
|
package com.cmhi.magent.service;
|
||||||
|
|
||||||
|
import com.cmhi.magent.pojo.po.FileStoreInfo;
|
||||||
import com.cmhi.magent.pojo.po.HwInfo;
|
import com.cmhi.magent.pojo.po.HwInfo;
|
||||||
|
import com.cmhi.magent.pojo.po.MemoryInfo;
|
||||||
|
import com.cmhi.magent.pojo.po.NetworkInfo;
|
||||||
import com.cmhi.magent.pojo.po.ProcessorInfo;
|
import com.cmhi.magent.pojo.po.ProcessorInfo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,4 +58,32 @@ public interface SystemInfoService {
|
||||||
* @return 硬件信息对象
|
* @return 硬件信息对象
|
||||||
*/
|
*/
|
||||||
HwInfo getHardwareInfo();
|
HwInfo getHardwareInfo();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取网络信息。
|
||||||
|
* <p>
|
||||||
|
* 封装网络的相关信息,如网络名称、网络类型、网络状态、网络速度等。结果以 {@link NetworkInfo} 对象形式返回。
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return 网络信息对象
|
||||||
|
*/
|
||||||
|
NetworkInfo getNetworkInfo();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取内存信息
|
||||||
|
* <p>
|
||||||
|
* 封装内存的相关信息,如物理内存大小,可用内存,当前内存占用情况等。结果以 {@link MemoryInfo} 对象形式返回。
|
||||||
|
* </p>
|
||||||
|
* @return 返回一个 MemoryInfo 对象,包含内存的详细信息
|
||||||
|
*/
|
||||||
|
MemoryInfo getMemoryInfo();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件存储信息
|
||||||
|
* <p>
|
||||||
|
* 封装操作系统文件系统相关信息,如磁盘大小,分区大小等。结果以 {@link FileStoreInfo} 对象形式返回。
|
||||||
|
* </p>
|
||||||
|
* @return 返回一个 MemoryInfo 对象,包含内存的详细信息
|
||||||
|
*/
|
||||||
|
FileStoreInfo getFileStoreInfo();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,15 @@
|
||||||
package com.cmhi.magent.service.impl;
|
package com.cmhi.magent.service.impl;
|
||||||
|
|
||||||
import com.cmhi.magent.pojo.mapper.IObjectConvert;
|
import com.cmhi.magent.pojo.mapper.IObjectConvert;
|
||||||
import com.cmhi.magent.pojo.po.FileStoreDetails;
|
|
||||||
import com.cmhi.magent.pojo.po.FileStoreInfo;
|
import com.cmhi.magent.pojo.po.FileStoreInfo;
|
||||||
import com.cmhi.magent.pojo.po.HwInfo;
|
import com.cmhi.magent.pojo.po.HwInfo;
|
||||||
|
import com.cmhi.magent.pojo.po.MemoryInfo;
|
||||||
|
import com.cmhi.magent.pojo.po.NetworkInfo;
|
||||||
import com.cmhi.magent.pojo.po.ProcessorInfo;
|
import com.cmhi.magent.pojo.po.ProcessorInfo;
|
||||||
import com.cmhi.magent.service.SystemInfoService;
|
import com.cmhi.magent.service.SystemInfoService;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import oshi.SystemInfo;
|
import oshi.SystemInfo;
|
||||||
import oshi.hardware.CentralProcessor;
|
import oshi.hardware.CentralProcessor;
|
||||||
import oshi.hardware.ComputerSystem;
|
|
||||||
import oshi.hardware.GlobalMemory;
|
|
||||||
import oshi.software.os.OSFileStore;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 系统信息服务实现类。
|
* 系统信息服务实现类。
|
||||||
|
@ -98,26 +94,46 @@ public class SystemInfoServiceImpl implements SystemInfoService {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public HwInfo getHardwareInfo() {
|
public HwInfo getHardwareInfo() {
|
||||||
ComputerSystem cs = si.getHardware().getComputerSystem();
|
return IObjectConvert.INSTANCE.toHwInfo(si);
|
||||||
GlobalMemory gm = si.getHardware().getMemory();
|
}
|
||||||
HwInfo hw = IObjectConvert.INSTANCE.toHwInfo(si);
|
|
||||||
|
|
||||||
// FileStoreInfo fi = new FileStoreInfo();
|
/**
|
||||||
// fi.setFileStore(new ArrayList<>());
|
* 获取网络信息。
|
||||||
//
|
* <p>
|
||||||
// for (OSFileStore os : si.getOperatingSystem().getFileSystem().getFileStores()) {
|
* 通过 OSHI 库获取网络接口的详细信息,包括网络名称、网络类型、网络状态、网络速度等,
|
||||||
// fi.getFileStore().add(FileStoreDetails.builder()
|
* 并通过 {@link IObjectConvert} 工具类将其转换为自定义的 {@link NetworkInfo} 对象。
|
||||||
// .name(os.getName())
|
* </p>
|
||||||
// .volume(os.getVolume())
|
*
|
||||||
// .mountPoint(os.getMount())
|
* @return 包含网络详细信息的 {@link NetworkInfo} 对象。
|
||||||
// .uuid(os.getUUID())
|
*/
|
||||||
// .fsType(os.getType())
|
@Override
|
||||||
// .description(os.getDescription())
|
public NetworkInfo getNetworkInfo() {
|
||||||
// .freeSpace(os.getFreeSpace())
|
return IObjectConvert.INSTANCE.toNetworkInfo(si, si.getOperatingSystem().getNetworkParams());
|
||||||
// .totalSpace(os.getTotalSpace())
|
}
|
||||||
// .usableSpace(os.getUsableSpace())
|
|
||||||
// .build());
|
/**
|
||||||
// }
|
* 获取内存信息
|
||||||
return hw;
|
* <p>
|
||||||
|
* 封装内存的相关信息,如物理内存大小,可用内存,当前内存占用情况等。结果以 {@link MemoryInfo} 对象形式返回。
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return 返回一个 MemoryInfo 对象,包含内存的详细信息
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public MemoryInfo getMemoryInfo() {
|
||||||
|
return IObjectConvert.INSTANCE.toMemoryInfo(si.getHardware().getMemory());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件存储信息
|
||||||
|
* <p>
|
||||||
|
* 封装操作系统文件系统相关信息,如磁盘大小,分区大小等。结果以 {@link FileStoreInfo} 对象形式返回。
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return 返回一个 MemoryInfo 对象,包含内存的详细信息
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public FileStoreInfo getFileStoreInfo() {
|
||||||
|
return IObjectConvert.INSTANCE.toFileStoreInfo(si.getOperatingSystem().getFileSystem(), si.getHardware().getDiskStores());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package com.cmhi.magent.controller;
|
package com.cmhi.magent.controller;
|
||||||
|
|
||||||
import com.cmhi.magent.common.TestBaseAuthentication;
|
import com.cmhi.magent.common.TestBaseAuthentication;
|
||||||
|
import com.cmhi.magent.pojo.vo.GetFileStoreInfoResp;
|
||||||
import com.cmhi.magent.pojo.vo.GetHwInfoResp;
|
import com.cmhi.magent.pojo.vo.GetHwInfoResp;
|
||||||
|
import com.cmhi.magent.pojo.vo.GetMemoryInfoResp;
|
||||||
|
import com.cmhi.magent.pojo.vo.GetNetworkInfoResp;
|
||||||
import com.cmhi.magent.pojo.vo.GetOsInfoResp;
|
import com.cmhi.magent.pojo.vo.GetOsInfoResp;
|
||||||
import com.cmhi.magent.pojo.vo.GetProcessorInfoResp;
|
import com.cmhi.magent.pojo.vo.GetProcessorInfoResp;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
@ -57,4 +60,46 @@ public class SystemInfoApiTest extends TestBaseAuthentication {
|
||||||
// 验证硬件信息数据的合法性
|
// 验证硬件信息数据的合法性
|
||||||
Assertions.assertNotNull(resp.getHwInfo(), "硬件信息不应为空");
|
Assertions.assertNotNull(resp.getHwInfo(), "硬件信息不应为空");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("获取内存信息")
|
||||||
|
void testGetMemoryInfo() throws Exception {
|
||||||
|
// 发起 GET 请求,调用 /api/systemInfo/memoryInfo 接口
|
||||||
|
GetMemoryInfoResp resp = (GetMemoryInfoResp) performanceRestful(RequestMethod.GET, null, "/api/systemInfo/memoryInfo",
|
||||||
|
new Class[] {GetMemoryInfoResp.class});
|
||||||
|
|
||||||
|
// 验证通用响应数据的合法性
|
||||||
|
AssertValidCommonResp(resp);
|
||||||
|
|
||||||
|
// 验证硬件信息数据的合法性
|
||||||
|
Assertions.assertNotNull(resp.getMemoryInfo(), "内存信息不应为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("获取网络信息")
|
||||||
|
void testGetNetworkInfo() throws Exception {
|
||||||
|
// 发起 GET 请求,调用 /api/systemInfo/networkInfo 接口
|
||||||
|
GetNetworkInfoResp resp = (GetNetworkInfoResp) performanceRestful(RequestMethod.GET, null, "/api/systemInfo/networkInfo",
|
||||||
|
new Class[] {GetNetworkInfoResp.class});
|
||||||
|
|
||||||
|
// 验证通用响应数据的合法性
|
||||||
|
AssertValidCommonResp(resp);
|
||||||
|
|
||||||
|
// 验证硬件信息数据的合法性
|
||||||
|
Assertions.assertNotNull(resp.getNetworkInfo(), "网络信息不应为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("获取文件存储信息")
|
||||||
|
void testGetFileStoreInfo() throws Exception {
|
||||||
|
// 发起 GET 请求,调用 /api/systemInfo/fileStoreInfo 接口
|
||||||
|
GetFileStoreInfoResp resp = (GetFileStoreInfoResp) performanceRestful(RequestMethod.GET, null, "/api/systemInfo/fileStoreInfo",
|
||||||
|
new Class[] {GetFileStoreInfoResp.class});
|
||||||
|
|
||||||
|
// 验证通用响应数据的合法性
|
||||||
|
AssertValidCommonResp(resp);
|
||||||
|
|
||||||
|
// 验证硬件信息数据的合法性
|
||||||
|
Assertions.assertNotNull(resp.getFileStoreInfo(), "文件存储信息不应为空");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,114 @@
|
||||||
|
package com.cmhi.magent.service;
|
||||||
|
|
||||||
|
import com.cmhi.magent.misc.HelperUtils;
|
||||||
|
import com.cmhi.magent.pojo.mapper.IObjectConvert;
|
||||||
|
import com.cmhi.magent.pojo.po.HwInfo;
|
||||||
|
import com.cmhi.magent.pojo.po.MemoryDetails;
|
||||||
|
import com.cmhi.magent.pojo.po.ProcessorInfo;
|
||||||
|
import com.cmhi.magent.service.impl.SystemInfoServiceImpl;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import oshi.SystemInfo;
|
||||||
|
import oshi.hardware.GlobalMemory;
|
||||||
|
import oshi.hardware.HWDiskStore;
|
||||||
|
import oshi.hardware.NetworkIF;
|
||||||
|
import oshi.hardware.PhysicalMemory;
|
||||||
|
import oshi.software.os.FileSystem;
|
||||||
|
import oshi.software.os.NetworkParams;
|
||||||
|
import oshi.software.os.OSFileStore;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class SystemInfoServiceTest {
|
||||||
|
|
||||||
|
private SystemInfoServiceImpl systemInfoService; // 实现类对象
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
// 实例化实现类
|
||||||
|
systemInfoService = new SystemInfoServiceImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetOsName() {
|
||||||
|
// 调用接口方法
|
||||||
|
String osName = systemInfoService.getOsName();
|
||||||
|
|
||||||
|
// 检查返回值是否符合预期(这里只能验证非空,因为操作系统名称依赖具体环境)
|
||||||
|
assertNotNull(osName, "操作系统名称不能为空!");
|
||||||
|
System.out.println("操作系统名称: " + osName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetOsBootTimeStamp() {
|
||||||
|
// 调用接口方法
|
||||||
|
long bootTimeStamp = systemInfoService.getOsBootTimeStamp();
|
||||||
|
|
||||||
|
// 验证时间戳(一般会大于当前时间减去一个合理的范围)
|
||||||
|
assertTrue(bootTimeStamp > 0, "启动时间戳应该是正值!");
|
||||||
|
System.out.println("操作系统启动时间戳: " + bootTimeStamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetProcessInfo() {
|
||||||
|
// 调用接口方法获取处理器信息
|
||||||
|
ProcessorInfo processorInfo = systemInfoService.getProcessInfo();
|
||||||
|
|
||||||
|
// 检查返回的处理器信息是否合理
|
||||||
|
assertNotNull(processorInfo, "处理器信息不能为空!");
|
||||||
|
assertTrue(processorInfo.getPhysicalCores() > 0, "处理器核心数量应该大于0!");
|
||||||
|
assertTrue(processorInfo.getLogicalCpu() >= processorInfo.getPhysicalCores(), "线程数量应该不小于核心数量!");
|
||||||
|
assertTrue(processorInfo.getCpuVendorFreq() > 0, "CPU频率应该大于0!");
|
||||||
|
System.out.println(
|
||||||
|
"处理器信息: 核心数量 = " + processorInfo.getPhysicalCores() + ", 线程数量 = " + processorInfo.getLogicalCpu() + ", 主频 = " + processorInfo.getCpuVendorFreq() + " GHz");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetHardwareInfo() {
|
||||||
|
// // 调用接口方法获取硬件信息
|
||||||
|
// HwInfo hardwareInfo = systemInfoService.getHardwareInfo();
|
||||||
|
//
|
||||||
|
// // 检查返回的硬件信息是否合理
|
||||||
|
// assertNotNull(hardwareInfo, "硬件信息不能为空!");
|
||||||
|
// assertTrue(hardwareInfo.get() > 0, "内存大小应该大于0!");
|
||||||
|
// assertTrue(hardwareInfo.getDiskCapacity() > 0, "磁盘容量应该大于0!");
|
||||||
|
// System.out.println("硬件信息: 内存大小 = " + hardwareInfo.getMemorySize() +
|
||||||
|
// " GB, 磁盘容量 = " + hardwareInfo.getDiskCapacity() + " GB");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetMemoryInfo() throws JsonProcessingException {
|
||||||
|
SystemInfo si = new SystemInfo();
|
||||||
|
GlobalMemory memory = si.getHardware().getMemory();
|
||||||
|
List<PhysicalMemory> ph = memory.getPhysicalMemory();
|
||||||
|
List<HWDiskStore> d = si.getHardware().getDiskStores();
|
||||||
|
FileSystem fs = si.getOperatingSystem().getFileSystem();
|
||||||
|
List<OSFileStore> os = fs.getFileStores(true);
|
||||||
|
List<NetworkIF> nf = si.getHardware().getNetworkIFs(true);
|
||||||
|
NetworkParams np = si.getOperatingSystem().getNetworkParams();
|
||||||
|
|
||||||
|
MemoryDetails md = new MemoryDetails();
|
||||||
|
md.setFreeMemory(1000L);
|
||||||
|
md.setTotalMemory(3000L);
|
||||||
|
|
||||||
|
HwInfo hw = IObjectConvert.INSTANCE.toHwInfo(si);
|
||||||
|
System.out.println(HelperUtils.getJson(hw));
|
||||||
|
// System.out.println(HelperUtils.getJson(d));
|
||||||
|
// System.out.println(HelperUtils.getJson(fs));
|
||||||
|
|
||||||
|
//System.out.println(objectMapper.writeValueAsString(md));
|
||||||
|
// System.out.println(HelperUtils.getJson(memory));
|
||||||
|
// System.out.println(HelperUtils.getJson(ph));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue