Add CPU info, memory info, jvm info api
This commit is contained in:
@@ -7,8 +7,7 @@ import org.springframework.web.bind.annotation.RequestMapping
|
|||||||
import org.springframework.web.bind.annotation.RestController
|
import org.springframework.web.bind.annotation.RestController
|
||||||
import top.fatweb.api.entity.common.ResponseResult
|
import top.fatweb.api.entity.common.ResponseResult
|
||||||
import top.fatweb.api.service.system.IStatisticsService
|
import top.fatweb.api.service.system.IStatisticsService
|
||||||
import top.fatweb.api.vo.system.HardwareInfoVo
|
import top.fatweb.api.vo.system.*
|
||||||
import top.fatweb.api.vo.system.SoftwareInfoVo
|
|
||||||
|
|
||||||
@Tag(name = "统计接口", description = "系统信息统计相关接口")
|
@Tag(name = "统计接口", description = "系统信息统计相关接口")
|
||||||
@RequestMapping("/system/statistics")
|
@RequestMapping("/system/statistics")
|
||||||
@@ -23,4 +22,16 @@ class StatisticsController(
|
|||||||
@Operation(summary = "获取硬件信息")
|
@Operation(summary = "获取硬件信息")
|
||||||
@GetMapping("/hardware")
|
@GetMapping("/hardware")
|
||||||
fun hardware(): ResponseResult<HardwareInfoVo> = ResponseResult.success(data = statisticsService.hardware())
|
fun hardware(): ResponseResult<HardwareInfoVo> = ResponseResult.success(data = statisticsService.hardware())
|
||||||
|
|
||||||
|
@Operation(summary = "获取 CPU 信息")
|
||||||
|
@GetMapping("/cpu")
|
||||||
|
fun cpu(): ResponseResult<CpuInfoVo> = ResponseResult.success(data = statisticsService.cpu())
|
||||||
|
|
||||||
|
@Operation(summary = "获取内存信息")
|
||||||
|
@GetMapping("/memory")
|
||||||
|
fun memory(): ResponseResult<MemoryInfoVo> = ResponseResult.success(data = statisticsService.memory())
|
||||||
|
|
||||||
|
@Operation(summary = "获取 jvm 信息")
|
||||||
|
@GetMapping("/jvm")
|
||||||
|
fun jvm(): ResponseResult<JvmInfoVo> = ResponseResult.success(data = statisticsService.jvm())
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,15 @@
|
|||||||
package top.fatweb.api.service.system
|
package top.fatweb.api.service.system
|
||||||
|
|
||||||
import top.fatweb.api.vo.system.HardwareInfoVo
|
import top.fatweb.api.vo.system.*
|
||||||
import top.fatweb.api.vo.system.SoftwareInfoVo
|
|
||||||
|
|
||||||
interface IStatisticsService {
|
interface IStatisticsService {
|
||||||
fun software(): SoftwareInfoVo
|
fun software(): SoftwareInfoVo
|
||||||
|
|
||||||
fun hardware(): HardwareInfoVo
|
fun hardware(): HardwareInfoVo
|
||||||
|
|
||||||
|
fun cpu(): CpuInfoVo
|
||||||
|
|
||||||
|
fun memory(): MemoryInfoVo
|
||||||
|
|
||||||
|
fun jvm(): JvmInfoVo
|
||||||
}
|
}
|
||||||
@@ -2,21 +2,23 @@ package top.fatweb.api.service.system.impl
|
|||||||
|
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import oshi.SystemInfo
|
import oshi.SystemInfo
|
||||||
|
import oshi.hardware.CentralProcessor
|
||||||
import top.fatweb.api.properties.ServerProperties
|
import top.fatweb.api.properties.ServerProperties
|
||||||
import top.fatweb.api.service.system.IStatisticsService
|
import top.fatweb.api.service.system.IStatisticsService
|
||||||
import top.fatweb.api.util.ByteUtil
|
import top.fatweb.api.util.ByteUtil
|
||||||
import top.fatweb.api.vo.system.HardwareInfoVo
|
import top.fatweb.api.vo.system.*
|
||||||
import top.fatweb.api.vo.system.SoftwareInfoVo
|
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.time.ZoneOffset
|
import java.time.ZoneOffset
|
||||||
import java.util.Properties
|
import java.util.Properties
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class StatisticsServiceImpl : IStatisticsService {
|
class StatisticsServiceImpl : IStatisticsService {
|
||||||
private val systemProperties: Properties = System.getProperties()
|
private val systemProperties: Properties = System.getProperties()
|
||||||
private val systemInfo: SystemInfo = SystemInfo()
|
private val systemInfo: SystemInfo = SystemInfo()
|
||||||
|
private val runtime: Runtime = Runtime.getRuntime()
|
||||||
|
|
||||||
override fun software(): SoftwareInfoVo = SoftwareInfoVo(
|
override fun software() = SoftwareInfoVo(
|
||||||
os = systemInfo.operatingSystem.toString(),
|
os = systemInfo.operatingSystem.toString(),
|
||||||
bitness = systemInfo.operatingSystem.bitness,
|
bitness = systemInfo.operatingSystem.bitness,
|
||||||
javaVersion = systemProperties.getProperty("java.version"),
|
javaVersion = systemProperties.getProperty("java.version"),
|
||||||
@@ -33,7 +35,7 @@ class StatisticsServiceImpl : IStatisticsService {
|
|||||||
serverStartupTime = ServerProperties.startupTime
|
serverStartupTime = ServerProperties.startupTime
|
||||||
)
|
)
|
||||||
|
|
||||||
override fun hardware(): HardwareInfoVo = HardwareInfoVo(
|
override fun hardware() = HardwareInfoVo(
|
||||||
cpu = systemInfo.hardware.processor.processorIdentifier.name,
|
cpu = systemInfo.hardware.processor.processorIdentifier.name,
|
||||||
arch = systemProperties.getProperty("os.arch"),
|
arch = systemProperties.getProperty("os.arch"),
|
||||||
is64Bit = systemInfo.hardware.processor.processorIdentifier.isCpu64bit,
|
is64Bit = systemInfo.hardware.processor.processorIdentifier.isCpu64bit,
|
||||||
@@ -41,7 +43,84 @@ class StatisticsServiceImpl : IStatisticsService {
|
|||||||
cpuPhysicalProcessorCount = systemInfo.hardware.processor.physicalProcessorCount,
|
cpuPhysicalProcessorCount = systemInfo.hardware.processor.physicalProcessorCount,
|
||||||
cpuLogicalProcessorCount = systemInfo.hardware.processor.logicalProcessorCount,
|
cpuLogicalProcessorCount = systemInfo.hardware.processor.logicalProcessorCount,
|
||||||
microarchitecture = systemInfo.hardware.processor.processorIdentifier.microarchitecture,
|
microarchitecture = systemInfo.hardware.processor.processorIdentifier.microarchitecture,
|
||||||
memories = "${ByteUtil.formatByteSize(systemInfo.hardware.memory.total)} (${systemInfo.hardware.memory.physicalMemory.joinToString(" + ") { ByteUtil.formatByteSize(it.capacity) }})",
|
memories = "${ByteUtil.formatByteSize(systemInfo.hardware.memory.total)} (${
|
||||||
disks = "${ByteUtil.formatByteSize(systemInfo.hardware.diskStores.sumOf { it.size })} (${systemInfo.hardware.diskStores.joinToString(" + ") {ByteUtil.formatByteSize(it.size)}})"
|
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()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
16
src/main/kotlin/top/fatweb/api/vo/system/CpuInfoVo.kt
Normal file
16
src/main/kotlin/top/fatweb/api/vo/system/CpuInfoVo.kt
Normal file
@@ -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<CpuInfoVo>? = null
|
||||||
|
)
|
||||||
6
src/main/kotlin/top/fatweb/api/vo/system/JvmInfoVo.kt
Normal file
6
src/main/kotlin/top/fatweb/api/vo/system/JvmInfoVo.kt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package top.fatweb.api.vo.system
|
||||||
|
|
||||||
|
data class JvmInfoVo(
|
||||||
|
val totalMemory: Long,
|
||||||
|
val freeMemory: Long,
|
||||||
|
)
|
||||||
10
src/main/kotlin/top/fatweb/api/vo/system/MemoryInfoVo.kt
Normal file
10
src/main/kotlin/top/fatweb/api/vo/system/MemoryInfoVo.kt
Normal file
@@ -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
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user