smart.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. DriveList DevicesList `json:"driveList"`
  27. SMARTInformation []SMART `json:"smartInformation"`
  28. }
  29. // DiskSmartInit Desktop script initiation
  30. func NewSmartListener() (*SMARTListener, error){
  31. smartExec := getBinary()
  32. log.Println("Starting SMART mointoring")
  33. if (smartExec == "") {
  34. return &SMARTListener{}, errors.New("Not supported platform")
  35. }
  36. if !(fileExists(smartExec)) {
  37. return &SMARTListener{}, errors.New("Smartctl not found")
  38. }
  39. driveList := scanAvailableDevices(smartExec);
  40. smartInformation := readSMARTDevices(smartExec,driveList);
  41. return &SMARTListener{
  42. SystemSmartExecutable: smartExec,
  43. DriveList: driveList,
  44. SMARTInformation: smartInformation,
  45. },nil
  46. }
  47. func scanAvailableDevices(smartExec string) DevicesList{
  48. rawInfo := execCommand(smartExec, "--scan", "--json=c")
  49. Devices := new(DevicesList)
  50. json.Unmarshal([]byte(rawInfo), &Devices)
  51. return *Devices
  52. }
  53. func readSMARTDevices(smartExec string, devicesList DevicesList) []SMART{
  54. SMARTInfo := []SMART{}
  55. for _, device := range devicesList.Devices {
  56. rawInfo := execCommand(smartExec, "-i", device.Name, "-a", "--json=c")
  57. deviceSMART := new(SMART)
  58. json.Unmarshal([]byte(rawInfo), &deviceSMART)
  59. SMARTInfo = append(SMARTInfo, *deviceSMART)
  60. }
  61. return SMARTInfo
  62. }
  63. func (s *SMARTListener)CheckDiskTable(w http.ResponseWriter, r *http.Request) {
  64. sendJSONResponse(w, string(""))
  65. }
  66. func (s *SMARTListener)CheckDiskTestStatus(w http.ResponseWriter, r *http.Request) {
  67. sendJSONResponse(w, string(""))
  68. }
  69. func (s *SMARTListener)GetSMART(w http.ResponseWriter, r *http.Request) {
  70. sendJSONResponse(w, string(""))
  71. }
  72. func getBinary() string{
  73. if runtime.GOOS == "windows" {
  74. return "./system/disk/smart/win/smartctl.exe"
  75. } else if runtime.GOOS == "linux" {
  76. if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
  77. return "./system/disk/smart/linux/smartctl_armv6"
  78. }
  79. if runtime.GOARCH == "386" || runtime.GOARCH == "amd64"{
  80. return "./system/disk/smart/linux/smartctl_i386"
  81. }
  82. }
  83. return ""
  84. }