1 Star 0 Fork 0

s-dy / go-balm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
runtime.go 2.07 KB
一键复制 编辑 原始数据 按行查看 历史
s-dy 提交于 2023-09-07 15:11 . first coding
package xruntime
import (
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/mem"
"runtime"
"time"
)
const (
B = 1
KB = 1024 * B
MB = 1024 * KB
GB = 1024 * MB
)
type Server struct {
Os Os `json:"os"`
Cpu Cpu `json:"cpu"`
Ram Ram `json:"ram"`
Disk Disk `json:"disk"`
}
type Os struct {
GOOS string `json:"goos"`
NumCPU int `json:"numCpu"`
Compiler string `json:"compiler"`
GoVersion string `json:"goVersion"`
}
type Cpu struct {
Cpus []float64 `json:"cpus"`
Cores int `json:"cores"`
}
type Ram struct {
UsedMB int `json:"usedMb"`
TotalMB int `json:"totalMb"`
}
type Disk struct {
UsedMB int `json:"usedMb"`
UsedGB int `json:"usedGb"`
TotalMB int `json:"totalMb"`
TotalGB int `json:"totalGb"`
}
//@function: GetOS
//@description: OS信息
//@return: o Os, err error
func GetOS() (o Os) {
o.GOOS = runtime.GOOS
o.NumCPU = runtime.NumCPU()
o.Compiler = runtime.Compiler
o.GoVersion = runtime.Version()
return o
}
//@function: GetCPU
//@description: CPU信息
//@return: c Cpu, err error
func GetCPU() (c Cpu, err error) {
if cores, err := cpu.Counts(false); err != nil {
return c, err
} else {
c.Cores = cores
}
if cpus, err := cpu.Percent(time.Duration(200)*time.Millisecond, true); err != nil {
return c, err
} else {
c.Cpus = cpus
}
return c, nil
}
//@function: InitRAM
//@description: RAM信息
//@return: r Ram, err error
func GetRAM() (r Ram, err error) {
if u, err := mem.VirtualMemory(); err != nil {
return r, err
} else {
r.UsedMB = int(u.Used) / MB
r.TotalMB = int(u.Total) / MB
}
return r, nil
}
//@function: InitDisk
//@description: 硬盘信息
//@return: d Disk, err error
func GetDisk() (d Disk, err error) {
if u, err := disk.Usage("/"); err != nil {
return d, err
} else {
d.UsedMB = int(u.Used) / MB
d.UsedGB = int(u.Used) / GB
d.TotalMB = int(u.Total) / MB
d.TotalGB = int(u.Total) / GB
}
return d, nil
}
// ProcessMemMB 进程内存
func ProcessMemMB() uint64 {
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
return ms.Sys / MB
}
Go
1
https://gitee.com/sdynasty/go-balm.git
git@gitee.com:sdynasty/go-balm.git
sdynasty
go-balm
go-balm
v0.0.1

搜索帮助