Sigar linux系统中 java编程简单实用例子

1.下载Sigar (主要是要获取sigar.jar 和so文件)
wget http://downloads.sourceforge.net/project/sigar/sigar/1.6/hyperic-sigar-1.6.4-src.tar.gz
wget http://downloads.sourceforge.net/project/sigar/sigar/1.6/hyperic-sigar-1.6.4-src.tar.gz
tar zxf hyperic-sigar-1.6.4-src.tar.gz
cd hyperic-sigar-1.6.4-src
cd src
cd bindings
cd java
ant (没有ant时候,先安装ant)

2.拷贝出需要的jar包(sigar.jar)和so文件(libsigar-amd64-linux.so,libsigar-x86-linux.so)

so文件在bindings/Java/sigar-bin/lib中,把libsigar-amd64-linux.so文件放入/usr/lib64/中,需要sudo chmod 744 或 chmod 777 -R 修改libsigar-amd64-linux.so文件权限。(根据自己电脑是32/64位选择)

将jar包导入java工程,这个jar包也在bindings/Java/sigar-bin/lib中。

也可以maven导入:


<dependency>
    <groupId>org.fusesourcegroupId>
    <artifactId>sigarartifactId>
    <version>1.6.4version>
dependency>

gradle导入:

//https://mvnrepository.com/artifact/org.fusesource/sigar
compile group: 'org.fusesource', name: 'sigar', version: '1.6.4'

3.在java项目中创建工具类(类中用的import有的你可能没有,自己下载或是找其他替换就好)
只获取cpu和内存信息,其他类似,可以自己扩展。

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.google.api.client.util.Maps;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;

/**
 *  

Description:

*

Copyright:Copyright (c) 2016

*

Company://////

*

Email:///////

* * @author weijian * @date 16-10-25:下午2:04 * */
public class SigarUtils { private static Sigar sigar; public static Sigar getInstance() { if (null == sigar) { sigar = new Sigar(); } return sigar; } /** * 获取cpu信息返回json * * @return String * @throws SigarException */ public static String getCpuInfosListMap() throws SigarException { Map retMap = Maps.newHashMap(); CpuInfo[] cpuInfos = getInstance().getCpuInfoList(); CpuPerc[] cpuPercs = getInstance().getCpuPercList(); int count = cpuInfos.length; for (int i = 0; i < count; i++) { Map cpuMap = Maps.newHashMap(); cpuMap.put("amounts", cpuInfos[i].getMhz());//总量Mhz cpuMap.put("Vendor", cpuInfos[i].getVendor());//供应商 cpuMap.put("model", cpuInfos[i].getModel());//类型 cpuMap.put("cacheSize", cpuInfos[i].getCacheSize());//缓冲存储器数量 cpuMap.put("user", cpuPercs[i].getUser());//用户使用率 cpuMap.put("sys", cpuPercs[i].getSys());//系统使用率 cpuMap.put("wait", cpuPercs[i].getWait());//当前等待率 cpuMap.put("nice", cpuPercs[i].getNice());//错误率 cpuMap.put("idle", cpuPercs[i].getIdle());//空闲率 cpuMap.put("combined", cpuPercs[i].getCombined());//总使用率 retMap.put("cpu"+ i, cpuMap); } return JSON.toJSONString(retMap); } /** * 获取内存信息返回json * * @return * @throws SigarException */ public static String getMemoryInfosMap() throws SigarException { Map retmap = Maps.newHashMap(); Mem mem = getInstance().getMem(); Swap swap = getInstance().getSwap(); retmap.put("Total", mem.getTotal() / 1024L + "K"); retmap.put("Used", mem.getUsed() / 1024L + "K"); retmap.put("Free", mem.getFree() / 1024L + "K"); retmap.put("swapTotal", swap.getTotal() / 1024L + "K"); retmap.put("swapUsed", swap.getUsed() / 1024L + "K"); retmap.put("swapFree", swap.getFree() / 1024L + "K"); return JSON.toJSONString(retmap); } public static void main(String[] args){ try { System.out.println(SigarUtils.getCpuInfosListMap()); System.out.println(SigarUtils.getMemoryInfosMap()); } catch (SigarException e) { } } }

运行结果:(json格式,可自己修改)

{  
   "cpu2":{  
      "wait":0,
      "cacheSize":6144,
      "amounts":3700,
      "idle":0.2916666666666667,
      "model":"Core(TM) i5-4590 CPU @ 3.30GHz",
      "Vendor":"Intel",
      "combined":0.7083333333333333,
      "sys":0.16666666666666666,
      "user":0.5416666666666666,
      "nice":0
   },
   "cpu3":{  
      "wait":0,
      "cacheSize":6144,
      "amounts":3700,
      "idle":0.6326530612244898,
      "model":"Core(TM) i5-4590 CPU @ 3.30GHz",
      "Vendor":"Intel",
      "combined":0.36734693877551017,
      "sys":0.08163265306122448,
      "user":0.2857142857142857,
      "nice":0
   },
   "cpu0":{  
      "wait":0,
      "cacheSize":6144,
      "amounts":3700,
      "idle":0.6458333333333334,
      "model":"Core(TM) i5-4590 CPU @ 3.30GHz",
      "Vendor":"Intel",
      "combined":0.3541666666666667,
      "sys":0.0625,
      "user":0.2916666666666667,
      "nice":0
   },
   "cpu1":{  
      "wait":0,
      "cacheSize":6144,
      "amounts":3700,
      "idle":0.52,
      "model":"Core(TM) i5-4590 CPU @ 3.30GHz",
      "Vendor":"Intel",
      "combined":0.48000000000000004,
      "sys":0.08,
      "user":0.4,
      "nice":0
   }
}
{  
   "swapFree":"6711980K",
   "Used":"7766396K",
   "swapUsed":"1554768K",
   "Total":"8055600K",
   "swapTotal":"8266748K",
   "Free":"289204K"
}

你可能感兴趣的:(java)