ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package jnpf.base.util;
 
import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateUtil;
import jnpf.base.model.monitor.*;
import jnpf.util.IpUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import oshi.SystemInfo;
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 oshi.util.FormatUtil;
import oshi.util.Util;
 
import java.lang.management.ManagementFactory;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
 
/**
 * 系统监控工具类
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com)
 * @date 2021-03-23
 */
@Data
public class MonitorUtil {
 
    private static final DecimalFormat DF = new DecimalFormat("0.00");
    private CpuModel cpu = null;
    private DiskModel disk = null;
    private MemoryModel memory = null;
    private SwapModel swap = null;
    private SystemModel system = null;
 
    public MonitorUtil() {
        SystemInfo si = new SystemInfo();
        OperatingSystem os = si.getOperatingSystem();
        HardwareAbstractionLayer hal = si.getHardware();
        this.cpu = getCpuInfo(hal.getProcessor());
        this.memory = getMemoryInfo(hal.getMemory());
        this.disk = getDiskInfo(os);
        this.system = getSystemInfo(os);
        this.swap = getSwapInfo(hal.getMemory());
    }
 
    /**
     * 获取磁盘信息
     *
     * @return /
     */
    private DiskModel getDiskInfo(OperatingSystem os) {
        DiskModel diskInfo = new DiskModel();
        FileSystem fileSystem = os.getFileSystem();
        List<OSFileStore> fsArray = fileSystem.getFileStores();
        long total = 0L;
        long available = 0L;
        long used = 0L;
        for (OSFileStore fs : fsArray) {
            total += fs.getTotalSpace();
            available += fs.getUsableSpace();
        }
        used = total - available;
        diskInfo.setTotal(FormatUtil.formatBytes(total));
        diskInfo.setAvailable(FormatUtil.formatBytes(available));
        diskInfo.setUsed(FormatUtil.formatBytes(used));
        diskInfo.setUsageRate(DF.format(used / (double) total * 100));
        return diskInfo;
    }
 
    /**
     * 获取交换区信息
     *
     * @param memory /
     * @return /
     */
    private SwapModel getSwapInfo(GlobalMemory memory) {
        SwapModel swapInfo = new SwapModel();
        swapInfo.setTotal(FormatUtil.formatBytes(memory.getVirtualMemory().getSwapTotal()));
        swapInfo.setAvailable(FormatUtil.formatBytes(memory.getVirtualMemory().getSwapTotal() - memory.getVirtualMemory().getSwapUsed()));
        swapInfo.setUsageRate(DF.format(memory.getVirtualMemory().getSwapUsed() / (double) memory.getVirtualMemory().getSwapTotal() * 100));
        swapInfo.setUsed(FormatUtil.formatBytes(memory.getVirtualMemory().getSwapUsed()));
        return swapInfo;
    }
 
    /**
     * 获取内存信息
     *
     * @param memory /
     * @return /
     */
    private MemoryModel getMemoryInfo(GlobalMemory memory) {
        MemoryModel memoryInfo = new MemoryModel();
        memoryInfo.setTotal(FormatUtil.formatBytes(memory.getTotal()));
        memoryInfo.setAvailable(FormatUtil.formatBytes(memory.getAvailable()));
        memoryInfo.setUsed(FormatUtil.formatBytes(memory.getTotal() - memory.getAvailable()));
        memoryInfo.setUsageRate(DF.format((memory.getTotal() - memory.getAvailable()) / (double) memory.getTotal() * 100));
        return memoryInfo;
    }
 
    /**
     * 获取Cpu相关信息
     *
     * @param processor /
     * @return /
     */
    private CpuModel getCpuInfo(CentralProcessor processor) {
        CpuModel cpuInfo = new CpuModel();
        cpuInfo.setName(processor.getProcessorIdentifier().getName());
        cpuInfo.setPackageName(processor.getPhysicalPackageCount() + "个物理CPU");
        cpuInfo.setCore(processor.getPhysicalProcessorCount() + "个物理核心");
        cpuInfo.setCoreNumber(processor.getPhysicalProcessorCount());
        cpuInfo.setLogic(processor.getLogicalProcessorCount() + "个逻辑CPU");
        // CPU信息
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        // 等待1秒...
        Util.sleep(1000);
        long[] ticks = processor.getSystemCpuLoadTicks();
        long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
        long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
        long sys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
        long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
        long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.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 totalCpu = user + nice + sys + idle + iowait + irq + softirq + steal;
        cpuInfo.setUsed(DF.format(100d * user / totalCpu + 100d * sys / totalCpu));
        cpuInfo.setIdle(DF.format(100d * idle / totalCpu));
        return cpuInfo;
    }
 
    /**
     * 获取系统相关信息,系统、运行天数、系统IP
     *
     * @return /
     */
    private SystemModel getSystemInfo(OperatingSystem operatingSystem) {
        SystemModel systemInfo = new SystemModel();
        String osName = System.getProperty("os.name");
        String os = osName;
        if(osName.contains("Linux")){
            os = operatingSystem.toString();
        }
        // jvm 运行时间
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        Date date = new Date(time);
        // 计算项目运行时间
        String formatBetween = DateUtil.formatBetween(date, new Date(), BetweenFormatter.Level.HOUR);
        // 系统信息
        systemInfo.setOs(os);
        systemInfo.setDay(formatBetween);
        systemInfo.setIp(getLocalhostIp());
        return systemInfo;
    }
 
    /**
     * 按天计时
     * @param time
     * @return
     */
    public static String countTime1(long time) {
        StringBuffer buffer = new StringBuffer();
        long days = time/ (1000 * 60 * 60 * 24);
        if(days!=0){
            buffer.append(days + " 天 ");
        }
        long hours = (time% (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        if(hours!=0){
            buffer.append(hours + " 小时 ");
        }
        long minutes = (time% (1000 * 60 * 60)) / (1000 * 60);
        if(minutes!=0){
            buffer.append(minutes + " 分钟 ");
        }
//        long seconds = (time% (1000 * 60)) / 1000;
//        if(seconds!=0){
//            buffer.append(seconds + "秒");
//        }
        return buffer.toString();
    }
 
 
    /**
     * 获取本地ip
     * @return
     */
    private String getLocalhostIp() {
        return IpUtil.getIpAddr();
    }
 
}