mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-03-30 00:46:42 +00:00
feat(performance): implement system performance monitoring with configurable thresholds
This commit is contained in:
33
common/performance_config.go
Normal file
33
common/performance_config.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package common
|
||||
|
||||
import "sync/atomic"
|
||||
|
||||
// PerformanceMonitorConfig 性能监控配置
|
||||
type PerformanceMonitorConfig struct {
|
||||
Enabled bool
|
||||
CPUThreshold int
|
||||
MemoryThreshold int
|
||||
DiskThreshold int
|
||||
}
|
||||
|
||||
var performanceMonitorConfig atomic.Value
|
||||
|
||||
func init() {
|
||||
// 初始化默认配置
|
||||
performanceMonitorConfig.Store(PerformanceMonitorConfig{
|
||||
Enabled: true,
|
||||
CPUThreshold: 90,
|
||||
MemoryThreshold: 90,
|
||||
DiskThreshold: 90,
|
||||
})
|
||||
}
|
||||
|
||||
// GetPerformanceMonitorConfig 获取性能监控配置
|
||||
func GetPerformanceMonitorConfig() PerformanceMonitorConfig {
|
||||
return performanceMonitorConfig.Load().(PerformanceMonitorConfig)
|
||||
}
|
||||
|
||||
// SetPerformanceMonitorConfig 设置性能监控配置
|
||||
func SetPerformanceMonitorConfig(config PerformanceMonitorConfig) {
|
||||
performanceMonitorConfig.Store(config)
|
||||
}
|
||||
81
common/system_monitor.go
Normal file
81
common/system_monitor.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
)
|
||||
|
||||
// DiskSpaceInfo 磁盘空间信息
|
||||
type DiskSpaceInfo struct {
|
||||
// 总空间(字节)
|
||||
Total uint64 `json:"total"`
|
||||
// 可用空间(字节)
|
||||
Free uint64 `json:"free"`
|
||||
// 已用空间(字节)
|
||||
Used uint64 `json:"used"`
|
||||
// 使用百分比
|
||||
UsedPercent float64 `json:"used_percent"`
|
||||
}
|
||||
|
||||
// SystemStatus 系统状态信息
|
||||
type SystemStatus struct {
|
||||
CPUUsage float64
|
||||
MemoryUsage float64
|
||||
DiskUsage float64
|
||||
}
|
||||
|
||||
var latestSystemStatus atomic.Value
|
||||
|
||||
func init() {
|
||||
latestSystemStatus.Store(SystemStatus{})
|
||||
}
|
||||
|
||||
// StartSystemMonitor 启动系统监控
|
||||
func StartSystemMonitor() {
|
||||
go func() {
|
||||
for {
|
||||
config := GetPerformanceMonitorConfig()
|
||||
if !config.Enabled {
|
||||
time.Sleep(30 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
updateSystemStatus()
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func updateSystemStatus() {
|
||||
var status SystemStatus
|
||||
|
||||
// CPU
|
||||
// 注意:cpu.Percent(0, false) 返回自上次调用以来的 CPU 使用率
|
||||
// 如果是第一次调用,可能会返回错误或不准确的值,但在循环中会逐渐正常
|
||||
percents, err := cpu.Percent(0, false)
|
||||
if err == nil && len(percents) > 0 {
|
||||
status.CPUUsage = percents[0]
|
||||
}
|
||||
|
||||
// Memory
|
||||
memInfo, err := mem.VirtualMemory()
|
||||
if err == nil {
|
||||
status.MemoryUsage = memInfo.UsedPercent
|
||||
}
|
||||
|
||||
// Disk
|
||||
diskInfo := GetDiskSpaceInfo()
|
||||
if diskInfo.Total > 0 {
|
||||
status.DiskUsage = diskInfo.UsedPercent
|
||||
}
|
||||
|
||||
latestSystemStatus.Store(status)
|
||||
}
|
||||
|
||||
// GetSystemStatus 获取当前系统状态
|
||||
func GetSystemStatus() SystemStatus {
|
||||
return latestSystemStatus.Load().(SystemStatus)
|
||||
}
|
||||
37
common/system_monitor_unix.go
Normal file
37
common/system_monitor_unix.go
Normal file
@@ -0,0 +1,37 @@
|
||||
//go:build !windows
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// GetDiskSpaceInfo 获取缓存目录所在磁盘的空间信息 (Unix/Linux/macOS)
|
||||
func GetDiskSpaceInfo() DiskSpaceInfo {
|
||||
cachePath := GetDiskCachePath()
|
||||
if cachePath == "" {
|
||||
cachePath = os.TempDir()
|
||||
}
|
||||
|
||||
info := DiskSpaceInfo{}
|
||||
|
||||
var stat unix.Statfs_t
|
||||
err := unix.Statfs(cachePath, &stat)
|
||||
if err != nil {
|
||||
return info
|
||||
}
|
||||
|
||||
// 计算磁盘空间 (显式转换以兼容 FreeBSD,其字段类型为 int64)
|
||||
bsize := uint64(stat.Bsize)
|
||||
info.Total = uint64(stat.Blocks) * bsize
|
||||
info.Free = uint64(stat.Bavail) * bsize
|
||||
info.Used = info.Total - uint64(stat.Bfree)*bsize
|
||||
|
||||
if info.Total > 0 {
|
||||
info.UsedPercent = float64(info.Used) / float64(info.Total) * 100
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
50
common/system_monitor_windows.go
Normal file
50
common/system_monitor_windows.go
Normal file
@@ -0,0 +1,50 @@
|
||||
//go:build windows
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// GetDiskSpaceInfo 获取缓存目录所在磁盘的空间信息 (Windows)
|
||||
func GetDiskSpaceInfo() DiskSpaceInfo {
|
||||
cachePath := GetDiskCachePath()
|
||||
if cachePath == "" {
|
||||
cachePath = os.TempDir()
|
||||
}
|
||||
|
||||
info := DiskSpaceInfo{}
|
||||
|
||||
kernel32 := syscall.NewLazyDLL("kernel32.dll")
|
||||
getDiskFreeSpaceEx := kernel32.NewProc("GetDiskFreeSpaceExW")
|
||||
|
||||
var freeBytesAvailable, totalBytes, totalFreeBytes uint64
|
||||
|
||||
pathPtr, err := syscall.UTF16PtrFromString(cachePath)
|
||||
if err != nil {
|
||||
return info
|
||||
}
|
||||
|
||||
ret, _, _ := getDiskFreeSpaceEx.Call(
|
||||
uintptr(unsafe.Pointer(pathPtr)),
|
||||
uintptr(unsafe.Pointer(&freeBytesAvailable)),
|
||||
uintptr(unsafe.Pointer(&totalBytes)),
|
||||
uintptr(unsafe.Pointer(&totalFreeBytes)),
|
||||
)
|
||||
|
||||
if ret == 0 {
|
||||
return info
|
||||
}
|
||||
|
||||
info.Total = totalBytes
|
||||
info.Free = freeBytesAvailable
|
||||
info.Used = totalBytes - totalFreeBytes
|
||||
|
||||
if info.Total > 0 {
|
||||
info.UsedPercent = float64(info.Used) / float64(info.Total) * 100
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
Reference in New Issue
Block a user