1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
| import org.slf4j.Logger; import org.slf4j.LoggerFactory; import oshi.SystemInfo; import oshi.hardware.*; import oshi.hardware.CentralProcessor.TickType; import oshi.software.os.*; import oshi.software.os.OperatingSystem.ProcessSort; import oshi.util.FormatUtil; import oshi.util.Util;
import java.util.Arrays; import java.util.List;
public class SystemInfoTest {
public static void main(String[] args) { Logger LOG = LoggerFactory.getLogger(SystemInfoTest.class);
LOG.info("Initializing System..."); SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware(); OperatingSystem os = si.getOperatingSystem();
System.out.println(os);
LOG.info("Checking computer system..."); printComputerSystem(hal.getComputerSystem());
LOG.info("Checking Processor..."); printProcessor(hal.getProcessor());
LOG.info("Checking Memory..."); printMemory(hal.getMemory());
LOG.info("Checking CPU..."); printCpu(hal.getProcessor());
LOG.info("Checking Processes..."); printProcesses(os, hal.getMemory());
LOG.info("Checking Sensors..."); printSensors(hal.getSensors());
LOG.info("Checking Power sources..."); printPowerSources(hal.getPowerSources());
LOG.info("Checking Disks..."); printDisks(hal.getDiskStores());
LOG.info("Checking File System..."); printFileSystem(os.getFileSystem());
LOG.info("Checking Network interfaces..."); printNetworkInterfaces(hal.getNetworkIFs());
LOG.info("Checking Network parameterss..."); printNetworkParameters(os.getNetworkParams());
LOG.info("Checking Displays..."); printDisplays(hal.getDisplays());
LOG.info("Checking USB Devices..."); printUsbDevices(hal.getUsbDevices(true)); }
private static void printComputerSystem(final ComputerSystem computerSystem) {
System.out.println("manufacturer: " + computerSystem.getManufacturer()); System.out.println("model: " + computerSystem.getModel()); System.out.println("serialnumber: " + computerSystem.getSerialNumber()); final Firmware firmware = computerSystem.getFirmware(); System.out.println("firmware:"); System.out.println(" manufacturer: " + firmware.getManufacturer()); System.out.println(" name: " + firmware.getName()); System.out.println(" description: " + firmware.getDescription()); System.out.println(" version: " + firmware.getVersion()); System.out.println(" release date: " + (firmware.getReleaseDate() == null ? "unknown" : firmware.getReleaseDate() == null ? "unknown" : FormatUtil.formatDate(firmware.getReleaseDate()))); final Baseboard baseboard = computerSystem.getBaseboard(); System.out.println("baseboard:"); System.out.println(" manufacturer: " + baseboard.getManufacturer()); System.out.println(" model: " + baseboard.getModel()); System.out.println(" version: " + baseboard.getVersion()); System.out.println(" serialnumber: " + baseboard.getSerialNumber()); }
private static void printProcessor(CentralProcessor processor) { System.out.println(processor); System.out.println(" " + processor.getPhysicalPackageCount() + " physical CPU package(s)"); System.out.println(" " + processor.getPhysicalProcessorCount() + " physical CPU core(s)"); System.out.println(" " + processor.getLogicalProcessorCount() + " logical CPU(s)");
System.out.println("Identifier: " + processor.getIdentifier()); System.out.println("ProcessorID: " + processor.getProcessorID()); }
private static void printMemory(GlobalMemory memory) { System.out.println("Memory: " + FormatUtil.formatBytes(memory.getAvailable()) + "/" + FormatUtil.formatBytes(memory.getTotal())); System.out.println("Swap used: " + FormatUtil.formatBytes(memory.getSwapUsed()) + "/" + FormatUtil.formatBytes(memory.getSwapTotal())); }
private static void printCpu(CentralProcessor processor) { System.out.println("Uptime: " + FormatUtil.formatElapsedSecs(processor.getSystemUptime())); System.out.println( "Context Switches/Interrupts: " + processor.getContextSwitches() + " / " + processor.getInterrupts());
long[] prevTicks = processor.getSystemCpuLoadTicks(); System.out.println("CPU, IOWait, and IRQ ticks @ 0 sec:" + Arrays.toString(prevTicks)); Util.sleep(1000); long[] ticks = processor.getSystemCpuLoadTicks(); System.out.println("CPU, IOWait, and IRQ ticks @ 1 sec:" + Arrays.toString(ticks)); long user = ticks[TickType.USER.getIndex()] - prevTicks[TickType.USER.getIndex()]; long nice = ticks[TickType.NICE.getIndex()] - prevTicks[TickType.NICE.getIndex()]; long sys = ticks[TickType.SYSTEM.getIndex()] - prevTicks[TickType.SYSTEM.getIndex()]; long idle = ticks[TickType.IDLE.getIndex()] - prevTicks[TickType.IDLE.getIndex()]; long iowait = ticks[TickType.IOWAIT.getIndex()] - prevTicks[TickType.IOWAIT.getIndex()]; long irq = ticks[TickType.IRQ.getIndex()] - prevTicks[TickType.IRQ.getIndex()]; long softirq = ticks[TickType.SOFTIRQ.getIndex()] - prevTicks[TickType.SOFTIRQ.getIndex()]; long steal = ticks[TickType.STEAL.getIndex()] - prevTicks[TickType.STEAL.getIndex()]; long totalCpu = user + nice + sys + idle + iowait + irq + softirq + steal;
System.out.format( "User: %.1f%% Nice: %.1f%% System: %.1f%% Idle: %.1f%% IOwait: %.1f%% IRQ: %.1f%% SoftIRQ: %.1f%% Steal: %.1f%%%n", 100d * user / totalCpu, 100d * nice / totalCpu, 100d * sys / totalCpu, 100d * idle / totalCpu, 100d * iowait / totalCpu, 100d * irq / totalCpu, 100d * softirq / totalCpu, 100d * steal / totalCpu); System.out.format("CPU load: %.1f%% (counting ticks)%n", processor.getSystemCpuLoadBetweenTicks() * 100); System.out.format("CPU load: %.1f%% (OS MXBean)%n", processor.getSystemCpuLoad() * 100); double[] loadAverage = processor.getSystemLoadAverage(3); System.out.println("CPU load averages:" + (loadAverage[0] < 0 ? " N/A" : String.format(" %.2f", loadAverage[0])) + (loadAverage[1] < 0 ? " N/A" : String.format(" %.2f", loadAverage[1])) + (loadAverage[2] < 0 ? " N/A" : String.format(" %.2f", loadAverage[2]))); StringBuilder procCpu = new StringBuilder("CPU load per processor:"); double[] load = processor.getProcessorCpuLoadBetweenTicks(); for (double avg : load) { procCpu.append(String.format(" %.1f%%", avg * 100)); } System.out.println(procCpu.toString()); }
private static void printProcesses(OperatingSystem os, GlobalMemory memory) { System.out.println("Processes: " + os.getProcessCount() + ", Threads: " + os.getThreadCount()); List<OSProcess> procs = Arrays.asList(os.getProcesses(5, ProcessSort.CPU));
System.out.println(" PID %CPU %MEM VSZ RSS Name"); for (int i = 0; i < procs.size() && i < 5; i++) { OSProcess p = procs.get(i); System.out.format(" %5d %5.1f %4.1f %9s %9s %s%n", p.getProcessID(), 100d * (p.getKernelTime() + p.getUserTime()) / p.getUpTime(), 100d * p.getResidentSetSize() / memory.getTotal(), FormatUtil.formatBytes(p.getVirtualSize()), FormatUtil.formatBytes(p.getResidentSetSize()), p.getName()); } }
private static void printSensors(Sensors sensors) { System.out.println("Sensors:"); System.out.format(" CPU Temperature: %.1f°C%n", sensors.getCpuTemperature()); System.out.println(" Fan Speeds: " + Arrays.toString(sensors.getFanSpeeds())); System.out.format(" CPU Voltage: %.1fV%n", sensors.getCpuVoltage()); }
private static void printPowerSources(PowerSource[] powerSources) { StringBuilder sb = new StringBuilder("Power: "); if (powerSources.length == 0) { sb.append("Unknown"); } else { double timeRemaining = powerSources[0].getTimeRemaining(); if (timeRemaining < -1d) { sb.append("Charging"); } else if (timeRemaining < 0d) { sb.append("Calculating time remaining"); } else { sb.append(String.format("%d:%02d remaining", (int) (timeRemaining / 3600), (int) (timeRemaining / 60) % 60)); } } for (PowerSource pSource : powerSources) { sb.append(String.format("%n %s @ %.1f%%", pSource.getName(), pSource.getRemainingCapacity() * 100d)); } System.out.println(sb.toString()); }
private static void printDisks(HWDiskStore[] diskStores) { System.out.println("Disks:"); for (HWDiskStore disk : diskStores) { boolean readwrite = disk.getReads() > 0 || disk.getWrites() > 0; System.out.format(" %s: (model: %s - S/N: %s) size: %s, reads: %s (%s), writes: %s (%s), xfer: %s ms%n", disk.getName(), disk.getModel(), disk.getSerial(), disk.getSize() > 0 ? FormatUtil.formatBytesDecimal(disk.getSize()) : "?", readwrite ? disk.getReads() : "?", readwrite ? FormatUtil.formatBytes(disk.getReadBytes()) : "?", readwrite ? disk.getWrites() : "?", readwrite ? FormatUtil.formatBytes(disk.getWriteBytes()) : "?", readwrite ? disk.getTransferTime() : "?"); HWPartition[] partitions = disk.getPartitions(); if (partitions == null) { continue; } for (HWPartition part : partitions) { System.out.format(" |-- %s: %s (%s) Maj:Min=%d:%d, size: %s%s%n", part.getIdentification(), part.getName(), part.getType(), part.getMajor(), part.getMinor(), FormatUtil.formatBytesDecimal(part.getSize()), part.getMountPoint().isEmpty() ? "" : " @ " + part.getMountPoint()); } } }
private static void printFileSystem(FileSystem fileSystem) { System.out.println("File System:");
System.out.format(" File Descriptors: %d/%d%n", fileSystem.getOpenFileDescriptors(), fileSystem.getMaxFileDescriptors());
OSFileStore[] fsArray = fileSystem.getFileStores(); for (OSFileStore fs : fsArray) { long usable = fs.getUsableSpace(); long total = fs.getTotalSpace(); System.out.format( " %s (%s) [%s] %s of %s free (%.1f%%) is %s " + (fs.getLogicalVolume() != null && fs.getLogicalVolume().length() > 0 ? "[%s]" : "%s") + " and is mounted at %s%n", fs.getName(), fs.getDescription().isEmpty() ? "file system" : fs.getDescription(), fs.getType(), FormatUtil.formatBytes(usable), FormatUtil.formatBytes(fs.getTotalSpace()), 100d * usable / total, fs.getVolume(), fs.getLogicalVolume(), fs.getMount()); } }
private static void printNetworkInterfaces(NetworkIF[] networkIFs) { System.out.println("Network interfaces:"); for (NetworkIF net : networkIFs) { System.out.format(" Name: %s (%s)%n", net.getName(), net.getDisplayName()); System.out.format(" MAC Address: %s %n", net.getMacaddr()); System.out.format(" MTU: %s, Speed: %s %n", net.getMTU(), FormatUtil.formatValue(net.getSpeed(), "bps")); System.out.format(" IPv4: %s %n", Arrays.toString(net.getIPv4addr())); System.out.format(" IPv6: %s %n", Arrays.toString(net.getIPv6addr())); boolean hasData = net.getBytesRecv() > 0 || net.getBytesSent() > 0 || net.getPacketsRecv() > 0 || net.getPacketsSent() > 0; System.out.format(" Traffic: received %s/%s%s; transmitted %s/%s%s %n", hasData ? net.getPacketsRecv() + " packets" : "?", hasData ? FormatUtil.formatBytes(net.getBytesRecv()) : "?", hasData ? " (" + net.getInErrors() + " err)" : "", hasData ? net.getPacketsSent() + " packets" : "?", hasData ? FormatUtil.formatBytes(net.getBytesSent()) : "?", hasData ? " (" + net.getOutErrors() + " err)" : ""); } }
private static void printNetworkParameters(NetworkParams networkParams) { System.out.println("Network parameters:"); System.out.format(" Host name: %s%n", networkParams.getHostName()); System.out.format(" Domain name: %s%n", networkParams.getDomainName()); System.out.format(" DNS servers: %s%n", Arrays.toString(networkParams.getDnsServers())); System.out.format(" IPv4 Gateway: %s%n", networkParams.getIpv4DefaultGateway()); System.out.format(" IPv6 Gateway: %s%n", networkParams.getIpv6DefaultGateway()); }
private static void printDisplays(Display[] displays) { System.out.println("Displays:"); int i = 0; for (Display display : displays) { System.out.println(" Display " + i + ":"); System.out.println(display.toString()); i++; } }
private static void printUsbDevices(UsbDevice[] usbDevices) { System.out.println("USB Devices:"); for (UsbDevice usbDevice : usbDevices) { System.out.println(usbDevice.toString()); } } }
|