package main
import (
"fmt"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
)
func collet() {
v, _ := mem.VirtualMemory()
c, _ := cpu.Info()
cc, _ := cpu.Percent(time.Second, false)
d, _ := disk.Usage("/")
n, _ := host.Info()
nv, _ := net.IOCounters(true)
boottime, _ := host.BootTime()
btime := time.Unix(int64(boottime), 0).Format("2006-01-02 15:04:05")
fmt.Printf(" 内存 : %v MB 剩余: %v MB 使用:%v 用量:%0.2f%%\n", v.Total/1024/1024, v.Available/1024/1024, v.Used/1024/1024, v.UsedPercent)
if len(c) > 1 {
for _, sub_cpu := range c {
modelname := sub_cpu.ModelName
cores := sub_cpu.Cores
fmt.Printf(" CPU : %v %v cores \n", modelname, cores)
}
} else {
sub_cpu := c[0]
modelname := sub_cpu.ModelName
cores := sub_cpu.Cores
fmt.Printf(" CPU : %v %v cores \n", modelname, cores)
}
fmt.Printf(" 网络 : %v MB / %v MB\n", nv[0].BytesRecv/1024/1024, nv[0].BytesSent/1024/1024)
fmt.Printf(" 启动时间 : %v\n", btime)
fmt.Printf(" CPU使用率 : %0.2f%% \n", cc[0])
fmt.Printf(" 硬盘 : %v GB 剩余: %v GB 用量:%0.2f%%\n", d.Total/1024/1024/1024, d.Free/1024/1024/1024, d.UsedPercent)
fmt.Printf(" 操作系统 : %v(%v) %v \n", n.Platform, n.PlatformFamily, n.PlatformVersion)
fmt.Printf(" 主机名 : %v \n", n.Hostname)
}
func main() {
collet()
}
显示效果如:
内存 : 7717 MB 剩余: 2756 MB 使用:3775 用量:48.92%
CPU : Intel(R) Core(TM) i5-7Y54 CPU @ 1.20GHz 1 cores
CPU : Intel(R) Core(TM) i5-7Y54 CPU @ 1.20GHz 1 cores
CPU : Intel(R) Core(TM) i5-7Y54 CPU @ 1.20GHz 1 cores
CPU : Intel(R) Core(TM) i5-7Y54 CPU @ 1.20GHz 1 cores
网络 : 0 MB / 0 MB
启动时间 : 2019-08-24 11:49:15
CPU使用率 : 11.90%
硬盘 : 232 GB 剩余: 97 GB 用量:55.79%
操作系统 : ubuntu(debian) 19.04
主机名 : MateBook