1
+ package com .xkcoding .websocket .model ;
2
+
3
+ import cn .hutool .core .util .NumberUtil ;
4
+ import com .xkcoding .websocket .model .server .*;
5
+ import com .xkcoding .websocket .util .IpUtil ;
6
+ import oshi .SystemInfo ;
7
+ import oshi .hardware .CentralProcessor ;
8
+ import oshi .hardware .CentralProcessor .TickType ;
9
+ import oshi .hardware .GlobalMemory ;
10
+ import oshi .hardware .HardwareAbstractionLayer ;
11
+ import oshi .software .os .FileSystem ;
12
+ import oshi .software .os .OSFileStore ;
13
+ import oshi .software .os .OperatingSystem ;
14
+ import oshi .util .Util ;
15
+
16
+ import java .net .UnknownHostException ;
17
+ import java .util .LinkedList ;
18
+ import java .util .List ;
19
+ import java .util .Properties ;
20
+
21
+ /**
22
+ * <p>
23
+ * 服务器相关信息实体
24
+ * </p>
25
+ *
26
+ * @package: com.xkcoding.websocket.model
27
+ * @description: 服务器相关信息实体
28
+ * @author: yangkai.shen
29
+ * @date: Created in 2018-12-14 16:09
30
+ * @copyright: Copyright (c) 2018
31
+ * @version: V1.0
32
+ * @modified: yangkai.shen
33
+ */
34
+ public class Server {
35
+
36
+ private static final int OSHI_WAIT_SECOND = 1000 ;
37
+
38
+ /**
39
+ * CPU相关信息
40
+ */
41
+ private Cpu cpu = new Cpu ();
42
+
43
+ /**
44
+ * 內存相关信息
45
+ */
46
+ private Mem mem = new Mem ();
47
+
48
+ /**
49
+ * JVM相关信息
50
+ */
51
+ private Jvm jvm = new Jvm ();
52
+
53
+ /**
54
+ * 服务器相关信息
55
+ */
56
+ private Sys sys = new Sys ();
57
+
58
+ /**
59
+ * 磁盘相关信息
60
+ */
61
+ private List <SysFile > sysFiles = new LinkedList <SysFile >();
62
+
63
+ public Cpu getCpu () {
64
+ return cpu ;
65
+ }
66
+
67
+ public void setCpu (Cpu cpu ) {
68
+ this .cpu = cpu ;
69
+ }
70
+
71
+ public Mem getMem () {
72
+ return mem ;
73
+ }
74
+
75
+ public void setMem (Mem mem ) {
76
+ this .mem = mem ;
77
+ }
78
+
79
+ public Jvm getJvm () {
80
+ return jvm ;
81
+ }
82
+
83
+ public void setJvm (Jvm jvm ) {
84
+ this .jvm = jvm ;
85
+ }
86
+
87
+ public Sys getSys () {
88
+ return sys ;
89
+ }
90
+
91
+ public void setSys (Sys sys ) {
92
+ this .sys = sys ;
93
+ }
94
+
95
+ public List <SysFile > getSysFiles () {
96
+ return sysFiles ;
97
+ }
98
+
99
+ public void setSysFiles (List <SysFile > sysFiles ) {
100
+ this .sysFiles = sysFiles ;
101
+ }
102
+
103
+ public void copyTo () throws Exception {
104
+ SystemInfo si = new SystemInfo ();
105
+ HardwareAbstractionLayer hal = si .getHardware ();
106
+
107
+ setCpuInfo (hal .getProcessor ());
108
+
109
+ setMemInfo (hal .getMemory ());
110
+
111
+ setSysInfo ();
112
+
113
+ setJvmInfo ();
114
+
115
+ setSysFiles (si .getOperatingSystem ());
116
+ }
117
+
118
+ /**
119
+ * 设置CPU信息
120
+ */
121
+ private void setCpuInfo (CentralProcessor processor ) {
122
+ // CPU信息
123
+ long [] prevTicks = processor .getSystemCpuLoadTicks ();
124
+ Util .sleep (OSHI_WAIT_SECOND );
125
+ long [] ticks = processor .getSystemCpuLoadTicks ();
126
+ long nice = ticks [TickType .NICE .getIndex ()] - prevTicks [TickType .NICE .getIndex ()];
127
+ long irq = ticks [TickType .IRQ .getIndex ()] - prevTicks [TickType .IRQ .getIndex ()];
128
+ long softirq = ticks [TickType .SOFTIRQ .getIndex ()] - prevTicks [TickType .SOFTIRQ .getIndex ()];
129
+ long steal = ticks [TickType .STEAL .getIndex ()] - prevTicks [TickType .STEAL .getIndex ()];
130
+ long cSys = ticks [TickType .SYSTEM .getIndex ()] - prevTicks [TickType .SYSTEM .getIndex ()];
131
+ long user = ticks [TickType .USER .getIndex ()] - prevTicks [TickType .USER .getIndex ()];
132
+ long iowait = ticks [TickType .IOWAIT .getIndex ()] - prevTicks [TickType .IOWAIT .getIndex ()];
133
+ long idle = ticks [TickType .IDLE .getIndex ()] - prevTicks [TickType .IDLE .getIndex ()];
134
+ long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal ;
135
+ cpu .setCpuNum (processor .getLogicalProcessorCount ());
136
+ cpu .setTotal (totalCpu );
137
+ cpu .setSys (cSys );
138
+ cpu .setUsed (user );
139
+ cpu .setWait (iowait );
140
+ cpu .setFree (idle );
141
+ }
142
+
143
+ /**
144
+ * 设置内存信息
145
+ */
146
+ private void setMemInfo (GlobalMemory memory ) {
147
+ mem .setTotal (memory .getTotal ());
148
+ mem .setUsed (memory .getTotal () - memory .getAvailable ());
149
+ mem .setFree (memory .getAvailable ());
150
+ }
151
+
152
+ /**
153
+ * 设置服务器信息
154
+ */
155
+ private void setSysInfo () {
156
+ Properties props = System .getProperties ();
157
+ sys .setComputerName (IpUtil .getHostName ());
158
+ sys .setComputerIp (IpUtil .getHostIp ());
159
+ sys .setOsName (props .getProperty ("os.name" ));
160
+ sys .setOsArch (props .getProperty ("os.arch" ));
161
+ sys .setUserDir (props .getProperty ("user.dir" ));
162
+ }
163
+
164
+ /**
165
+ * 设置Java虚拟机
166
+ */
167
+ private void setJvmInfo () throws UnknownHostException {
168
+ Properties props = System .getProperties ();
169
+ jvm .setTotal (Runtime .getRuntime ().totalMemory ());
170
+ jvm .setMax (Runtime .getRuntime ().maxMemory ());
171
+ jvm .setFree (Runtime .getRuntime ().freeMemory ());
172
+ jvm .setVersion (props .getProperty ("java.version" ));
173
+ jvm .setHome (props .getProperty ("java.home" ));
174
+ }
175
+
176
+ /**
177
+ * 设置磁盘信息
178
+ */
179
+ private void setSysFiles (OperatingSystem os ) {
180
+ FileSystem fileSystem = os .getFileSystem ();
181
+ OSFileStore [] fsArray = fileSystem .getFileStores ();
182
+ for (OSFileStore fs : fsArray ) {
183
+ long free = fs .getUsableSpace ();
184
+ long total = fs .getTotalSpace ();
185
+ long used = total - free ;
186
+ SysFile sysFile = new SysFile ();
187
+ sysFile .setDirName (fs .getMount ());
188
+ sysFile .setSysTypeName (fs .getType ());
189
+ sysFile .setTypeName (fs .getName ());
190
+ sysFile .setTotal (convertFileSize (total ));
191
+ sysFile .setFree (convertFileSize (free ));
192
+ sysFile .setUsed (convertFileSize (used ));
193
+ sysFile .setUsage (NumberUtil .mul (NumberUtil .div (used , total , 4 ), 100 ));
194
+ sysFiles .add (sysFile );
195
+ }
196
+ }
197
+
198
+ /**
199
+ * 字节转换
200
+ *
201
+ * @param size 字节大小
202
+ * @return 转换后值
203
+ */
204
+ public String convertFileSize (long size ) {
205
+ long kb = 1024 ;
206
+ long mb = kb * 1024 ;
207
+ long gb = mb * 1024 ;
208
+ if (size >= gb ) {
209
+ return String .format ("%.1f GB" , (float ) size / gb );
210
+ } else if (size >= mb ) {
211
+ float f = (float ) size / mb ;
212
+ return String .format (f > 100 ? "%.0f MB" : "%.1f MB" , f );
213
+ } else if (size >= kb ) {
214
+ float f = (float ) size / kb ;
215
+ return String .format (f > 100 ? "%.0f KB" : "%.1f KB" , f );
216
+ } else {
217
+ return String .format ("%d B" , size );
218
+ }
219
+ }
220
+ }
0 commit comments