sysinfo_freebsd.go 6.3 KB

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