| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package mac
- import (
- // "fmt"
- "time"
- cpu "github.com/shirou/gopsutil/cpu"
- host "github.com/shirou/gopsutil/host"
- mem "github.com/shirou/gopsutil/mem"
- )
- // 服务器硬件信息
- type MachMeta struct {
- MemTotal uint64
- MemUsed uint64
- MemAvailable uint64
- MemPercent float64
- CpuNum int
- CpuCoreNum int32
- CpuModelName string
- CpuPercent []float64
- Hostname string
- UpTime uint64
- BootTime uint64
- OS string
- Platform string
- KernelVer string
- Arch string
- }
- func GetMacInfoImmutablePart(mac *MachMeta) {
- cpu, _ := cpu.Info()
- mac.CpuNum = len(cpu)
- mac.CpuModelName = cpu[0].ModelName
- mac.CpuCoreNum = 0
- for _, core := range cpu {
- mac.CpuCoreNum += core.Cores
- }
- host, _ := host.Info()
- // fmt.Printf("Hostname:%v, uptime:%v, boottime:%v,OS:%v Platform:%v KernelVersion:%v KernelArch:%v\n",
- // host.Hostname, host.Uptime, host.BootTime, host.OS, host.Platform, host.KernelVersion, host.KernelArch)
- mac.Hostname = host.Hostname
- mac.UpTime = host.Uptime
- mac.BootTime = host.BootTime
- mac.OS = host.OS
- mac.Platform = host.Platform
- mac.KernelVer = host.KernelVersion
- mac.Arch = host.KernelArch
- }
- func GetMachInfoMutablePart(mac *MachMeta) {
- cpup, _ := cpu.Percent(time.Second, true)
- // fmt.Println(len(cpup))
- // fmt.Println(cpup[0])
- mac.CpuPercent = append(mac.CpuPercent, cpup...)
- mem, _ := mem.VirtualMemory()
- // fmt.Printf("Total:%vMB, Free:%vMB Used:%vMB Usage%f%%\n",
- // mem.Total/1024/1024, mem.Available/1024/1024, mem.Used/1024/1024, mem.UsedPercent)
- mac.MemTotal = mem.Total
- mac.MemAvailable = mem.Available
- mac.MemUsed = mem.Used
- mac.MemPercent = mem.UsedPercent
- }
- func GetMachInfo(mac *MachMeta) {
- GetMacInfoImmutablePart(mac)
- GetMachInfoMutablePart(mac)
- }
|