java獲得辦事器根本信息的辦法。本站提示廣大學習愛好者:(java獲得辦事器根本信息的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是java獲得辦事器根本信息的辦法正文
本文實例講述了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
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法式設計有所贊助。