smart.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. //"os/exec"
  15. "runtime"
  16. "errors"
  17. "time"
  18. )
  19. // SMART was used for storing all Devices data
  20. type SMART struct {
  21. Port string `json:"Port"`
  22. DriveSmart *DeviceSMART `json:"SMART"`
  23. }
  24. type SMARTListener struct{
  25. SystemSmartExecutable string
  26. LastScanTime int64
  27. SMARTInformation []**SMART
  28. ReadingInProgress bool
  29. }
  30. // DiskSmartInit Desktop script initiation
  31. func NewSmartListener() (*SMARTListener, error){
  32. SystemSmartExecutable := getBinary()
  33. log.Println("Starting SMART mointoring")
  34. if (SystemSmartExecutable == "") {
  35. return &SMARTListener{}, errors.New("Not supported platform")
  36. }
  37. if !(fileExists(SystemSmartExecutable)) {
  38. return &SMARTListener{}, errors.New("Smartctl not found")
  39. }
  40. devicesList := scanAvailableDevices(SystemSmartExecutable)
  41. devicesSMART := readSMARTDevices(SystemSmartExecutable,devicesList)
  42. return &SMARTListener{
  43. SystemSmartExecutable: SystemSmartExecutable,
  44. LastScanTime: time.Now().Unix(),
  45. SMARTInformation: devicesSMART,
  46. },nil
  47. }
  48. func scanAvailableDevices(SystemSmartExecutable string) *DevicesList{
  49. rawInfo := execCommand(SystemSmartExecutable, "--scan", "--json=c")
  50. Devices := new(DevicesList)
  51. json.Unmarshal([]byte(rawInfo), &Devices)
  52. return Devices
  53. }
  54. func readSMARTDevices(SystemSmartExecutable string, devicesList *DevicesList) []**SMART{
  55. SMARTInfo := []**SMART{}
  56. for _, device := range devicesList.Devices {
  57. rawInfo := execCommand(SystemSmartExecutable, "-i", device.Name, "-a", "--json=c")
  58. deviceSMART := new(SMART)
  59. json.Unmarshal([]byte(rawInfo), &deviceSMART)
  60. SMARTInfo = append(SMARTInfo, &deviceSMART)
  61. }
  62. return SMARTInfo
  63. }
  64. func getBinary() string{
  65. if runtime.GOOS == "windows" {
  66. return "./system/disk/smart/win/smartctl.exe"
  67. } else if runtime.GOOS == "linux" {
  68. if runtime.GOARCH == "arm" {
  69. return "./system/disk/smart/linux/smartctl_armv6"
  70. }
  71. if runtime.GOARCH == "arm64" {
  72. return "./system/disk/smart/linux/smartctl_armv6"
  73. }
  74. if runtime.GOARCH == "386" {
  75. return "./system/disk/smart/linux/smartctl_i386"
  76. }
  77. if runtime.GOARCH == "amd64" {
  78. return "./system/disk/smart/linux/smartctl_i386"
  79. }
  80. }
  81. return ""
  82. }
  83. func (s *SMARTListener)CheckDiskTable(w http.ResponseWriter, r *http.Request) {
  84. sendJSONResponse(w, string(""))
  85. }
  86. func (s *SMARTListener)CheckDiskTestStatus(w http.ResponseWriter, r *http.Request) {
  87. sendJSONResponse(w, string(""))
  88. }
  89. func (s *SMARTListener)GetSMART(w http.ResponseWriter, r *http.Request) {
  90. sendJSONResponse(w, string(""))
  91. }