增加获取各项监控的单独api
This commit is contained in:
parent
d3e3cf5d58
commit
baf64a9272
@ -0,0 +1,5 @@
|
||||
package systemApiStructs
|
||||
|
||||
type MonitorGetDiskStateByPathReq struct {
|
||||
Path string `json:"path"`
|
||||
}
|
@ -1,14 +1,21 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"sun-panel/api/api_v1/common/apiData/systemApiStructs"
|
||||
"sun-panel/api/api_v1/common/apiReturn"
|
||||
"sun-panel/global"
|
||||
"sun-panel/lib/monitor"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
type MonitorApi struct{}
|
||||
|
||||
const cacheSecond = 3
|
||||
|
||||
// 弃用
|
||||
func (a *MonitorApi) GetAll(c *gin.Context) {
|
||||
if value, ok := global.SystemMonitor.Get("value"); ok {
|
||||
apiReturn.SuccessData(c, value)
|
||||
@ -16,3 +23,65 @@ func (a *MonitorApi) GetAll(c *gin.Context) {
|
||||
}
|
||||
apiReturn.Error(c, "failed")
|
||||
}
|
||||
|
||||
func (a *MonitorApi) GetCpuState(c *gin.Context) {
|
||||
if v, ok := global.SystemMonitor.Get(global.SystemMonitor_CPU_INFO); ok {
|
||||
global.Logger.Debugln("读取缓存的的CPU信息")
|
||||
apiReturn.SuccessData(c, v)
|
||||
return
|
||||
}
|
||||
cpuInfo, err := monitor.GetCPUInfo()
|
||||
|
||||
if err != nil {
|
||||
apiReturn.Error(c, "failed")
|
||||
return
|
||||
}
|
||||
// 缓存
|
||||
global.SystemMonitor.Set(global.SystemMonitor_CPU_INFO, cpuInfo, cacheSecond*time.Second)
|
||||
apiReturn.SuccessData(c, cpuInfo)
|
||||
}
|
||||
|
||||
func (a *MonitorApi) GetMemonyState(c *gin.Context) {
|
||||
if v, ok := global.SystemMonitor.Get(global.SystemMonitor_MEMORY_INFO); ok {
|
||||
global.Logger.Debugln("读取缓存的的RAM信息")
|
||||
apiReturn.SuccessData(c, v)
|
||||
return
|
||||
}
|
||||
memoryInfo, err := monitor.GetMemoryInfo()
|
||||
|
||||
if err != nil {
|
||||
apiReturn.Error(c, "failed")
|
||||
return
|
||||
}
|
||||
|
||||
// 缓存
|
||||
global.SystemMonitor.Set(global.SystemMonitor_MEMORY_INFO, memoryInfo, cacheSecond*time.Second)
|
||||
apiReturn.SuccessData(c, memoryInfo)
|
||||
}
|
||||
|
||||
func (a *MonitorApi) GetDiskStateByPath(c *gin.Context) {
|
||||
|
||||
req := systemApiStructs.MonitorGetDiskStateByPathReq{}
|
||||
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||||
apiReturn.ErrorParamFomat(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
cacheDiskName := global.SystemMonitor_DISK_INFO + req.Path
|
||||
|
||||
if v, ok := global.SystemMonitor.Get(cacheDiskName); ok {
|
||||
global.Logger.Debugln("读取缓存的的RAM信息")
|
||||
apiReturn.SuccessData(c, v)
|
||||
return
|
||||
}
|
||||
|
||||
diskState, err := monitor.GetDiskInfoByPath(req.Path)
|
||||
if err != nil {
|
||||
apiReturn.Error(c, "failed")
|
||||
return
|
||||
}
|
||||
|
||||
// 缓存
|
||||
global.SystemMonitor.Set(cacheDiskName, diskState, cacheSecond*time.Second)
|
||||
apiReturn.SuccessData(c, diskState)
|
||||
}
|
||||
|
@ -35,6 +35,6 @@ var (
|
||||
Db *gorm.DB
|
||||
RedisDb *redis.Client
|
||||
SystemSetting *systemSetting.SystemSettingCache
|
||||
SystemMonitor cache.Cacher[ModelSystemMonitor]
|
||||
SystemMonitor cache.Cacher[interface{}]
|
||||
RateLimit *RateLimiter
|
||||
)
|
||||
|
@ -1,6 +1,14 @@
|
||||
package global
|
||||
|
||||
import "sun-panel/lib/monitor"
|
||||
import (
|
||||
"sun-panel/lib/monitor"
|
||||
)
|
||||
|
||||
const (
|
||||
SystemMonitor_CPU_INFO = "CPU_INFO"
|
||||
SystemMonitor_MEMORY_INFO = "MEMORY_INFO"
|
||||
SystemMonitor_DISK_INFO = "DISK_INFO"
|
||||
)
|
||||
|
||||
type ModelSystemMonitor struct {
|
||||
CPUInfo monitor.CPUInfo `json:"cpuInfo"`
|
||||
|
@ -12,7 +12,6 @@ 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"
|
||||
@ -90,8 +89,7 @@ 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)
|
||||
global.SystemMonitor = global.NewCache[interface{}](5*time.Hour, -1, "systemMonitorCache")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -97,6 +97,20 @@ func GetDiskInfo() ([]DiskInfo, error) {
|
||||
return disks, nil
|
||||
}
|
||||
|
||||
func GetDiskInfoByPath(path string) (*DiskInfo, error) {
|
||||
diskInfo := DiskInfo{}
|
||||
usage, err := disk.Usage(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
diskInfo.Free = usage.Free
|
||||
diskInfo.Mountpoint = usage.Path
|
||||
diskInfo.Total = usage.Total
|
||||
diskInfo.Used = usage.Used
|
||||
diskInfo.UsedPercent = usage.UsedPercent
|
||||
return &diskInfo, nil
|
||||
}
|
||||
|
||||
// 获取网络统计信息
|
||||
func GetNetIOCountersInfo() ([]NetIOCountersInfo, error) {
|
||||
netInfo := []NetIOCountersInfo{}
|
||||
|
@ -15,5 +15,8 @@ func InitMonitorRouter(router *gin.RouterGroup) {
|
||||
rPublic := router.Group("", middleware.PublicModeInterceptor)
|
||||
{
|
||||
rPublic.POST("/system/monitor/getAll", api.GetAll)
|
||||
rPublic.POST("/system/monitor/getCpuState", api.GetCpuState)
|
||||
rPublic.POST("/system/monitor/getDiskStateByPath", api.GetDiskStateByPath)
|
||||
rPublic.POST("/system/monitor/getMemonyState", api.GetMemonyState)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user