server.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package monitor
  2. import (
  3. "runtime"
  4. "github.com/gin-gonic/gin"
  5. "github.com/shirou/gopsutil/cpu"
  6. "github.com/shirou/gopsutil/disk"
  7. "github.com/shirou/gopsutil/mem"
  8. "device-manage/tools"
  9. "device-manage/tools/app"
  10. )
  11. const (
  12. B = 1
  13. KB = 1024 * B
  14. MB = 1024 * KB
  15. GB = 1024 * MB
  16. )
  17. // @Summary 系统信息
  18. // @Description 获取JSON
  19. // @Tags 系统信息
  20. // @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
  21. // @Router /api/v1/settings/serverInfo [get]
  22. func ServerInfo(c *gin.Context) {
  23. osDic := make(map[string]interface{}, 0)
  24. osDic["goOs"] = runtime.GOOS
  25. osDic["arch"] = runtime.GOARCH
  26. osDic["mem"] = runtime.MemProfileRate
  27. osDic["compiler"] = runtime.Compiler
  28. osDic["version"] = runtime.Version()
  29. osDic["numGoroutine"] = runtime.NumGoroutine()
  30. osDic["ip"] = tools.GetLocaHonst()
  31. osDic["projectDir"] = tools.GetCurrentPath()
  32. dis, _ := disk.Usage("/")
  33. diskTotalGB := int(dis.Total) / GB
  34. diskFreeGB := int(dis.Free) / GB
  35. diskDic := make(map[string]interface{}, 0)
  36. diskDic["total"] = diskTotalGB
  37. diskDic["free"] = diskFreeGB
  38. mem, _ := mem.VirtualMemory()
  39. memUsedMB := int(mem.Used) / GB
  40. memTotalMB := int(mem.Total) / GB
  41. memFreeMB := int(mem.Free) / GB
  42. memUsedPercent := int(mem.UsedPercent)
  43. memDic := make(map[string]interface{}, 0)
  44. memDic["total"] = memTotalMB
  45. memDic["used"] = memUsedMB
  46. memDic["free"] = memFreeMB
  47. memDic["usage"] = memUsedPercent
  48. cpuDic := make(map[string]interface{}, 0)
  49. cpuDic["cpuInfo"], _ = cpu.Info()
  50. percent, _ := cpu.Percent(0, false)
  51. cpuDic["Percent"] = tools.Round(percent[0], 2)
  52. cpuDic["cpuNum"], _ = cpu.Counts(false)
  53. app.Custum(c, gin.H{
  54. "code": 200,
  55. "os": osDic,
  56. "mem": memDic,
  57. "cpu": cpuDic,
  58. "disk": diskDic,
  59. })
  60. }