2022-11-16 18:27:22 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-11-24 23:56:48 +08:00
|
|
|
"encoding/json"
|
2023-10-17 17:54:30 +08:00
|
|
|
"fmt"
|
2023-03-31 11:20:19 +08:00
|
|
|
"strings"
|
2022-11-16 18:27:22 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2023-10-17 17:54:30 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-03-31 11:20:19 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2023-02-03 10:13:59 +08:00
|
|
|
"github.com/shirou/gopsutil/v3/cpu"
|
|
|
|
"github.com/shirou/gopsutil/v3/disk"
|
|
|
|
"github.com/shirou/gopsutil/v3/host"
|
|
|
|
"github.com/shirou/gopsutil/v3/load"
|
|
|
|
"github.com/shirou/gopsutil/v3/mem"
|
|
|
|
"github.com/shirou/gopsutil/v3/net"
|
2022-11-16 18:27:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type DashboardService struct{}
|
|
|
|
|
|
|
|
type IDashboardService interface {
|
2022-11-24 23:56:48 +08:00
|
|
|
LoadBaseInfo(ioOption string, netOption string) (*dto.DashboardBase, error)
|
|
|
|
LoadCurrentInfo(ioOption string, netOption string) *dto.DashboardCurrent
|
2023-10-17 17:54:30 +08:00
|
|
|
|
|
|
|
Restart(operation string) error
|
2022-11-16 18:27:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIDashboardService() IDashboardService {
|
|
|
|
return &DashboardService{}
|
|
|
|
}
|
2023-10-17 17:54:30 +08:00
|
|
|
|
|
|
|
func (u *DashboardService) Restart(operation string) error {
|
2023-11-13 15:02:08 +08:00
|
|
|
if operation != "1panel" && operation != "system" {
|
2023-10-17 17:54:30 +08:00
|
|
|
return fmt.Errorf("handle restart operation %s failed, err: nonsupport such operation", operation)
|
|
|
|
}
|
|
|
|
itemCmd := fmt.Sprintf("%s 1pctl restart", cmd.SudoHandleCmd())
|
|
|
|
if operation == "system" {
|
|
|
|
itemCmd = fmt.Sprintf("%s reboot", cmd.SudoHandleCmd())
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
stdout, err := cmd.Exec(itemCmd)
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("handle %s failed, err: %v", itemCmd, stdout)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-24 23:56:48 +08:00
|
|
|
func (u *DashboardService) LoadBaseInfo(ioOption string, netOption string) (*dto.DashboardBase, error) {
|
2022-11-16 18:27:22 +08:00
|
|
|
var baseInfo dto.DashboardBase
|
|
|
|
hostInfo, err := host.Info()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-24 23:56:48 +08: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)
|
|
|
|
|
2023-02-07 16:29:54 +08:00
|
|
|
appInstall, err := appInstallRepo.ListBy()
|
2022-11-24 23:56:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-07 15:46:44 +08:00
|
|
|
baseInfo.AppInstalledNumber = len(appInstall)
|
2022-11-16 18:27:22 +08:00
|
|
|
dbs, err := mysqlRepo.List()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
baseInfo.DatabaseNumber = len(dbs)
|
2022-11-24 23:56:48 +08:00
|
|
|
website, err := websiteRepo.GetBy()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
baseInfo.WebsiteNumber = len(website)
|
2023-10-07 15:46:44 +08:00
|
|
|
cronjobs, err := cronjobRepo.List()
|
2022-11-16 18:27:22 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-07 15:46:44 +08:00
|
|
|
baseInfo.CronjobNumber = len(cronjobs)
|
2022-11-16 18:27:22 +08:00
|
|
|
|
|
|
|
cpuInfo, err := cpu.Info()
|
2023-02-02 15:08:42 +08:00
|
|
|
if err == nil {
|
|
|
|
baseInfo.CPUModelName = cpuInfo[0].ModelName
|
2022-11-16 18:27:22 +08:00
|
|
|
}
|
2023-02-02 15:08:42 +08:00
|
|
|
|
2022-11-16 18:27:22 +08:00
|
|
|
baseInfo.CPUCores, _ = cpu.Counts(false)
|
|
|
|
baseInfo.CPULogicalCores, _ = cpu.Counts(true)
|
2022-11-24 23:56:48 +08: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()
|
2022-12-27 18:57:11 +08:00
|
|
|
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 23:56:48 +08:00
|
|
|
currentInfo.Procs = hostInfo.Procs
|
|
|
|
|
|
|
|
currentInfo.CPUTotal, _ = cpu.Counts(true)
|
|
|
|
totalPercent, _ := cpu.Percent(0, false)
|
2022-11-16 18:27:22 +08:00
|
|
|
if len(totalPercent) == 1 {
|
2022-11-24 23:56:48 +08:00
|
|
|
currentInfo.CPUUsedPercent = totalPercent[0]
|
|
|
|
currentInfo.CPUUsed = currentInfo.CPUUsedPercent * 0.01 * float64(currentInfo.CPUTotal)
|
2022-11-16 18:27:22 +08:00
|
|
|
}
|
2022-11-24 23:56:48 +08:00
|
|
|
currentInfo.CPUPercent, _ = cpu.Percent(0, true)
|
2022-11-16 18:27:22 +08:00
|
|
|
|
2022-11-24 23:56:48 +08: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
|
|
|
|
|
2023-03-29 21:28:13 +08:00
|
|
|
currentInfo.DiskData = loadDiskInfo()
|
2022-11-24 23:56:48 +08:00
|
|
|
|
|
|
|
if ioOption == "all" {
|
|
|
|
diskInfo, _ := disk.IOCounters()
|
|
|
|
for _, state := range diskInfo {
|
|
|
|
currentInfo.IOReadBytes += state.ReadBytes
|
|
|
|
currentInfo.IOWriteBytes += state.WriteBytes
|
|
|
|
currentInfo.IOCount += (state.ReadCount + state.WriteCount)
|
2023-03-29 20:56:13 +08:00
|
|
|
currentInfo.IOReadTime += state.ReadTime
|
|
|
|
currentInfo.IOWriteTime += state.WriteTime
|
2022-11-24 23:56:48 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
diskInfo, _ := disk.IOCounters(ioOption)
|
|
|
|
for _, state := range diskInfo {
|
|
|
|
currentInfo.IOReadBytes += state.ReadBytes
|
|
|
|
currentInfo.IOWriteBytes += state.WriteBytes
|
2023-03-29 20:56:13 +08:00
|
|
|
currentInfo.IOCount += (state.ReadCount + state.WriteCount)
|
|
|
|
currentInfo.IOReadTime += state.ReadTime
|
|
|
|
currentInfo.IOWriteTime += state.WriteTime
|
2022-11-24 23:56:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ¤tInfo
|
2022-11-16 18:27:22 +08:00
|
|
|
}
|
2023-03-29 21:28:13 +08:00
|
|
|
|
2023-03-31 11:20:19 +08:00
|
|
|
type diskInfo struct {
|
|
|
|
Type string
|
|
|
|
Mount string
|
|
|
|
Device string
|
|
|
|
}
|
|
|
|
|
2023-03-29 21:28:13 +08:00
|
|
|
func loadDiskInfo() []dto.DiskInfo {
|
|
|
|
var datas []dto.DiskInfo
|
2023-03-31 11:20:19 +08:00
|
|
|
stdout, err := cmd.Exec("df -hT -P|grep '/'|grep -v tmpfs|grep -v 'snap/core'|grep -v udev")
|
2023-03-29 21:28:13 +08:00
|
|
|
if err != nil {
|
|
|
|
return datas
|
|
|
|
}
|
2023-03-31 11:20:19 +08:00
|
|
|
lines := strings.Split(stdout, "\n")
|
|
|
|
|
|
|
|
var mounts []diskInfo
|
2023-03-29 21:28:13 +08:00
|
|
|
var excludes = []string{"/mnt/cdrom", "/boot", "/boot/efi", "/dev", "/dev/shm", "/run/lock", "/run", "/run/shm", "/run/user"}
|
2023-03-31 11:20:19 +08:00
|
|
|
for _, line := range lines {
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
if len(fields) < 7 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fields[1] == "tmpfs" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(fields[2], "M") || strings.Contains(fields[2], "K") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(fields[6], "docker") {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-29 21:28:13 +08:00
|
|
|
isExclude := false
|
|
|
|
for _, exclude := range excludes {
|
2023-03-31 11:20:19 +08:00
|
|
|
if exclude == fields[6] {
|
2023-03-29 21:28:13 +08:00
|
|
|
isExclude = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isExclude {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-31 11:20:19 +08:00
|
|
|
mounts = append(mounts, diskInfo{Type: fields[1], Device: fields[0], Mount: fields[6]})
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(mounts); i++ {
|
2023-04-13 10:14:32 +08:00
|
|
|
state, err := disk.Usage(mounts[i].Mount)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-29 21:28:13 +08:00
|
|
|
var itemData dto.DiskInfo
|
2023-03-31 11:20:19 +08:00
|
|
|
itemData.Path = mounts[i].Mount
|
|
|
|
itemData.Type = mounts[i].Type
|
|
|
|
itemData.Device = mounts[i].Device
|
2023-03-29 21:28:13 +08:00
|
|
|
itemData.Total = state.Total
|
|
|
|
itemData.Free = state.Free
|
|
|
|
itemData.Used = state.Used
|
|
|
|
itemData.UsedPercent = state.UsedPercent
|
|
|
|
itemData.InodesTotal = state.InodesTotal
|
|
|
|
itemData.InodesUsed = state.InodesUsed
|
|
|
|
itemData.InodesFree = state.InodesFree
|
|
|
|
itemData.InodesUsedPercent = state.InodesUsedPercent
|
|
|
|
datas = append(datas, itemData)
|
|
|
|
}
|
|
|
|
return datas
|
|
|
|
}
|