配置参看上一篇文章
import org.hyperic.sigar.*;
public class SystemInfoDetector {
public SystemInfo detect(){
Sigar sigar = new Sigar();
long pid = sigar.getPid();
SystemInfo systemInfo = new SystemInfo();
try {
ProcMem name1 = sigar.getProcMem(pid);
//app使用内存
systemInfo.setAppMemUse(name1.getRss());
//app使用cpu
systemInfo.setAppCpuUse(sigar.getProcCpu(pid).getPercent());
//系统内存
systemInfo.setSystemTotalMem(sigar.getMem().getTotal());
systemInfo.setSystemMemUse(sigar.getMem().getUsed());
//cpu
CpuPerc[] cpuList = sigar.getCpuPercList();
double cpuUsed = 0;
for(CpuPerc cpuPerc:cpuList){
cpuUsed+=cpuPerc.getCombined();
}
cpuUsed = cpuUsed/cpuList.length;
systemInfo.setCpuUse(cpuUsed);
//硬盘
String osName = System.getProperties().getProperty("os.name").toLowerCase();
if(osName.indexOf("window")>=0){
//windows系统,判断盘符大小
String cwd = sigar.getProcExe(pid).getCwd();
char fsName = cwd.charAt(0);
FileSystemUsage usage = sigar.getFileSystemUsage(fsName+":\\");
systemInfo.setDiskSize(usage.getTotal());
systemInfo.setDiskUsedSize(usage.getUsed());
}else if(osName.indexOf("linux")>=0){
//linux系统,判断整个大小
FileSystemUsage usage = sigar.getFileSystemUsage("/");
systemInfo.setDiskSize(usage.getTotal());
systemInfo.setDiskUsedSize(usage.getAvail());
}
} catch (SigarException e) {
e.printStackTrace();
}
return systemInfo;
}
public static void main(String[] args){
SystemInfoDetector systemInfoDetector = new SystemInfoDetector();
SystemInfo systemInfo = systemInfoDetector.detect();
System.out.println(systemInfo);
}
}
public class SystemInfo {
private long systemTotalMem;//系统总内存
private long systemMemUse;//系统内存使用
private double cpuUse;//当前cpu占用
private long appMemUse;//本程序内存使用
private double appCpuUse;//本程序CPU占用
private long diskSize;//硬盘大小
private long diskUsedSize;//硬盘已使用
public long getSystemTotalMem() {
return systemTotalMem;
}
public void setSystemTotalMem(long systemTotalMem) {
this.systemTotalMem = systemTotalMem;
}
public long getSystemMemUse() {
return systemMemUse;
}
public void setSystemMemUse(long systemMemUse) {
this.systemMemUse = systemMemUse;
}
public double getCpuUse() {
return cpuUse;
}
public void setCpuUse(double cpuUse) {
this.cpuUse = cpuUse;
}
public long getAppMemUse() {
return appMemUse;
}
public void setAppMemUse(long appMemUse) {
this.appMemUse = appMemUse;
}
public double getAppCpuUse() {
return appCpuUse;
}
public void setAppCpuUse(double appCpuUse) {
this.appCpuUse = appCpuUse;
}
public long getDiskSize() {
return diskSize;
}
public void setDiskSize(long diskSize) {
this.diskSize = diskSize;
}
public long getDiskUsedSize() {
return diskUsedSize;
}
public void setDiskUsedSize(long diskUsedSize) {
this.diskUsedSize = diskUsedSize;
}
@Override
public String toString() {
int m = 1024*1024;
return "SystemInfo{" +
"systemTotalMem=" + (long)(systemTotalMem/m) +"M"+
", systemMemUse=" + (long)(systemMemUse/m) +"M"+
", cpuUse=" + cpuUse +
", appMemUse=" + (long)(appMemUse/m) +"M"+
", appCpuUse=" + appCpuUse +
", diskSize=" + (int)(diskSize/m)+"G" +
", diskUsedSize=" + (int)(diskUsedSize/m) +"G" +
'}';
}
}