1/** 2 * @version 1.0 3 * @author 勋辉 4 * @createDate 2014年3月15日 上午10:02:18 5 * @since JDK1.6 6 * 7 * 8 */ 9package com.topwalk.moc.etl.util; 10 11import java.math.BigDecimal; 12import java.math.RoundingMode; 13import java.util.ArrayList; 14import java.util.List; 15 16import org.apache.log4j.Logger; 17import org.hyperic.sigar.CpuPerc; 18import org.hyperic.sigar.FileSystem; 19import org.hyperic.sigar.FileSystemUsage; 20import org.hyperic.sigar.Mem; 21import org.hyperic.sigar.Sigar; 22import org.hyperic.sigar.SigarException; 23 24public class SystemInfoUtils { 25 26 private static Logger logger = Logger.getLogger(SystemInfoUtils.class); 27 public static void main(String[] args) { 28 getDiskInfo(); 29 } 30 31 /** 32 * 获取CPU使用率 33 * @Title: getCpuPerc 34 * @return double 35 * @exception 异常 36 * @throws 37 * @see 需要参见的其它内容 38 * @since ISSP v1.5 39 * @author 勋辉 40 * @time 2014年3月15日上午10:40:21 41 */ 42 public static double getCpuPerc(){ 43 Sigar sigar = new Sigar(); 44 CpuPerc cpuPerc[] =null; 45 double result =0d; 46 try { 47 /* CPU 信息列表 */ 48 cpuPerc=sigar.getCpuPercList(); 49 List<BigDecimal> list = new ArrayList<BigDecimal>(); 50 /* 获取每颗CPU的总共使用量 */ 51 for (int i = 0; i < cpuPerc.length; i++) { 52 printCpuInfo(cpuPerc[i],i); 53 BigDecimal b = new BigDecimal(Double.toString(cpuPerc[i].getCombined())); 54 list.add(b); 55 } 56 /* 相加 */ 57 BigDecimal add = list.get(0); 58 for (int i = 1; i < list.size(); i++) { 59 add=add.add(list.get(i)); 60 } 61 /* 求平均值 */ 62 result = add.divide(new BigDecimal(list.size()),2,RoundingMode.HALF_EVEN).doubleValue(); 63 } catch (SigarException e) { 64 logger.error("[获取CPU使用率失败]",e); 65 } 66 return result; 67 } 68 69 70 private static void printCpuInfo(CpuPerc cpuPerc,int i ){ 71 logger.debug("[ CPU"+i+" ]总共使用率:"+CpuPerc.format(cpuPerc.getCombined())+" ]"); 72 } 73 74 75 /** 76 * 获取内存信息 77 * @Title: getPhysicalMemory 78 * @param 79 * @return void 80 * @exception 异常 81 * @throws 82 * @see 需要参见的其它内容 83 * @since ISSP v1.5 84 * @author 勋辉 85 * @time 2014年3月15日上午10:50:55 86 */ 87 public static double getPhysicalMemory(){ 88 double result = 0d; 89 Sigar sigar = new Sigar(); 90 Mem men = null; 91 try { 92 men=sigar.getMem(); 93 logger.debug("[系统内存总量:"+men.getTotal()/1024L+"k]"); 94 logger.debug("[系统内存用量:"+men.getUsed()/1024L+"k]"); 95 logger.debug("[使用率百分比:"+men.getUsedPercent()+"]"); 96 97 BigDecimal userPrect = new BigDecimal(men.getUsedPercent()); 98 result = userPrect.divide(new BigDecimal(1),2,RoundingMode.HALF_EVEN).doubleValue(); 99 100 } catch (SigarException e) { 101 logger.error("[获取系统内存使用率]",e); 102 } 103 return result; 104 } 105 106 107 /** 108 * 获取磁盘信息 109 * @Title: getDiskInfo 110 * @param 111 * @return void 112 * @exception 异常 113 * @throws 114 * @see 需要参见的其它内容 115 * @since ISSP v1.5 116 * @author 勋辉 117 * @time 2014年3月15日上午11:47:06 118 */ 119 public static double getDiskInfo(){ 120 double result = 0d; 121 BigDecimal total = new BigDecimal(0); 122 BigDecimal used = new BigDecimal(0); 123 Sigar sigar = new Sigar(); 124 125 try { 126 FileSystem fslist[] = sigar.getFileSystemList(); 127 // String dir = System.getProperty("user.home");// 当前用户文件夹路径 128 for (int i = 0; i < fslist.length; i++) { 129 FileSystem fs = fslist[i]; 130 FileSystemUsage usage = null; 131 try { 132 usage = sigar.getFileSystemUsage(fs.getDirName()); 133 } catch (SigarException e) { 134 if (fs.getType() == 2) 135 continue; 136 } 137 switch (fs.getType()) { 138 case 0: // TYPE_UNKNOWN :未知 139 break; 140 case 1: // TYPE_NONE 141 break; 142 case 2: // TYPE_LOCAL_DISK : 本地硬盘 143 // 文件系统总大小 144 total= total.add(new BigDecimal((float)usage.getTotal()/1024/1024)); 145 // 文件系统已经使用量 146 used=used.add(new BigDecimal((float)usage.getUsed()/1024/1024)); 147 break; 148 case 3:// TYPE_NETWORK :网络 149 break; 150 case 4:// TYPE_RAM_DISK :闪存 151 break; 152 case 5:// TYPE_CDROM :光驱 153 break; 154 case 6:// TYPE_SWAP :页面交换 155 break; 156 } 157 } 158 } catch (SigarException e) { 159 e.printStackTrace(); 160 } 161 result=used.divide(total,2,RoundingMode.HALF_EVEN).doubleValue(); 162 return result; 163 } 164}
API地址:http://www.hyperic.com/support/docs/sigar/org/hyperic/sigar/Sigar.html
主页 : https://support.hyperic.com/display/SIGAR/Home
下载地址 :