1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 08:19:15 +08:00

pref: 完善告警通知 (#6227)

This commit is contained in:
2024-08-23 21:53:00 +08:00 committed by GitHub
parent e782f75863
commit 00cc1b62ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 29 deletions

View File

@ -318,3 +318,23 @@ func SplitStr(str string, spi ...string) []string {
func IsValidIP(ip string) bool { func IsValidIP(ip string) bool {
return net.ParseIP(ip) != nil return net.ParseIP(ip) != nil
} }
const (
b = uint64(1)
kb = 1024 * b
mb = 1024 * kb
gb = 1024 * mb
)
func FormatBytes(bytes uint64) string {
switch {
case bytes < kb:
return fmt.Sprintf("%dB", bytes)
case bytes < mb:
return fmt.Sprintf("%.2fKB", float64(bytes)/float64(kb))
case bytes < gb:
return fmt.Sprintf("%.2fMB", float64(bytes)/float64(mb))
default:
return fmt.Sprintf("%.2fGB", float64(bytes)/float64(gb))
}
}

View File

@ -9,6 +9,7 @@ import (
"time" "time"
"github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/files"
"github.com/shirou/gopsutil/v3/host" "github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/net" "github.com/shirou/gopsutil/v3/net"
@ -147,26 +148,6 @@ func getDownloadProcess(progress DownloadProgress) (res []byte, err error) {
return return
} }
const (
b = uint64(1)
kb = 1024 * b
mb = 1024 * kb
gb = 1024 * mb
)
func formatBytes(bytes uint64) string {
switch {
case bytes < kb:
return fmt.Sprintf("%dB", bytes)
case bytes < mb:
return fmt.Sprintf("%.2fKB", float64(bytes)/float64(kb))
case bytes < gb:
return fmt.Sprintf("%.2fMB", float64(bytes)/float64(mb))
default:
return fmt.Sprintf("%.2fGB", float64(bytes)/float64(gb))
}
}
func getProcessData(processConfig PsProcessConfig) (res []byte, err error) { func getProcessData(processConfig PsProcessConfig) (res []byte, err error) {
var processes []*process.Process var processes []*process.Process
processes, err = process.Processes() processes, err = process.Processes()
@ -230,14 +211,14 @@ func getProcessData(processConfig PsProcessConfig) (res []byte, err error) {
procData.CpuPercent = fmt.Sprintf("%.2f", procData.CpuValue) + "%" procData.CpuPercent = fmt.Sprintf("%.2f", procData.CpuValue) + "%"
menInfo, procErr := proc.MemoryInfo() menInfo, procErr := proc.MemoryInfo()
if procErr == nil { if procErr == nil {
procData.Rss = formatBytes(menInfo.RSS) procData.Rss = common.FormatBytes(menInfo.RSS)
procData.RssValue = menInfo.RSS procData.RssValue = menInfo.RSS
procData.Data = formatBytes(menInfo.Data) procData.Data = common.FormatBytes(menInfo.Data)
procData.VMS = formatBytes(menInfo.VMS) procData.VMS = common.FormatBytes(menInfo.VMS)
procData.HWM = formatBytes(menInfo.HWM) procData.HWM = common.FormatBytes(menInfo.HWM)
procData.Stack = formatBytes(menInfo.Stack) procData.Stack = common.FormatBytes(menInfo.Stack)
procData.Locked = formatBytes(menInfo.Locked) procData.Locked = common.FormatBytes(menInfo.Locked)
procData.Swap = formatBytes(menInfo.Swap) procData.Swap = common.FormatBytes(menInfo.Swap)
} else { } else {
procData.Rss = "--" procData.Rss = "--"
procData.Data = "--" procData.Data = "--"
@ -251,8 +232,8 @@ func getProcessData(processConfig PsProcessConfig) (res []byte, err error) {
} }
ioStat, procErr := proc.IOCounters() ioStat, procErr := proc.IOCounters()
if procErr == nil { if procErr == nil {
procData.DiskWrite = formatBytes(ioStat.WriteBytes) procData.DiskWrite = common.FormatBytes(ioStat.WriteBytes)
procData.DiskRead = formatBytes(ioStat.ReadBytes) procData.DiskRead = common.FormatBytes(ioStat.ReadBytes)
} else { } else {
procData.DiskWrite = "--" procData.DiskWrite = "--"
procData.DiskRead = "--" procData.DiskRead = "--"