sysinfo_freebsd.go 6.3 KB

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