sys_server_monitor.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package apis
  2. import (
  3. "fmt"
  4. "github.com/shirou/gopsutil/host"
  5. "runtime"
  6. "strconv"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "github.com/go-admin-team/go-admin-core/sdk/api"
  10. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  11. _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  12. "github.com/shirou/gopsutil/cpu"
  13. "github.com/shirou/gopsutil/disk"
  14. "github.com/shirou/gopsutil/mem"
  15. )
  16. const (
  17. B = 1
  18. KB = 1024 * B
  19. MB = 1024 * KB
  20. GB = 1024 * MB
  21. )
  22. type ServerMonitor struct {
  23. api.Api
  24. }
  25. //获取相差时间
  26. func GetHourDiffer(startTime, endTime string) int64 {
  27. var hour int64
  28. t1, err := time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local)
  29. t2, err := time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local)
  30. if err == nil && t1.Before(t2) {
  31. diff := t2.Unix() - t1.Unix() //
  32. hour = diff / 3600
  33. return hour
  34. } else {
  35. return hour
  36. }
  37. }
  38. // ServerInfo 获取系统信息
  39. // @Summary 系统信息
  40. // @Description 获取JSON
  41. // @Tags 系统信息
  42. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  43. // @Router /api/v1/server-monitor [get]
  44. // @Security Bearer
  45. func (e ServerMonitor) ServerInfo(c *gin.Context) {
  46. e.Context = c
  47. sysInfo, err := host.Info()
  48. osDic := make(map[string]interface{}, 0)
  49. osDic["goOs"] = runtime.GOOS
  50. osDic["arch"] = runtime.GOARCH
  51. osDic["mem"] = runtime.MemProfileRate
  52. osDic["compiler"] = runtime.Compiler
  53. osDic["version"] = runtime.Version()
  54. osDic["numGoroutine"] = runtime.NumGoroutine()
  55. osDic["ip"] = pkg.GetLocaHonst()
  56. osDic["projectDir"] = pkg.GetCurrentPath()
  57. osDic["hostName"] = sysInfo.Hostname
  58. osDic["time"] = time.Now().Format("2006-01-02 15:04:05")
  59. dis, _ := disk.Usage("/")
  60. diskTotalGB := int(dis.Total) / GB
  61. diskFreeGB := int(dis.Free) / GB
  62. diskDic := make(map[string]interface{}, 0)
  63. diskDic["total"] = diskTotalGB
  64. diskDic["free"] = diskFreeGB
  65. mem, _ := mem.VirtualMemory()
  66. memUsedMB := int(mem.Used) / GB
  67. memTotalMB := int(mem.Total) / GB
  68. memFreeMB := int(mem.Free) / GB
  69. memUsedPercent := int(mem.UsedPercent)
  70. memDic := make(map[string]interface{}, 0)
  71. memDic["total"] = memTotalMB
  72. memDic["used"] = memUsedMB
  73. memDic["free"] = memFreeMB
  74. memDic["usage"] = memUsedPercent
  75. cpuDic := make(map[string]interface{}, 0)
  76. cpuDic["cpuInfo"], _ = cpu.Info()
  77. percent, _ := cpu.Percent(0, false)
  78. cpuDic["Percent"] = pkg.Round(percent[0], 2)
  79. cpuDic["cpuNum"], _ = cpu.Counts(false)
  80. //服务器磁盘信息
  81. disklist := make([]disk.UsageStat, 0)
  82. //所有分区
  83. diskInfo, err := disk.Partitions(true)
  84. if err == nil {
  85. for _, p := range diskInfo {
  86. diskDetail, err := disk.Usage(p.Mountpoint)
  87. if err == nil {
  88. diskDetail.UsedPercent, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", diskDetail.UsedPercent), 64)
  89. diskDetail.Total = diskDetail.Total / 1024 / 1024
  90. diskDetail.Used = diskDetail.Used / 1024 / 1024
  91. diskDetail.Free = diskDetail.Free / 1024 / 1024
  92. disklist = append(disklist, *diskDetail)
  93. }
  94. }
  95. }
  96. e.Custom(gin.H{
  97. "code": 200,
  98. "os": osDic,
  99. "mem": memDic,
  100. "cpu": cpuDic,
  101. "disk": diskDic,
  102. "diskList": disklist,
  103. })
  104. }