Created
September 23, 2014 12:30
-
-
Save vegaasen/f658e3c022614ed7a118 to your computer and use it in GitHub Desktop.
SystemInformationUtils for Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @author <a href="[email protected]">vegard.aasen/t769765</a> | |
| */ | |
| public final class SystemInformationUtils { | |
| private SystemInformationUtils() { | |
| } | |
| public synchronized static SystemInformation getCurrentSystemInformation() { | |
| final SystemInformation systemInformation = new SystemInformation(); | |
| final OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class); | |
| if (operatingSystemMXBean != null) { | |
| systemInformation.setOs(new OS.Builder() | |
| .name(operatingSystemMXBean.getName()) | |
| .architecture(operatingSystemMXBean.getArch()) | |
| .numProcessors(operatingSystemMXBean.getAvailableProcessors()) | |
| .processorLoad(Double.parseDouble(new DecimalFormat("#0.00").format((operatingSystemMXBean.getSystemCpuLoad() * 100d)))) | |
| .description(operatingSystemMXBean.getVersion()) | |
| .build()); | |
| } | |
| final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); | |
| if (memoryMXBean != null) { | |
| systemInformation.setMemory(new Memory.Builder() | |
| .heapMemory(memoryMXBean.getHeapMemoryUsage()) | |
| .nonHeapMemory(memoryMXBean.getNonHeapMemoryUsage()) | |
| .build()); | |
| } | |
| systemInformation.setStartTime(ManagementFactory.getRuntimeMXBean().getStartTime()); | |
| systemInformation.setUpTime(ManagementFactory.getRuntimeMXBean().getUptime()); | |
| return systemInformation; | |
| } | |
| } | |
| // ############################################ Models | |
| /** | |
| * @author <a href="[email protected]">vegard.aasen/t769765</a> | |
| */ | |
| public final class SystemInformation implements Serializable { | |
| private long upTime; | |
| private long startTime; | |
| private OS os; | |
| private Memory memory; | |
| public OS getOs() { | |
| return os; | |
| } | |
| public void setOs(OS os) { | |
| this.os = os; | |
| } | |
| public Memory getMemory() { | |
| return memory; | |
| } | |
| public void setMemory(Memory memory) { | |
| this.memory = memory; | |
| } | |
| public long getUpTime(TimeUnit timeUnit) { | |
| return timeUnit.convert(upTime, timeUnit); | |
| } | |
| public long getUpTime() { | |
| return upTime; | |
| } | |
| public void setUpTime(long upTime) { | |
| this.upTime = upTime; | |
| } | |
| public long getStartTime(TimeUnit timeUnit) { | |
| return timeUnit.convert(startTime, timeUnit); | |
| } | |
| public long getStartTime() { | |
| return startTime; | |
| } | |
| public void setStartTime(long startTime) { | |
| this.startTime = startTime; | |
| } | |
| } | |
| /** | |
| * @author <a href="[email protected]">vegard.aasen/t769765</a> | |
| */ | |
| public final class OS implements Serializable { | |
| private String name; | |
| private String description; | |
| private String architecture; | |
| private int numProcessors; | |
| // in % | |
| private double processorLoad; | |
| private OS(final Builder builder) { | |
| this.name = builder.name; | |
| this.description = builder.description; | |
| this.architecture = builder.architecture; | |
| this.numProcessors = builder.numProcessors; | |
| this.processorLoad = builder.processorLoad; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public String getDescription() { | |
| return description; | |
| } | |
| public String getArchitecture() { | |
| return architecture; | |
| } | |
| public int getNumProcessors() { | |
| return numProcessors; | |
| } | |
| public double getProcessorLoad() { | |
| return processorLoad; | |
| } | |
| public double getSplitProcessorLoad() { | |
| return processorLoad / numProcessors; | |
| } | |
| public static final class Builder { | |
| private String name; | |
| private String description; | |
| private String architecture; | |
| private int numProcessors; | |
| private double processorLoad; | |
| public Builder name(final String s) { | |
| name = s; | |
| return this; | |
| } | |
| public Builder description(final String s) { | |
| description = s; | |
| return this; | |
| } | |
| public Builder architecture(final String s) { | |
| architecture = s; | |
| return this; | |
| } | |
| public Builder numProcessors(final int s) { | |
| numProcessors = s; | |
| return this; | |
| } | |
| public Builder processorLoad(final double s) { | |
| processorLoad = s; | |
| return this; | |
| } | |
| public OS build() { | |
| return new OS(this); | |
| } | |
| } | |
| } | |
| /** | |
| * @author <a href="[email protected]">vegard.aasen/t769765</a> | |
| */ | |
| public final class Memory implements Serializable { | |
| private MemoryUsage heapMemory; | |
| private MemoryUsage nonHeapMemory; | |
| private Memory(Builder builder) { | |
| this.nonHeapMemory = builder.nonHeapMemory; | |
| this.heapMemory = builder.heapMemory; | |
| } | |
| public MemoryUsage getHeapMemory() { | |
| return heapMemory; | |
| } | |
| public MemoryUsage getNonHeapMemory() { | |
| return nonHeapMemory; | |
| } | |
| public static final class Builder { | |
| private MemoryUsage heapMemory; | |
| private MemoryUsage nonHeapMemory; | |
| public Builder heapMemory(MemoryUsage m) { | |
| heapMemory = m; | |
| return this; | |
| } | |
| public Builder nonHeapMemory(MemoryUsage m) { | |
| nonHeapMemory = m; | |
| return this; | |
| } | |
| public Memory build() { | |
| return new Memory(this); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Imports for SystemInformationUtils:
import com.sun.management.OperatingSystemMXBean;
import com.telenor.fun.stuff.system.model.Memory;
import com.telenor.fun.stuff.system.model.OS;
import com.telenor.fun.stuff.system.model.SystemInformation;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.text.DecimalFormat;