1Panel/backend/cron/job/monitor.go

131 lines
4.4 KiB
Go
Raw Normal View History

2022-09-08 11:39:14 +08:00
package job
import (
2022-09-14 23:27:17 +08:00
"strconv"
2022-09-08 11:39:14 +08:00
"time"
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/global"
2022-09-08 11:39:14 +08:00
"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() {
2022-09-14 23:27:17 +08:00
settingRepo := repo.NewISettingRepo()
monitorStatus, _ := settingRepo.Get(settingRepo.WithByKey("MonitorStatus"))
if monitorStatus.Value == "disable" {
return
}
2022-09-08 11:39:14 +08:00
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
2022-09-14 23:27:17 +08:00
if err := global.DB.Create(&itemModel).Error; err != nil {
2023-01-31 15:22:05 +08:00
global.LOG.Debugf("Insert basic monitoring data failed, err: %v", err)
2022-09-14 23:27:17 +08:00
}
2022-09-08 11:39:14 +08:00
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
}
2022-09-14 23:27:17 +08:00
if err := global.DB.Create(&itemIO).Error; err != nil {
2023-01-31 15:22:05 +08:00
global.LOG.Debugf("Insert io monitoring data failed, err: %v", err)
2022-09-14 23:27:17 +08:00
}
2022-09-08 11:39:14 +08:00
}
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
2022-09-14 23:27:17 +08:00
if err := global.DB.Create(&itemNet).Error; err != nil {
2023-01-31 15:22:05 +08:00
global.LOG.Debugf("Insert network monitoring data failed, err: %v", err)
2022-09-14 23:27:17 +08:00
}
2022-09-08 11:39:14 +08:00
}
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
2022-09-14 23:27:17 +08:00
if err := global.DB.Create(&itemNet).Error; err != nil {
2023-01-31 15:22:05 +08:00
global.LOG.Debugf("Insert network all monitoring data failed, err: %v", err)
2022-09-14 23:27:17 +08:00
}
}
MonitorStoreDays, err := settingRepo.Get(settingRepo.WithByKey("MonitorStoreDays"))
if err != nil {
return
2022-09-08 11:39:14 +08:00
}
2022-09-14 23:27:17 +08:00
storeDays, _ := strconv.Atoi(MonitorStoreDays.Value)
timeForDelete := time.Now().AddDate(0, 0, -storeDays)
_ = global.DB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorBase{}).Error
_ = global.DB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorIO{}).Error
_ = global.DB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorNetwork{}).Error
2022-09-08 11:39:14 +08:00
}