Get system info using sigar library in java

제일 기본적인 정보(CPU, Memory, Disk)만 가져오는 소스코드를 작성해보았다. 가져온 정보를 JSON 형식으로 변환해서 반환한다. json simple 라이브러리가 필요하다.

좀더 간단하게 짤 수 있을거같은데…

“`java
import org.hyperic.sigar.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class Usage {
public static Sigar sigar = null;
Usage(){
sigar = new Sigar();
}

public JSONObject getCPUUsage() throws SigarException {

CpuPerc cpu = sigar.getCpuPerc();
CpuPerc[] cpus = sigar.getCpuPercList();

// cpu array
JSONArray cpuArray = new JSONArray();

JSONObject cpuTotal = new JSONObject();
// usage array
JSONArray usageArray = new JSONArray();
JSONObject usage = new JSONObject();
usage.put(“USER”, CpuPerc.format(cpu.getUser()));
usage.put(“SYS”, CpuPerc.format(cpu.getSys()));
usage.put(“IDLE”, CpuPerc.format(cpu.getIdle()));
usageArray.add(0, usage);
cpuTotal.put(“Total”, usageArray);
cpuArray.add(0, cpuTotal);

for (int i = 0; i < cpus.length; i++){ JSONObject cpuItem = new JSONObject(); // usage array JSONArray cpuUsageArray = new JSONArray(); JSONObject cpuUsage = new JSONObject(); cpuUsage.put("USER", CpuPerc.format(cpus[i].getUser())); cpuUsage.put("SYS", CpuPerc.format(cpus[i].getSys())); cpuUsage.put("IDLE", CpuPerc.format(cpus[i].getIdle())); cpuUsageArray.add(0, cpuUsage); cpuItem.put("CPU " + i, cpuUsageArray); cpuArray.add(i+1, cpuItem); } JSONObject cpuItems = new JSONObject(); cpuItems.put("CPU", cpuArray); return cpuItems; } public JSONObject getMemUsage() throws SigarException { Mem mem = sigar.getMem(); JSONArray usageArray = new JSONArray(); JSONObject usage = new JSONObject(); usage.put("Total", format(mem.getTotal())); usage.put("Used", format(mem.getUsed())); usage.put("UsedPercent", format_percent(mem.getUsedPercent())); usage.put("Free", format(mem.getFree())); usage.put("FreePercent", format_percent(mem.getFreePercent())); usageArray.add(0, usage); JSONObject memItem = new JSONObject(); memItem.put("Memory", usageArray); return memItem; } public JSONObject getDiskUsage() throws SigarException { FileSystem[] fs = sigar.getFileSystemList(); FileSystemUsage fsu = null; JSONArray diskArray = new JSONArray(); int diskCount = 0; // total data; long total = 0, used = 0, free = 0; double usedPercent = 0.0; for(int i = 0; i < fs.length; i++){ try { fsu = sigar.getMountedFileSystemUsage(fs[i].getDirName()); // mount size 가 0인 더미부분은 스킵한다. (linux) if (fsu.getTotal() == 0) continue; } catch (SigarException e) { System.out.print(e.getMessage()); continue; } // total data total += fsu.getTotal(); used += fsu.getUsed(); usedPercent += fsu.getUsePercent(); free += fsu.getFree(); JSONObject diskItem = new JSONObject(); JSONArray diskUsageArray = new JSONArray(); JSONObject diskUsage = new JSONObject(); diskUsage.put("Total", format_giga(fsu.getTotal())); diskUsage.put("Used", format_giga(fsu.getUsed())); diskUsage.put("UsedPercent", format(fsu.getUsePercent())); diskUsage.put("Free", format_giga(fsu.getFree())); diskUsage.put("FSType", fs[i].getSysTypeName()); diskUsageArray.add(0, diskUsage); diskItem.put(fs[i].getDirName(), diskUsageArray); diskArray.add(diskCount++, diskItem); } // disk total data JSONObject diskTotalItem = new JSONObject(); JSONArray diskTotalUsageArray = new JSONArray(); JSONObject diskTotalUsage = new JSONObject(); diskTotalUsage.put("Total", format_giga(total)); diskTotalUsage.put("Used", format_giga(used)); diskTotalUsage.put("UsedPercent", format((usedPercent/diskCount))); diskTotalUsage.put("Free", format_giga(free)); diskTotalUsageArray.add(0, diskTotalUsage); diskTotalItem.put("Total", diskTotalUsageArray); diskArray.add(diskCount, diskTotalItem); JSONObject diskItems = new JSONObject(); diskItems.put("Disk", diskArray); return diskItems; } private String format_percent(double value) { return String.format("%.2f%%", value); } private String format_giga(long value) { return String.format("%.2fGB", (double)value / 1024 / 1024); } private String format(long value) { return Long.toString(new Long(value / 1024)); } private String format(double value) { return String.format("%.0f%%",value*100); } } ``` 각 메소드를 호출시 아래와 같은 형식으로 결과값이 나온다. ##### getCPUUsage() ```json { "CPU": [{ "Total": [{ "IDLE": "100.0%", "USER": "0.0%", "SYS": "0.0%" }] }, { "CPU 0": [{ "IDLE": "100.0%", "USER": "0.0%", "SYS": "0.0%" }] }, { "CPU 1": [{ "IDLE": "100.0%", "USER": "0.0%", "SYS": "0.0%" }] }, { "CPU 2": [{ "IDLE": "100.0%", "USER": "0.0%", "SYS": "0.0%" }] }, { "CPU 3": [{ "IDLE": "100.0%", "USER": "0.0%", "SYS": "0.0%" }] }] } ``` ##### getMemUsage() ```json { "Memory": [{ "Used": "3167720", "Total": "4073188", "UsedPercent": "75.61%", "Free": "905468", "FreePercent": "24.39%" }] } ``` ##### getDiskUsage() ```json { "Disk": [{ "C:\\": [{ "Used": "357.81GB", "Total": "698.54GB", "UsedPercent": "52%", "FSType": "NTFS", "Free": "340.72GB" }] }, { "Y:\\": [{ "Used": "58.49GB", "Total": "118.04GB", "UsedPercent": "50%", "FSType": "NTFS", "Free": "59.55GB" }] }, { "Z:\\": [{ "Used": "2927.31GB", "Total": "3664.62GB", "UsedPercent": "80%", "FSType": "NTFS", "Free": "737.31GB" }] }, { "Total": [{ "Used": "3343.61GB", "Total": "4481.20GB", "UsedPercent": "61%", "Free": "1137.59GB" }] }] } ```

Leave a Reply