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