代码拉取完成,页面将自动刷新
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"`
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。