国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java獲取服務器基本信息的方法

java獲取服務器基本信息的方法

2019-12-28 14:53罪惡的花生 JAVA教程

這篇文章主要介紹了java獲取服務器基本信息的方法,涉及java獲取系統CPU、內存及操作系統等相關信息的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了java獲取服務器基本信息的方法。分享給大家供大家參考。具體如下:

利用第三方的jar包:(Hyperic-hq官方網站:http://www.hyperic.com) 通過Hyperic-hq產品的基礎包sigar.jar來實現服務器狀態數據的獲取。Sigar.jar包是通過本地方法來調用操作系統API來獲取系統相關數據。Windows操作系統下Sigar.jar依賴sigar-amd64-winnt.dll或sigar-x86-winnt.dll,linux 操作系統下則依賴libsigar-amd64-linux.so或libsigar-x86-linux.so

?
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
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarNotImplementedException;
import org.hyperic.sigar.Swap;
public class SysInfo {
// 1.CPU資源信息 // a)CPU數量(單位:個)
public static int getCpuCount() throws SigarException {
 Sigar sigar = new Sigar();
 try {
  return sigar.getCpuInfoList().length;
 } finally {
  sigar.close();
 }
} // b)CPU的總量(單位:HZ)及CPU的相關信息
public void getCpuTotal() {
 Sigar sigar = new Sigar();
 CpuInfo[] infos;
 try {
  infos = sigar.getCpuInfoList();
  for (int i = 0; i < infos.length; i++) {// 不管是單塊CPU還是多CPU都適用
  CpuInfo info = infos[i];
  System.out.println("mhz=" + info.getMhz());// CPU的總量MHz
  System.out.println("vendor=" + info.getVendor());// 獲得CPU的賣主,如:Intel
  System.out.println("model=" + info.getModel());// 獲得CPU的類別,如:Celeron
  System.out.println("cache size=" + info.getCacheSize());// 緩沖存儲器數量
  }
 } catch (SigarException e) {
  e.printStackTrace();
 }
} // c)CPU的用戶使用量、系統使用剩余量、總的剩余量、總的使用占用量等(單位:100%)
public void testCpuPerc() {
 Sigar sigar = new Sigar();
 // 方式一,主要是針對一塊CPU的情況
 CpuPerc cpu;
 try {
  cpu = sigar.getCpuPerc();
  printCpuPerc(cpu);
 } catch (SigarException e) {
  e.printStackTrace();
 }
 // 方式二,不管是單塊CPU還是多CPU都適用
 CpuPerc cpuList[] = null;
 try {
  cpuList = sigar.getCpuPercList();
 } catch (SigarException e) {
  e.printStackTrace();
  return;
 }
 for (int i = 0; i < cpuList.length; i++) {
  printCpuPerc(cpuList[i]);
 }
} private void printCpuPerc(CpuPerc cpu) {
 System.out.println("User :" + CpuPerc.format(cpu.getUser()));// 用戶使用率
 System.out.println("Sys :" + CpuPerc.format(cpu.getSys()));// 系統使用率
 System.out.println("Wait :" + CpuPerc.format(cpu.getWait()));// 當前等待率
 System.out.println("Nice :" + CpuPerc.format(cpu.getNice()));//
 System.out.println("Idle :" + CpuPerc.format(cpu.getIdle()));// 當前空閑率
 System.out.println("Total :" + CpuPerc.format(cpu.getCombined()));// 總的使用率
} // 2.內存資源信息
public void getPhysicalMemory() {
 // a)物理內存信息
 Sigar sigar = new Sigar();
 Mem mem;
 try {
  mem = sigar.getMem();
  // 內存總量
  System.out.println("Total = " + mem.getTotal() / 1024L + "K av");
  // 當前內存使用量
  System.out.println("Used = " + mem.getUsed() / 1024L + "K used");
  // 當前內存剩余量
  System.out.println("Free = " + mem.getFree() / 1024L + "K free");  // b)系統頁面文件交換區信息
  Swap swap = sigar.getSwap();
  // 交換區總量
  System.out.println("Total = " + swap.getTotal() / 1024L + "K av");
  // 當前交換區使用量
  System.out.println("Used = " + swap.getUsed() / 1024L + "K used");
  // 當前交換區剩余量
  System.out.println("Free = " + swap.getFree() / 1024L + "K free");
 } catch (SigarException e) {
  e.printStackTrace();
 }
} // 3.操作系統信息 // a)取到當前操作系統的名稱:
public String getPlatformName() {
 String hostname = "";
 try {
  hostname = InetAddress.getLocalHost().getHostName();
 } catch (Exception exc) {
  Sigar sigar = new Sigar();
  try {
  hostname = sigar.getNetInfo().getHostName();
  } catch (SigarException e) {
  hostname = "localhost.unknown";
  } finally {
  sigar.close();
  }
 }
 return hostname;
} // b)取當前操作系統的信息
public void testGetOSInfo() {
 OperatingSystem OS = OperatingSystem.getInstance();
 // 操作系統內核類型如: 386、486、586等x86
 System.out.println("OS.getArch() = " + OS.getArch());
 System.out.println("OS.getCpuEndian() = " + OS.getCpuEndian());//
 System.out.println("OS.getDataModel() = " + OS.getDataModel());//
 // 系統描述
 System.out.println("OS.getDescription() = " + OS.getDescription());
 System.out.println("OS.getMachine() = " + OS.getMachine());//
 // 操作系統類型
 System.out.println("OS.getName() = " + OS.getName());
 System.out.println("OS.getPatchLevel() = " + OS.getPatchLevel());//
 // 操作系統的賣主
 System.out.println("OS.getVendor() = " + OS.getVendor());
 // 賣主名稱
 System.out
  .println("OS.getVendorCodeName() = " + OS.getVendorCodeName());
 // 操作系統名稱
 System.out.println("OS.getVendorName() = " + OS.getVendorName());
 // 操作系統賣主類型
 System.out.println("OS.getVendorVersion() = " + OS.getVendorVersion());
 // 操作系統的版本號
 System.out.println("OS.getVersion() = " + OS.getVersion());
} // c)取當前系統進程表中的用戶信息
public void testWho() {
 try {
  Sigar sigar = new Sigar();
  org.hyperic.sigar.Who[] who = sigar.getWhoList();
  if (who != null && who.length > 0) {
  for (int i = 0; i < who.length; i++) {
   System.out.println("\n~~~~~~~~~" + String.valueOf(i)+ "~~~~~~~~~");
   org.hyperic.sigar.Who _who = who[i];
   System.out.println("getDevice() = " + _who.getDevice());
   System.out.println("getHost() = " + _who.getHost());
   System.out.println("getTime() = " + _who.getTime());
   // 當前系統進程表中的用戶名
   System.out.println("getUser() = " + _who.getUser());
  }
  }
 } catch (SigarException e) {
  e.printStackTrace();
 }
} // 4.資源信息(主要是硬盤) // a)取硬盤已有的分區及其詳細信息(通過sigar.getFileSystemList()來獲得FileSystem列表對象,然后對其進行編歷):
public void testFileSystemInfo() throws Exception {
 Sigar sigar = new Sigar();
 FileSystem fslist[] = sigar.getFileSystemList();
 //String dir = System.getProperty("user.home");// 當前用戶文件夾路徑
 for (int i = 0; i < fslist.length; i++) {
  System.out.println("\n~~~~~~~~~~" + i + "~~~~~~~~~~");
  FileSystem fs = fslist[i];
  // 分區的盤符名稱
  System.out.println("fs.getDevName() = " + fs.getDevName());
  // 分區的盤符名稱
  System.out.println("fs.getDirName() = " + fs.getDirName());
  System.out.println("fs.getFlags() = " + fs.getFlags());//
  // 文件系統類型,比如 FAT32、NTFS
  System.out.println("fs.getSysTypeName() = " + fs.getSysTypeName());
  // 文件系統類型名,比如本地硬盤、光驅、網絡文件系統等
  System.out.println("fs.getTypeName() = " + fs.getTypeName());
  // 文件系統類型
  System.out.println("fs.getType() = " + fs.getType());
  FileSystemUsage usage = null;
  try {
  usage = sigar.getFileSystemUsage(fs.getDirName());
  } catch (SigarException e) {
  if (fs.getType() == 2)
   throw e;
  continue;
  }
  switch (fs.getType()) {
  case 0: // TYPE_UNKNOWN :未知
  break;
  case 1: // TYPE_NONE
  break;
  case 2: // TYPE_LOCAL_DISK : 本地硬盤
  // 文件系統總大小
  System.out.println(" Total = " + usage.getTotal() + "KB");
  // 文件系統剩余大小
  System.out.println(" Free = " + usage.getFree() + "KB");
  // 文件系統可用大小
  System.out.println(" Avail = " + usage.getAvail() + "KB");
  // 文件系統已經使用量
  System.out.println(" Used = " + usage.getUsed() + "KB");
  double usePercent = usage.getUsePercent() * 100D;
  // 文件系統資源的利用率
  System.out.println(" Usage = " + usePercent + "%");
  break;
  case 3:// TYPE_NETWORK :網絡
  break;
  case 4:// TYPE_RAM_DISK :閃存
  break;
  case 5:// TYPE_CDROM :光驅
  break;
  case 6:// TYPE_SWAP :頁面交換
  break;
  }
  System.out.println(" DiskReads = " + usage.getDiskReads());
  System.out.println(" DiskWrites = " + usage.getDiskWrites());
 }
 return;
} // 5.網絡信息 // a)當前機器的正式域名
public String getFQDN() {
 Sigar sigar = null;
 try {
  return InetAddress.getLocalHost().getCanonicalHostName();
 } catch (UnknownHostException e) {
  try {
  sigar = new Sigar();
  return sigar.getFQDN();
  } catch (SigarException ex) {
  return null;
  } finally {
  sigar.close();
  }
 }
} // b)取到當前機器的IP地址
public String getDefaultIpAddress() {
 String address = null;
 try {
  address = InetAddress.getLocalHost().getHostAddress();
  // 沒有出現異常而正常當取到的IP時,如果取到的不是網卡循回地址時就返回
  // 否則再通過Sigar工具包中的方法來獲取
  if (!NetFlags.LOOPBACK_ADDRESS.equals(address)) {
  return address;
  }
 } catch (UnknownHostException e) {
  // hostname not in DNS or /etc/hosts
 }
 Sigar sigar = new Sigar();
 try {
  address = sigar.getNetInterfaceConfig().getAddress();
 } catch (SigarException e) {
  address = NetFlags.LOOPBACK_ADDRESS;
 } finally {
  sigar.close();
 }
 return address;
} // c)取到當前機器的MAC地址
public String getMAC() {
 Sigar sigar = null;
 try {
  sigar = new Sigar();
  String[] ifaces = sigar.getNetInterfaceList();
  String hwaddr = null;
  for (int i = 0; i < ifaces.length; i++) {
  NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);
  if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress())
   || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0
   || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {
   continue;
  }
  /*
   * 如果存在多張網卡包括虛擬機的網卡,默認只取第一張網卡的MAC地址,如果要返回所有的網卡(包括物理的和虛擬的)則可以修改方法的返回類型為數組或Collection
   * ,通過在for循環里取到的多個MAC地址。
   */
  hwaddr = cfg.getHwaddr();
  break;
  }
  return hwaddr != null  hwaddr : null;
 } catch (Exception e) {
  return null;
 } finally {
  if (sigar != null)
  sigar.close();
 }
} // d)獲取網絡流量等信息
public void testNetIfList() throws Exception {
 Sigar sigar = new Sigar();
 String ifNames[] = sigar.getNetInterfaceList();
 for (int i = 0; i < ifNames.length; i++) {
  String name = ifNames[i];
  NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
  print("\nname = " + name);// 網絡設備名
  print("Address = " + ifconfig.getAddress());// IP地址
  print("Netmask = " + ifconfig.getNetmask());// 子網掩碼
  if ((ifconfig.getFlags() & 1L) <= 0L) {
  print("!IFF_UP...skipping getNetInterfaceStat");
  continue;
  }
  try {
  NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
  print("RxPackets = " + ifstat.getRxPackets());// 接收的總包裹數
  print("TxPackets = " + ifstat.getTxPackets());// 發送的總包裹數
  print("RxBytes = " + ifstat.getRxBytes());// 接收到的總字節數
  print("TxBytes = " + ifstat.getTxBytes());// 發送的總字節數
  print("RxErrors = " + ifstat.getRxErrors());// 接收到的錯誤包數
  print("TxErrors = " + ifstat.getTxErrors());// 發送數據包時的錯誤數
  print("RxDropped = " + ifstat.getRxDropped());// 接收時丟棄的包數
  print("TxDropped = " + ifstat.getTxDropped());// 發送時丟棄的包數
  } catch (SigarNotImplementedException e) {
  } catch (SigarException e) {
  print(e.getMessage());
  }
 }
} void print(String msg) {
 System.out.println(msg);
} // e)一些其他的信息
public void getEthernetInfo() {
 Sigar sigar = null;
 try {
  sigar = new Sigar();
  String[] ifaces = sigar.getNetInterfaceList();
  for (int i = 0; i < ifaces.length; i++) {
  NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);
  if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress())
   || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0
   || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {
   continue;
  }
  System.out.println("cfg.getAddress() = " + cfg.getAddress());// IP地址
  System.out
   .println("cfg.getBroadcast() = " + cfg.getBroadcast());// 網關廣播地址
  System.out.println("cfg.getHwaddr() = " + cfg.getHwaddr());// 網卡MAC地址
  System.out.println("cfg.getNetmask() = " + cfg.getNetmask());// 子網掩碼
  System.out.println("cfg.getDescription() = "
   + cfg.getDescription());// 網卡描述信息
  System.out.println("cfg.getType() = " + cfg.getType());//
  System.out.println("cfg.getDestination() = "
   + cfg.getDestination());
  System.out.println("cfg.getFlags() = " + cfg.getFlags());//
  System.out.println("cfg.getMetric() = " + cfg.getMetric());
  System.out.println("cfg.getMtu() = " + cfg.getMtu());
  System.out.println("cfg.getName() = " + cfg.getName());
  System.out.println();
  }
 } catch (Exception e) {
  System.out.println("Error while creating GUID" + e);
 } finally {
  if (sigar != null)
  sigar.close();
 }
}
}

希望本文所述對大家的java程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 四虎影院网 | 欧美午夜一区二区三区免费大片 | 久久99蜜桃综合影院免费观看 | 久久久看片 | 日韩一区在线播放 | 久久手机免费视频 | 免费黄色在线观看 | 国产精品99久久久久久动医院 | 日韩国产一区二区 | 久久久久av69精品 | 黄色av电影 | 亚洲视频在线播放 | 日韩有码在线播放 | av观看| 亚洲人人 | 日本综合色 | 久久99精品久久久 | 亚洲精品一区二区三区蜜桃久 | 玖玖爱国产 | 色在线影院 | 亚色一区| 国产淫片在线观看 | 欧美综合婷婷 | 国产精品久久久久久久午夜 | 国产成人精品一区二区三区网站观看 | 国产一区精品视频 | 成人a免费| 亚洲综合一区二区 | 日韩精品在线播放 | 日本视频网 | 中文字幕一区三级久久日本 | 精品国产一二三区 | 天天干夜夜爽 | 精品在线一区二区 | 久久精品2| 99最新在线视频 | 日韩欧美精品在线 | 国产成人精品av | 精品亚洲一区二区 | 国产一区影院 | 日韩欧美视频 |