sysinfo_darwin.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //go:build darwin
  2. // +build darwin
  3. package hardwareinfo
  4. import (
  5. "encoding/json"
  6. "imuslab.com/arozos/mod/utils"
  7. "log"
  8. "net/http"
  9. "os/exec"
  10. "strconv"
  11. "strings"
  12. )
  13. /*
  14. System Info
  15. original author: HyperXraft
  16. modified by: Alanyeung
  17. original date: 2021-02-18
  18. modified by: 2021-07-25
  19. This module get the CPU information on different platform using
  20. native terminal commands on FreeBSD platform
  21. DEFINITIONS
  22. ===========
  23. CPUModel: Refers to the Marketing name of the CPU, e.g. Intel Xeon E7 8890
  24. CPUHardware: Refers to the CPUID name, e.g. GenuineIntel-6-3A-9
  25. CPUArch: Refers to the ISA of the CPU, e.g. aarch64
  26. CPUFreq: Refers to the CPU frequency in terms of gigahertz, e.g. 0.8GHz
  27. */
  28. //usbinfo from https://apple.stackexchange.com/questions/170105/list-usb-devices-on-osx-command-line
  29. const unknown_string = "??? "
  30. const query_frequency_command = "sysctl machdep.cpu.brand_string | awk '{print $NF}'"
  31. const query_cpumodel_command = "sysctl machdep.cpu.brand_string | awk '{for(i=1;++i<=NF-3;) printf $i\" \"; print $(NF-2)}'"
  32. const query_cpuarch_command = "sysctl hw.machine | awk '{print $NF}'"
  33. const query_cpuhardware_command = "sysctl machdep.cpu.stepping | awk '{print $NF}'"
  34. const query_netinfo_command = "networksetup -listallhardwareports"
  35. const query_usbinfo_command = "ioreg -p IOUSB -w0 | sed 's/[^o]*o //; s/@.*$//' | grep -v '^Root.*'"
  36. const query_memsize_command = "sysctl hw.memsize | awk '{print $NF}'"
  37. // GetCPUFreq() -> String
  38. // Returns the CPU frequency in the terms of MHz
  39. func GetCPUFreq() string {
  40. shell := exec.Command("bash", "-c", query_frequency_command) // Run command
  41. freqByteArr, err := shell.CombinedOutput() // Response from cmdline
  42. if err != nil { // If done w/ errors then
  43. log.Println(err)
  44. return unknown_string
  45. }
  46. freqStr := strings.ReplaceAll(string(freqByteArr), "GHz", "")
  47. freqStr = strings.ReplaceAll(freqStr, "\n", "")
  48. freqStr = strings.ReplaceAll(freqStr, " ", "")
  49. freqFloat, _ := strconv.ParseFloat(freqStr, 8)
  50. freqFloat = freqFloat * 1000
  51. freqStrMHz := strconv.FormatFloat(freqFloat, 'f', -1, 64)
  52. return freqStrMHz
  53. }
  54. // GetCPUModel -> String
  55. // Returns the CPU model name string
  56. func GetCPUModel() string {
  57. shell := exec.Command("bash", "-c", query_cpumodel_command) // Run command
  58. modelStr, err := shell.CombinedOutput() // Response from cmdline
  59. if err != nil { // If done w/ errors then
  60. log.Println(err)
  61. return unknown_string
  62. }
  63. return string(modelStr)
  64. }
  65. // GetCPUHardware -> String
  66. // Returns the CPU ID string
  67. func GetCPUHardware() string {
  68. shell := exec.Command("bash", "-c", query_cpuhardware_command) // Run command
  69. hwStr, err := shell.CombinedOutput() // Response from cmdline
  70. if err != nil { // If done w/ errors then
  71. log.Println(err)
  72. return unknown_string
  73. }
  74. return string(hwStr)
  75. }
  76. // GetCPUArch -> String
  77. // Returns the CPU architecture string
  78. func GetCPUArch() string {
  79. shell := exec.Command("bash", "-c", query_cpuarch_command) // Run command
  80. archStr, err := shell.CombinedOutput() // Response from cmdline
  81. if err != nil { // If done w/ errors then
  82. log.Println(err)
  83. return unknown_string
  84. }
  85. return string(archStr)
  86. }
  87. // Inherited code from sysinfo_window.go
  88. func GetCPUInfo(w http.ResponseWriter, r *http.Request) {
  89. CPUInfo := CPUInfo{
  90. Freq: GetCPUFreq(),
  91. Hardware: GetCPUHardware(),
  92. Instruction: GetCPUArch(),
  93. Model: GetCPUModel(),
  94. Revision: "unknown",
  95. }
  96. var jsonData []byte
  97. jsonData, err := json.Marshal(CPUInfo)
  98. if err != nil {
  99. log.Println(err)
  100. }
  101. utils.SendTextResponse(w, string(jsonData))
  102. }
  103. // Inherited code from sysinfo.go
  104. func Ifconfig(w http.ResponseWriter, r *http.Request) {
  105. cmdin := query_netinfo_command
  106. cmd := exec.Command("bash", "-c", cmdin)
  107. networkInterfaces, err := cmd.CombinedOutput()
  108. if err != nil {
  109. networkInterfaces = []byte{}
  110. }
  111. nic := strings.Split(string(networkInterfaces), "\n")
  112. var arr []string
  113. for _, info := range nic {
  114. thisInfo := string(info)
  115. arr = append(arr, thisInfo)
  116. }
  117. var jsonData []byte
  118. jsonData, err = json.Marshal(arr)
  119. if err != nil {
  120. log.Println(err)
  121. }
  122. utils.SendTextResponse(w, string(jsonData))
  123. }
  124. // Inherited code from sysinfo.go
  125. func GetDriveStat(w http.ResponseWriter, r *http.Request) {
  126. //Get drive status using df command
  127. cmdin := `df -k | sed -e /Filesystem/d`
  128. cmd := exec.Command("bash", "-c", cmdin)
  129. dev, err := cmd.CombinedOutput()
  130. if err != nil {
  131. dev = []byte{}
  132. }
  133. drives := strings.Split(string(dev), "\n")
  134. if len(drives) == 0 {
  135. utils.SendErrorResponse(w, "Invalid disk information")
  136. return
  137. }
  138. var arr []LogicalDisk
  139. for _, driveInfo := range drives {
  140. if driveInfo == "" {
  141. continue
  142. }
  143. for strings.Contains(driveInfo, " ") {
  144. driveInfo = strings.Replace(driveInfo, " ", " ", -1)
  145. }
  146. driveInfoChunk := strings.Split(driveInfo, " ")
  147. tmp, _ := strconv.Atoi(driveInfoChunk[3])
  148. freespaceInByte := int64(tmp)
  149. LogicalDisk := LogicalDisk{
  150. DriveLetter: driveInfoChunk[8],
  151. FileSystem: driveInfoChunk[0],
  152. FreeSpace: strconv.FormatInt(freespaceInByte*1024, 10), //df show disk space in 1KB blocks
  153. }
  154. arr = append(arr, LogicalDisk)
  155. }
  156. var jsonData []byte
  157. jsonData, err = json.Marshal(arr)
  158. if err != nil {
  159. log.Println(err)
  160. }
  161. utils.SendTextResponse(w, string(jsonData))
  162. }
  163. // GetUSB(ResponseWriter, HttpRequest) -> nil
  164. // Takes in http.ResponseWriter w and *http.Request r,
  165. // Send TextResponse containing USB information extracted from shell in JSON
  166. func GetUSB(w http.ResponseWriter, r *http.Request) {
  167. cmdin := query_usbinfo_command
  168. cmd := exec.Command("bash", "-c", cmdin)
  169. usbd, err := cmd.CombinedOutput()
  170. if err != nil {
  171. usbd = []byte{}
  172. }
  173. usbDrives := strings.Split(string(usbd), "\n")
  174. var arr []string
  175. for _, info := range usbDrives {
  176. arr = append(arr, info)
  177. }
  178. var jsonData []byte
  179. jsonData, err = json.Marshal(arr)
  180. if err != nil {
  181. log.Println(err)
  182. }
  183. utils.SendTextResponse(w, string(jsonData))
  184. }
  185. // GetRamInfo(w ResponseWriter, r *HttpRequest) -> nil
  186. // Takes in http.ResponseWriter w and *http.Request r,
  187. // Send TextResponse containing physical memory size
  188. // extracted from shell in JSON
  189. func GetRamInfo(w http.ResponseWriter, r *http.Request) {
  190. cmd := exec.Command("bash", "-c", query_memsize_command)
  191. out, _ := cmd.CombinedOutput()
  192. strOut := string(out)
  193. strOut = strings.ReplaceAll(strOut, "\n", "")
  194. ramSize, _ := strconv.ParseInt(strOut, 10, 64)
  195. ramSizeInt := ramSize
  196. var jsonData []byte
  197. jsonData, err := json.Marshal(ramSizeInt)
  198. if err != nil {
  199. log.Println(err)
  200. }
  201. utils.SendTextResponse(w, string(jsonData))
  202. }