Commit 53589c29 authored by Quxl's avatar Quxl

x

parent 6ee38964
...@@ -21,9 +21,29 @@ import org.hyperic.sigar.Sigar; ...@@ -21,9 +21,29 @@ import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap; import org.hyperic.sigar.Swap;
import com.alibaba.fastjson.JSON;
/**
*
* 使用此类前,请先将hyperic-sigar运行库添加至系统变量PATH下
* hyperic-sigar版本1.6.4(与pom.xml中 sigar.jar 版本保持一致)
* @author Quxl
*
*/
public class Usage { public class Usage {
public static void main(String[] args) throws SigarException {
System.out.println(JSON.toJSONString(new Usage().get()));
}
private final Log logger = LogFactory.getLog(Usage.class); private final Log logger = LogFactory.getLog(Usage.class);
private Sigar sigar = null;
public Usage() throws SigarException {
Sigar.load();
sigar = new Sigar();
}
public Map<String, ?> get() { public Map<String, ?> get() {
Map<String, Object> map = new LinkedHashMap<String, Object>(); Map<String, Object> map = new LinkedHashMap<String, Object>();
...@@ -86,7 +106,6 @@ public class Usage { ...@@ -86,7 +106,6 @@ public class Usage {
private Map<String, Object> getMemoryInfo() { private Map<String, Object> getMemoryInfo() {
Map<String, Object> map = new LinkedHashMap<String, Object>(); Map<String, Object> map = new LinkedHashMap<String, Object>();
try { try {
Sigar sigar = new Sigar();
Mem mem = sigar.getMem(); Mem mem = sigar.getMem();
Swap swap = sigar.getSwap(); Swap swap = sigar.getSwap();
map.put("内存总量", mem.getTotal() / 1024L + "K av"); map.put("内存总量", mem.getTotal() / 1024L + "K av");
...@@ -104,7 +123,6 @@ public class Usage { ...@@ -104,7 +123,6 @@ public class Usage {
private List<Map<String, Object>> getCpuInfo() { private List<Map<String, Object>> getCpuInfo() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try { try {
Sigar sigar = new Sigar();
CpuInfo infos[] = sigar.getCpuInfoList(); CpuInfo infos[] = sigar.getCpuInfoList();
CpuPerc cpuList[] = null; CpuPerc cpuList[] = null;
cpuList = sigar.getCpuPercList(); cpuList = sigar.getCpuPercList();
...@@ -135,7 +153,6 @@ public class Usage { ...@@ -135,7 +153,6 @@ public class Usage {
private List<Map<String, Object>> getFileSystemInfo() { private List<Map<String, Object>> getFileSystemInfo() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try { try {
Sigar sigar = new Sigar();
FileSystem fslist[] = sigar.getFileSystemList(); FileSystem fslist[] = sigar.getFileSystemList();
for (int i = 0; i < fslist.length; i++) { for (int i = 0; i < fslist.length; i++) {
Map<String, Object> map = new LinkedHashMap<String, Object>(); Map<String, Object> map = new LinkedHashMap<String, Object>();
...@@ -188,7 +205,6 @@ public class Usage { ...@@ -188,7 +205,6 @@ public class Usage {
private List<Map<String, Object>> getNetworkInfo() { private List<Map<String, Object>> getNetworkInfo() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try { try {
Sigar sigar = new Sigar();
String ifNames[] = sigar.getNetInterfaceList(); String ifNames[] = sigar.getNetInterfaceList();
for (int i = 0; i < ifNames.length; i++) { for (int i = 0; i < ifNames.length; i++) {
Map<String, Object> map = new LinkedHashMap<String, Object>(); Map<String, Object> map = new LinkedHashMap<String, Object>();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment