2022-11-16 10:27:22 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-11-24 15:56:48 +00:00
|
|
|
"encoding/json"
|
2023-10-17 09:54:30 +00:00
|
|
|
"fmt"
|
2024-08-27 05:39:36 +00:00
|
|
|
network "net"
|
|
|
|
"os"
|
2023-11-29 06:38:07 +00:00
|
|
|
"sort"
|
2023-03-31 03:20:19 +00:00
|
|
|
"strings"
|
2023-11-27 09:42:08 +00:00
|
|
|
"sync"
|
2022-11-16 10:27:22 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2024-06-28 06:56:58 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-10-17 09:54:30 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-03-31 03:20:19 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2024-04-11 06:16:10 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/copier"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/xpack"
|
2023-02-03 02:13:59 +00: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 10:27:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DashboardService struct{}
|
|
|
|
|
|
|
|
type IDashboardService interface {
|
2024-03-04 14:59:11 +00:00
|
|
|
LoadOsInfo() (*dto.OsInfo, error)
|
2022-11-24 15:56:48 +00:00
|
|
|
LoadBaseInfo(ioOption string, netOption string) (*dto.DashboardBase, error)
|
|
|
|
LoadCurrentInfo(ioOption string, netOption string) *dto.DashboardCurrent
|
2023-10-17 09:54:30 +00:00
|
|
|
|
|
|
|
Restart(operation string) error
|
2022-11-16 10:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIDashboardService() IDashboardService {
|
|
|
|
return &DashboardService{}
|
|
|
|
}
|
2023-10-17 09:54:30 +00:00
|
|
|
|
|
|
|
func (u *DashboardService) Restart(operation string) error {
|
2023-11-13 07:02:08 +00:00
|
|
|
if operation != "1panel" && operation != "system" {
|
2023-10-17 09:54:30 +00: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
|
|
|
|
}
|
|
|
|
|
2024-03-04 14:59:11 +00:00
|
|
|
func (u *DashboardService) LoadOsInfo() (*dto.OsInfo, error) {
|
|
|
|
var baseInfo dto.OsInfo
|
|
|
|
hostInfo, err := host.Info()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
baseInfo.OS = hostInfo.OS
|
|
|
|
baseInfo.Platform = hostInfo.Platform
|
|
|
|
baseInfo.PlatformFamily = hostInfo.PlatformFamily
|
|
|
|
baseInfo.KernelArch = hostInfo.KernelArch
|
|
|
|
baseInfo.KernelVersion = hostInfo.KernelVersion
|
|
|
|
|
2024-06-18 10:08:54 +00:00
|
|
|
diskInfo, err := disk.Usage(global.CONF.System.BaseDir)
|
|
|
|
if err == nil {
|
|
|
|
baseInfo.DiskSize = int64(diskInfo.Free)
|
|
|
|
}
|
|
|
|
|
2024-03-04 14:59:11 +00:00
|
|
|
if baseInfo.KernelArch == "armv7l" {
|
|
|
|
baseInfo.KernelArch = "armv7"
|
|
|
|
}
|
|
|
|
if baseInfo.KernelArch == "x86_64" {
|
|
|
|
baseInfo.KernelArch = "amd64"
|
|
|
|
}
|
|
|
|
return &baseInfo, nil
|
|
|
|
}
|
|
|
|
|
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)
|
2024-08-27 05:39:36 +00:00
|
|
|
baseInfo.IpV4Addr = GetOutboundIP()
|
|
|
|
httpProxy := os.Getenv("http_proxy")
|
|
|
|
if httpProxy == "" {
|
|
|
|
httpProxy = os.Getenv("HTTP_PROXY")
|
|
|
|
}
|
|
|
|
if httpProxy != "" {
|
|
|
|
baseInfo.SystemProxy = httpProxy
|
|
|
|
}
|
|
|
|
baseInfo.SystemProxy = "noProxy"
|
2023-02-07 08:29:54 +00:00
|
|
|
appInstall, err := appInstallRepo.ListBy()
|
2022-11-24 15:56:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-27 05:39:36 +00:00
|
|
|
|
2023-10-07 07:46:44 +00:00
|
|
|
baseInfo.AppInstalledNumber = len(appInstall)
|
2024-06-28 05:44:58 +00:00
|
|
|
postgresqlDbs, err := postgresqlRepo.List()
|
2022-11-16 10:27:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-28 05:44:58 +00:00
|
|
|
mysqlDbs, err := mysqlRepo.List()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
baseInfo.DatabaseNumber = len(mysqlDbs) + len(postgresqlDbs)
|
2022-11-24 15:56:48 +00:00
|
|
|
website, err := websiteRepo.GetBy()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
baseInfo.WebsiteNumber = len(website)
|
2023-10-07 07:46:44 +00:00
|
|
|
cronjobs, err := cronjobRepo.List()
|
2022-11-16 10:27:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-07 07:46:44 +00:00
|
|
|
baseInfo.CronjobNumber = len(cronjobs)
|
2022-11-16 10:27:22 +00:00
|
|
|
|
|
|
|
cpuInfo, err := cpu.Info()
|
2023-02-02 07:08:42 +00:00
|
|
|
if err == nil {
|
|
|
|
baseInfo.CPUModelName = cpuInfo[0].ModelName
|
2022-11-16 10:27:22 +00:00
|
|
|
}
|
2023-02-02 07:08:42 +00:00
|
|
|
|
2022-11-16 10:27:22 +00:00
|
|
|
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()
|
2022-12-27 10:57:11 +00:00
|
|
|
currentInfo.Uptime = hostInfo.Uptime
|
2024-06-28 06:04:08 +00:00
|
|
|
currentInfo.TimeSinceUptime = time.Now().Add(-time.Duration(hostInfo.Uptime) * time.Second).Format(constant.DateTimeLayout)
|
2022-11-24 15:56:48 +00:00
|
|
|
currentInfo.Procs = hostInfo.Procs
|
|
|
|
|
|
|
|
currentInfo.CPUTotal, _ = cpu.Counts(true)
|
2024-09-05 08:39:48 +00:00
|
|
|
totalPercent, _ := cpu.Percent(1*time.Second, 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
|
|
|
}
|
2024-09-05 08:39:48 +00:00
|
|
|
currentInfo.CPUPercent, _ = cpu.Percent(1*time.Second, 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
|
|
|
|
|
2023-11-17 02:12:07 +00:00
|
|
|
swapInfo, _ := mem.SwapMemory()
|
|
|
|
currentInfo.SwapMemoryTotal = swapInfo.Total
|
|
|
|
currentInfo.SwapMemoryAvailable = swapInfo.Free
|
|
|
|
currentInfo.SwapMemoryUsed = swapInfo.Used
|
|
|
|
currentInfo.SwapMemoryUsedPercent = swapInfo.UsedPercent
|
|
|
|
|
2023-03-29 13:28:13 +00:00
|
|
|
currentInfo.DiskData = loadDiskInfo()
|
2024-04-11 06:16:10 +00:00
|
|
|
currentInfo.GPUData = loadGPUInfo()
|
2022-11-24 15:56:48 +00: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 12:56:13 +00:00
|
|
|
currentInfo.IOReadTime += state.ReadTime
|
|
|
|
currentInfo.IOWriteTime += state.WriteTime
|
2022-11-24 15:56:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
diskInfo, _ := disk.IOCounters(ioOption)
|
|
|
|
for _, state := range diskInfo {
|
|
|
|
currentInfo.IOReadBytes += state.ReadBytes
|
|
|
|
currentInfo.IOWriteBytes += state.WriteBytes
|
2023-03-29 12:56:13 +00:00
|
|
|
currentInfo.IOCount += (state.ReadCount + state.WriteCount)
|
|
|
|
currentInfo.IOReadTime += state.ReadTime
|
|
|
|
currentInfo.IOWriteTime += state.WriteTime
|
2022-11-24 15:56:48 +00: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 10:27:22 +00:00
|
|
|
}
|
2023-03-29 13:28:13 +00:00
|
|
|
|
2024-08-27 05:39:36 +00:00
|
|
|
func GetOutboundIP() string {
|
|
|
|
conn, err := network.Dial("udp", "8.8.8.8:80")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "IPNotFound"
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
localAddr := conn.LocalAddr().(*network.UDPAddr)
|
|
|
|
return localAddr.IP.String()
|
|
|
|
}
|
|
|
|
|
2023-03-31 03:20:19 +00:00
|
|
|
type diskInfo struct {
|
|
|
|
Type string
|
|
|
|
Mount string
|
|
|
|
Device string
|
|
|
|
}
|
|
|
|
|
2023-03-29 13:28:13 +00:00
|
|
|
func loadDiskInfo() []dto.DiskInfo {
|
|
|
|
var datas []dto.DiskInfo
|
2023-11-27 09:42:08 +00:00
|
|
|
stdout, err := cmd.ExecWithTimeOut("df -hT -P|grep '/'|grep -v tmpfs|grep -v 'snap/core'|grep -v udev", 2*time.Second)
|
2023-03-29 13:28:13 +00:00
|
|
|
if err != nil {
|
2024-03-05 10:06:08 +00:00
|
|
|
stdout, err = cmd.ExecWithTimeOut("df -lhT -P|grep '/'|grep -v tmpfs|grep -v 'snap/core'|grep -v udev", 1*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
return datas
|
|
|
|
}
|
2023-03-29 13:28:13 +00:00
|
|
|
}
|
2023-03-31 03:20:19 +00:00
|
|
|
lines := strings.Split(stdout, "\n")
|
|
|
|
|
|
|
|
var mounts []diskInfo
|
2023-03-29 13:28:13 +00:00
|
|
|
var excludes = []string{"/mnt/cdrom", "/boot", "/boot/efi", "/dev", "/dev/shm", "/run/lock", "/run", "/run/shm", "/run/user"}
|
2023-03-31 03:20:19 +00:00
|
|
|
for _, line := range lines {
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
if len(fields) < 7 {
|
|
|
|
continue
|
|
|
|
}
|
2024-05-29 06:27:11 +00:00
|
|
|
if strings.HasPrefix(fields[6], "/snap") || len(strings.Split(fields[6], "/")) > 10 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.TrimSpace(fields[1]) == "tmpfs" {
|
2023-03-31 03:20:19 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-05-29 02:39:09 +00:00
|
|
|
if strings.Contains(fields[2], "K") {
|
2023-03-31 03:20:19 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(fields[6], "docker") {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-29 13:28:13 +00:00
|
|
|
isExclude := false
|
|
|
|
for _, exclude := range excludes {
|
2023-03-31 03:20:19 +00:00
|
|
|
if exclude == fields[6] {
|
2023-03-29 13:28:13 +00:00
|
|
|
isExclude = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isExclude {
|
|
|
|
continue
|
|
|
|
}
|
2024-07-04 09:54:32 +00:00
|
|
|
mounts = append(mounts, diskInfo{Type: fields[1], Device: fields[0], Mount: strings.Join(fields[6:], " ")})
|
2023-03-31 03:20:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 07:34:07 +00:00
|
|
|
var (
|
|
|
|
wg sync.WaitGroup
|
|
|
|
mu sync.Mutex
|
|
|
|
)
|
2023-11-27 09:42:08 +00:00
|
|
|
wg.Add(len(mounts))
|
2023-03-31 03:20:19 +00:00
|
|
|
for i := 0; i < len(mounts); i++ {
|
2023-11-29 06:38:07 +00:00
|
|
|
go func(timeoutCh <-chan time.Time, mount diskInfo) {
|
2023-11-27 09:42:08 +00:00
|
|
|
defer wg.Done()
|
2023-11-29 06:38:07 +00:00
|
|
|
|
|
|
|
var itemData dto.DiskInfo
|
|
|
|
itemData.Path = mount.Mount
|
|
|
|
itemData.Type = mount.Type
|
|
|
|
itemData.Device = mount.Device
|
2023-11-27 09:42:08 +00:00
|
|
|
select {
|
2023-11-29 06:38:07 +00:00
|
|
|
case <-timeoutCh:
|
2023-11-29 07:34:07 +00:00
|
|
|
mu.Lock()
|
2023-11-29 06:38:07 +00:00
|
|
|
datas = append(datas, itemData)
|
2023-11-29 07:34:07 +00:00
|
|
|
mu.Unlock()
|
2023-11-29 06:38:07 +00:00
|
|
|
global.LOG.Errorf("load disk info from %s failed, err: timeout", mount.Mount)
|
2023-11-27 09:42:08 +00:00
|
|
|
default:
|
2023-11-29 06:38:07 +00:00
|
|
|
state, err := disk.Usage(mount.Mount)
|
2023-11-27 09:42:08 +00:00
|
|
|
if err != nil {
|
2023-11-29 07:34:07 +00:00
|
|
|
mu.Lock()
|
|
|
|
datas = append(datas, itemData)
|
|
|
|
mu.Unlock()
|
|
|
|
global.LOG.Errorf("load disk info from %s failed, err: %v", mount.Mount, err)
|
2023-11-27 09:42:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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
|
2023-11-29 07:34:07 +00:00
|
|
|
mu.Lock()
|
2023-11-27 09:42:08 +00:00
|
|
|
datas = append(datas, itemData)
|
2023-11-29 07:34:07 +00:00
|
|
|
mu.Unlock()
|
2023-11-27 09:42:08 +00:00
|
|
|
}
|
2023-11-29 06:38:07 +00:00
|
|
|
}(time.After(5*time.Second), mounts[i])
|
2023-03-29 13:28:13 +00:00
|
|
|
}
|
2023-11-27 09:42:08 +00:00
|
|
|
wg.Wait()
|
2023-11-29 06:38:07 +00:00
|
|
|
|
|
|
|
sort.Slice(datas, func(i, j int) bool {
|
|
|
|
return datas[i].Path < datas[j].Path
|
|
|
|
})
|
2023-03-29 13:28:13 +00:00
|
|
|
return datas
|
|
|
|
}
|
2024-04-11 06:16:10 +00:00
|
|
|
|
|
|
|
func loadGPUInfo() []dto.GPUInfo {
|
|
|
|
list := xpack.LoadGpuInfo()
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var data []dto.GPUInfo
|
|
|
|
for _, gpu := range list {
|
|
|
|
var dataItem dto.GPUInfo
|
|
|
|
if err := copier.Copy(&dataItem, &gpu); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dataItem.PowerUsage = dataItem.PowerDraw + " / " + dataItem.MaxPowerLimit
|
|
|
|
dataItem.MemoryUsage = dataItem.MemUsed + " / " + dataItem.MemTotal
|
|
|
|
data = append(data, dataItem)
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|