-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSystemInfo.java
More file actions
397 lines (370 loc) · 15.6 KB
/
SystemInfo.java
File metadata and controls
397 lines (370 loc) · 15.6 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import com.sun.management.OperatingSystemMXBean;
import java.lang.management.ManagementFactory;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
public class SystemInfo {
private static String osName = System.getProperty("os.name");
private static final int CPUTIME = 500;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
/**
* 功能:获取Linux和Window系统cpu使用率
* */
public static double getCpuUsage() {
// 如果是window系统
System.out.println(osName);
if (osName.toLowerCase().contains("windows")
|| osName.toLowerCase().contains("win")) {
try {
String procCmd = System.getenv("windir")
+ "//system32//wbem//wmic.exe process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
// 取进程信息
long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));//第一次读取CPU信息
Thread.sleep(CPUTIME);//睡500ms
long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));//第二次读取CPU信息
if (c0 != null && c1 != null) {
long idletime = c1[0] - c0[0];//空闲时间
long busytime = c1[1] - c0[1];//使用时间
Double cpusage = Double.valueOf(PERCENT * (busytime) * 1.0 / (busytime + idletime));
BigDecimal b1 = new BigDecimal(cpusage);
double cpuUsage = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return cpuUsage;
} else {
return 0.0;
}
} catch (Exception ex) {
System.out.println(ex);
return 0.0;
}
} else {
try {
Map<?, ?> map1 = SystemInfo.cpuinfo();
Thread.sleep(CPUTIME);
Map<?, ?> map2 = SystemInfo.cpuinfo();
long user1 = Long.parseLong(map1.get("user").toString());
long nice1 = Long.parseLong(map1.get("nice").toString());
long system1 = Long.parseLong(map1.get("system").toString());
long idle1 = Long.parseLong(map1.get("idle").toString());
long user2 = Long.parseLong(map2.get("user").toString());
long nice2 = Long.parseLong(map2.get("nice").toString());
long system2 = Long.parseLong(map2.get("system").toString());
long idle2 = Long.parseLong(map2.get("idle").toString());
long total1 = user1 + system1 + nice1;
long total2 = user2 + system2 + nice2;
float total = total2 - total1;
long totalIdle1 = user1 + nice1 + system1 + idle1;
long totalIdle2 = user2 + nice2 + system2 + idle2;
float totalidle = totalIdle2 - totalIdle1;
float cpusage = (total / totalidle) * 100;
BigDecimal b1 = new BigDecimal(cpusage);
double cpuUsage = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return cpuUsage;
} catch (InterruptedException e) {
System.out.println(e);
}
}
return 0;
}
/**
* 功能:Linux CPU使用信息
* */
public static Map<?, ?> cpuinfo() {
InputStreamReader inputs = null;
BufferedReader buffer = null;
Map<String, Object> map = new HashMap<String, Object>();
try {
inputs = new InputStreamReader(new FileInputStream("/proc/stat"));
buffer = new BufferedReader(inputs);
String line = "";
while (true) {
line = buffer.readLine();
if (line == null) {
break;
}
if (line.startsWith("cpu")) {
StringTokenizer tokenizer = new StringTokenizer(line);
List<String> temp = new ArrayList<String>();
while (tokenizer.hasMoreElements()) {
String value = tokenizer.nextToken();
temp.add(value);
}
map.put("user", temp.get(1));
map.put("nice", temp.get(2));
map.put("system", temp.get(3));
map.put("idle", temp.get(4));
map.put("iowait", temp.get(5));
map.put("irq", temp.get(6));
map.put("softirq", temp.get(7));
map.put("stealstolen", temp.get(8));
break;
}
}
} catch (Exception e) {
System.out.println(e);
} finally {
try {
buffer.close();
inputs.close();
} catch (Exception e2) {
System.out.println(e2);
}
}
return map;
}
/**
* 功能:Linux 和 Window 内存使用率
* */
public static double getMemUsage() {
if (osName.toLowerCase().contains("windows")
|| osName.toLowerCase().contains("win")) {
try {
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
// 总的物理内存+虚拟内存
long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
// 剩余的物理内存
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
Double usage = (Double) (1 - freePhysicalMemorySize * 1.0 / totalvirtualMemory) * 100;
BigDecimal b1 = new BigDecimal(usage);
double memoryUsage = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return memoryUsage;
} catch (Exception e) {
System.out.println(e);
}
} else {
Map<String, Object> map = new HashMap<String, Object>();
InputStreamReader inputs = null;
BufferedReader buffer = null;
try {
inputs = new InputStreamReader(new FileInputStream("/proc/meminfo"));
buffer = new BufferedReader(inputs);
String line = "";
while (true) {
line = buffer.readLine();
if (line == null)
break;
int beginIndex = 0;
int endIndex = line.indexOf(":");
if (endIndex != -1) {
String key = line.substring(beginIndex, endIndex);
beginIndex = endIndex + 1;
endIndex = line.length();
String memory = line.substring(beginIndex, endIndex);
String value = memory.replace("kB", "").trim();
map.put(key, value);
}
}
long memTotal = Long.parseLong(map.get("MemTotal").toString());
long memFree = Long.parseLong(map.get("MemFree").toString());
long memused = memTotal - memFree;
long buffers = Long.parseLong(map.get("Buffers").toString());
long cached = Long.parseLong(map.get("Cached").toString());
double usage = (double) (memused - buffers - cached) / memTotal * 100;
BigDecimal b1 = new BigDecimal(usage);
double memoryUsage = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return memoryUsage;
} catch (Exception e) {
System.out.println(e);
} finally {
try {
buffer.close();
inputs.close();
} catch (Exception e2) {
System.out.println(e2);
}
}
}
return 0.0;
}
/**
* Window 和Linux 得到磁盘的使用率
*
* @return
* @throws Exception
*/
public static double getDiskUsage() throws Exception {
double totalHD = 0;
double usedHD = 0;
if (osName.toLowerCase().contains("windows")
|| osName.toLowerCase().contains("win")) {
long allTotal = 0;
long allFree = 0;
for (char c = 'A'; c <= 'Z'; c++) {
String dirName = c + ":/";
File win = new File(dirName);
if (win.exists()) {
long total = (long) win.getTotalSpace();
long free = (long) win.getFreeSpace();
allTotal = allTotal + total;
allFree = allFree + free;
}
}
Double precent = (Double) (1 - allFree * 1.0 / allTotal) * 100;
BigDecimal b1 = new BigDecimal(precent);
precent = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return precent;
} else {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("df -hl");// df -hl 查看硬盘空间
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
++m;
if (tmp.indexOf("G") != -1) {
if (m == 2) {
if (!tmp.equals("") && !tmp.equals("0"))
totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1)) * 1024;
}
if (m == 3) {
if (!tmp.equals("none") && !tmp.equals("0"))
usedHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1)) * 1024;
}
}
if (tmp.indexOf("M") != -1) {
if (m == 2) {
if (!tmp.equals("") && !tmp.equals("0"))
totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1));
}
if (m == 3) {
if (!tmp.equals("none") && !tmp.equals("0"))
usedHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1));
}
}
}
}
} catch (Exception e) {
System.out.println(e);
} finally {
in.close();
}
// 保留2位小数
double precent = (usedHD / totalHD) * 100;
BigDecimal b1 = new BigDecimal(precent);
precent = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return precent;
}
}
// window读取cpu相关信息
private static long[] readCpu(final Process proc) {
long[] retn = new long[2];
try {
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() < FAULTLENGTH) {
return null;
}
int capidx = line.indexOf("Caption");
int cmdidx = line.indexOf("CommandLine");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int kmtidx = line.indexOf("KernelModeTime");
int wocidx = line.indexOf("WriteOperationCount");
long idletime = 0;
long kneltime = 0;//读取物理设备时间
long usertime = 0;//执行代码占用时间
while ((line = input.readLine()) != null) {
if (line.length() < wocidx) {
continue;
}
// 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount
String caption = substring(line, capidx, cmdidx - 1).trim();
// System.out.println("caption:"+caption);
String cmd = substring(line, cmdidx, kmtidx - 1).trim();
// System.out.println("cmd:"+cmd);
if (cmd.indexOf("wmic.exe") >= 0) {
continue;
}
String s1 = substring(line, kmtidx, rocidx - 1).trim();
String s2 = substring(line, umtidx, wocidx - 1).trim();
List<String> digitS1 = getDigit(s1);
List<String> digitS2 = getDigit(s2);
// System.out.println("s1:"+digitS1.get(0));
// System.out.println("s2:"+digitS2.get(0));
if (caption.equals("System Idle Process") || caption.equals("System")) {
if (s1.length() > 0) {
if (!digitS1.get(0).equals("") && digitS1.get(0) != null) {
idletime += Long.valueOf(digitS1.get(0)).longValue();
}
}
if (s2.length() > 0) {
if (!digitS2.get(0).equals("") && digitS2.get(0) != null) {
idletime += Long.valueOf(digitS2.get(0)).longValue();
}
}
continue;
}
if (s1.length() > 0) {
if (!digitS1.get(0).equals("") && digitS1.get(0) != null) {
kneltime += Long.valueOf(digitS1.get(0)).longValue();
}
}
if (s2.length() > 0) {
if (!digitS2.get(0).equals("") && digitS2.get(0) != null) {
kneltime += Long.valueOf(digitS2.get(0)).longValue();
}
}
}
retn[0] = idletime;
retn[1] = kneltime + usertime;
return retn;
} catch (Exception ex) {
System.out.println(ex);
} finally {
try {
proc.getInputStream().close();
} catch (Exception e) {
System.out.println(e);
}
}
return null;
}
/**
* 从字符串文本中获得数字
*
* @param text
* @return
*/
private static List<String> getDigit(String text) {
List<String> digitList = new ArrayList<String>();
digitList.add(text.replaceAll("\\D", ""));
return digitList;
}
/**
* 由于String.subString对汉字处理存在问题(把一个汉字视为一个字节),因此在 包含汉字的字符串时存在隐患,现调整如下:
*
* @param src
* 要截取的字符串
* @param start_idx
* 开始坐标(包括该坐标)
* @param end_idx
* 截止坐标(包括该坐标)
* @return
*/
private static String substring(String src, int start_idx, int end_idx) {
byte[] b = src.getBytes();
String tgt = "";
for (int i = start_idx; i <= end_idx; i++) {
tgt += (char) b[i];
}
return tgt;
}
}