Sigar(System Information Gatherer And Reporter),是一個開源的工具,提供了跨平臺的系統信息收集的API,核心由C語言實現的。php
能夠被如下語音調用:java
C/C++python
Java (sigar.jar auto-loads the native library)linux
Perl (requires bindings/perl build)ruby
.NET C# (requires bindings/csharp build)網絡
Ruby (requires bindings/ruby build)jvm
Python (requires bindings/python build)工具
PHP (requires bindings/php build)ui
Erlang (requires bindings/erl build)url
能夠收集的信息包括:
1, CPU信息,包括基本信息(vendor、model、mhz、cacheSize)和統計信息(user、sys、idle、nice、wait)
2, 文件系統信息,包括Filesystem、Size、Used、Avail、Use%、Type
3, 事件信息,相似Service Control Manager
4, 內存信息,物理內存和交換內存的總數、使用數、剩餘數;RAM的大小
5, 網絡信息,包括網絡接口信息和網絡路由信息
6, 進程信息,包括每一個進程的內存、CPU佔用數、狀態、參數、句柄
7, IO信息,包括IO的狀態,讀寫大小等
8, 服務狀態信息
9, 系統信息,包括操做系統版本,系統資源限制狀況,系統運行時間以及負載,JAVA的版本信息等.
將sigar.jar、jug-2.0.0-asl.jar、junit-3.8.2.jar、log4j-1.2.14.jar、libsigar-amd64-linux.so、libsigar-x86-linux.so等文件放到java須要調用的lib庫下。
String str=System.getProperty("java.library.path"); System.out.println(str);
上面能夠查看java.library.path加載路徑。
package com.nesec.util; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.hyperic.sigar.CpuPerc; import org.hyperic.sigar.FileSystem; import org.hyperic.sigar.FileSystemUsage; import org.hyperic.sigar.Mem; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Swap; import com.nesec.bean.ServerInfoFormMap; public class SystemInfo { public static ServerInfoFormMap SystemProperty() { ServerInfoFormMap monitorMap = new ServerInfoFormMap(); Runtime r = Runtime.getRuntime(); Properties props = System.getProperties(); InetAddress addr = null; String ip = ""; String hostName = ""; try { addr = InetAddress.getLocalHost(); } catch (UnknownHostException e) { ip = "沒法獲取主機IP"; hostName = "沒法獲取主機名"; } if (null != addr) { try { ip = addr.getHostAddress(); } catch (Exception e) { ip = "沒法獲取主機IP"; } try { hostName = addr.getHostName(); } catch (Exception e) { hostName = "沒法獲取主機名"; } } monitorMap.put("hostIp", ip);// 本地ip地址 monitorMap.put("hostName", hostName);// 本地主機名 monitorMap.put("osName", props.getProperty("os.name"));// 操做系統的名稱 monitorMap.put("arch", props.getProperty("os.arch"));// 操做系統的構架 monitorMap.put("osVersion", props.getProperty("os.version"));// 操做系統的版本 monitorMap.put("processors", r.availableProcessors());// JVM能夠使用的處理器個數 monitorMap.put("javaVersion", props.getProperty("java.version"));// Java的運行環境版本 monitorMap.put("vendor", props.getProperty("java.vendor"));// Java的運行環境供應商 monitorMap.put("javaUrl", props.getProperty("java.vendor.url"));// Java供應商的URL monitorMap.put("javaHome", props.getProperty("java.home"));// Java的安裝路徑 monitorMap.put("tmpdir", props.getProperty("java.io.tmpdir"));// 默認的臨時文件路徑 return monitorMap; } public static ServerInfoFormMap memory(Sigar sigar) { ServerInfoFormMap monitorMap = new ServerInfoFormMap(); try { Runtime r = Runtime.getRuntime(); monitorMap.put("jvmTotal", Common.div(r.totalMemory(), (1024 * 1024), 2) + "M");// java總內存 monitorMap.put("jvmUse", Common.div(r.totalMemory() - r.freeMemory(), (1024 * 1024), 2) + "M");// JVM使用內存 monitorMap.put("jvmFree", Common.div(r.freeMemory(), (1024 * 1024), 2) + "M");// JVM剩餘內存 monitorMap.put("jvmUsage", Common.div(r.totalMemory() - r.freeMemory(), r.totalMemory(), 2));// JVM使用率 Sigar sigar2 = new Sigar(); Mem mem = sigar2.getMem(); // 內存總量 monitorMap.put("ramTotal", Common.div(mem.getTotal(), (1024 * 1024 * 1024), 2) + "G");// 內存總量 monitorMap.put("ramUse", Common.div(mem.getUsed(), (1024 * 1024 * 1024), 2) + "G");// 當前內存使用量 monitorMap.put("ramFree", Common.div(mem.getFree(), (1024 * 1024 * 1024), 2) + "G");// 當前內存剩餘量 monitorMap.put("ramUsage", Common.div(mem.getUsed(), mem.getTotal(), 2));// 內存使用率 Swap swap = sigar2.getSwap(); // 交換區總量 monitorMap.put("swapTotal", Common.div(swap.getTotal(), (1024 * 1024 * 1024), 2) + "G"); // 當前交換區使用量 monitorMap.put("swapUse", Common.div(swap.getUsed(), (1024 * 1024 * 1024), 2) + "G"); // 當前交換區剩餘量 monitorMap.put("swapFree", Common.div(swap.getFree(), (1024 * 1024 * 1024), 2) + "G"); monitorMap.put("swapUsage", Common.div(swap.getUsed(), swap.getTotal(), 2));// } catch (Exception e) { } return monitorMap; } public static ServerInfoFormMap usage(Sigar sigar) { ServerInfoFormMap monitorMap = new ServerInfoFormMap(); try { Runtime r = Runtime.getRuntime(); monitorMap.put("jvmUsage", Math.round(Common.div(r.totalMemory()-r.freeMemory(), r.totalMemory(), 2)*100));// JVM使用率 Mem mem = sigar.getMem(); // 內存總量 monitorMap.put("ramUsage", Math.round(Common.div(mem.getUsed(), mem.getTotal(), 2)*100));// 內存使用率 List<ServerInfoFormMap> cpu = cpuInfos(sigar); double b = 0.0; for (ServerInfoFormMap m : cpu) { b += Double.valueOf(m.get("cpuTotal")+""); } monitorMap.put("cpuUsage", Math.round(b/cpu.size()));// cpu使用率 } catch (Exception e) { } return monitorMap; } public static List<ServerInfoFormMap> cpuInfos(Sigar sigar) { List<ServerInfoFormMap> monitorMaps = new ArrayList<ServerInfoFormMap>(); try { CpuPerc cpuList[] = sigar.getCpuPercList(); for (CpuPerc cpuPerc : cpuList) { ServerInfoFormMap monitorMap = new ServerInfoFormMap(); monitorMap.put("cpuUserUse", Math.round(cpuPerc.getUser()*100));// 用戶使用率 monitorMap.put("cpuSysUse", Math.round(cpuPerc.getSys()*100));// 系統使用率 monitorMap.put("cpuWait", Math.round(cpuPerc.getWait()*100));// 當前等待率 monitorMap.put("cpuFree", Math.round(cpuPerc.getIdle()*100));// 當前空閒率 monitorMap.put("cpuTotal",Math.round(cpuPerc.getCombined()*100));// 總的使用率 monitorMaps.add(monitorMap); } } catch (Exception e) { } return monitorMaps; } public static List<ServerInfoFormMap> diskInfos(Sigar sigar) throws Exception { List<ServerInfoFormMap> monitorMaps = new ArrayList<ServerInfoFormMap>(); FileSystem fslist[] = sigar.getFileSystemList(); for (int i = 0; i < fslist.length; i++) { ServerInfoFormMap monitorMap = new ServerInfoFormMap(); FileSystem fs = fslist[i]; // 文件系統類型名,好比本地硬盤、光驅、網絡文件系統等 FileSystemUsage usage = null; usage = sigar.getFileSystemUsage(fs.getDirName()); switch (fs.getType()) { case 0: // TYPE_UNKNOWN :未知 break; case 1: // TYPE_NONE break; case 2: // TYPE_LOCAL_DISK : 本地硬盤 monitorMap.put("diskName", fs.getDevName());// 系統盤名稱 monitorMap.put("diskType", fs.getSysTypeName());// 盤類型 // 文件系統總大小 monitorMap.put("diskTotal", usage.getTotal()); // 文件系統剩餘大小 monitorMap.put("diskFree", usage.getFree()); // 文件系統已經使用量 monitorMap.put("diskUse", usage.getUsed()); double usePercent = usage.getUsePercent() * 100D; // 文件系統資源的利用率 monitorMap.put("diskUsage", usePercent);// 內存使用率 monitorMaps.add(monitorMap); break; case 3:// TYPE_NETWORK :網絡 break; case 4:// TYPE_RAM_DISK :閃存 break; case 5:// TYPE_CDROM :光驅 break; case 6:// TYPE_SWAP :頁面交換 break; } } return monitorMaps; } public static void main(String[] args) throws Exception{ //ServerInfoFormMap serverinfo = SystemInfo.SystemProperty(); //ServerInfoFormMap memory = SystemInfo.memory(new Sigar()); //ServerInfoFormMap usage = SystemInfo.usage(new Sigar()); //List<ServerInfoFormMap> cpuInfos = SystemInfo.cpuInfos(new Sigar()); //List<ServerInfoFormMap> diskInfos = SystemInfo.diskInfos(sigar); } }
結合Highcharts做視圖的展現: