人不读书,则尘俗生其间,照镜则面目可憎,对人则语言无味。一一北宋·黄庭坚

首先是依赖

1
2
3
4
5
6
<!-- 获取系统信息 -->
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>3.9.1</version>
</dependency>

然后是工具类

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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package com.ruben.utils.server;

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.Util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
* 服务器信息
*
* @author Administrator
*/
public class ServerOsInfo {


public static ServerInfo getInfo() {
ServerInfo info = new ServerInfo();
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
CentralProcessor processor = hal.getProcessor(); //获取cpu信息
info.setCpu(setCpuInfo(processor));
GlobalMemory memory = hal.getMemory(); //获取内存信息
info.setMem(setMemInfo(memory));
info.setSys(setSysInfo()); //服务器信息
info.setJvm(setJvmInfo()); //jvm信息
OperatingSystem op = si.getOperatingSystem();
info.setFile(setSysFiles(op)); //磁盘信息
info.setIp(getHostIp());
info.setHostname(getHostName());
return info;
}

/*
* cpu信息
*
* @param processor
*/
private static ServerInfo.Cpu setCpuInfo(CentralProcessor processor) { // CPU信息
long[] prevTicks = processor.getSystemCpuLoadTicks();
Util.sleep(1000);
long[] ticks = processor.getSystemCpuLoadTicks();
double nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
double irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
double softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
double steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
/*
* CPU系统使用率
*/
double sys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
/*
* CPU用户使用率
*/
double used = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
/*
* CPU当前等待率
*/
double iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
/*
* CPU当前空闲率
*/
double free = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
/*
* CPU总的使用率
*/
double total = used + nice + sys + free + iowait + irq + softirq + steal;

/*
* 核心数
*/
int cpuNum = processor.getLogicalProcessorCount();

/*
* CPU当前等待率
*/

ServerInfo.Cpu cpu = new ServerInfo.Cpu();
cpu.setCpucard(processor);//cpu信息
cpu.setNum(cpuNum);//cpu核心数
cpu.setUsed(round(mul((sys + used) / total, 100), 2));//cpu 用户使用率;
cpu.setFree(round(mul(free / total, 100), 2));//cpu 空闲率
return cpu;
}

/**
* 内存信息
*/
private static ServerInfo.MemoryInfo setMemInfo(GlobalMemory memory) {
/*
* 内存总量
*/
double total = +memory.getTotal();

/*
* 已用内存
*/
double used = memory.getTotal() - memory.getAvailable();

/*
* 剩余内存
*/
double free = memory.getAvailable();
ServerInfo.MemoryInfo mem = new ServerInfo.MemoryInfo();
mem.setTotal(div(total, (1024 * 1024 * 1024), 2));// 内存总大小
mem.setUsedMem(div(used, (1024 * 1024 * 1024), 2));// 已使用内存大小
mem.setFree(round(mul(free / total, 100), 2));// 剩余内存大小
mem.setUsed(round(mul(used / total, 100), 2));
return mem;
}

/*
* 服务器信息
*/
private static Properties setSysInfo() {
return System.getProperties();
}


/*
* Java虚拟机
*/
private static ServerInfo.Jvm setJvmInfo() {
ServerInfo.Jvm jvm = new ServerInfo.Jvm();
jvm.setTotal(div(Runtime.getRuntime().totalMemory(), 1024 * 1024, 2));
jvm.setMaxTotal(div(Runtime.getRuntime().maxMemory(), 1024 * 1024, 2));
jvm.setUsedMem(div((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()), 1024 * 1024, 2));
jvm.setUsed(round(mul((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) * 1.0 / Runtime.getRuntime().totalMemory(), 100), 2));
return jvm;
}

/*
* 设置磁盘信息
*/
private static List<ServerInfo.FileItem> setSysFiles(OperatingSystem os) {
FileSystem fileSystem = os.getFileSystem();
OSFileStore[] fsArray = fileSystem.getFileStores();
List<ServerInfo.FileItem> list = new ArrayList<>();
for (OSFileStore fs : fsArray) {
long free = fs.getUsableSpace();
long total = fs.getTotalSpace();
long used = total - free;
ServerInfo.FileItem fileItem = new ServerInfo.FileItem();
fileItem.setPath(fs.getMount());
fileItem.setType(fs.getType());
fileItem.setName(fs.getName());
fileItem.setTotal(convertFileSize(total));
fileItem.setFree(convertFileSize(free));
fileItem.setUsed(convertFileSize(used));
fileItem.setRate(div(used, total, 4) * 100);
list.add(fileItem);
}
return list;
}

/*
* 字节转换
*
* @param size 字节大小
* @return 转换后值
*/
public static String convertFileSize(long size) {
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
if (size >= gb) {
return String.format("%.1f GB", (float) size / gb);
} else if (size >= mb) {
float f = (float) size / mb;
return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
} else if (size >= kb) {
float f = (float) size / kb;
return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
} else {
return String.format("%d B", size);
}
}

/*
* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
* 定精度,以后的数字四舍五入。
*
* @param v1 被除数
* @param v2 除数
* @param scale 表示表示需要精确到小数点以后几位。
* @return 两个参数的商
*/
public static double div(double v1, double v2, int scale) {
return ((double) ((int) (v1 / v2 * Math.pow(10, scale)))) / Math.pow(10, scale);

}

/**
* 提供精确的小数位四舍五入处理。
*
* @param v 需要四舍五入的数字
* @param scale 小数点后保留几位
* @return 四舍五入后的结果
*/
public static double round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, RoundingMode.HALF_UP).doubleValue();
}

/**
* 提供精确的乘法运算。
*
* @param v1 被乘数
* @param v2 乘数
* @return 两个参数的积
*/
public static double mul(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2).doubleValue();
}

public static String getHostIp() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException ignored) {
}
return "127.0.0.1";
}

public static String getHostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException ignored) {
}
return "未知";
}

public static void main(String[] args) {
System.out.println(div(19, 7, 4));
System.out.println(getInfo());
}
}

运行main结果

image-20210222203031275