machine.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package mac
  2. import (
  3. // "fmt"
  4. "time"
  5. cpu "github.com/shirou/gopsutil/cpu"
  6. host "github.com/shirou/gopsutil/host"
  7. mem "github.com/shirou/gopsutil/mem"
  8. )
  9. // 服务器硬件信息
  10. type MachMeta struct {
  11. MemTotal uint64
  12. MemUsed uint64
  13. MemAvailable uint64
  14. MemPercent float64
  15. CpuNum int
  16. CpuCoreNum int32
  17. CpuModelName string
  18. CpuPercent []float64
  19. Hostname string
  20. UpTime uint64
  21. BootTime uint64
  22. OS string
  23. Platform string
  24. KernelVer string
  25. Arch string
  26. }
  27. func GetMacInfoImmutablePart(mac *MachMeta) {
  28. cpu, _ := cpu.Info()
  29. mac.CpuNum = len(cpu)
  30. mac.CpuModelName = cpu[0].ModelName
  31. mac.CpuCoreNum = 0
  32. for _, core := range cpu {
  33. mac.CpuCoreNum += core.Cores
  34. }
  35. host, _ := host.Info()
  36. // fmt.Printf("Hostname:%v, uptime:%v, boottime:%v,OS:%v Platform:%v KernelVersion:%v KernelArch:%v\n",
  37. // host.Hostname, host.Uptime, host.BootTime, host.OS, host.Platform, host.KernelVersion, host.KernelArch)
  38. mac.Hostname = host.Hostname
  39. mac.UpTime = host.Uptime
  40. mac.BootTime = host.BootTime
  41. mac.OS = host.OS
  42. mac.Platform = host.Platform
  43. mac.KernelVer = host.KernelVersion
  44. mac.Arch = host.KernelArch
  45. }
  46. func GetMachInfoMutablePart(mac *MachMeta) {
  47. cpup, _ := cpu.Percent(time.Second, true)
  48. // fmt.Println(len(cpup))
  49. // fmt.Println(cpup[0])
  50. mac.CpuPercent = append(mac.CpuPercent, cpup...)
  51. mem, _ := mem.VirtualMemory()
  52. // fmt.Printf("Total:%vMB, Free:%vMB Used:%vMB Usage%f%%\n",
  53. // mem.Total/1024/1024, mem.Available/1024/1024, mem.Used/1024/1024, mem.UsedPercent)
  54. mac.MemTotal = mem.Total
  55. mac.MemAvailable = mem.Available
  56. mac.MemUsed = mem.Used
  57. mac.MemPercent = mem.UsedPercent
  58. }
  59. func GetMachInfo(mac *MachMeta) {
  60. GetMacInfoImmutablePart(mac)
  61. GetMachInfoMutablePart(mac)
  62. }