public static ComputerModel getComputerMessage() { try { ComputerModel computerModel = new ComputerModel(); long totalDisk=0; Sigar sigar = new Sigar(); CpuInfo infos[] = sigar.getCpuInfoList(); Mem mem = sigar.getMem(); String totalMem = bytes2GB(mem.getTotal()); CpuPerc cpuList[] = null; cpuList = sigar.getCpuPercList(); CpuInfo info = infos[0]; FileSystem fslist[] = sigar.getFileSystemList(); for (int i = 0; i < fslist.length; i++) { FileSystem fs = fslist[i]; FileSystemUsage usage = sigar.getFileSystemUsage(fs.getDirName()); switch (fs.getType()) { case 2: // TYPE_LOCAL_DISK : 本地硬盘 // 文件系统总大小 totalDisk += usage.getTotal(); break; } } computerModel.setCpuModel(info.getModel()); computerModel.setTotleMemory(totalMem); computerModel.setTotleDisk((totalDisk/1024/1024)+""); computerModel.setCpuCount(infos.length); return computerModel; }catch (Exception e){ e.printStackTrace(); } return null; }
public static String bytes2GB(long bytes) { DecimalFormat df = new DecimalFormat("#0.00"); return df.format((double) bytes / 1024/1024/1024); }