1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sysinfo.go 2.75 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-09-02 14:15 . cpu,disk, mem info
package system
import (
"fmt"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/mem"
)
func Info() string {
inf := info{}
inf.D = Disk()
inf.C = Cpu()
inf.M = Mem()
in := fmt.Sprintf("Memory=> Total: %v, Available: %v, Used: %v, UsedPercent: %v\r\n"+
"Cpu=> User: %v, System: %v, Idle: %v, Nice: %v\r\n"+
"Disk=> Path: %v, Total: %v, Free: %v, Used: %v, UsedPercent: %v\r\n",
inf.M.Total, inf.M.Available, inf.M.Used, inf.M.UsedPercent,
inf.C.User, inf.C.System, inf.C.Idle, inf.C.Nice,
inf.D.Path, inf.D.Total, inf.D.Free, inf.D.Used, inf.D.UsedPercent)
return in
}
type info struct {
M MemInfo `json:"mem"`
C CpuInfo `json:"cpu"`
D DiskPath `json:"disk"`
}
const GB = 1024 * 1024 * 1024
func Mem() MemInfo {
v, er := mem.VirtualMemory()
if er != nil {
return MemInfo{}
}
return MemInfo{
Total: v.Total / GB,
Available: v.Available / GB,
Used: v.Used / GB,
UsedPercent: v.UsedPercent,
}
}
type MemInfo struct {
// Total amount of RAM on this system
Total uint64 `json:"total"`
// RAM available for programs to allocate
//
// This value is computed from the kernel specific values.
Available uint64 `json:"available"`
// RAM used by programs
//
// This value is computed from the kernel specific values.
Used uint64 `json:"used"`
// Percentage of RAM used by programs
//
// This value is computed from the kernel specific values.
UsedPercent float64 `json:"usedPercent"`
}
func Cpu() CpuInfo {
in, er := cpu.Times(false) //(0*time.Second, false)
if er != nil {
return CpuInfo{}
}
var inf CpuInfo
for i := range in {
inf.User = in[i].User
inf.System = in[i].System
inf.Idle = in[i].Idle
inf.Nice = in[i].Nice
}
return inf
}
type CpuInfo struct {
User float64 `json:"user"`
System float64 `json:"system"`
Idle float64 `json:"idle"`
Nice float64 `json:"nice"`
}
func Disk() DiskPath {
//var info DiskPath
//partitions, er := disk.Partitions(false)
//if er != nil {
// return DiskInfo{}
//}
// for i := range partitions {
d, err := disk.Usage("/")
if err != nil {
return DiskPath{}
// continue
}
return DiskPath{
Path: d.Path,
Total: d.Total / GB,
Free: d.Free / GB,
Used: d.Used / GB,
UsedPercent: d.UsedPercent,
}
//info.Paths = append(info.Paths, path)
//info.Total += path.Total
//info.Free += path.Free
//info.Used += path.Used
//info.UsedPercent += path.UsedPercent
////}
//return info
}
//type DiskInfo struct {
// DiskPath
// Paths []DiskPath `json:"paths"`
//}
type DiskPath struct {
Path string `json:"path,omitempty"`
Total uint64 `json:"total"`
Free uint64 `json:"free"`
Used uint64 `json:"used"`
UsedPercent float64 `json:"usedPercent"`
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.2.10

搜索帮助