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
| package com.ruben.utils.server;
import oshi.SystemInfo; import oshi.hardware.CentralProcessor; import oshi.hardware.GlobalMemory; import oshi.hardware.HardwareAbstractionLayer; import oshi.software.os.FileSystem; import oshi.software.os.OSFileStore; import oshi.software.os.OperatingSystem; import oshi.util.Util;
import java.math.BigDecimal; import java.math.RoundingMode; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import java.util.Properties;
public class ServerOsInfo {
public static ServerInfo getInfo() { ServerInfo info = new ServerInfo(); SystemInfo si = new SystemInfo(); HardwareAbstractionLayer hal = si.getHardware(); CentralProcessor processor = hal.getProcessor(); info.setCpu(setCpuInfo(processor)); GlobalMemory memory = hal.getMemory(); info.setMem(setMemInfo(memory)); info.setSys(setSysInfo()); info.setJvm(setJvmInfo()); OperatingSystem op = si.getOperatingSystem(); info.setFile(setSysFiles(op)); info.setIp(getHostIp()); info.setHostname(getHostName()); return info; }
private static ServerInfo.Cpu setCpuInfo(CentralProcessor processor) { long[] prevTicks = processor.getSystemCpuLoadTicks(); Util.sleep(1000); long[] ticks = processor.getSystemCpuLoadTicks(); double nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()]; double irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()]; double softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()]; double steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
double sys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
double used = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
double iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
double free = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
double total = used + nice + sys + free + iowait + irq + softirq + steal;
int cpuNum = processor.getLogicalProcessorCount();
ServerInfo.Cpu cpu = new ServerInfo.Cpu(); cpu.setCpucard(processor); cpu.setNum(cpuNum); cpu.setUsed(round(mul((sys + used) / total, 100), 2)); cpu.setFree(round(mul(free / total, 100), 2)); return cpu; }
private static ServerInfo.MemoryInfo setMemInfo(GlobalMemory memory) {
double total = +memory.getTotal();
double used = memory.getTotal() - memory.getAvailable();
double free = memory.getAvailable(); ServerInfo.MemoryInfo mem = new ServerInfo.MemoryInfo(); mem.setTotal(div(total, (1024 * 1024 * 1024), 2)); mem.setUsedMem(div(used, (1024 * 1024 * 1024), 2)); mem.setFree(round(mul(free / total, 100), 2)); mem.setUsed(round(mul(used / total, 100), 2)); return mem; }
private static Properties setSysInfo() { return System.getProperties(); }
private static ServerInfo.Jvm setJvmInfo() { ServerInfo.Jvm jvm = new ServerInfo.Jvm(); jvm.setTotal(div(Runtime.getRuntime().totalMemory(), 1024 * 1024, 2)); jvm.setMaxTotal(div(Runtime.getRuntime().maxMemory(), 1024 * 1024, 2)); jvm.setUsedMem(div((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()), 1024 * 1024, 2)); jvm.setUsed(round(mul((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) * 1.0 / Runtime.getRuntime().totalMemory(), 100), 2)); return jvm; }
private static List<ServerInfo.FileItem> setSysFiles(OperatingSystem os) { FileSystem fileSystem = os.getFileSystem(); OSFileStore[] fsArray = fileSystem.getFileStores(); List<ServerInfo.FileItem> list = new ArrayList<>(); for (OSFileStore fs : fsArray) { long free = fs.getUsableSpace(); long total = fs.getTotalSpace(); long used = total - free; ServerInfo.FileItem fileItem = new ServerInfo.FileItem(); fileItem.setPath(fs.getMount()); fileItem.setType(fs.getType()); fileItem.setName(fs.getName()); fileItem.setTotal(convertFileSize(total)); fileItem.setFree(convertFileSize(free)); fileItem.setUsed(convertFileSize(used)); fileItem.setRate(div(used, total, 4) * 100); list.add(fileItem); } return list; }
public static String convertFileSize(long size) { long kb = 1024; long mb = kb * 1024; long gb = mb * 1024; if (size >= gb) { return String.format("%.1f GB", (float) size / gb); } else if (size >= mb) { float f = (float) size / mb; return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f); } else if (size >= kb) { float f = (float) size / kb; return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f); } else { return String.format("%d B", size); } }
public static double div(double v1, double v2, int scale) { return ((double) ((int) (v1 / v2 * Math.pow(10, scale)))) / Math.pow(10, scale);
}
public static double round(double v, int scale) { if (scale < 0) { throw new IllegalArgumentException( "The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal one = new BigDecimal("1"); return b.divide(one, scale, RoundingMode.HALF_UP).doubleValue(); }
public static double mul(double v1, double v2) { BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.multiply(b2).doubleValue(); }
public static String getHostIp() { try { return InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException ignored) { } return "127.0.0.1"; }
public static String getHostName() { try { return InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException ignored) { } return "未知"; }
public static void main(String[] args) { System.out.println(div(19, 7, 4)); System.out.println(getInfo()); } }
|