获取硬件基本信息

使用第三方jar包


    com.github.oshi
    oshi-core
    3.5.0

java代码

import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.HWDiskStore;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.util.FormatUtil;

import java.util.Properties;

public class HardWareUtil {
    public R getServerHardWareInfo(){
        R r = new R();
        ServerHardware serverHardware = new ServerHardware();
        SystemInfo si = new SystemInfo();

        HardwareAbstractionLayer hal = si.getHardware();

        Properties props=System.getProperties();

        serverHardware.setOsName(props.getProperty("os.name"));
        serverHardware.setOsVersion(props.getProperty("os.version"));
        serverHardware.setOsArch(props.getProperty("os.arch"));

        CentralProcessor processor =hal.getProcessor();
        serverHardware.setCpuModel(processor.getName());
        serverHardware.setCpuCoreNumber(processor.getPhysicalProcessorCount());

        serverHardware.setMemSize(FormatUtil.formatBytes(hal.getMemory().getTotal()));

        HWDiskStore[] diskStores = hal.getDiskStores();
        long totalSize = 0L;
        for(HWDiskStore diskStore:diskStores){
            totalSize = diskStore.getSize()+totalSize;
        }
        serverHardware.setDevTotalSize(FormatUtil.formatBytes(totalSize));
        Runtime run = Runtime.getRuntime();
        long max = run.maxMemory();
        serverHardware.setJvm(FormatUtil.formatBytes(max));
        r.put("info",serverHardware);
        return r;
    }
}
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 硬件信息类
 */
@ApiModel(value="硬件信息类")
@Data
public class ServerHardware {
    //硬盘大小
    @ApiModelProperty("硬盘大小")
    private String devTotalSize;
    //内存大小
    @ApiModelProperty("内存大小")
    private String memSize;
    //CPU
    @ApiModelProperty("cpu类型")
    private String cpuModel;
    //cpu核心数
    @ApiModelProperty("cpu核心数")
    private int cpuCoreNumber;
    //操作系统名称
    @ApiModelProperty("操作系统名称")
    private String osName;
    //操作系统架构
    @ApiModelProperty("操作系统架构")
    private String osArch;
    //操作系统版本
    @ApiModelProperty("操作系统版本")
    private String osVersion;
    //java虚拟内存
    @ApiModelProperty("java虚拟内存")
    private String jvm;
}
import java.util.HashMap;
import java.util.Map;

/**
 * 返回数据
 *
 */
public class R extends HashMap {
    private static final long serialVersionUID = 1L;
    
    public R() {
        put("code", 0);
        put("msg", "success");
    }
    
    public static R error() {
        return error(500, "未知异常,请联系管理员");
    }
    
    public static R error(String msg) {
        return error(500, msg);
    }
    
    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }
    
    public static R ok(Map map) {
        R r = new R();
        r.putAll(map);
        return r;
    }
    
    public static R ok() {
        return new R();
    }

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
    public R putDesc(String value) {
        super.put("logDesc", value);
        return this;
    }
    public String getDesc() {
        Object o = this.get("logDesc");
        if (o == null) {
            return null;
        }
        return o.toString();
    }
    public R putData(Object value) {
        super.put("data", value);
        return this;
    }
    public Object getData() {
        return get("data");
    }

    public int getCode() {
        Object o = this.get("code");
        if (o == null){
            return 0;
        }
        if (o instanceof String) {
            return Integer.parseInt(o.toString());
        }
        return (int)o;
    }
    public String  getMsg() {
        return this.get("msg").toString();
    }

}

你可能感兴趣的:(获取硬件基本信息)