JSONObject json = new JSONObject();
try {
// CPU utilization
cpuinfo = sigar.getCpuInfoList();
cpuPerc = sigar.getCpuPercList();
JSONArray cpuList = new JSONArray();
for (int i = 0; i < cpuinfo.length; i++) {
JSONObject cpuMap = new JSONObject();
cpuMap.putAll(cpuinfo[i].toMap());
cpuMap.put("combined", cpuPerc[i].getCombined());
cpuMap.put("user", cpuPerc[i].getUser());
cpuMap.put("sys", cpuPerc[i].getSys());
cpuMap.put("idle", cpuPerc[i].getIdle());
cpuMap.put("wait", cpuPerc[i].getWait());
cpuMap.put("nice", cpuPerc[i].getNice());
cpuMap.put("irq", cpuPerc[i].getIrq());
cpuList.add(cpuMap);
}
sigar.getCpuPerc();
json.put("cpu", cpuList);
// Uptime
uptime = sigar.getUptime();
json.put("uptime", uptime.getUptime());
// Load Average
loadavg = sigar.getLoadAverage();
JSONArray load = new JSONArray();
load.add(loadavg[0]);
load.add(loadavg[1]);
load.add(loadavg[2]);
json.put("loadavg", load);
// Memory Utilization
mem = sigar.getMem();
JSONObject memMap = new JSONObject();
memMap.putAll(mem.toMap());
json.put("memory", memMap);
// Network Utilization
netIf = sigar.getNetInterfaceList();
JSONArray netInterfaces = new JSONArray();
for (int i = 0; i < netIf.length; i++) {
NetInterfaceStat net = new NetInterfaceStat();
net = sigar.getNetInterfaceStat(netIf[i]);
JSONObject netMap = new JSONObject();
netMap.putAll(net.toMap());
if(previousNetworkStats.containsKey(netIf[i])) {
JSONObject deltaMap = previousNetworkStats.get(netIf[i]);
deltaMap.put("RxBytes", Long.parseLong(netMap.get("RxBytes").toString()) - Long.parseLong(deltaMap.get("RxBytes").toString()));
deltaMap.put("RxDropped", Long.parseLong(netMap.get("RxDropped").toString()) - Long.parseLong(deltaMap.get("RxDropped").toString()));
deltaMap.put("RxErrors", Long.parseLong(netMap.get("RxErrors").toString()) - Long.parseLong(deltaMap.get("RxErrors").toString()));
deltaMap.put("RxPackets", Long.parseLong(netMap.get("RxPackets").toString()) - Long.parseLong(deltaMap.get("RxPackets").toString()));
deltaMap.put("TxBytes", Long.parseLong(netMap.get("TxBytes").toString()) - Long.parseLong(deltaMap.get("TxBytes").toString()));
deltaMap.put("TxCollisions", Long.parseLong(netMap.get("TxCollisions").toString()) - Long.parseLong(deltaMap.get("TxCollisions").toString()));
deltaMap.put("TxErrors", Long.parseLong(netMap.get("TxErrors").toString()) - Long.parseLong(deltaMap.get("TxErrors").toString()));
deltaMap.put("TxPackets", Long.parseLong(netMap.get("TxPackets").toString()) - Long.parseLong(deltaMap.get("TxPackets").toString()));
netInterfaces.add(deltaMap);
skip = false;
} else {
netInterfaces.add(netMap);
skip = true;
}
previousNetworkStats.put(netIf[i], netMap);
}
json.put("network", netInterfaces);
// Filesystem Utilization
fs = sigar.getFileSystemList();
JSONArray fsList = new JSONArray();
for (int i = 0; i < fs.length; i++) {
FileSystemUsage usage = sigar.getFileSystemUsage(fs[i].getDirName());
JSONObject fsMap = new JSONObject();
fsMap.putAll(fs[i].toMap());
fsMap.put("ReadBytes", usage.getDiskReadBytes());
fsMap.put("Reads", usage.getDiskReads());
fsMap.put("WriteBytes", usage.getDiskWriteBytes());
fsMap.put("Writes", usage.getDiskWrites());
if(previousDiskStats.containsKey(fs[i].getDevName())) {
JSONObject deltaMap = previousDiskStats.get(fs[i].getDevName());
deltaMap.put("ReadBytes", usage.getDiskReadBytes() - (Long) deltaMap.get("ReadBytes"));
deltaMap.put("Reads", usage.getDiskReads() - (Long) deltaMap.get("Reads"));
deltaMap.put("WriteBytes", usage.getDiskWriteBytes() - (Long) deltaMap.get("WriteBytes"));
deltaMap.put("Writes", usage.getDiskWrites() - (Long) deltaMap.get("Writes"));
deltaMap.putAll(fs[i].toMap());
fsList.add(deltaMap);
skip = false;
} else {
fsList.add(fsMap);
skip = true;
}
previousDiskStats.put(fs[i].getDevName(), fsMap);
}
json.put("disk", fsList);