阅读更多
http://sharejava.iteye.com/blog/1221048
采用singar.jar实现,需要将sigar-x86-winnt.dll、sigar-x86-winnt.lib放到系统的ClassPath中,同样支持Linux系统,需要加入libsigar-x86-linux.so到类路径中,
singar项目网站:http://support.hyperic.com/display/SIGAR/Home
代码:
Java代码
1.import java.net.InetAddress;
2.import java.net.UnknownHostException;
3.
4.import org.hyperic.sigar.CpuInfo;
5.import org.hyperic.sigar.CpuPerc;
6.import org.hyperic.sigar.FileSystem;
7.import org.hyperic.sigar.FileSystemUsage;
8.import org.hyperic.sigar.Mem;
9.import org.hyperic.sigar.NetFlags;
10.import org.hyperic.sigar.NetInterfaceConfig;
11.import org.hyperic.sigar.NetInterfaceStat;
12.import org.hyperic.sigar.OperatingSystem;
13.import org.hyperic.sigar.Sigar;
14.import org.hyperic.sigar.SigarException;
15.import org.hyperic.sigar.SigarNotImplementedException;
16.import org.hyperic.sigar.Swap;
17.
18.public class SysInfo {
19. // 1.CPU资源信息
20. // a)CPU数量(单位:个)
21. public static int getCpuCount() throws SigarException {
22. Sigar sigar = new Sigar();
23. try {
24. return sigar.getCpuInfoList().length;
25. } finally {
26. sigar.close();
27. }
28. }
29.
30. // b)CPU的总量(单位:HZ)及CPU的相关信息
31. public void getCpuTotal() {
32. Sigar sigar = new Sigar();
33. CpuInfo[] infos;
34. try {
35. infos = sigar.getCpuInfoList();
36. for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用
37. CpuInfo info = infos[i];
38. System.out.println("mhz=" + info.getMhz());// CPU的总量MHz
39. System.out.println("vendor=" + info.getVendor());// 获得CPU的卖主,如:Intel
40. System.out.println("model=" + info.getModel());// 获得CPU的类别,如:Celeron
41. System.out.println("cache size=" + info.getCacheSize());// 缓冲存储器数量
42. }
43. } catch (SigarException e) {
44. e.printStackTrace();
45. }
46. }
47.
48. // c)CPU的用户使用量、系统使用剩余量、总的剩余量、总的使用占用量等(单位:100%)
49. public void testCpuPerc() {
50. Sigar sigar = new Sigar();
51. // 方式一,主要是针对一块CPU的情况
52. CpuPerc cpu;
53. try {
54. cpu = sigar.getCpuPerc();
55. printCpuPerc(cpu);
56. } catch (SigarException e) {
57. e.printStackTrace();
58. }
59. // 方式二,不管是单块CPU还是多CPU都适用
60. CpuPerc cpuList[] = null;
61. try {
62. cpuList = sigar.getCpuPercList();
63. } catch (SigarException e) {
64. e.printStackTrace();
65. return;
66. }
67. for (int i = 0; i < cpuList.length; i++) {
68. printCpuPerc(cpuList[i]);
69. }
70. }
71.
72. private void printCpuPerc(CpuPerc cpu) {
73. System.out.println("User :" + CpuPerc.format(cpu.getUser()));// 用户使用率
74. System.out.println("Sys :" + CpuPerc.format(cpu.getSys()));// 系统使用率
75. System.out.println("Wait :" + CpuPerc.format(cpu.getWait()));// 当前等待率
76. System.out.println("Nice :" + CpuPerc.format(cpu.getNice()));//
77. System.out.println("Idle :" + CpuPerc.format(cpu.getIdle()));// 当前空闲率
78. System.out.println("Total :" + CpuPerc.format(cpu.getCombined()));// 总的使用率
79. }
80.
81. // 2.内存资源信息
82. public void getPhysicalMemory() {
83. // a)物理内存信息
84. Sigar sigar = new Sigar();
85. Mem mem;
86. try {
87. mem = sigar.getMem();
88. // 内存总量
89. System.out.println("Total = " + mem.getTotal() / 1024L + "K av");
90. // 当前内存使用量
91. System.out.println("Used = " + mem.getUsed() / 1024L + "K used");
92. // 当前内存剩余量
93. System.out.println("Free = " + mem.getFree() / 1024L + "K free");
94. // b)系统页面文件交换区信息
95. Swap swap = sigar.getSwap();
96. // 交换区总量
97. System.out.println("Total = " + swap.getTotal() / 1024L + "K av");
98. // 当前交换区使用量
99. System.out.println("Used = " + swap.getUsed() / 1024L + "K used");
100. // 当前交换区剩余量
101. System.out.println("Free = " + swap.getFree() / 1024L + "K free");
102. } catch (SigarException e) {
103. e.printStackTrace();
104. }
105. }
106.
107. // 3.操作系统信息
108. // a)取到当前操作系统的名称:
109. public String getPlatformName() {
110. String hostname = "";
111. try {
112. hostname = InetAddress.getLocalHost().getHostName();
113. } catch (Exception exc) {
114. Sigar sigar = new Sigar();
115. try {
116. hostname = sigar.getNetInfo().getHostName();
117. } catch (SigarException e) {
118. hostname = "localhost.unknown";
119. } finally {
120. sigar.close();
121. }
122. }
123. return hostname;
124. }
125.
126. // b)取当前操作系统的信息
127. public void testGetOSInfo() {
128. OperatingSystem OS = OperatingSystem.getInstance();
129. // 操作系统内核类型如: 386、486、586等x86
130. System.out.println("OS.getArch() = " + OS.getArch());
131. System.out.println("OS.getCpuEndian() = " + OS.getCpuEndian());//
132. System.out.println("OS.getDataModel() = " + OS.getDataModel());//
133. // 系统描述
134. System.out.println("OS.getDescription() = " + OS.getDescription());
135. System.out.println("OS.getMachine() = " + OS.getMachine());//
136. // 操作系统类型
137. System.out.println("OS.getName() = " + OS.getName());
138. System.out.println("OS.getPatchLevel() = " + OS.getPatchLevel());//
139. // 操作系统的卖主
140. System.out.println("OS.getVendor() = " + OS.getVendor());
141. // 卖主名称
142. System.out.println("OS.getVendorCodeName() = " + OS.getVendorCodeName());
143. // 操作系统名称
144. System.out.println("OS.getVendorName() = " + OS.getVendorName());
145. // 操作系统卖主类型
146. System.out.println("OS.getVendorVersion() = " + OS.getVendorVersion());
147. // 操作系统的版本号
148. System.out.println("OS.getVersion() = " + OS.getVersion());
149. }
150.
151. // c)取当前系统进程表中的用户信息
152. public void testWho() {
153. try {
154. Sigar sigar = new Sigar();
155. org.hyperic.sigar.Who[] who = sigar.getWhoList();
156. if (who != null && who.length > 0) {
157. for (int i = 0; i < who.length; i++) {
158. System.out.println("\n~~~~~~~~~" + String.valueOf(i) + "~~~~~~~~~");
159. org.hyperic.sigar.Who _who = who[i];
160. System.out.println("getDevice() = " + _who.getDevice());
161. System.out.println("getHost() = " + _who.getHost());
162. System.out.println("getTime() = " + _who.getTime());
163. // 当前系统进程表中的用户名
164. System.out.println("getUser() = " + _who.getUser());
165. }
166. }
167. } catch (SigarException e) {
168. e.printStackTrace();
169. }
170. }
171.
172. // 4.资源信息(主要是硬盘)
173. // a)取硬盘已有的分区及其详细信息(通过sigar.getFileSystemList()来获得FileSystem列表对象,然后对其进行编历):
174. public void testFileSystemInfo() throws Exception {
175. Sigar sigar = new Sigar();
176. FileSystem fslist[] = sigar.getFileSystemList();
177. // String dir = System.getProperty("user.home");// 当前用户文件夹路径
178. for (int i = 0; i < fslist.length; i++) {
179. System.out.println("\n~~~~~~~~~~" + i + "~~~~~~~~~~");
180. FileSystem fs = fslist[i];
181. // 分区的盘符名称
182. System.out.println("fs.getDevName() = " + fs.getDevName());
183. // 分区的盘符名称
184. System.out.println("fs.getDirName() = " + fs.getDirName());
185. System.out.println("fs.getFlags() = " + fs.getFlags());//
186. // 文件系统类型,比如 FAT32、NTFS
187. System.out.println("fs.getSysTypeName() = " + fs.getSysTypeName());
188. // 文件系统类型名,比如本地硬盘、光驱、网络文件系统等
189. System.out.println("fs.getTypeName() = " + fs.getTypeName());
190. // 文件系统类型
191. System.out.println("fs.getType() = " + fs.getType());
192. FileSystemUsage usage = null;
193. try {
194. usage = sigar.getFileSystemUsage(fs.getDirName());
195. } catch (SigarException e) {
196. if (fs.getType() == 2)
197. throw e;
198. continue;
199. }
200. switch (fs.getType()) {
201. case 0: // TYPE_UNKNOWN :未知
202. break;
203. case 1: // TYPE_NONE
204. break;
205. case 2: // TYPE_LOCAL_DISK : 本地硬盘
206. // 文件系统总大小
207. System.out.println(" Total = " + usage.getTotal() + "KB");
208. // 文件系统剩余大小
209. System.out.println(" Free = " + usage.getFree() + "KB");
210. // 文件系统可用大小
211. System.out.println(" Avail = " + usage.getAvail() + "KB");
212. // 文件系统已经使用量
213. System.out.println(" Used = " + usage.getUsed() + "KB");
214. double usePercent = usage.getUsePercent() * 100D;
215. // 文件系统资源的利用率
216. System.out.println(" Usage = " + usePercent + "%");
217. break;
218. case 3:// TYPE_NETWORK :网络
219. break;
220. case 4:// TYPE_RAM_DISK :闪存
221. break;
222. case 5:// TYPE_CDROM :光驱
223. break;
224. case 6:// TYPE_SWAP :页面交换
225. break;
226. }
227. System.out.println(" DiskReads = " + usage.getDiskReads());
228. System.out.println(" DiskWrites = " + usage.getDiskWrites());
229. }
230. return;
231. }
232.
233. // 5.网络信息
234. // a)当前机器的正式域名
235. public String getFQDN() {
236. Sigar sigar = null;
237. try {
238. return InetAddress.getLocalHost().getCanonicalHostName();
239. } catch (UnknownHostException e) {
240. try {
241. sigar = new Sigar();
242. return sigar.getFQDN();
243. } catch (SigarException ex) {
244. return null;
245. } finally {
246. sigar.close();
247. }
248. }
249. }
250.
251. // b)取到当前机器的IP地址
252. public String getDefaultIpAddress() {
253. String address = null;
254. try {
255. address = InetAddress.getLocalHost().getHostAddress();
256. // 没有出现异常而正常当取到的IP时,如果取到的不是网卡循回地址时就返回
257. // 否则再通过Sigar工具包中的方法来获取
258. if (!NetFlags.LOOPBACK_ADDRESS.equals(address)) {
259. return address;
260. }
261. } catch (UnknownHostException e) {
262. // hostname not in DNS or /etc/hosts
263. }
264. Sigar sigar = new Sigar();
265. try {
266. address = sigar.getNetInterfaceConfig().getAddress();
267. } catch (SigarException e) {
268. address = NetFlags.LOOPBACK_ADDRESS;
269. } finally {
270. sigar.close();
271. }
272. return address;
273. }
274.
275. // c)取到当前机器的MAC地址
276. public String getMAC() {
277. Sigar sigar = null;
278. try {
279. sigar = new Sigar();
280. String[] ifaces = sigar.getNetInterfaceList();
281. String hwaddr = null;
282. for (int i = 0; i < ifaces.length; i++) {
283. NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);
284. if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0
285. || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {
286. continue;
287. }
288. /*
289. * 如果存在多张网卡包括虚拟机的网卡,默认只取第一张网卡的MAC地址,如果要返回所有的网卡(包括物理的和虚拟的)则可以修改方法的返回类型为数组或Collection
290. * ,通过在for循环里取到的多个MAC地址。
291. */
292. hwaddr = cfg.getHwaddr();
293. break;
294. }
295. return hwaddr != null ? hwaddr : null;
296. } catch (Exception e) {
297. return null;
298. } finally {
299. if (sigar != null)
300. sigar.close();
301. }
302. }
303.
304. // d)获取网络流量等信息
305. public void testNetIfList() throws Exception {
306. Sigar sigar = new Sigar();
307. String ifNames[] = sigar.getNetInterfaceList();
308. for (int i = 0; i < ifNames.length; i++) {
309. String name = ifNames[i];
310. NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
311. print("\nname = " + name);// 网络设备名
312. print("Address = " + ifconfig.getAddress());// IP地址
313. print("Netmask = " + ifconfig.getNetmask());// 子网掩码
314. if ((ifconfig.getFlags() & 1L) <= 0L) {
315. print("!IFF_UP...skipping getNetInterfaceStat");
316. continue;
317. }
318. try {
319. NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
320. print("RxPackets = " + ifstat.getRxPackets());// 接收的总包裹数
321. print("TxPackets = " + ifstat.getTxPackets());// 发送的总包裹数
322. print("RxBytes = " + ifstat.getRxBytes());// 接收到的总字节数
323. print("TxBytes = " + ifstat.getTxBytes());// 发送的总字节数
324. print("RxErrors = " + ifstat.getRxErrors());// 接收到的错误包数
325. print("TxErrors = " + ifstat.getTxErrors());// 发送数据包时的错误数
326. print("RxDropped = " + ifstat.getRxDropped());// 接收时丢弃的包数
327. print("TxDropped = " + ifstat.getTxDropped());// 发送时丢弃的包数
328. } catch (SigarNotImplementedException e) {
329. } catch (SigarException e) {
330. print(e.getMessage());
331. }
332. }
333. }
334.
335. void print(String msg) {
336. System.out.println(msg);
337. }
338.
339. // e)一些其他的信息
340. public void getEthernetInfo() {
341. Sigar sigar = null;
342. try {
343. sigar = new Sigar();
344. String[] ifaces = sigar.getNetInterfaceList();
345. for (int i = 0; i < ifaces.length; i++) {
346. NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);
347. if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0
348. || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {
349. continue;
350. }
351. System.out.println("cfg.getAddress() = " + cfg.getAddress());// IP地址
352. System.out.println("cfg.getBroadcast() = " + cfg.getBroadcast());// 网关广播地址
353. System.out.println("cfg.getHwaddr() = " + cfg.getHwaddr());// 网卡MAC地址
354. System.out.println("cfg.getNetmask() = " + cfg.getNetmask());// 子网掩码
355. System.out.println("cfg.getDescription() = " + cfg.getDescription());// 网卡描述信息
356. System.out.println("cfg.getType() = " + cfg.getType());//
357. System.out.println("cfg.getDestination() = " + cfg.getDestination());
358. System.out.println("cfg.getFlags() = " + cfg.getFlags());//
359. System.out.println("cfg.getMetric() = " + cfg.getMetric());
360. System.out.println("cfg.getMtu() = " + cfg.getMtu());
361. System.out.println("cfg.getName() = " + cfg.getName());
362. System.out.println();
363. }
364. } catch (Exception e) {
365. System.out.println("Error while creating GUID" + e);
366. } finally {
367. if (sigar != null)
368. sigar.close();
369. }
370. }
371.
372. public static void main(String[] arg) {
373. try {
374. SysInfo s = new SysInfo();
375. s.getCpuTotal();
376. s.getPhysicalMemory();
377. s.testGetOSInfo();
378. s.testWho();
379. s.testFileSystemInfo();
380. s.testNetIfList();
381. s.getEthernetInfo();
382. } catch (Exception e) {
383. // TODO Auto-generated catch block
384. e.printStackTrace();
385. }
386. }
387.}
- sigar.rar (487.8 KB)
- 下载次数: 9
- libsigar-x86-linux.rar (68.6 KB)
- 下载次数: 3