1Panel/backend/app/api/v1/monitor.go

135 lines
4.2 KiB
Go
Raw Normal View History

2022-09-08 11:39:14 +08:00
package v1
import (
"sort"
2022-09-08 11:39:14 +08:00
"time"
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
2023-05-04 12:00:37 +08:00
"github.com/1Panel-dev/1Panel/backend/utils/common"
2022-09-08 11:39:14 +08:00
"github.com/gin-gonic/gin"
2023-02-03 10:13:59 +08:00
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/net"
2022-09-08 11:39:14 +08:00
)
2024-04-08 16:52:07 +08:00
// @Tags Monitor
// @Summary Load monitor datas
// @Description 获取监控数据
// @Param request body dto.MonitorSearch true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /hosts/monitor/search [post]
2022-09-08 11:39:14 +08:00
func (b *BaseApi) LoadMonitor(c *gin.Context) {
var req dto.MonitorSearch
2023-10-27 14:19:26 +08:00
if err := helper.CheckBindAndValidate(&req, c); err != nil {
2022-09-08 11:39:14 +08:00
return
}
2023-10-27 14:19:26 +08:00
2024-08-19 18:30:42 +08:00
loc, _ := time.LoadLocation(common.LoadTimeZoneByCmd())
2023-05-04 12:00:37 +08:00
req.StartTime = req.StartTime.In(loc)
req.EndTime = req.EndTime.In(loc)
2022-09-08 11:39:14 +08:00
var backdatas []dto.MonitorData
if req.Param == "all" || req.Param == "cpu" || req.Param == "memory" || req.Param == "load" {
var bases []model.MonitorBase
if err := global.MonitorDB.
2022-09-08 11:39:14 +08:00
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.MonitorDB.
2022-09-08 11:39:14 +08:00
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.MonitorDB.
2022-09-08 11:39:14 +08:00
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)
}
2024-04-08 16:52:07 +08:00
// @Tags Monitor
// @Summary Clean monitor datas
// @Description 清空监控数据
// @Success 200
// @Security ApiKeyAuth
// @Router /hosts/monitor/clean [post]
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"清空监控数据","formatEN":"clean monitor datas"}
func (b *BaseApi) CleanMonitor(c *gin.Context) {
2024-04-08 17:48:08 +08:00
if err := global.MonitorDB.Exec("DELETE FROM monitor_bases").Error; err != nil {
2024-04-08 16:52:07 +08:00
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
2024-04-08 17:48:08 +08:00
if err := global.MonitorDB.Exec("DELETE FROM monitor_ios").Error; err != nil {
2024-04-08 16:52:07 +08:00
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
2024-04-08 17:48:08 +08:00
if err := global.MonitorDB.Exec("DELETE FROM monitor_networks").Error; err != nil {
2024-04-08 16:52:07 +08:00
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}
2022-09-08 11:39:14 +08:00
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)
}
sort.Strings(options)
2022-09-08 11:39:14 +08:00
helper.SuccessWithData(c, options)
}
2022-11-24 23:56:48 +08:00
func (b *BaseApi) GetIOOptions(c *gin.Context) {
diskStat, _ := disk.IOCounters()
var options []string
options = append(options, "all")
for _, net := range diskStat {
options = append(options, net.Name)
}
sort.Strings(options)
2022-11-24 23:56:48 +08:00
helper.SuccessWithData(c, options)
}