sysinfo_darwin.go 6.6 KB

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