From 4ced069428bcbd9f174f8dc48d59449404b2bcee Mon Sep 17 00:00:00 2001 From: ssongliu Date: Thu, 8 Sep 2022 11:39:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=9B=91=E6=8E=A7=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=87=87=E9=9B=86=E8=90=BD=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/monitor.go | 93 +++++++++++++++++++ backend/app/dto/monitor.go | 16 ++++ backend/app/model/monitor.go | 38 ++++++++ backend/cron/corn.go | 21 +++++ backend/cron/job/monitor.go | 105 ++++++++++++++++++++++ backend/global/global.go | 3 + backend/go.mod | 13 ++- backend/go.sum | 18 +++- backend/init/migration/migrate.go | 1 + backend/init/migration/migrations/init.go | 7 ++ backend/init/router/router.go | 1 + backend/router/entry.go | 1 + backend/router/ro_monitor.go | 19 ++++ backend/server/server.go | 2 + 14 files changed, 330 insertions(+), 8 deletions(-) create mode 100644 backend/app/api/v1/monitor.go create mode 100644 backend/app/dto/monitor.go create mode 100644 backend/app/model/monitor.go create mode 100644 backend/cron/corn.go create mode 100644 backend/cron/job/monitor.go create mode 100644 backend/router/ro_monitor.go diff --git a/backend/app/api/v1/monitor.go b/backend/app/api/v1/monitor.go new file mode 100644 index 000000000..0fdc4cf4e --- /dev/null +++ b/backend/app/api/v1/monitor.go @@ -0,0 +1,93 @@ +package v1 + +import ( + "time" + + "github.com/1Panel-dev/1Panel/app/api/v1/helper" + "github.com/1Panel-dev/1Panel/app/dto" + "github.com/1Panel-dev/1Panel/app/model" + "github.com/1Panel-dev/1Panel/constant" + "github.com/1Panel-dev/1Panel/global" + "github.com/gin-gonic/gin" + "github.com/shirou/gopsutil/net" +) + +func (b *BaseApi) LoadMonitor(c *gin.Context) { + var req dto.MonitorSearch + if err := c.ShouldBindJSON(&req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + if err := global.VALID.Struct(req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + if global.CONF.System.DbType == "sqlite" { + req.StartTime = req.StartTime.Add(8 * time.Hour) + req.EndTime = req.EndTime.Add(8 * time.Hour) + } + + var backdatas []dto.MonitorData + if req.Param == "all" || req.Param == "cpu" || req.Param == "memory" || req.Param == "load" { + var bases []model.MonitorBase + if err := global.DB. + Where("created_at > ? AND created_at < ?", req.StartTime, req.EndTime). + Find(&bases).Error; err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + + var itemData dto.MonitorData + itemData.Param = "base" + for _, base := range bases { + itemData.Date = append(itemData.Date, base.CreatedAt) + itemData.Value = append(itemData.Value, base) + } + backdatas = append(backdatas, itemData) + } + if req.Param == "all" || req.Param == "io" { + var bases []model.MonitorIO + if err := global.DB. + Where("created_at > ? AND created_at < ?", req.StartTime, req.EndTime). + Find(&bases).Error; err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + + var itemData dto.MonitorData + itemData.Param = "io" + for _, base := range bases { + itemData.Date = append(itemData.Date, base.CreatedAt) + itemData.Value = append(itemData.Value, base) + } + backdatas = append(backdatas, itemData) + } + if req.Param == "all" || req.Param == "network" { + var bases []model.MonitorNetwork + if err := global.DB. + Where("name = ? AND created_at > ? AND created_at < ?", req.Info, req.StartTime, req.EndTime). + Find(&bases).Error; err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + + var itemData dto.MonitorData + itemData.Param = "network" + for _, base := range bases { + itemData.Date = append(itemData.Date, base.CreatedAt) + itemData.Value = append(itemData.Value, base) + } + backdatas = append(backdatas, itemData) + } + helper.SuccessWithData(c, backdatas) +} + +func (b *BaseApi) GetNetworkOptions(c *gin.Context) { + netStat, _ := net.IOCounters(true) + var options []string + options = append(options, "all") + for _, net := range netStat { + options = append(options, net.Name) + } + helper.SuccessWithData(c, options) +} diff --git a/backend/app/dto/monitor.go b/backend/app/dto/monitor.go new file mode 100644 index 000000000..780b1c790 --- /dev/null +++ b/backend/app/dto/monitor.go @@ -0,0 +1,16 @@ +package dto + +import "time" + +type MonitorSearch struct { + Param string `json:"param" validate:"required,oneof=all cpu memory load io network"` + Info string `json:"info"` + StartTime time.Time `json:"startTime"` + EndTime time.Time `json:"endTime"` +} + +type MonitorData struct { + Param string `json:"param" validate:"required,oneof=cpu memory load io network"` + Date []time.Time `json:"date"` + Value []interface{} `json:"value"` +} diff --git a/backend/app/model/monitor.go b/backend/app/model/monitor.go new file mode 100644 index 000000000..84b6f3be8 --- /dev/null +++ b/backend/app/model/monitor.go @@ -0,0 +1,38 @@ +package model + +type MonitorBase struct { + BaseModel + Cpu float64 `gorm:"type:float" json:"cpu"` + + LoadUsage float64 `gorm:"type:float" json:"loadUsage"` + CpuLoad1 float64 `gorm:"type:float" json:"cpuLoad1"` + CpuLoad5 float64 `gorm:"type:float" json:"cpuLoad5"` + CpuLoad15 float64 `gorm:"type:float" json:"cpuLoad15"` + + Memory float64 `gorm:"type:float" json:"memory"` +} + +type MonitorIO struct { + BaseModel + Name string `json:"name"` + ReadCount uint64 `gorm:"type:decimal" json:"readCount"` + WriteCount uint64 `gorm:"type:decimal" json:"writeCount"` + ReadTime uint64 `gorm:"type:decimal" json:"readTime"` + WriteTime uint64 `gorm:"type:decimal" json:"writeTime"` + ReadByte uint64 `gorm:"type:decimal(32)" json:"readByte"` + WriteByte uint64 `gorm:"type:decimal(32)" json:"writeByte"` + + Read uint64 `gorm:"type:decimal" json:"read"` + Write uint64 `gorm:"type:decimal" json:"write"` + Count uint64 `gorm:"type:decimal" json:"count"` + Time uint64 `gorm:"type:decimal" json:"time"` +} + +type MonitorNetwork struct { + BaseModel + Name string `json:"name"` + BytesSent uint64 `gorm:"type:decimal(32)" json:"bytesSent"` + BytesRecv uint64 `gorm:"type:decimal(32)" json:"bytesRecv"` + Up float64 `gorm:"type:float" json:"up"` + Down float64 `gorm:"type:float" json:"down"` +} diff --git a/backend/cron/corn.go b/backend/cron/corn.go new file mode 100644 index 000000000..c75c5611b --- /dev/null +++ b/backend/cron/corn.go @@ -0,0 +1,21 @@ +package cron + +import ( + "time" + + "github.com/1Panel-dev/1Panel/cron/job" + "github.com/1Panel-dev/1Panel/global" + "github.com/robfig/cron/v3" +) + +func Run() { + nyc, _ := time.LoadLocation("Asia/Shanghai") + Cron := cron.New(cron.WithLocation(nyc)) + _, err := Cron.AddJob("@every 1m", job.NewMonitorJob()) + if err != nil { + global.LOG.Errorf("can not add corn job: %s", err.Error()) + } + Cron.Start() + + global.Corn = Cron +} diff --git a/backend/cron/job/monitor.go b/backend/cron/job/monitor.go new file mode 100644 index 000000000..008242e80 --- /dev/null +++ b/backend/cron/job/monitor.go @@ -0,0 +1,105 @@ +package job + +import ( + "time" + + "github.com/1Panel-dev/1Panel/app/model" + "github.com/1Panel-dev/1Panel/global" + "github.com/shirou/gopsutil/cpu" + "github.com/shirou/gopsutil/disk" + "github.com/shirou/gopsutil/load" + "github.com/shirou/gopsutil/mem" + "github.com/shirou/gopsutil/net" +) + +type monitor struct{} + +func NewMonitorJob() *monitor { + return &monitor{} +} + +func (m *monitor) Run() { + var itemModel model.MonitorBase + totalPercent, _ := cpu.Percent(3*time.Second, false) + if len(totalPercent) == 1 { + itemModel.Cpu = totalPercent[0] + } + cpuCount, _ := cpu.Counts(false) + + loadInfo, _ := load.Avg() + itemModel.CpuLoad1 = loadInfo.Load1 + itemModel.CpuLoad5 = loadInfo.Load5 + itemModel.CpuLoad15 = loadInfo.Load15 + itemModel.LoadUsage = loadInfo.Load1 / (float64(cpuCount*2) * 0.75) * 100 + + memoryInfo, _ := mem.VirtualMemory() + itemModel.Memory = memoryInfo.UsedPercent + + _ = global.DB.Create(&itemModel) + + ioStat, _ := disk.IOCounters() + for _, v := range ioStat { + var itemIO model.MonitorIO + itemIO.Name = v.Name + itemIO.ReadCount = v.ReadCount + itemIO.WriteCount = v.WriteCount + itemIO.ReadByte = v.ReadBytes + itemIO.WriteByte = v.WriteBytes + itemIO.ReadTime = v.ReadTime + itemIO.WriteTime = v.WriteTime + var aheadData model.MonitorIO + if err := global.DB.Where("name = ?", v.Name).Order("created_at").Find(&aheadData).Error; err != nil { + _ = global.DB.Create(&itemIO) + continue + } + stime := time.Since(aheadData.CreatedAt).Seconds() + itemIO.Read = uint64(float64(v.ReadBytes-aheadData.ReadByte) / stime) + itemIO.Write = uint64(float64(v.WriteBytes-aheadData.WriteByte) / stime) + + itemIO.Count = uint64(float64(v.ReadCount-aheadData.ReadCount) / stime) + writeCount := uint64(float64(v.WriteCount-aheadData.WriteCount) / stime) + if writeCount > itemIO.Count { + itemIO.Count = writeCount + } + + itemIO.Time = uint64(float64(v.ReadTime-aheadData.ReadTime) / stime) + writeTime := uint64(float64(v.WriteTime-aheadData.WriteTime) / stime) + if writeTime > itemIO.Time { + itemIO.Time = writeTime + } + _ = global.DB.Create(&itemIO) + } + + netStat, _ := net.IOCounters(true) + for _, v := range netStat { + var itemNet model.MonitorNetwork + var aheadData model.MonitorNetwork + itemNet.Name = v.Name + itemNet.BytesSent = v.BytesSent + itemNet.BytesRecv = v.BytesRecv + if err := global.DB.Where("name = ?", v.Name).Order("created_at").Find(&aheadData).Error; err != nil { + _ = global.DB.Create(&itemNet) + continue + } + stime := time.Since(aheadData.CreatedAt).Seconds() + itemNet.Up = float64(v.BytesSent-aheadData.BytesSent) / 1024 / stime + itemNet.Down = float64(v.BytesRecv-aheadData.BytesRecv) / 1024 / stime + _ = global.DB.Create(&itemNet) + } + netStatAll, _ := net.IOCounters(false) + if len(netStatAll) != 0 { + var itemNet model.MonitorNetwork + var aheadData model.MonitorNetwork + itemNet.Name = netStatAll[0].Name + itemNet.BytesSent = netStatAll[0].BytesSent + itemNet.BytesRecv = netStatAll[0].BytesRecv + if err := global.DB.Where("name = ?", netStatAll[0].Name).Order("created_at").Find(&aheadData).Error; err != nil { + _ = global.DB.Create(&itemNet) + return + } + stime := time.Since(aheadData.CreatedAt).Seconds() + itemNet.Up = float64(netStatAll[0].BytesSent-aheadData.BytesSent) / 1024 / stime + itemNet.Down = float64(netStatAll[0].BytesRecv-aheadData.BytesRecv) / 1024 / stime + _ = global.DB.Create(&itemNet) + } +} diff --git a/backend/global/global.go b/backend/global/global.go index 3756155c2..74f061a98 100644 --- a/backend/global/global.go +++ b/backend/global/global.go @@ -5,6 +5,7 @@ import ( "github.com/1Panel-dev/1Panel/init/cache/badger_db" "github.com/1Panel-dev/1Panel/init/session/psession" "github.com/go-playground/validator/v10" + "github.com/robfig/cron/v3" "github.com/sirupsen/logrus" "gorm.io/gorm" ) @@ -16,4 +17,6 @@ var ( VALID *validator.Validate SESSION *psession.PSession CACHE *badger_db.Cache + + Corn *cron.Cron ) diff --git a/backend/go.mod b/backend/go.mod index 564aad669..fdd9d8fab 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -6,6 +6,7 @@ require ( github.com/dgraph-io/badger/v3 v3.2103.2 github.com/fsnotify/fsnotify v1.5.4 github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6 + github.com/gabriel-vasile/mimetype v1.4.1 github.com/gin-contrib/i18n v0.0.1 github.com/gin-gonic/gin v1.8.1 github.com/go-gormigrate/gormigrate/v2 v2.0.2 @@ -16,12 +17,16 @@ require ( github.com/gwatts/gin-adapter v1.0.0 github.com/jinzhu/copier v0.3.5 github.com/kr/pty v1.1.1 + github.com/mholt/archiver/v4 v4.0.0-alpha.7 github.com/mojocn/base64Captcha v1.3.5 github.com/natefinch/lumberjack v2.0.0+incompatible github.com/nicksnyder/go-i18n/v2 v2.1.2 github.com/pkg/errors v0.9.1 + github.com/robfig/cron/v3 v3.0.1 github.com/satori/go.uuid v1.2.0 + github.com/shirou/gopsutil v3.21.11+incompatible github.com/sirupsen/logrus v1.9.0 + github.com/spf13/afero v1.8.2 github.com/spf13/viper v1.12.0 github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a github.com/swaggo/gin-swagger v1.5.1 @@ -44,8 +49,8 @@ require ( github.com/dgraph-io/ristretto v0.1.0 // indirect github.com/dsnet/compress v0.0.1 // indirect github.com/dustin/go-humanize v1.0.0 // indirect - github.com/gabriel-vasile/mimetype v1.4.1 // indirect github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect github.com/go-openapi/jsonreference v0.19.6 // indirect github.com/go-openapi/spec v0.20.4 // indirect @@ -74,7 +79,6 @@ require ( github.com/mailru/easyjson v0.7.6 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect - github.com/mholt/archiver/v4 v4.0.0-alpha.7 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect @@ -82,15 +86,16 @@ require ( github.com/pelletier/go-toml v1.9.5 // indirect github.com/pelletier/go-toml/v2 v2.0.2 // indirect github.com/pierrec/lz4/v4 v4.1.15 // indirect - github.com/satori/go.uuid v1.2.0 // indirect - github.com/spf13/afero v1.8.2 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.3.0 // indirect github.com/therootcompany/xz v1.0.1 // indirect + github.com/tklauser/go-sysconf v0.3.10 // indirect + github.com/tklauser/numcpus v0.4.0 // indirect github.com/ugorji/go/codec v1.2.7 // indirect github.com/ulikunitz/xz v0.5.10 // indirect + github.com/yusufpapurcu/wmi v1.2.2 // indirect go.opencensus.io v0.23.0 // indirect golang.org/x/image v0.0.0-20190802002840-cff245a6509b // indirect golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect diff --git a/backend/go.sum b/backend/go.sum index 554687d12..cc711f921 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -113,6 +113,8 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2 github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gormigrate/gormigrate/v2 v2.0.2 h1:YV4Lc5yMQX8ahVW0ENPq6sPhrhdkGukc6fPRYmZ1R6Y= github.com/go-gormigrate/gormigrate/v2 v2.0.2/go.mod h1:vld36QpBTfTzLealsHsmQQJK5lSwJt6wiORv+oFX8/I= +github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY= github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= @@ -178,7 +180,6 @@ github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= @@ -260,7 +261,6 @@ github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfV github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= -github.com/klauspost/compress v1.12.3 h1:G5AfA94pHPysR56qqrkO2pxEexdDzrpFJ6yt/VqWxVU= github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY= github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= @@ -334,6 +334,8 @@ github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qR github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= @@ -342,6 +344,8 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= +github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= @@ -390,6 +394,10 @@ github.com/swaggo/swag v1.8.4 h1:oGB351qH1JqUqK1tsMYEE5qTBbPk394BhsZxmUfebcI= github.com/swaggo/swag v1.8.4/go.mod h1:jMLeXOOmYyjk8PvHTsXBdrubsNd9gUJTTCzL5iBnseg= github.com/therootcompany/xz v1.0.1 h1:CmOtsn1CbtmyYiusbfmhmkpAAETj0wBIH6kCYaX+xzw= github.com/therootcompany/xz v1.0.1/go.mod h1:3K3UH1yCKgBneZYhuQUvJ9HPD19UEXEI0BWbMn8qNMY= +github.com/tklauser/go-sysconf v0.3.10 h1:IJ1AZGZRWbY8T5Vfk04D9WOA5WSejdflXxP03OUqALw= +github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYaQ2hGm7jmxEFk= +github.com/tklauser/numcpus v0.4.0 h1:E53Dm1HjH1/R2/aoCtXtPgzmElmn51aOkhCFSuZq//o= +github.com/tklauser/numcpus v0.4.0/go.mod h1:1+UI3pD8NW14VMwdgJNJ1ESk2UnwhAnz5hMwiKKqXCQ= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= @@ -406,6 +414,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg= +github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= @@ -499,8 +509,6 @@ golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1 golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= -golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 h1:NWy5+hlRbC7HK+PmcXVUmW1IMyFce7to56IUvhUFm7Y= -golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e h1:TsQ7F31D3bUCLeqPT0u+yjp1guoArKaNKmCr22PYgTQ= golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -533,6 +541,7 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -566,6 +575,7 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= diff --git a/backend/init/migration/migrate.go b/backend/init/migration/migrate.go index 46e818141..b6d4f7f72 100644 --- a/backend/init/migration/migrate.go +++ b/backend/init/migration/migrate.go @@ -13,6 +13,7 @@ func Init() { migrations.AddData, migrations.AddTableOperationLog, migrations.AddTableHost, + migrations.AddTablemonitor, }) if err := m.Migrate(); err != nil { global.LOG.Error(err) diff --git a/backend/init/migration/migrations/init.go b/backend/init/migration/migrations/init.go index 88fd8cb71..be571dde6 100644 --- a/backend/init/migration/migrations/init.go +++ b/backend/init/migration/migrations/init.go @@ -53,3 +53,10 @@ var AddTableHost = &gormigrate.Migration{ return nil }, } + +var AddTablemonitor = &gormigrate.Migration{ + ID: "20200905-add-table-monitor", + Migrate: func(tx *gorm.DB) error { + return tx.AutoMigrate(&model.MonitorBase{}, &model.MonitorIO{}, &model.MonitorNetwork{}) + }, +} diff --git a/backend/init/router/router.go b/backend/init/router/router.go index 3a4c010ba..b3b2f88a2 100644 --- a/backend/init/router/router.go +++ b/backend/init/router/router.go @@ -43,6 +43,7 @@ func Routers() *gin.Engine { systemRouter.InitGroupRouter(PrivateGroup) systemRouter.InitCommandRouter(PrivateGroup) systemRouter.InitTerminalRouter(PrivateGroup) + systemRouter.InitMonitorRouter(PrivateGroup) systemRouter.InitOperationLogRouter(PrivateGroup) systemRouter.InitFileRouter(PrivateGroup) } diff --git a/backend/router/entry.go b/backend/router/entry.go index ed7444635..0fd569033 100644 --- a/backend/router/entry.go +++ b/backend/router/entry.go @@ -6,6 +6,7 @@ type RouterGroup struct { HostRouter GroupRouter CommandRouter + MonitorRouter OperationLogRouter FileRouter } diff --git a/backend/router/ro_monitor.go b/backend/router/ro_monitor.go new file mode 100644 index 000000000..51f4bbfbd --- /dev/null +++ b/backend/router/ro_monitor.go @@ -0,0 +1,19 @@ +package router + +import ( + v1 "github.com/1Panel-dev/1Panel/app/api/v1" + "github.com/1Panel-dev/1Panel/middleware" + + "github.com/gin-gonic/gin" +) + +type MonitorRouter struct{} + +func (s *MonitorRouter) InitMonitorRouter(Router *gin.RouterGroup) { + monitorRouter := Router.Group("monitors").Use(middleware.JwtAuth()).Use(middleware.SessionAuth()) + baseApi := v1.ApiGroupApp.BaseApi + { + monitorRouter.POST("/search", baseApi.LoadMonitor) + monitorRouter.GET("/netoptions", baseApi.GetNetworkOptions) + } +} diff --git a/backend/server/server.go b/backend/server/server.go index 3f9c4994e..f69a9fc5f 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -5,6 +5,7 @@ import ( "fmt" "time" + "github.com/1Panel-dev/1Panel/cron" "github.com/1Panel-dev/1Panel/init/cache" "github.com/1Panel-dev/1Panel/init/session" "github.com/1Panel-dev/1Panel/init/session/psession" @@ -31,6 +32,7 @@ func Start() { cache.Init() session.Init() gin.SetMode(global.CONF.System.Level) + cron.Run() routers := router.Routers() address := fmt.Sprintf(":%d", global.CONF.System.Port)