feat: add system summary information api
| | |
| | | |
| | | util.ResponseFormat(c, code.UpdateSuccess, "配置成功") |
| | | } |
| | | |
| | | // @Summary 查询系统运行信息 |
| | | // @Description 获取当前系统的运行状态,CPU, GPU, 内存等 |
| | | // @Produce json |
| | | // @Tags sysset |
| | | // @Success 200 {string} json "{"code":200, msg:"目录结构数据", success:true}" |
| | | // @Failure 500 {string} json "{"code":500, msg:"返回错误信息", success:false}" |
| | | // @Router /data/api-v/sysset/sysinfo [GET] |
| | | func (sset SysSetController) GetSysInfo(c *gin.Context) { |
| | | info := sys.GetSysInfo() |
| | | util.ResponseFormat(c, code.UpdateSuccess, info) |
| | | } |
New file |
| | |
| | | package sys |
| | | |
| | | import ( |
| | | "sort" |
| | | "time" |
| | | "webserver/extend/util" |
| | | |
| | | "basic.com/valib/gogpu.git" |
| | | |
| | | "github.com/shirou/gopsutil/cpu" |
| | | "github.com/shirou/gopsutil/disk" |
| | | "github.com/shirou/gopsutil/mem" |
| | | ) |
| | | |
| | | type SummaryInfo struct { |
| | | Cpu []float64 `json:"cpu" example:"cpu使用率"` |
| | | Gpu []gogpu.GpuUnitInfo `json:"gpu" example:"gpu使用率"` |
| | | Mem mem.VirtualMemoryStat `json:"mem" example:"内存使用率"` |
| | | Disk disk.UsageStat `json:"disk" example:"磁盘空间"` |
| | | } |
| | | |
| | | const Interval time.Duration = time.Second * 60 |
| | | |
| | | var Stat = &SummaryInfo{} |
| | | |
| | | func GatherStat() { |
| | | for range time.Tick(Interval) { |
| | | // Cpu |
| | | if cpuUsedPercent, err := cpu.Percent(time.Second, true); err == nil { |
| | | Stat.Cpu = Stat.Cpu[0:0] |
| | | for _, v := range cpuUsedPercent { |
| | | Stat.Cpu = append(Stat.Cpu, v) |
| | | } |
| | | } |
| | | |
| | | // Memory |
| | | if meminfo, err := mem.VirtualMemory(); err == nil { |
| | | Stat.Mem = *meminfo |
| | | } |
| | | |
| | | // Disk |
| | | if disk, err := disk.Usage("/"); err == nil { |
| | | Stat.Disk = *disk |
| | | } |
| | | |
| | | // Gpu |
| | | if gpuInfo, err := gogpu.Info(); err == nil { |
| | | sort.Sort(gogpu.GPURank(gpuInfo.Info)) |
| | | Stat.Gpu = Stat.Gpu[0:0] |
| | | for _, v := range gpuInfo.Info { |
| | | Stat.Gpu = append(Stat.Gpu, v) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | func GetSysInfo() map[string]interface{} { |
| | | return util.Struct2Map(Stat) |
| | | } |
| | |
| | | package main |
| | | |
| | | import ( |
| | | "basic.com/dbapi.git" |
| | | "basic.com/valib/logger.git" |
| | | "flag" |
| | | "github.com/spf13/viper" |
| | | "strconv" |
| | | "webserver/cache" |
| | | "webserver/extend/config" |
| | | "webserver/extend/sys" |
| | | "webserver/router" |
| | | |
| | | "basic.com/dbapi.git" |
| | | "basic.com/valib/logger.git" |
| | | "github.com/spf13/viper" |
| | | ) |
| | | |
| | | var envirment = flag.String("e", "dev", "") |
| | |
| | | go cache.Init(initchan, *dbIp, *surveyPort,*pubPort) |
| | | logger.Debug("heartBeat with db done!",<-initchan) |
| | | |
| | | // 统计系统运行状态 |
| | | go sys.GatherStat() |
| | | |
| | | r := router.NewRouter() |
| | | r.Run("0.0.0.0:8000") |
| | | } |