sysinfo_darwin.go 6.6 KB

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