smart.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "strings"
  15. //"os/exec"
  16. "errors"
  17. "runtime"
  18. //"time"
  19. )
  20. type SMARTListener struct {
  21. SystemSmartExecutable string
  22. DriveList DevicesList `json:"driveList"`
  23. }
  24. // DiskSmartInit Desktop script initiation
  25. func NewSmartListener() (*SMARTListener, error) {
  26. smartExec := getBinary()
  27. log.Println("Starting SMART mointoring")
  28. if smartExec == "" {
  29. return &SMARTListener{}, errors.New("not supported platform")
  30. }
  31. if !(fileExists(smartExec)) {
  32. return &SMARTListener{}, errors.New("smartctl not found")
  33. }
  34. driveList := scanAvailableDevices(smartExec)
  35. readSMARTDevices(smartExec, &driveList)
  36. return &SMARTListener{
  37. SystemSmartExecutable: smartExec,
  38. DriveList: driveList,
  39. }, nil
  40. }
  41. func scanAvailableDevices(smartExec string) DevicesList {
  42. rawInfo := execCommand(smartExec, "--scan", "--json=c")
  43. devicesList := new(DevicesList)
  44. json.Unmarshal([]byte(rawInfo), &devicesList)
  45. //used to remove csmi devices (Intel RAID Devices)
  46. numOfRemoved := 0
  47. for i, device := range devicesList.Devices {
  48. if strings.Contains(device.Name, "/dev/csmi") {
  49. devicesList.Devices = append(devicesList.Devices[:i-numOfRemoved], devicesList.Devices[i+1-numOfRemoved:]...)
  50. numOfRemoved++
  51. }
  52. }
  53. return *devicesList
  54. }
  55. func readSMARTDevices(smartExec string, devicesList *DevicesList) {
  56. for i, device := range devicesList.Devices {
  57. rawInfo := execCommand(smartExec, device.Name, "--info", "--all", "--json=c")
  58. deviceSMART := new(DeviceSMART)
  59. json.Unmarshal([]byte(rawInfo), &deviceSMART)
  60. devicesList.Devices[i].Smart = *deviceSMART
  61. }
  62. }
  63. func (s *SMARTListener) GetSMART(w http.ResponseWriter, r *http.Request) {
  64. jsonText, _ := json.Marshal(s.DriveList)
  65. sendJSONResponse(w, string(jsonText))
  66. }
  67. func getBinary() string {
  68. if runtime.GOOS == "windows" {
  69. return ".\\system\\disk\\smart\\win\\smartctl.exe"
  70. } else if runtime.GOOS == "linux" {
  71. if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
  72. return "./system/disk/smart/linux/smartctl_armv6"
  73. }
  74. if runtime.GOARCH == "386" || runtime.GOARCH == "amd64" {
  75. return "./system/disk/smart/linux/smartctl_i386"
  76. }
  77. }
  78. return ""
  79. }