system.info.go 3.6 KB

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