最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2015年07月24日 12:05:19   作者:罪惡的花生  
這篇文章主要介紹了java獲取服務器基本信息的方法,涉及java獲取系統(tǒng)CPU、內(nèi)存及操作系統(tǒng)等相關(guān)信息的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

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

利用第三方的jar包:(Hyperic-hq官方網(wǎng)站:http://www.hyperic.com) 通過Hyperic-hq產(chǎn)品的基礎(chǔ)包sigar.jar來實現(xiàn)服務器狀態(tài)數(shù)據(jù)的獲取。Sigar.jar包是通過本地方法來調(diào)用操作系統(tǒng)API來獲取系統(tǒng)相關(guān)數(shù)據(jù)。Windows操作系統(tǒng)下Sigar.jar依賴sigar-amd64-winnt.dll或sigar-x86-winnt.dll,linux 操作系統(tǒng)下則依賴libsigar-amd64-linux.so或libsigar-x86-linux.so

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數(shù)量(單位:個) 
public static int getCpuCount() throws SigarException { 
 Sigar sigar = new Sigar(); 
 try { 
  return sigar.getCpuInfoList().length; 
 } finally { 
  sigar.close(); 
 } 
} // b)CPU的總量(單位:HZ)及CPU的相關(guān)信息 
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());// 緩沖存儲器數(shù)量 
  } 
 } catch (SigarException e) { 
  e.printStackTrace(); 
 } 
} // c)CPU的用戶使用量、系統(tǒng)使用剩余量、總的剩余量、總的使用占用量等(單位: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()));// 系統(tǒng)使用率 
 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.內(nèi)存資源信息 
