2
0

server.go 2.7 KB

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