使用sigar.jar来监控系统的信息

使用sigar.jar来监控系统的信息。参考了http://blog.csdn.net/aoxida/article/details/8080492

     首先要下载hyperic-sigar-1.6.4.zip,官网是http://sigar.hyperic.com/    我在csdn上上传了资源http://download.csdn.net/detail/xdryhp164/8152133

     然后把hyperic-sigar-1.6.4\sigar-bin\lib下面的sigar.jar加到项目的buildPath里面,如果是windows系统,再把sigar-amd64-winnt.dll放到环境变量的路径中,我在这里加到了我的C:\Windows\system32目录下。

 

 

package com.sigar.hp;
import org.hyperic.sigar.Cpu;
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.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
  
/**
 * 
 * @author hp
 * 使用Sigar获得:
 * 			CPU的基本信息、使用百分比、使用时间 
 * 			内存信息
 * 			磁盘IO
 * 			网卡IO
 */
public class SigarTest {  
    private CpuInfo info;  
    private CpuPerc perc;  
    private Cpu timer;  
    private Mem mem;
    private FileSystem config;    
    private FileSystemUsage stat;
    private NetInterfaceStat netstat; 
    private NetInterfaceConfig netconfig; 
    private long rxbps;    
    private long txbps;  

  
    public SigarTest() {  
//    	System.out.println(System.getProperty("java.library.path"));//可以把sigar-amd64-winnt.dll放到C:\Windows\system32目录下。
    }  
  
    public void calcuate(Sigar sigar) throws SigarException, InterruptedException {  
        //cpu,mem
    	info = sigar.getCpuInfoList()[0];  
        perc = sigar.getCpuPerc();  
        timer = sigar.getCpu(); 
        mem = sigar.getMem(); 
        
        System.out.println("info:"+info);
        System.out.println("perc:"+perc);
        System.out.println("timer:"+timer);
        System.out.println("mem:"+mem);
        
        //磁盘IO
        FileSystem[] fsArr = sigar.getFileSystemList();
		for (FileSystem fs : fsArr) {
			config = fs;
			try {
				stat = sigar.getFileSystemUsage(fs.getDirName());
				System.out.println("stat:"+stat);
			} catch (SigarException e) {
				e.printStackTrace();
			}
		}
		//网卡IO
		String[] netIfs = sigar.getNetInterfaceList();    
		for (String name : netIfs) {
			netconfig = sigar.getNetInterfaceConfig(name);
			long start = System.currentTimeMillis();
			NetInterfaceStat statStart = sigar.getNetInterfaceStat(name);
			long rxBytesStart = statStart.getRxBytes();
			long txBytesStart = statStart.getTxBytes();
			Thread.sleep(1000);
			long end = System.currentTimeMillis();
			NetInterfaceStat statEnd = sigar.getNetInterfaceStat(name);
			long rxBytesEnd = statEnd.getRxBytes();
			long txBytesEnd = statEnd.getTxBytes();

			rxbps = (rxBytesEnd - rxBytesStart) * 8 / (end - start) * 1000;
			txbps = (txBytesEnd - txBytesStart) * 8 / (end - start) * 1000;
			netstat = sigar.getNetInterfaceStat(name);
			System.out.println(name+":netstat:"+netstat);
		}		
    }  
   
    public static void main(String[] args) throws Exception {  
        Sigar sigar = new Sigar();  
        SigarTest t = new SigarTest();
        t.calcuate(sigar);
    }  
  
}  


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值