jiangping
2024-12-24 fe3c26b11d209d046b885930ff1accf6b3ce7c0b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.doumee.core.utils;
 
import com.doumee.core.utils.monitor.*;
import com.sun.jna.platform.mac.DiskArbitration;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;
 
import java.io.Serializable;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
 
/**
 * @author Eva.Caesar Liu
 * @date 2023/03/21 14:49
 */
@Data
@ApiModel("监听信息")
public class Monitor implements Serializable {
 
    @ApiModelProperty(value = "操作系统名称")
    private String osName;
 
    @ApiModelProperty(value = "操作系统架构")
    private String osArch;
 
    @ApiModelProperty(value = "操作系统版本")
    private String osVersion;
 
    @ApiModelProperty(value = "服务器IP地址")
    private String ip;
 
    @ApiModelProperty(value = "MAC地址")
    private String mac;
 
    @ApiModelProperty(value = "服务器时间")
    private Date currentTime;
 
    @ApiModelProperty(value = "内存信息")
    private Memory memory;
 
    @ApiModelProperty(value = "CPU信息")
    private CPU cpu;
 
    @ApiModelProperty(value = "磁盘列表")
    private List<Disk> disks;
 
    @ApiModelProperty(value = "JVM信息")
    private JVM jvm;
 
    /**
     * 获取实时信息
     */
    public Monitor current() {
        oshi.SystemInfo systemInfo = new oshi.SystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();
        systemInfo.getOperatingSystem().getSystemBootTime();
        // 系统信息
        OperatingSystemMXBean osMXBean = ManagementFactory.getOperatingSystemMXBean();
        this.setOsName(osMXBean.getName());
        this.setOsVersion(osMXBean.getVersion());
        this.setOsArch(osMXBean.getArch());
        this.setIp(Utils.Server.getIP());
        this.setMac(Utils.Server.getMAC());
        this.setCurrentTime(new Date(System.currentTimeMillis()));
        this.setMemory(hardware.getMemory());
        this.setCpu(hardware.getProcessor());
        this.setJvm();
        this.setDisks(systemInfo.getOperatingSystem());
        return this;
    }
 
    /**
     * 设置内存信息
     */
    private void setMemory(GlobalMemory memory) {
        this.memory = new Memory();
        this.memory.setSize(this.toM(memory.getTotal()));
        this.memory.setFreeSpace(this.toM(memory.getAvailable()));
    }
 
    /**
     * 设置CPU信息
     */
    private void setCpu(CentralProcessor processor) {
        this.cpu = new CPU();
        cpu.setLogicalCount(processor.getLogicalProcessorCount());
        cpu.setPhysicalCount(processor.getPhysicalProcessorCount());
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        try {
            TimeUnit.SECONDS.sleep(1);
            long[] ticks = processor.getSystemCpuLoadTicks();
            long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
            long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
            long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
            long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
            long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
            long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
            long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
            long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
            long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
            cpu.setSystemRatio(cSys * 100.0 / totalCpu);
            cpu.setUserRatio(user * 100.0 / totalCpu);
            cpu.setUseRatio(100.0 - idle * 100.0 / totalCpu);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 设置JVM信息
     */
    private void setJvm() {
        this.jvm = new JVM();
        Properties props = System.getProperties();
        jvm.setMemory(new Memory());
        jvm.getMemory().setSize(this.toM(Runtime.getRuntime().totalMemory()));
        jvm.getMemory().setFreeSpace(this.toM(Runtime.getRuntime().freeMemory()));
        jvm.setVersion(props.getProperty("java.version"));
        jvm.setHome(props.getProperty("java.home"));
        jvm.setBootTime(new Date(ManagementFactory.getRuntimeMXBean().getStartTime()));
    }
 
    /**
     * 设置磁盘信息
     */
    private void setDisks(OperatingSystem os) {
        try {
            this.disks = new LinkedList<>();
            FileSystem fileSystem = os.getFileSystem();
            List<OSFileStore> fsArray = fileSystem.getFileStores();
            for (OSFileStore fileStore : fsArray) {
                Disk disk = new Disk();
                disk.setName(fileStore.getName());
                disk.setDir(fileStore.getMount());
                disk.setFsType(fileStore.getType());
                disk.setSize(this.toM(fileStore.getTotalSpace()));
                disk.setFreeSpace(this.toM(fileStore.getFreeSpace()));
                this.disks.add(disk);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 转为兆
     */
    private double toM(long value) {
        return value * 1.0 / 1024 / 1024;
    }
}