public void getPhysicalMemory() { 
 // a)物理內(nèi)存信息 
 Sigar sigar = new Sigar(); 
 Mem mem; 
 try { 
  mem = sigar.getMem(); 
  // 內(nèi)存總量 
  System.out.println("Total = " + mem.getTotal() / 1024L + "K av"); 
  // 當前內(nèi)存使用量 
  System.out.println("Used = " + mem.getUsed() / 1024L + "K used"); 
  // 當前內(nèi)存剩余量 
  System.out.println("Free = " + mem.getFree() / 1024L + "K free");  // b)系統(tǒng)頁面文件交換區(qū)信息 
  Swap swap = sigar.getSwap(); 
  // 交換區(qū)總量 
  System.out.println("Total = " + swap.getTotal() / 1024L + "K av"); 
  // 當前交換區(qū)使用量 
  System.out.println("Used = " + swap.getUsed() / 1024L + "K used"); 
  // 當前交換區(qū)剩余量 
  System.out.println("Free = " + swap.getFree() / 1024L + "K free"); 
 } catch (SigarException e) { 
  e.printStackTrace(); 
 } 
} // 3.操作系統(tǒng)信息 // a)取到當前操作系統(tǒng)的名稱: 
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)取當前操作系統(tǒng)的信息 
public void testGetOSInfo() { 
 OperatingSystem OS = OperatingSystem.getInstance(); 
 // 操作系統(tǒng)內(nèi)核類型如: 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());// 
 // 系統(tǒng)描述 
 System.out.println("OS.getDescription() = " + OS.getDescription()); 
 System.out.println("OS.getMachine() = " + OS.getMachine());// 
 // 操作系統(tǒng)類型 
 System.out.println("OS.getName() = " + OS.getName()); 
 System.out.println("OS.getPatchLevel() = " + OS.getPatchLevel());// 
 // 操作系統(tǒng)的賣主 
 System.out.println("OS.getVendor() = " + OS.getVendor()); 
 // 賣主名稱 
 System.out 
  .println("OS.getVendorCodeName() = " + OS.getVendorCodeName()); 
 // 操作系統(tǒng)名稱 
 System.out.println("OS.getVendorName() = " + OS.getVendorName()); 
 // 操作系統(tǒng)賣主類型 
 System.out.println("OS.getVendorVersion() = " + OS.getVendorVersion()); 
 // 操作系統(tǒng)的版本號 
 System.out.println("OS.getVersion() = " + OS.getVersion()); 
} // c)取當前系統(tǒng)進程表中的用戶信息 
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()); 
   // 當前系統(tǒng)進程表中的用戶名 
   System.out.println("getUser() = " + _who.getUser()); 
  } 
  } 
 } catch (SigarException e) { 
  e.printStackTrace(); 
 } 
} // 4.資源信息(主要是硬盤) // a)取硬盤已有的分區(qū)及其詳細信息(通過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]; 
  // 分區(qū)的盤符名稱 
  System.out.println("fs.getDevName() = " + fs.getDevName()); 
  // 分區(qū)的盤符名稱 
  System.out.println("fs.getDirName() = " + fs.getDirName()); 
  System.out.println("fs.getFlags() = " + fs.getFlags());// 
  // 文件系統(tǒng)類型,比如 FAT32、NTFS 
  System.out.println("fs.getSysTypeName() = " + fs.getSysTypeName()); 
  // 文件系統(tǒng)類型名,比如本地硬盤、光驅(qū)、網(wǎng)絡文件系統(tǒng)等 
  System.out.println("fs.getTypeName() = " + fs.getTypeName()); 
  // 文件系統(tǒng)類型 
  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 : 本地硬盤 
  // 文件系統(tǒng)總大小 
  System.out.println(" Total = " + usage.getTotal() + "KB"); 
  // 文件系統(tǒng)剩余大小 
  System.out.println(" Free = " + usage.getFree() + "KB"); 
  // 文件系統(tǒng)可用大小 
  System.out.println(" Avail = " + usage.getAvail() + "KB"); 
  // 文件系統(tǒng)已經(jīng)使用量 
  System.out.println(" Used = " + usage.getUsed() + "KB"); 
  double usePercent = usage.getUsePercent() * 100D; 
  // 文件系統(tǒng)資源的利用率 
  System.out.println(" Usage = " + usePercent + "%"); 
  break; 
  case 3:// TYPE_NETWORK :網(wǎng)絡 
  break; 
  case 4:// TYPE_RAM_DISK :閃存 
  break; 
  case 5:// TYPE_CDROM :光驅(qū) 
  break; 
  case 6:// TYPE_SWAP :頁面交換 
  break; 
  } 
  System.out.println(" DiskReads = " + usage.getDiskReads()); 
  System.out.println(" DiskWrites = " + usage.getDiskWrites()); 
 } 
 return; 
} // 5.網(wǎng)絡信息 // 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(); 
  // 沒有出現(xiàn)異常而正常當取到的IP時,如果取到的不是網(wǎng)卡循回地址時就返回 
  // 否則再通過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; 
  } 
  /* 
   * 如果存在多張網(wǎng)卡包括虛擬機的網(wǎng)卡,默認只取第一張網(wǎng)卡的MAC地址,如果要返回所有的網(wǎng)卡(包括物理的和虛擬的)則可以修改方法的返回類型為數(shù)組或Collection 
   * ,通過在for循環(huán)里取到的多個MAC地址。 
   */
  hwaddr = cfg.getHwaddr(); 
  break; 
  } 
  return hwaddr != null  hwaddr : null; 
 } catch (Exception e) { 
  return null; 
 } finally { 
  if (sigar != null) 
  sigar.close(); 
 } 
} // d)獲取網(wǎng)絡流量等信息 
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);// 網(wǎng)絡設備名 
  print("Address = " + ifconfig.getAddress());// IP地址 
  print("Netmask = " + ifconfig.getNetmask());// 子網(wǎng)掩碼 
  if ((ifconfig.getFlags() & 1L) <= 0L) { 
  print("!IFF_UP...skipping getNetInterfaceStat"); 
  continue; 
  } 
  try { 
  NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name); 
  print("RxPackets = " + ifstat.getRxPackets());// 接收的總包裹數(shù) 
  print("TxPackets = " + ifstat.getTxPackets());// 發(fā)送的總包裹數(shù) 
  print("RxBytes = " + ifstat.getRxBytes());// 接收到的總字節(jié)數(shù) 
  print("TxBytes = " + ifstat.getTxBytes());// 發(fā)送的總字節(jié)數(shù) 
  print("RxErrors = " + ifstat.getRxErrors());// 接收到的錯誤包數(shù) 
  print("TxErrors = " + ifstat.getTxErrors());// 發(fā)送數(shù)據(jù)包時的錯誤數(shù) 
  print("RxDropped = " + ifstat.getRxDropped());// 接收時丟棄的包數(shù) 
  print("TxDropped = " + ifstat.getTxDropped());// 發(fā)送時丟棄的包數(shù) 
  } 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());// 網(wǎng)關(guān)廣播地址 
  System.out.println("cfg.getHwaddr() = " + cfg.getHwaddr());// 網(wǎng)卡MAC地址 
  System.out.println("cfg.getNetmask() = " + cfg.getNetmask());// 子網(wǎng)掩碼 
  System.out.println("cfg.getDescription() = "
   + cfg.getDescription());// 網(wǎng)卡描述信息 
  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程序設計有所幫助。

