hardwareinfo.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package hardwareinfo
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "os/exec"
  7. "strings"
  8. )
  9. /*
  10. Hardware Info
  11. author: tobychui
  12. This module is a migrated module from the original system.info.go script
  13. */
  14. type CPUInfo struct {
  15. Model string
  16. Freq string
  17. Instruction string
  18. Hardware string
  19. Revision string
  20. }
  21. type LogicalDisk struct {
  22. DriveLetter string
  23. FileSystem string
  24. FreeSpace string
  25. }
  26. type ArOZInfo struct {
  27. BuildVersion string
  28. DeviceVendor string
  29. DeviceModel string
  30. VendorIcon string
  31. SN string
  32. HostOS string
  33. CPUArch string
  34. HostName string
  35. }
  36. type Server struct {
  37. hostInfo ArOZInfo
  38. }
  39. func NewInfoServer(a ArOZInfo) *Server {
  40. return &Server{
  41. hostInfo: a,
  42. }
  43. }
  44. /*
  45. PrintSystemHardwareDebugMessage print system information on Windows.
  46. Which is lagging but helpful for debugging wmic on Windows
  47. */
  48. func PrintSystemHardwareDebugMessage() {
  49. log.Println("Windows Version: " + wmicGetinfo("os", "Caption")[0])
  50. log.Println("Total Memory: " + wmicGetinfo("ComputerSystem", "TotalPhysicalMemory")[0] + "B")
  51. log.Println("Processor: " + wmicGetinfo("cpu", "Name")[0])
  52. log.Println("Following disk was detected:")
  53. for _, info := range wmicGetinfo("diskdrive", "Model") {
  54. log.Println(info)
  55. }
  56. }
  57. func (s *Server) GetArOZInfo(w http.ResponseWriter, r *http.Request) {
  58. var jsonData []byte
  59. jsonData, err := json.Marshal(s.hostInfo)
  60. if err != nil {
  61. log.Println(err)
  62. }
  63. sendJSONResponse(w, string(jsonData))
  64. }
  65. func wmicGetinfo(wmicName string, itemName string) []string {
  66. //get systeminfo
  67. var InfoStorage []string
  68. cmd := exec.Command("chcp", "65001")
  69. cmd = exec.Command("wmic", wmicName, "list", "full", "/format:list")
  70. if wmicName == "os" {
  71. cmd = exec.Command("wmic", wmicName, "get", "*", "/format:list")
  72. }
  73. if len(wmicName) > 6 {
  74. if wmicName[0:6] == "Win32_" {
  75. cmd = exec.Command("wmic", "path", wmicName, "get", "*", "/format:list")
  76. }
  77. }
  78. out, _ := cmd.CombinedOutput()
  79. strOut := string(out)
  80. strSplitedOut := strings.Split(strOut, "\n")
  81. for _, strConfig := range strSplitedOut {
  82. if strings.Contains(strConfig, "=") {
  83. strSplitedConfig := strings.SplitN(strConfig, "=", 2)
  84. if strSplitedConfig[0] == itemName {
  85. strSplitedConfigReplaced := strings.Replace(strSplitedConfig[1], "\r", "", -1)
  86. InfoStorage = append(InfoStorage, strSplitedConfigReplaced)
  87. }
  88. }
  89. }
  90. if len(InfoStorage) == 0 {
  91. InfoStorage = append(InfoStorage, "Undefined")
  92. }
  93. return InfoStorage
  94. }
  95. func filterGrepResults(result string, sep string) string {
  96. if strings.Contains(result, sep) == false {
  97. return result
  98. }
  99. tmp := strings.Split(result, sep)
  100. resultString := tmp[1]
  101. return strings.TrimSpace(resultString)
  102. }