java获取系统信息_Java获取系统信息

/**

* @version 1.0

* @author 勋辉

* @createDate 2014年3月15日 上午10:02:18

* @since JDK1.6

*

*

*/

package com.topwalk.moc.etl.util;

import java.math.BigDecimal;

import java.math.RoundingMode;

import java.util.ArrayList;

import java.util.List;

import org.apache.log4j.Logger;

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.SigarException;

public class SystemInfoUtils {

private static Logger logger = Logger.getLogger(SystemInfoUtils.class);

public static void main(String[] args) {

getDiskInfo();

}

/**

* 获取CPU使用率

* @Title: getCpuPerc

* @returndouble

* @exception   异常

* @throws

* @see           需要参见的其它内容

* @sinceISSP v1.5

* @author勋辉

* @time 2014年3月15日上午10:40:21

*/

public static double  getCpuPerc(){

Sigar sigar = new Sigar();

CpuPerc cpuPerc[] =null;

double result =0d;

try {

/* CPU 信息列表 */

cpuPerc=sigar.getCpuPercList();

List list = new ArrayList();

/* 获取每颗CPU的总共使用量  */

for (int i = 0; i 

printCpuInfo(cpuPerc[i],i);

BigDecimal b = new BigDecimal(Double.toString(cpuPerc[i].getCombined()));

list.add(b);

}

/* 相加 */

BigDecimal add = list.get(0);

for (int i = 1; i 

add=add.add(list.get(i));

}

/* 求平均值 */

result = add.divide(new BigDecimal(list.size()),2,RoundingMode.HALF_EVEN).doubleValue();

} catch (SigarException e) {

logger.error("[获取CPU使用率失败]",e);

}

return result;

}

private static void printCpuInfo(CpuPerc cpuPerc,int i ){

logger.debug("[ CPU"+i+"   ]总共使用率:"+CpuPerc.format(cpuPerc.getCombined())+" ]");

}

/**

* 获取内存信息

* @Title: getPhysicalMemory

* @param

* @returnvoid

* @exception   异常

* @throws

* @see           需要参见的其它内容

* @sinceISSP v1.5

* @author勋辉

* @time 2014年3月15日上午10:50:55

*/

public static double getPhysicalMemory(){

double result = 0d;

Sigar sigar = new Sigar();

Mem men = null;

try {

men=sigar.getMem();

logger.debug("[系统内存总量:"+men.getTotal()/1024L+"k]");

logger.debug("[系统内存用量:"+men.getUsed()/1024L+"k]");

logger.debug("[使用率百分比:"+men.getUsedPercent()+"]");

BigDecimal userPrect = new BigDecimal(men.getUsedPercent());

result = userPrect.divide(new BigDecimal(1),2,RoundingMode.HALF_EVEN).doubleValue();

} catch (SigarException e) {

logger.error("[获取系统内存使用率]",e);

}

return result;

}

/**

* 获取磁盘信息

* @Title: getDiskInfo

* @param

* @returnvoid

* @exception   异常

* @throws

* @see           需要参见的其它内容

* @sinceISSP v1.5

* @author勋辉

* @time 2014年3月15日上午11:47:06

*/

public static double getDiskInfo(){

double result = 0d;

BigDecimal total = new BigDecimal(0);

BigDecimal used = new BigDecimal(0);

Sigar sigar = new Sigar();

try {

FileSystem fslist[] = sigar.getFileSystemList();

// String dir = System.getProperty("user.home");// 当前用户文件夹路径

for (int i = 0; i 

FileSystem fs = fslist[i];

FileSystemUsage usage = null;

try {

usage = sigar.getFileSystemUsage(fs.getDirName());

} catch (SigarException e) {

if (fs.getType() == 2)

continue;

}

switch (fs.getType()) {

case 0: // TYPE_UNKNOWN :未知

break;

case 1: // TYPE_NONE

break;

case 2: // TYPE_LOCAL_DISK : 本地硬盘

// 文件系统总大小

total= total.add(new BigDecimal((float)usage.getTotal()/1024/1024));

// 文件系统已经使用量

used=used.add(new BigDecimal((float)usage.getUsed()/1024/1024));

break;

case 3:// TYPE_NETWORK :网络

break;

case 4:// TYPE_RAM_DISK :闪存

break;

case 5:// TYPE_CDROM :光驱

break;

case 6:// TYPE_SWAP :页面交换

break;

}

}

} catch (SigarException e) {

e.printStackTrace();

}

result=used.divide(total,2,RoundingMode.HALF_EVEN).doubleValue();

return result;

}

}

下载地址 :

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值