From 86c2bcf97b683c181bf1b503ccb74dae973ff765 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Thu, 7 Dec 2023 18:37:22 +0800 Subject: [PATCH] Add CPU info, memory info, jvm info api --- .../controller/system/StatisticsController.kt | 15 ++- .../api/service/system/IStatisticsService.kt | 9 +- .../system/impl/StatisticsServiceImpl.kt | 91 +++++++++++++++++-- .../top/fatweb/api/vo/system/CpuInfoVo.kt | 16 ++++ .../top/fatweb/api/vo/system/JvmInfoVo.kt | 6 ++ .../top/fatweb/api/vo/system/MemoryInfoVo.kt | 10 ++ 6 files changed, 137 insertions(+), 10 deletions(-) create mode 100644 src/main/kotlin/top/fatweb/api/vo/system/CpuInfoVo.kt create mode 100644 src/main/kotlin/top/fatweb/api/vo/system/JvmInfoVo.kt create mode 100644 src/main/kotlin/top/fatweb/api/vo/system/MemoryInfoVo.kt diff --git a/src/main/kotlin/top/fatweb/api/controller/system/StatisticsController.kt b/src/main/kotlin/top/fatweb/api/controller/system/StatisticsController.kt index 045ebda..765d231 100644 --- a/src/main/kotlin/top/fatweb/api/controller/system/StatisticsController.kt +++ b/src/main/kotlin/top/fatweb/api/controller/system/StatisticsController.kt @@ -7,8 +7,7 @@ import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController import top.fatweb.api.entity.common.ResponseResult import top.fatweb.api.service.system.IStatisticsService -import top.fatweb.api.vo.system.HardwareInfoVo -import top.fatweb.api.vo.system.SoftwareInfoVo +import top.fatweb.api.vo.system.* @Tag(name = "统计接口", description = "系统信息统计相关接口") @RequestMapping("/system/statistics") @@ -23,4 +22,16 @@ class StatisticsController( @Operation(summary = "获取硬件信息") @GetMapping("/hardware") fun hardware(): ResponseResult = ResponseResult.success(data = statisticsService.hardware()) + + @Operation(summary = "获取 CPU 信息") + @GetMapping("/cpu") + fun cpu(): ResponseResult = ResponseResult.success(data = statisticsService.cpu()) + + @Operation(summary = "获取内存信息") + @GetMapping("/memory") + fun memory(): ResponseResult = ResponseResult.success(data = statisticsService.memory()) + + @Operation(summary = "获取 jvm 信息") + @GetMapping("/jvm") + fun jvm(): ResponseResult = ResponseResult.success(data = statisticsService.jvm()) } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/system/IStatisticsService.kt b/src/main/kotlin/top/fatweb/api/service/system/IStatisticsService.kt index c6134dd..1e267fb 100644 --- a/src/main/kotlin/top/fatweb/api/service/system/IStatisticsService.kt +++ b/src/main/kotlin/top/fatweb/api/service/system/IStatisticsService.kt @@ -1,10 +1,15 @@ package top.fatweb.api.service.system -import top.fatweb.api.vo.system.HardwareInfoVo -import top.fatweb.api.vo.system.SoftwareInfoVo +import top.fatweb.api.vo.system.* interface IStatisticsService { fun software(): SoftwareInfoVo fun hardware(): HardwareInfoVo + + fun cpu(): CpuInfoVo + + fun memory(): MemoryInfoVo + + fun jvm(): JvmInfoVo } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/system/impl/StatisticsServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/system/impl/StatisticsServiceImpl.kt index 693129e..be363fd 100644 --- a/src/main/kotlin/top/fatweb/api/service/system/impl/StatisticsServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/system/impl/StatisticsServiceImpl.kt @@ -2,21 +2,23 @@ package top.fatweb.api.service.system.impl import org.springframework.stereotype.Service import oshi.SystemInfo +import oshi.hardware.CentralProcessor import top.fatweb.api.properties.ServerProperties import top.fatweb.api.service.system.IStatisticsService import top.fatweb.api.util.ByteUtil -import top.fatweb.api.vo.system.HardwareInfoVo -import top.fatweb.api.vo.system.SoftwareInfoVo +import top.fatweb.api.vo.system.* import java.time.LocalDateTime import java.time.ZoneOffset import java.util.Properties +import java.util.concurrent.TimeUnit @Service class StatisticsServiceImpl : IStatisticsService { private val systemProperties: Properties = System.getProperties() private val systemInfo: SystemInfo = SystemInfo() + private val runtime: Runtime = Runtime.getRuntime() - override fun software(): SoftwareInfoVo = SoftwareInfoVo( + override fun software() = SoftwareInfoVo( os = systemInfo.operatingSystem.toString(), bitness = systemInfo.operatingSystem.bitness, javaVersion = systemProperties.getProperty("java.version"), @@ -33,7 +35,7 @@ class StatisticsServiceImpl : IStatisticsService { serverStartupTime = ServerProperties.startupTime ) - override fun hardware(): HardwareInfoVo = HardwareInfoVo( + override fun hardware() = HardwareInfoVo( cpu = systemInfo.hardware.processor.processorIdentifier.name, arch = systemProperties.getProperty("os.arch"), is64Bit = systemInfo.hardware.processor.processorIdentifier.isCpu64bit, @@ -41,7 +43,84 @@ class StatisticsServiceImpl : IStatisticsService { cpuPhysicalProcessorCount = systemInfo.hardware.processor.physicalProcessorCount, cpuLogicalProcessorCount = systemInfo.hardware.processor.logicalProcessorCount, microarchitecture = systemInfo.hardware.processor.processorIdentifier.microarchitecture, - memories = "${ByteUtil.formatByteSize(systemInfo.hardware.memory.total)} (${systemInfo.hardware.memory.physicalMemory.joinToString(" + ") { ByteUtil.formatByteSize(it.capacity) }})", - disks = "${ByteUtil.formatByteSize(systemInfo.hardware.diskStores.sumOf { it.size })} (${systemInfo.hardware.diskStores.joinToString(" + ") {ByteUtil.formatByteSize(it.size)}})" + memories = "${ByteUtil.formatByteSize(systemInfo.hardware.memory.total)} (${ + systemInfo.hardware.memory.physicalMemory.joinToString( + " + " + ) { ByteUtil.formatByteSize(it.capacity) } + })", + disks = "${ByteUtil.formatByteSize(systemInfo.hardware.diskStores.sumOf { it.size })} (${ + systemInfo.hardware.diskStores.joinToString( + " + " + ) { ByteUtil.formatByteSize(it.size) } + })" + ) + + override fun cpu(): CpuInfoVo { + val processor = systemInfo.hardware.processor + val prevTicks = processor.systemCpuLoadTicks + val processorPrevTicksList = processor.processorCpuLoadTicks + TimeUnit.MILLISECONDS.sleep(500) + val ticks = processor.systemCpuLoadTicks + val processorTicksList = processor.processorCpuLoadTicks + + val user = ticks[CentralProcessor.TickType.USER.index] - prevTicks[CentralProcessor.TickType.USER.index] + val nice = ticks[CentralProcessor.TickType.NICE.index] - prevTicks[CentralProcessor.TickType.NICE.index] + val system = ticks[CentralProcessor.TickType.SYSTEM.index] - prevTicks[CentralProcessor.TickType.SYSTEM.index] + val idle = ticks[CentralProcessor.TickType.IDLE.index] - prevTicks[CentralProcessor.TickType.IDLE.index] + val iowait = ticks[CentralProcessor.TickType.IOWAIT.index] - prevTicks[CentralProcessor.TickType.IOWAIT.index] + val irq = ticks[CentralProcessor.TickType.IRQ.index] - prevTicks[CentralProcessor.TickType.IRQ.index] + val softirq = + ticks[CentralProcessor.TickType.SOFTIRQ.index] - prevTicks[CentralProcessor.TickType.SOFTIRQ.index] + val steal = ticks[CentralProcessor.TickType.STEAL.index] - prevTicks[CentralProcessor.TickType.STEAL.index] + + return CpuInfoVo(user, nice, system, idle, iowait, irq, softirq, steal, mutableListOf()).apply { + processorPrevTicksList.forEachIndexed { index, processorPrevTicks -> + run { + val processorTicks = processorTicksList[index] + val processorUser = + processorTicks[CentralProcessor.TickType.USER.index] - processorPrevTicks[CentralProcessor.TickType.USER.index] + val processorNice = + processorTicks[CentralProcessor.TickType.NICE.index] - processorPrevTicks[CentralProcessor.TickType.NICE.index] + val processorSystem = + processorTicks[CentralProcessor.TickType.SYSTEM.index] - processorPrevTicks[CentralProcessor.TickType.SYSTEM.index] + val processorIdle = + processorTicks[CentralProcessor.TickType.IDLE.index] - processorPrevTicks[CentralProcessor.TickType.IDLE.index] + val processorIowait = + processorTicks[CentralProcessor.TickType.IOWAIT.index] - processorPrevTicks[CentralProcessor.TickType.IOWAIT.index] + val processorIrq = + processorTicks[CentralProcessor.TickType.IRQ.index] - processorPrevTicks[CentralProcessor.TickType.IRQ.index] + val processorSoftirq = + processorTicks[CentralProcessor.TickType.SOFTIRQ.index] - processorPrevTicks[CentralProcessor.TickType.SOFTIRQ.index] + val processorSteal = + processorTicks[CentralProcessor.TickType.STEAL.index] - processorPrevTicks[CentralProcessor.TickType.STEAL.index] + processors?.add( + CpuInfoVo( + processorUser, + processorNice, + processorSystem, + processorIdle, + processorIowait, + processorIrq, + processorSoftirq, + processorSteal + ) + ) + } + } + } + } + + override fun memory() = MemoryInfoVo( + total = systemInfo.hardware.memory.total, + free = systemInfo.hardware.memory.available, + virtualMax = systemInfo.hardware.memory.virtualMemory.virtualMax, + virtualInUse = systemInfo.hardware.memory.virtualMemory.virtualInUse, + swapTotal = systemInfo.hardware.memory.virtualMemory.swapTotal, + swapUsed = systemInfo.hardware.memory.virtualMemory.swapUsed + ) + + override fun jvm() = JvmInfoVo( + totalMemory = runtime.totalMemory(), + freeMemory = runtime.freeMemory() ) } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/vo/system/CpuInfoVo.kt b/src/main/kotlin/top/fatweb/api/vo/system/CpuInfoVo.kt new file mode 100644 index 0000000..10c3534 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/vo/system/CpuInfoVo.kt @@ -0,0 +1,16 @@ +package top.fatweb.api.vo.system + +import com.fasterxml.jackson.annotation.JsonInclude + +@JsonInclude(JsonInclude.Include.NON_EMPTY) +data class CpuInfoVo( + val user: Long, + val nice: Long, + val system: Long, + val idle: Long, + val iowait: Long, + val irq: Long, + val softirq: Long, + val steal: Long, + val processors: MutableList? = null +) diff --git a/src/main/kotlin/top/fatweb/api/vo/system/JvmInfoVo.kt b/src/main/kotlin/top/fatweb/api/vo/system/JvmInfoVo.kt new file mode 100644 index 0000000..41f70c4 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/vo/system/JvmInfoVo.kt @@ -0,0 +1,6 @@ +package top.fatweb.api.vo.system + +data class JvmInfoVo( + val totalMemory: Long, + val freeMemory: Long, +) diff --git a/src/main/kotlin/top/fatweb/api/vo/system/MemoryInfoVo.kt b/src/main/kotlin/top/fatweb/api/vo/system/MemoryInfoVo.kt new file mode 100644 index 0000000..12cbd03 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/vo/system/MemoryInfoVo.kt @@ -0,0 +1,10 @@ +package top.fatweb.api.vo.system + +data class MemoryInfoVo( + val total: Long, + val free: Long, + val virtualInUse: Long, + val virtualMax: Long, + val swapTotal: Long, + val swapUsed: Long +)