相關(guān)文章

  • java設計模式—靜態(tài)代理模式(聚合與繼承方式對比)

    java設計模式—靜態(tài)代理模式(聚合與繼承方式對比)

    下面小編就為大家?guī)硪黄猨ava設計模式—靜態(tài)代理模式(聚合與繼承方式對比)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • java下使用kaptcha生成驗證碼

    java下使用kaptcha生成驗證碼

    這篇文章主要介紹了java下使用kaptcha生成驗證碼,感興趣的小伙伴們可以參考一下
    2015-12-12
  • SpringBoot中默認緩存實現(xiàn)方案的示例代碼

    SpringBoot中默認緩存實現(xiàn)方案的示例代碼

    這篇文章主要介紹了SpringBoot中默認緩存實現(xiàn)方案,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • Java數(shù)據(jù)結(jié)構(gòu)之圖(動力節(jié)點Java學院整理)

    Java數(shù)據(jù)結(jié)構(gòu)之圖(動力節(jié)點Java學院整理)

    本文章主要講解學習如何使用JAVA語言以鄰接表的方式實現(xiàn)了數(shù)據(jù)結(jié)構(gòu)---圖(Graph)。對java數(shù)據(jù)結(jié)構(gòu)之圖相關(guān)知識感興趣的朋友一起學習吧
    2017-04-04
  • SpringBoot自定義start詳細圖文教程

    SpringBoot自定義start詳細圖文教程

    這篇文章主要給大家介紹了關(guān)于SpringBoot自定義start的相關(guān)資料,主要講述如何自定義start,實現(xiàn)一些自定義類的自動裝配,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • 簡單談談Java垃圾回收

    簡單談談Java垃圾回收

    本文是看了James Gosling的<<Java程序設計語言>>后結(jié)合自己的一些項目經(jīng)驗,簡單總結(jié)下關(guān)于java的垃圾回收問題的看法,有需要的小伙伴可以參考下
    2016-05-05
  • Springboot項目中內(nèi)嵌sqlite數(shù)據(jù)庫的配置流程

    Springboot項目中內(nèi)嵌sqlite數(shù)據(jù)庫的配置流程

    這篇文章主要介紹了Springboot項目中內(nèi)嵌sqlite數(shù)據(jù)庫的配置流程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • eclipse自動創(chuàng)建SpringBoot項目報錯的解決

    eclipse自動創(chuàng)建SpringBoot項目報錯的解決

    這篇文章主要介紹了eclipse自動創(chuàng)建SpringBoot項目報錯的解決方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 使用hutool工具進行導入導出excel表格

    使用hutool工具進行導入導出excel表格

    如何在后臺添加導入導出表格的功能呢,本期的文章將會帶領(lǐng)小伙伴們一起實現(xiàn)此功能,文中有詳細的代碼示例和圖文介紹,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2023-10-10
  • java文件上傳下載功能實現(xiàn)代碼

    java文件上傳下載功能實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了java文件上傳下載功能實現(xiàn)代碼,具有一定的參考價值,感興趣的朋友可以參考一下
    2016-06-06

最新評論

绥阳县| 郧西县| 体育| 双桥区| 兴化市| 桓仁| 博罗县| 灵石县| 双柏县| 北票市| 尖扎县| 华容县| 井陉县| 昆明市| 监利县| 通渭县| 莲花县| 宾川县| 葫芦岛市| 尉犁县| 克山县| 眉山市| 抚远县| 广州市| 博湖县| 建昌县| 绩溪县| 曲阳县| 敦化市| 武陟县| 双桥区| 石林| 徐闻县| 女性| 乌兰浩特市| 亳州市| 来安县| 彭泽县| 桐乡市| 滕州市| 彭山县|