smart.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package smart
  2. /*
  3. DISK SMART Service Listener
  4. Original author: alanyeung
  5. Rewritten by tobychui in Oct 2020 for system arch upgrade
  6. This module is not the core part of aroz online system.
  7. If you want to remove disk smart handler (e.g. running in VM?)
  8. remove the corrisponding code in disk.go
  9. */
  10. import (
  11. "encoding/json"
  12. "log"
  13. "net/http"
  14. "strconv"
  15. "strings"
  16. //"os/exec"
  17. "errors"
  18. "runtime"
  19. "imuslab.com/arozos/mod/utils"
  20. //"time"
  21. )
  22. type SMARTListener struct {
  23. SystemSmartExecutable string
  24. DriveList DevicesList `json:"driveList"`
  25. }
  26. // DiskSmartInit Desktop script initiation
  27. func NewSmartListener() (*SMARTListener, error) {
  28. smartExec := getBinary()
  29. log.Println("Starting SMART mointoring")
  30. if smartExec == "" {
  31. return &SMARTListener{}, errors.New("not supported platform")
  32. }
  33. if !(utils.FileExists(smartExec)) {
  34. return &SMARTListener{}, errors.New("smartctl not found")
  35. }
  36. driveList := scanAvailableDevices(smartExec)
  37. readSMARTDevices(smartExec, &driveList)
  38. fillHealthyStatus(&driveList)
  39. fillCapacity(&driveList)
  40. return &SMARTListener{
  41. SystemSmartExecutable: smartExec,
  42. DriveList: driveList,
  43. }, nil
  44. }
  45. //this function used for fetch available devices by using smartctl
  46. func scanAvailableDevices(smartExec string) DevicesList {
  47. rawInfo := execCommand(smartExec, "--scan", "--json=c")
  48. devicesList := new(DevicesList)
  49. json.Unmarshal([]byte(rawInfo), &devicesList)
  50. //used to remove csmi devices (Intel RAID Devices)
  51. numOfRemoved := 0
  52. for i, device := range devicesList.Devices {
  53. if strings.Contains(device.Name, "/dev/csmi") {
  54. devicesList.Devices = append(devicesList.Devices[:i-numOfRemoved], devicesList.Devices[i+1-numOfRemoved:]...)
  55. numOfRemoved++
  56. }
  57. }
  58. return *devicesList
  59. }
  60. //this function used for merge SMART Information into devicesList
  61. func readSMARTDevices(smartExec string, devicesList *DevicesList) {
  62. for i, device := range devicesList.Devices {
  63. rawInfo := execCommand(smartExec, device.Name, "--info", "--all", "--json=c")
  64. deviceSMART := new(DeviceSMART)
  65. json.Unmarshal([]byte(rawInfo), &deviceSMART)
  66. devicesList.Devices[i].Smart = *deviceSMART
  67. }
  68. }
  69. //used for fill the healthy status to the array
  70. func fillHealthyStatus(devicesList *DevicesList) {
  71. devicesList.Healthy = "Normal"
  72. for i, device := range devicesList.Devices {
  73. for j, smartTableElement := range device.Smart.AtaSmartAttributes.Table {
  74. devicesList.Devices[i].Smart.Healthy = "Normal"
  75. devicesList.Devices[i].Smart.AtaSmartAttributes.Table[j].Healthy = "Normal"
  76. if smartTableElement.WhenFailed == "FAILING_NOW" {
  77. devicesList.Devices[i].Smart.AtaSmartAttributes.Table[j].Healthy = "Failing"
  78. devicesList.Devices[i].Smart.Healthy = "Failing"
  79. devicesList.Healthy = "Failing"
  80. break
  81. }
  82. if smartTableElement.WhenFailed == "In_the_past" {
  83. devicesList.Devices[i].Smart.AtaSmartAttributes.Table[j].Healthy = "Attention"
  84. devicesList.Devices[i].Smart.Healthy = "Attention"
  85. devicesList.Healthy = "Attention"
  86. break
  87. }
  88. }
  89. }
  90. }
  91. //fill the capacity if windows
  92. func fillCapacity(devicesList *DevicesList) {
  93. if runtime.GOOS == "windows" {
  94. DiskNames := wmicGetinfo("diskdrive", "Model")
  95. DiskSizes := wmicGetinfo("diskdrive", "Size")
  96. for i, device := range devicesList.Devices {
  97. for j := range DiskNames {
  98. //since Intel driver will alter drive name to "XXXX SCSI Disk Device"
  99. //so remove the string to increase the match probability
  100. DiskNames[j] = strings.ReplaceAll(DiskNames[j], " SCSI Disk Device", "")
  101. //if the name match && capacity == 0
  102. if device.Smart.ModelName == DiskNames[j] && devicesList.Devices[i].Smart.UserCapacity.Bytes == 0 {
  103. capacity, _ := strconv.ParseInt(DiskSizes[j], 10, 64)
  104. devicesList.Devices[i].Smart.UserCapacity.Bytes = capacity
  105. }
  106. }
  107. }
  108. }
  109. }
  110. func (s *SMARTListener) GetSMART(w http.ResponseWriter, r *http.Request) {
  111. jsonText, _ := json.Marshal(s.DriveList)
  112. utils.SendJSONResponse(w, string(jsonText))
  113. }
  114. func getBinary() string {
  115. if runtime.GOOS == "windows" {
  116. return ".\\system\\disk\\smart\\win\\smartctl.exe"
  117. } else if runtime.GOOS == "linux" {
  118. if runtime.GOARCH == "arm" {
  119. return "./system/disk/smart/linux/smartctl_armv6"
  120. }
  121. if runtime.GOARCH == "arm64" {
  122. return "./system/disk/smart/linux/smartctl_arm64"
  123. }
  124. if runtime.GOARCH == "386" || runtime.GOARCH == "amd64" {
  125. return "./system/disk/smart/linux/smartctl_i386"
  126. }
  127. }
  128. return ""
  129. }