You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.4 KiB

3 years ago
  1. package utils
  2. import (
  3. "runtime"
  4. "time"
  5. "github.com/shirou/gopsutil/cpu"
  6. "github.com/shirou/gopsutil/disk"
  7. "github.com/shirou/gopsutil/mem"
  8. )
  9. const (
  10. B = 1
  11. KB = 1024 * B
  12. MB = 1024 * KB
  13. GB = 1024 * MB
  14. )
  15. type Server struct {
  16. Os Os `json:"os"`
  17. Cpu Cpu `json:"cpu"`
  18. Rrm Rrm `json:"ram"`
  19. Disk Disk `json:"disk"`
  20. }
  21. type Os struct {
  22. GOOS string `json:"goos"`
  23. NumCPU int `json:"numCpu"`
  24. Compiler string `json:"compiler"`
  25. GoVersion string `json:"goVersion"`
  26. NumGoroutine int `json:"numGoroutine"`
  27. }
  28. type Cpu struct {
  29. Cpus []float64 `json:"cpus"`
  30. Cores int `json:"cores"`
  31. }
  32. type Rrm struct {
  33. UsedMB int `json:"usedMb"`
  34. TotalMB int `json:"totalMb"`
  35. UsedPercent int `json:"usedPercent"`
  36. }
  37. type Disk struct {
  38. UsedMB int `json:"usedMb"`
  39. UsedGB int `json:"usedGb"`
  40. TotalMB int `json:"totalMb"`
  41. TotalGB int `json:"totalGb"`
  42. UsedPercent int `json:"usedPercent"`
  43. }
  44. //@author: [SliverHorn](https://github.com/SliverHorn)
  45. //@function: InitCPU
  46. //@description: OS信息
  47. //@return: o Os, err error
  48. func InitOS() (o Os) {
  49. o.GOOS = runtime.GOOS
  50. o.NumCPU = runtime.NumCPU()
  51. o.Compiler = runtime.Compiler
  52. o.GoVersion = runtime.Version()
  53. o.NumGoroutine = runtime.NumGoroutine()
  54. return o
  55. }
  56. //@author: [SliverHorn](https://github.com/SliverHorn)
  57. //@function: InitCPU
  58. //@description: CPU信息
  59. //@return: c Cpu, err error
  60. func InitCPU() (c Cpu, err error) {
  61. if cores, err := cpu.Counts(false); err != nil {
  62. return c, err
  63. } else {
  64. c.Cores = cores
  65. }
  66. if cpus, err := cpu.Percent(time.Duration(200)*time.Millisecond, true); err != nil {
  67. return c, err
  68. } else {
  69. c.Cpus = cpus
  70. }
  71. return c, nil
  72. }
  73. //@author: [SliverHorn](https://github.com/SliverHorn)
  74. //@function: InitRAM
  75. //@description: ARM信息
  76. //@return: r Rrm, err error
  77. func InitRAM() (r Rrm, err error) {
  78. if u, err := mem.VirtualMemory(); err != nil {
  79. return r, err
  80. } else {
  81. r.UsedMB = int(u.Used) / MB
  82. r.TotalMB = int(u.Total) / MB
  83. r.UsedPercent = int(u.UsedPercent)
  84. }
  85. return r, nil
  86. }
  87. //@author: [SliverHorn](https://github.com/SliverHorn)
  88. //@function: InitDisk
  89. //@description: 硬盘信息
  90. //@return: d Disk, err error
  91. func InitDisk() (d Disk, err error) {
  92. if u, err := disk.Usage("/"); err != nil {
  93. return d, err
  94. } else {
  95. d.UsedMB = int(u.Used) / MB
  96. d.UsedGB = int(u.Used) / GB
  97. d.TotalMB = int(u.Total) / MB
  98. d.TotalGB = int(u.Total) / GB
  99. d.UsedPercent = int(u.UsedPercent)
  100. }
  101. return d, nil
  102. }