From c447884d77349553864e0e045b83b3a4b67345da Mon Sep 17 00:00:00 2001 From: Sun <95302870@qq.com> Date: Tue, 2 Jan 2024 22:11:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9F=BA=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E7=9B=91=E6=8E=A7=E7=B1=BB=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/global/global.go | 1 + service/global/monitoer.go | 12 ++ service/initialize/A_ENTER.go | 4 + .../initialize/systemMonitor/systemMonitor.go | 50 +++++++ service/lib/cache/base.go | 5 + service/lib/monitor/monitor.go | 126 ++++++++++++++++++ 6 files changed, 198 insertions(+) create mode 100644 service/global/monitoer.go create mode 100644 service/initialize/systemMonitor/systemMonitor.go create mode 100644 service/lib/monitor/monitor.go diff --git a/service/global/global.go b/service/global/global.go index 0e133c6..20d6910 100644 --- a/service/global/global.go +++ b/service/global/global.go @@ -35,5 +35,6 @@ var ( Db *gorm.DB RedisDb *redis.Client SystemSetting *systemSetting.SystemSettingCache + SystemMonitor cache.Cacher[ModelSystemMonitor] RateLimit *RateLimiter ) diff --git a/service/global/monitoer.go b/service/global/monitoer.go new file mode 100644 index 0000000..d35e431 --- /dev/null +++ b/service/global/monitoer.go @@ -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 + +} diff --git a/service/initialize/A_ENTER.go b/service/initialize/A_ENTER.go index 449561f..010b732 100644 --- a/service/initialize/A_ENTER.go +++ b/service/initialize/A_ENTER.go @@ -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 } diff --git a/service/initialize/systemMonitor/systemMonitor.go b/service/initialize/systemMonitor/systemMonitor.go new file mode 100644 index 0000000..43843fe --- /dev/null +++ b/service/initialize/systemMonitor/systemMonitor.go @@ -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 +} diff --git a/service/lib/cache/base.go b/service/lib/cache/base.go index b522d70..99dbf1e 100644 --- a/service/lib/cache/base.go +++ b/service/lib/cache/base.go @@ -4,6 +4,11 @@ import ( "time" ) +const ( + CACHE_DRIVE_REDIS = "redis" + CACHE_DRIVE_MEMORY = "memory" +) + // 缓存接口-支持Redis和内存使用 type Cacher[T any] interface { // 设置 diff --git a/service/lib/monitor/monitor.go b/service/lib/monitor/monitor.go new file mode 100644 index 0000000..b7dff6f --- /dev/null +++ b/service/lib/monitor/monitor.go @@ -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) +// }