I am trying to find a few system statistics (CPU Utilisation%, Disk Utilisation% and Memory Utilisation%) through a Java program. For this purpose I have been using SIGAR. Although (I think) I am getting the latter two correct, I am not getting the CPU Utilisation% correct. Appending my code below, can someone help me understand what is going wrong here?
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import org.hyperic.sigar.CpuTimer;
import org.hyperic.sigar.DiskUsage;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
public class SystemMonitor {
private static Sigar sigar = new Sigar();
public static void getSystemStatistics(){
Mem mem = null;
CpuTimer cputimer = null;
FileSystemUsage filesystemusage = null;
try {
mem = sigar.getMem();
cputimer = new CpuTimer(sigar);
filesystemusage = sigar.getFileSystemUsage("C:");
} catch (SigarException se) {
se.printStackTrace();
}
System.out.print(mem.getUsedPercent()+"\t");
System.out.print(cputimer.getCpuUsage()+"\t");
System.out.print(filesystemusage.getUsePercent()+"\n");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SystemMonitor m1 = new SystemMonitor();
for(int i=0; i<1000;i++){
m1.getSystemStatistics();
}
}
}
I am getting a CPUUtilisation % of 0 consistently, which I think is incorrect.
I was interpreting CpuTimer incorrectly. I could find the CPU utilisation by using CpuPerc instead. Appending the code snippet:
public static void getSystemStatistics(){
Mem mem = null;
CpuPerc cpuperc = null;
FileSystemUsage filesystemusage = null;
try {
mem = sigar.getMem();
cpuperc = sigar.getCpuPerc();
filesystemusage = sigar.getFileSystemUsage("C:");
} catch (SigarException se) {
se.printStackTrace();
}
System.out.print(mem.getUsedPercent()+"\t");
System.out.print((cpuperc.getCombined()*100)+"\t");
System.out.print(filesystemusage.getUsePercent()+"\n");
}