package smart /* DISK SMART Service Listener Original author: alanyeung Rewritten by tobychui in Oct 2020 for system arch upgrade This module is not the core part of aroz online system. If you want to remove disk smart handler (e.g. running in VM?) remove the corrisponding code in disk.go */ import ( "encoding/json" "log" "net/http" //"os/exec" "runtime" "errors" "time" ) // SMART was used for storing all Devices data type SMART struct { Port string `json:"Port"` DriveSmart *DeviceSMART `json:"SMART"` } type SMARTListener struct{ SystemSmartExecutable string LastScanTime int64 SMARTInformation []**SMART ReadingInProgress bool } // DiskSmartInit Desktop script initiation func NewSmartListener() (*SMARTListener, error){ SystemSmartExecutable := getBinary() log.Println("Starting SMART mointoring") if (SystemSmartExecutable == "") { return &SMARTListener{}, errors.New("Not supported platform") } if !(fileExists(SystemSmartExecutable)) { return &SMARTListener{}, errors.New("Smartctl not found") } devicesList := scanAvailableDevices(SystemSmartExecutable) devicesSMART := readSMARTDevices(SystemSmartExecutable,devicesList) return &SMARTListener{ SystemSmartExecutable: SystemSmartExecutable, LastScanTime: time.Now().Unix(), SMARTInformation: devicesSMART, },nil } func scanAvailableDevices(SystemSmartExecutable string) *DevicesList{ rawInfo := execCommand(SystemSmartExecutable, "--scan", "--json=c") Devices := new(DevicesList) json.Unmarshal([]byte(rawInfo), &Devices) return Devices } func readSMARTDevices(SystemSmartExecutable string, devicesList *DevicesList) []**SMART{ SMARTInfo := []**SMART{} for _, device := range devicesList.Devices { rawInfo := execCommand(SystemSmartExecutable, "-i", device.Name, "-a", "--json=c") deviceSMART := new(SMART) json.Unmarshal([]byte(rawInfo), &deviceSMART) SMARTInfo = append(SMARTInfo, &deviceSMART) } return SMARTInfo } func getBinary() string{ if runtime.GOOS == "windows" { return "./system/disk/smart/win/smartctl.exe" } else if runtime.GOOS == "linux" { if runtime.GOARCH == "arm" { return "./system/disk/smart/linux/smartctl_armv6" } if runtime.GOARCH == "arm64" { return "./system/disk/smart/linux/smartctl_armv6" } if runtime.GOARCH == "386" { return "./system/disk/smart/linux/smartctl_i386" } if runtime.GOARCH == "amd64" { return "./system/disk/smart/linux/smartctl_i386" } } return "" } func (s *SMARTListener)CheckDiskTable(w http.ResponseWriter, r *http.Request) { sendJSONResponse(w, string("")) } func (s *SMARTListener)CheckDiskTestStatus(w http.ResponseWriter, r *http.Request) { sendJSONResponse(w, string("")) } func (s *SMARTListener)GetSMART(w http.ResponseWriter, r *http.Request) { sendJSONResponse(w, string("")) }