| package com.zp.utils; |
| |
| import com.alibaba.fastjson.JSONArray; |
| import com.alibaba.fastjson.JSONObject; |
| 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.net.InetAddress; |
| import java.net.UnknownHostException; |
| import java.text.DecimalFormat; |
| import java.util.Properties; |
| |
| |
| * 系统消息工具类 |
| * |
| **/ |
| public class SystemInfoUtils { |
| private static final int OSHI_WAIT_SECOND = 1000; |
| private static SystemInfo systemInfo = new SystemInfo(); |
| private static HardwareAbstractionLayer hardware = systemInfo.getHardware(); |
| private static OperatingSystem operatingSystem = systemInfo.getOperatingSystem(); |
| |
| public static JSONObject getCpuInfo() { |
| JSONObject cpuInfo = new JSONObject(); |
| CentralProcessor processor = hardware.getProcessor(); |
| |
| long[] prevTicks = processor.getSystemCpuLoadTicks(); |
| Util.sleep(OSHI_WAIT_SECOND); |
| long[] ticks = processor.getSystemCpuLoadTicks(); |
| long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()]; |
| long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()]; |
| long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()]; |
| long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()]; |
| long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()]; |
| long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()]; |
| long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()]; |
| long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()]; |
| long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal; |
| |
| cpuInfo.put("cpuNum", processor.getLogicalProcessorCount()); |
| |
| cpuInfo.put("cSys", new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu)); |
| |
| cpuInfo.put("user", new DecimalFormat("#.##%").format(user * 1.0 / totalCpu)); |
| |
| cpuInfo.put("iowait", new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu)); |
| |
| cpuInfo.put("idle", new DecimalFormat("#.##%").format(1.0 - (idle * 1.0 / totalCpu))); |
| return cpuInfo; |
| } |
| |
| |
| * 系统 jvm 信息 |
| */ |
| public static JSONObject getJvmInfo() { |
| JSONObject cpuInfo = new JSONObject(); |
| Properties props = System.getProperties(); |
| Runtime runtime = Runtime.getRuntime(); |
| long jvmTotalMemoryByte = runtime.totalMemory(); |
| long freeMemoryByte = runtime.freeMemory(); |
| |
| cpuInfo.put("total", formatByte(runtime.totalMemory())); |
| |
| cpuInfo.put("free", formatByte(runtime.freeMemory())); |
| |
| cpuInfo.put("max", formatByte(runtime.maxMemory())); |
| |
| cpuInfo.put("user", formatByte(jvmTotalMemoryByte - freeMemoryByte)); |
| |
| cpuInfo.put("usageRate", new DecimalFormat("#.##%").format((jvmTotalMemoryByte - freeMemoryByte) * 1.0 / jvmTotalMemoryByte)); |
| |
| cpuInfo.put("jdkVersion", props.getProperty("java.version")); |
| |
| cpuInfo.put("jdkHome", props.getProperty("java.home")); |
| return cpuInfo; |
| } |
| |
| |
| * 系统内存信息 |
| */ |
| public static JSONObject getMemInfo() { |
| JSONObject cpuInfo = new JSONObject(); |
| GlobalMemory memory = systemInfo.getHardware().getMemory(); |
| |
| long totalByte = memory.getTotal(); |
| |
| long acaliableByte = memory.getAvailable(); |
| |
| cpuInfo.put("total", formatByte(totalByte)); |
| |
| cpuInfo.put("used", formatByte(totalByte - acaliableByte)); |
| |
| cpuInfo.put("free", formatByte(acaliableByte)); |
| |
| cpuInfo.put("usageRate", new DecimalFormat("#.##%").format((totalByte - acaliableByte) * 1.0 / totalByte)); |
| return cpuInfo; |
| } |
| |
| |
| * 系统盘符信息 |
| */ |
| public static JSONArray getSysFileInfo() { |
| JSONObject cpuInfo; |
| JSONArray sysFiles = new JSONArray(); |
| FileSystem fileSystem = operatingSystem.getFileSystem(); |
| OSFileStore[] fsArray = fileSystem.getFileStores(); |
| for (OSFileStore fs : fsArray) { |
| cpuInfo = new JSONObject(); |
| |
| cpuInfo.put("dirName", fs.getMount()); |
| |
| cpuInfo.put("sysTypeName", fs.getType()); |
| |
| cpuInfo.put("typeName", fs.getName()); |
| |
| cpuInfo.put("total", formatByte(fs.getTotalSpace())); |
| |
| cpuInfo.put("free", formatByte(fs.getUsableSpace())); |
| |
| cpuInfo.put("used", formatByte(fs.getTotalSpace() - fs.getUsableSpace())); |
| if (fs.getTotalSpace() == 0) { |
| |
| cpuInfo.put("usage", 0); |
| } else { |
| cpuInfo.put("usage",new DecimalFormat("#.##%").format((fs.getTotalSpace() - fs.getUsableSpace()) * 1.0 / fs.getTotalSpace())); |
| } |
| sysFiles.add(cpuInfo); |
| } |
| return sysFiles; |
| } |
| |
| |
| * 系统信息 |
| */ |
| public static JSONObject getSysInfo() throws UnknownHostException { |
| JSONObject cpuInfo = new JSONObject(); |
| Properties props = System.getProperties(); |
| |
| cpuInfo.put("osName", props.getProperty("os.name")); |
| |
| cpuInfo.put("osArch", props.getProperty("os.arch")); |
| |
| cpuInfo.put("computerName", InetAddress.getLocalHost().getHostName()); |
| |
| cpuInfo.put("computerIp", InetAddress.getLocalHost().getHostAddress()); |
| |
| cpuInfo.put("userDir", props.getProperty("user.dir")); |
| return cpuInfo; |
| } |
| |
| |
| * 所有系统信息 |
| */ |
| public static JSONObject getInfo() throws UnknownHostException { |
| JSONObject info = new JSONObject(); |
| info.put("cpuInfo", getCpuInfo()); |
| info.put("jvmInfo", getJvmInfo()); |
| info.put("memInfo", getMemInfo()); |
| info.put("sysInfo", getSysInfo()); |
| info.put("sysFileInfo", getSysFileInfo()); |
| return info; |
| } |
| |
| |
| * 单位转换 |
| */ |
| private static String formatByte(long byteNumber) { |
| |
| double FORMAT = 1024.0; |
| double kbNumber = byteNumber / FORMAT; |
| if (kbNumber < FORMAT) { |
| return new DecimalFormat("#.##KB").format(kbNumber); |
| } |
| double mbNumber = kbNumber / FORMAT; |
| if (mbNumber < FORMAT) { |
| return new DecimalFormat("#.##MB").format(mbNumber); |
| } |
| double gbNumber = mbNumber / FORMAT; |
| if (gbNumber < FORMAT) { |
| return new DecimalFormat("#.##GB").format(gbNumber); |
| } |
| double tbNumber = gbNumber / FORMAT; |
| return new DecimalFormat("#.##TB").format(tbNumber); |
| } |
| } |