system.info.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "runtime"
  7. "time"
  8. info "imuslab.com/arozos/mod/info/hardwareinfo"
  9. usage "imuslab.com/arozos/mod/info/usageinfo"
  10. prout "imuslab.com/arozos/mod/prouter"
  11. )
  12. //InitShowSysInformation xxx
  13. func SystemInfoInit() {
  14. log.Println("Operation System: " + runtime.GOOS)
  15. log.Println("System Architecture: " + runtime.GOARCH)
  16. //Updates 5 Dec 2020, Added permission router
  17. router := prout.NewModuleRouter(prout.RouterOption{
  18. AdminOnly: false,
  19. UserHandler: userHandler,
  20. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  21. sendErrorResponse(w, "Permission Denied")
  22. },
  23. })
  24. //Create Info Server Object
  25. var infoServer *info.Server = nil
  26. if *allow_hardware_management {
  27. infoServer = info.NewInfoServer(info.ArOZInfo{
  28. BuildVersion: build_version + "." + internal_version,
  29. DeviceVendor: deviceVendor,
  30. DeviceModel: deviceModel,
  31. VendorIcon: "../../" + iconVendor,
  32. SN: deviceUUID,
  33. HostOS: runtime.GOOS,
  34. CPUArch: runtime.GOARCH,
  35. HostName: *host_name,
  36. })
  37. router.HandleFunc("/system/info/getCPUinfo", info.GetCPUInfo)
  38. router.HandleFunc("/system/info/ifconfig", info.Ifconfig)
  39. router.HandleFunc("/system/info/getDriveStat", info.GetDriveStat)
  40. router.HandleFunc("/system/info/usbPorts", info.GetUSB)
  41. router.HandleFunc("/system/info/getRAMinfo", info.GetRamInfo)
  42. //Register as a system setting
  43. registerSetting(settingModule{
  44. Name: "Host Info",
  45. Desc: "System Information",
  46. IconPath: "SystemAO/info/img/small_icon.png",
  47. Group: "Info",
  48. StartDir: "SystemAO/info/index.html",
  49. })
  50. /*
  51. CPU and RAM usage interface
  52. */
  53. registerSetting(settingModule{
  54. Name: "Performance",
  55. Desc: "System CPU and RAM usage",
  56. IconPath: "SystemAO/info/img/small_icon.png",
  57. Group: "Info",
  58. StartDir: "SystemAO/info/taskManagerFrame.html",
  59. })
  60. router.HandleFunc("/system/info/getUsageInfo", InfoHandleTaskInfo)
  61. } else {
  62. //Make a simpler page for the information of system for hardware management disabled nodes
  63. registerSetting(settingModule{
  64. Name: "Overview",
  65. Desc: "Overview for user information",
  66. IconPath: "SystemAO/info/img/small_icon.png",
  67. Group: "Info",
  68. StartDir: "SystemAO/info/overview.html",
  69. })
  70. //Remve hardware information from the infoServer
  71. infoServer = info.NewInfoServer(info.ArOZInfo{
  72. BuildVersion: build_version + "." + internal_version,
  73. DeviceVendor: deviceVendor,
  74. DeviceModel: deviceModel,
  75. VendorIcon: "../../" + iconVendor,
  76. SN: deviceUUID,
  77. HostOS: "virtualized",
  78. CPUArch: "generic",
  79. HostName: *host_name,
  80. })
  81. }
  82. //Register endpoints that do not involve hardware management
  83. router.HandleFunc("/system/info/getRuntimeInfo", InfoHandleGetRuntimeInfo)
  84. //ArOZ Info do not need permission router
  85. http.HandleFunc("/system/info/getArOZInfo", infoServer.GetArOZInfo)
  86. }
  87. func InfoHandleGetRuntimeInfo(w http.ResponseWriter, r *http.Request) {
  88. type RuntimeInfo struct {
  89. StartupTime int64
  90. ContinuesRuntime int64
  91. }
  92. runtimeInfo := RuntimeInfo{
  93. StartupTime: startupTime,
  94. ContinuesRuntime: time.Now().Unix() - startupTime,
  95. }
  96. js, _ := json.Marshal(runtimeInfo)
  97. sendJSONResponse(w, string(js))
  98. }
  99. func InfoHandleTaskInfo(w http.ResponseWriter, r *http.Request) {
  100. type UsageInfo struct {
  101. CPU float64
  102. UsedRAM string
  103. TotalRam string
  104. RamUsage float64
  105. }
  106. cpuUsage := usage.GetCPUUsage()
  107. usedRam, totalRam, usagePercentage := usage.GetRAMUsage()
  108. info := UsageInfo{
  109. cpuUsage,
  110. usedRam,
  111. totalRam,
  112. usagePercentage,
  113. }
  114. js, _ := json.Marshal(info)
  115. sendJSONResponse(w, string(js))
  116. }