完成基本的系统监控类库

This commit is contained in:
Sun 2024-01-02 22:11:34 +08:00
parent 2e0af5f147
commit c447884d77
6 changed files with 198 additions and 0 deletions

View File

@ -35,5 +35,6 @@ var (
Db *gorm.DB
RedisDb *redis.Client
SystemSetting *systemSetting.SystemSettingCache
SystemMonitor cache.Cacher[ModelSystemMonitor]
RateLimit *RateLimiter
)

View File

@ -0,0 +1,12 @@
package global
import "sun-panel/lib/monitor"
type ModelSystemMonitor struct {
CPUInfo monitor.CPUInfo
DiskInfo []monitor.DiskInfo
NetIOCountersInfo []monitor.NetIOCountersInfo
MemoryInfo monitor.MemoryInfo
// NetIOCountersInfo monitor.NetIOCountersInfo
}

View File

@ -12,11 +12,13 @@ import (
"sun-panel/initialize/other"
"sun-panel/initialize/redis"
"sun-panel/initialize/runlog"
"sun-panel/initialize/systemMonitor"
"sun-panel/initialize/systemSettingCache"
"sun-panel/initialize/userToken"
"sun-panel/lib/cmn"
"sun-panel/models"
"sun-panel/structs"
"time"
"log"
@ -88,6 +90,8 @@ func InitApp() error {
// 其他的初始化
global.VerifyCodeCachePool = other.InitVerifyCodeCachePool()
global.SystemSetting = systemSettingCache.InItSystemSettingCache()
global.SystemMonitor = global.NewCache[global.ModelSystemMonitor](5*time.Hour, -1, "systemMonitorCache")
systemMonitor.Start(global.SystemMonitor, 3*time.Second)
return nil
}

View File

@ -0,0 +1,50 @@
package systemMonitor
import (
"sun-panel/global"
"sun-panel/lib/cache"
"sun-panel/lib/monitor"
"time"
)
func Start(cacher cache.Cacher[global.ModelSystemMonitor], interval time.Duration) {
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
go func() {
GetInfo()
}()
}
}
}()
}
func GetInfo() global.ModelSystemMonitor {
var modelSystemMonitor global.ModelSystemMonitor
if cpuInfo, err := monitor.GetCPUInfo(); err == nil {
modelSystemMonitor.CPUInfo = cpuInfo
}
if v, err := monitor.GetDiskInfo(); err == nil {
modelSystemMonitor.DiskInfo = v
}
if v, err := monitor.GetNetIOCountersInfo(); err == nil {
modelSystemMonitor.NetIOCountersInfo = v
}
if v, err := monitor.GetMemoryInfo(); err == nil {
modelSystemMonitor.MemoryInfo = v
}
return modelSystemMonitor
}

View File

@ -4,6 +4,11 @@ import (
"time"
)
const (
CACHE_DRIVE_REDIS = "redis"
CACHE_DRIVE_MEMORY = "memory"
)
// 缓存接口-支持Redis和内存使用
type Cacher[T any] interface {
// 设置

View File

@ -0,0 +1,126 @@
package monitor
import (
"time"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/net"
)
type CPUInfo struct {
CoreCount int32
CPUNum int
Model string
Usages []float64
}
type DiskInfo struct {
Mountpoint string
Total uint64
Used uint64
Free uint64
UsedPercent float64
}
type NetIOCountersInfo struct {
BytesSent uint64
BytesRecv uint64
Name string
}
type MemoryInfo struct {
Total uint64
Free uint64
}
// 获取CPU信息
func GetCPUInfo() (CPUInfo, error) {
cpuInfoRes := CPUInfo{}
cpuInfo, err := cpu.Info()
if err == nil && len(cpuInfo) > 0 {
cpuInfoRes.CoreCount = cpuInfo[0].Cores
cpuInfoRes.Model = cpuInfo[0].ModelName
}
numCPU, _ := cpu.Counts(true)
cpuInfoRes.CPUNum = numCPU
cpuPercentages, err := cpu.Percent(time.Second, true)
cpuInfoRes.Usages = cpuPercentages
return cpuInfoRes, err
}
// 获取内存信息 单位MB
func GetMemoryInfo() (MemoryInfo, error) {
memoryInfo := MemoryInfo{}
// 获取内存信息
memInfo, err := mem.VirtualMemory()
if err == nil {
return memoryInfo, err
}
memoryInfo.Free = memInfo.Free
memoryInfo.Total = memInfo.Total
return memoryInfo, err
}
// 获取每个磁盘分区使用情况
func GetDiskInfo() ([]DiskInfo, error) {
disks := []DiskInfo{}
// 获取所有磁盘分区的信息
partitions, err := disk.Partitions(true)
if err != nil {
return disks, err
}
for _, partition := range partitions {
usage, err := disk.Usage(partition.Mountpoint)
if err != nil {
// fmt.Printf("Error getting disk usage for %s: %v\n", partition.Mountpoint, err)
continue
}
disks = append(disks, DiskInfo{
Mountpoint: partition.Mountpoint,
Total: usage.Total / 1024 / 1024,
Used: usage.Used / 1024 / 1024,
Free: usage.Free / 1024 / 1024,
UsedPercent: usage.UsedPercent,
})
}
return disks, nil
}
// 获取网络统计信息
func GetNetIOCountersInfo() ([]NetIOCountersInfo, error) {
netInfo := []NetIOCountersInfo{}
netStats, err := net.IOCounters(true)
if err == nil {
for _, netStat := range netStats {
netInfo = append(netInfo, NetIOCountersInfo{
BytesRecv: netStat.BytesRecv,
BytesSent: netStat.BytesSent,
Name: netStat.Name,
})
}
}
return netInfo, err
}
// func GetCountDiskInfo() {
// // 获取所有磁盘的总使用情况
// allUsage, err := disk.Usage("/")
// if err != nil {
// fmt.Printf("Error getting total disk usage: %v\n", err)
// return
// }
// // 打印所有磁盘的总使用情况
// fmt.Println("Total Disk Usage:")
// fmt.Printf("Total: %d MB\n", allUsage.Total/1024/1024)
// fmt.Printf("Used: %d MB\n", allUsage.Used/1024/1024)
// fmt.Printf("Free: %d MB\n", allUsage.Free/1024/1024)
// fmt.Printf("Usage: %.2f%%\n", allUsage.UsedPercent)
// }