From 862415f6ba294ad678e3dd0e66695d7594f856ae Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@iotlink.com>
Date: 星期二, 22 十月 2019 16:52:12 +0800
Subject: [PATCH] feat: add system summary information api
---
controllers/syssetcont.go | 12 ++++++
extend/sys/sysinfo.go | 59 +++++++++++++++++++++++++++++
main.go | 17 +++++---
3 files changed, 82 insertions(+), 6 deletions(-)
diff --git a/controllers/syssetcont.go b/controllers/syssetcont.go
index 6cd0e0a..127056e 100644
--- a/controllers/syssetcont.go
+++ b/controllers/syssetcont.go
@@ -334,3 +334,15 @@
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)
+}
diff --git a/extend/sys/sysinfo.go b/extend/sys/sysinfo.go
new file mode 100644
index 0000000..208be3e
--- /dev/null
+++ b/extend/sys/sysinfo.go
@@ -0,0 +1,59 @@
+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)
+}
diff --git a/main.go b/main.go
index 21f6665..425e46d 100644
--- a/main.go
+++ b/main.go
@@ -1,14 +1,16 @@
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", "")
@@ -40,7 +42,7 @@
viper.GetInt("LogLevel") >= logger.PanicLevel &&
viper.GetInt("LogLevel") <= logger.DebugLevel {
logger.Config(logFile, viper.GetInt("LogLevel"))
- }else{
+ } else {
logger.Config(logFile, logger.DebugLevel)
}
logger.SetSaveDays(logSaveDays)
@@ -56,8 +58,11 @@
// @BasePath /
func main() {
flag.Parse()
- go cache.Init(initchan, *dbIp, *surveyPort,*pubPort)
- logger.Debug("heartBeat with db done!",<-initchan)
+ 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")
--
Gitblit v1.8.0