1Panel/backend/app/service/dashboard.go

175 lines
5.0 KiB
Go
Raw Normal View History

2022-11-16 10:27:22 +00:00
package service
import (
2022-11-24 15:56:48 +00:00
"encoding/json"
2022-11-16 10:27:22 +00:00
"time"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/shirou/gopsutil/cpu"
2022-11-24 15:56:48 +00:00
"github.com/shirou/gopsutil/disk"
2022-11-16 10:27:22 +00:00
"github.com/shirou/gopsutil/host"
2022-11-24 15:56:48 +00:00
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
2022-11-16 10:27:22 +00:00
)
type DashboardService struct{}
type IDashboardService interface {
2022-11-24 15:56:48 +00:00
LoadBaseInfo(ioOption string, netOption string) (*dto.DashboardBase, error)
LoadCurrentInfo(ioOption string, netOption string) *dto.DashboardCurrent
2022-11-16 10:27:22 +00:00
}
func NewIDashboardService() IDashboardService {
return &DashboardService{}
}
2022-11-24 15:56:48 +00:00
func (u *DashboardService) LoadBaseInfo(ioOption string, netOption string) (*dto.DashboardBase, error) {
2022-11-16 10:27:22 +00:00
var baseInfo dto.DashboardBase
hostInfo, err := host.Info()
if err != nil {
return nil, err
}
2022-11-24 15:56:48 +00:00
baseInfo.Hostname = hostInfo.Hostname
baseInfo.OS = hostInfo.OS
baseInfo.Platform = hostInfo.Platform
baseInfo.PlatformFamily = hostInfo.PlatformFamily
baseInfo.PlatformVersion = hostInfo.PlatformVersion
baseInfo.KernelArch = hostInfo.KernelArch
baseInfo.KernelVersion = hostInfo.KernelVersion
ss, _ := json.Marshal(hostInfo)
baseInfo.VirtualizationSystem = string(ss)
apps, err := appRepo.GetBy()
2022-11-16 10:27:22 +00:00
if err != nil {
return nil, err
}
2022-11-24 15:56:48 +00:00
for _, app := range apps {
switch app.Key {
2022-11-16 10:27:22 +00:00
case "dateease":
2022-11-24 15:56:48 +00:00
baseInfo.DateeaseID = app.ID
2022-11-16 10:27:22 +00:00
case "halo":
2022-11-24 15:56:48 +00:00
baseInfo.HaloID = app.ID
2022-11-16 10:27:22 +00:00
case "metersphere":
2022-11-24 15:56:48 +00:00
baseInfo.MeterSphereID = app.ID
2022-11-16 10:27:22 +00:00
case "jumpserver":
2022-11-24 15:56:48 +00:00
baseInfo.JumpServerID = app.ID
case "kubeoperator":
baseInfo.KubeoperatorID = app.ID
case "kubepi":
baseInfo.KubepiID = app.ID
2022-11-16 10:27:22 +00:00
}
}
2022-11-24 15:56:48 +00:00
appInstall, err := appInstallRepo.GetBy()
if err != nil {
return nil, err
}
2022-11-16 10:27:22 +00:00
baseInfo.AppInstalldNumber = len(appInstall)
dbs, err := mysqlRepo.List()
if err != nil {
return nil, err
}
baseInfo.DatabaseNumber = len(dbs)
2022-11-24 15:56:48 +00:00
website, err := websiteRepo.GetBy()
if err != nil {
return nil, err
}
baseInfo.WebsiteNumber = len(website)
2022-11-16 10:27:22 +00:00
cornjobs, err := cronjobRepo.List()
if err != nil {
return nil, err
}
2022-12-06 07:05:13 +00:00
baseInfo.CronjobNumber = len(cornjobs)
2022-11-16 10:27:22 +00:00
cpuInfo, err := cpu.Info()
if err != nil {
return nil, err
}
baseInfo.CPUModelName = cpuInfo[0].ModelName
baseInfo.CPUCores, _ = cpu.Counts(false)
baseInfo.CPULogicalCores, _ = cpu.Counts(true)
2022-11-24 15:56:48 +00:00
baseInfo.CurrentInfo = *u.LoadCurrentInfo(ioOption, netOption)
return &baseInfo, nil
}
func (u *DashboardService) LoadCurrentInfo(ioOption string, netOption string) *dto.DashboardCurrent {
var currentInfo dto.DashboardCurrent
hostInfo, _ := host.Info()
currentInfo.Uptime = hostInfo.Uptime
currentInfo.TimeSinceUptime = time.Now().Add(-time.Duration(hostInfo.Uptime) * time.Second).Format("2006-01-02 15:04:05")
2022-11-24 15:56:48 +00:00
currentInfo.Procs = hostInfo.Procs
currentInfo.CPUTotal, _ = cpu.Counts(true)
totalPercent, _ := cpu.Percent(0, false)
2022-11-16 10:27:22 +00:00
if len(totalPercent) == 1 {
2022-11-24 15:56:48 +00:00
currentInfo.CPUUsedPercent = totalPercent[0]
currentInfo.CPUUsed = currentInfo.CPUUsedPercent * 0.01 * float64(currentInfo.CPUTotal)
2022-11-16 10:27:22 +00:00
}
2022-11-24 15:56:48 +00:00
currentInfo.CPUPercent, _ = cpu.Percent(0, true)
2022-11-16 10:27:22 +00:00
2022-11-24 15:56:48 +00:00
loadInfo, _ := load.Avg()
currentInfo.Load1 = loadInfo.Load1
currentInfo.Load5 = loadInfo.Load5
currentInfo.Load15 = loadInfo.Load15
currentInfo.LoadUsagePercent = loadInfo.Load1 / (float64(currentInfo.CPUTotal*2) * 0.75) * 100
memoryInfo, _ := mem.VirtualMemory()
currentInfo.MemoryTotal = memoryInfo.Total
currentInfo.MemoryAvailable = memoryInfo.Available
currentInfo.MemoryUsed = memoryInfo.Used
currentInfo.MemoryUsedPercent = memoryInfo.UsedPercent
state, _ := disk.Usage("/")
currentInfo.Total = state.Total
currentInfo.Free = state.Free
currentInfo.Used = state.Used
currentInfo.UsedPercent = state.UsedPercent
currentInfo.InodesTotal = state.InodesTotal
currentInfo.InodesUsed = state.InodesUsed
currentInfo.InodesFree = state.InodesFree
currentInfo.InodesUsedPercent = state.InodesUsedPercent
if ioOption == "all" {
diskInfo, _ := disk.IOCounters()
for _, state := range diskInfo {
currentInfo.IOReadBytes += state.ReadBytes
currentInfo.IOWriteBytes += state.WriteBytes
currentInfo.IOCount += (state.ReadCount + state.WriteCount)
currentInfo.IOTime += state.ReadTime / 1000 / 1000
if state.WriteTime > state.ReadTime {
currentInfo.IOTime += state.WriteTime / 1000 / 1000
}
}
} else {
diskInfo, _ := disk.IOCounters(ioOption)
for _, state := range diskInfo {
currentInfo.IOReadBytes += state.ReadBytes
currentInfo.IOWriteBytes += state.WriteBytes
currentInfo.IOTime += state.ReadTime / 1000 / 1000
if state.WriteTime > state.ReadTime {
currentInfo.IOTime += state.WriteTime / 1000 / 1000
}
}
}
if netOption == "all" {
netInfo, _ := net.IOCounters(false)
if len(netInfo) != 0 {
currentInfo.NetBytesSent = netInfo[0].BytesSent
currentInfo.NetBytesRecv = netInfo[0].BytesRecv
}
} else {
netInfo, _ := net.IOCounters(true)
for _, state := range netInfo {
if state.Name == netOption {
currentInfo.NetBytesSent = state.BytesSent
currentInfo.NetBytesRecv = state.BytesRecv
}
}
}
currentInfo.ShotTime = time.Now()
return &currentInfo
2022-11-16 10:27:22 +00:00
}