api_set.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package admin
  2. import (
  3. "fmt"
  4. "net/http"
  5. "runtime"
  6. "github.com/bjdgyc/anylink/base"
  7. "github.com/bjdgyc/anylink/dbdata"
  8. "github.com/bjdgyc/anylink/pkg/utils"
  9. "github.com/bjdgyc/anylink/sessdata"
  10. "github.com/shirou/gopsutil/cpu"
  11. "github.com/shirou/gopsutil/disk"
  12. "github.com/shirou/gopsutil/host"
  13. "github.com/shirou/gopsutil/load"
  14. "github.com/shirou/gopsutil/mem"
  15. )
  16. func SetHome(w http.ResponseWriter, r *http.Request) {
  17. data := make(map[string]interface{})
  18. sess := sessdata.OnlineSess()
  19. data["counts"] = map[string]int{
  20. "online": len(sess),
  21. "user": dbdata.CountAll(&dbdata.User{}),
  22. "group": dbdata.CountAll(&dbdata.Group{}),
  23. "ip_map": dbdata.CountAll(&dbdata.IpMap{}),
  24. }
  25. RespSucess(w, data)
  26. }
  27. func SetSystem(w http.ResponseWriter, r *http.Request) {
  28. data := make(map[string]interface{})
  29. m, _ := mem.VirtualMemory()
  30. data["mem"] = map[string]interface{}{
  31. "total": utils.HumanByte(m.Total),
  32. "free": utils.HumanByte(m.Free),
  33. "percent": decimal(m.UsedPercent),
  34. }
  35. d, _ := disk.Usage("/")
  36. data["disk"] = map[string]interface{}{
  37. "total": utils.HumanByte(d.Total),
  38. "free": utils.HumanByte(d.Free),
  39. "percent": decimal(d.UsedPercent),
  40. }
  41. cc, _ := cpu.Counts(true)
  42. c, _ := cpu.Info()
  43. ci := c[0]
  44. cpuUsedPercent, _ := cpu.Percent(0, false)
  45. cup := cpuUsedPercent[0]
  46. if cup == 0 {
  47. cup = 1
  48. }
  49. data["cpu"] = map[string]interface{}{
  50. "core": cc,
  51. "modelName": ci.ModelName,
  52. "ghz": fmt.Sprintf("%.2f GHz", ci.Mhz/1000),
  53. "percent": decimal(cup),
  54. }
  55. hi, _ := host.Info()
  56. l, _ := load.Avg()
  57. data["sys"] = map[string]interface{}{
  58. "goOs": runtime.GOOS,
  59. "goArch": runtime.GOARCH,
  60. "goVersion": runtime.Version(),
  61. "goroutine": runtime.NumGoroutine(),
  62. "appVersion": "v" + base.APP_VER,
  63. "appCommitId": base.CommitId,
  64. "hostname": hi.Hostname,
  65. "platform": fmt.Sprintf("%v %v %v", hi.Platform, hi.PlatformFamily, hi.PlatformVersion),
  66. "kernel": hi.KernelVersion,
  67. "load": fmt.Sprint(l.Load1, l.Load5, l.Load15),
  68. }
  69. RespSucess(w, data)
  70. }
  71. func SetSoft(w http.ResponseWriter, r *http.Request) {
  72. data := base.ServerCfg2Slice()
  73. RespSucess(w, data)
  74. }
  75. func decimal(f float64) float64 {
  76. i := int(f * 100)
  77. return float64(i) / 100
  78. }