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 DriveList DevicesList `json:"driveList"` SMARTInformation []SMART `json:"smartInformation"` } // DiskSmartInit Desktop script initiation func NewSmartListener() (*SMARTListener, error){ smartExec := getBinary() log.Println("Starting SMART mointoring") if (smartExec == "") { return &SMARTListener{}, errors.New("Not supported platform") } if !(fileExists(smartExec)) { return &SMARTListener{}, errors.New("Smartctl not found") } driveList := scanAvailableDevices(smartExec); smartInformation := readSMARTDevices(smartExec,driveList); return &SMARTListener{ SystemSmartExecutable: smartExec, DriveList: driveList, SMARTInformation: smartInformation, },nil } func scanAvailableDevices(smartExec string) DevicesList{ rawInfo := execCommand(smartExec, "--scan", "--json=c") Devices := new(DevicesList) json.Unmarshal([]byte(rawInfo), &Devices) return *Devices } func readSMARTDevices(smartExec string, devicesList DevicesList) []SMART{ SMARTInfo := []SMART{} for _, device := range devicesList.Devices { rawInfo := execCommand(smartExec, "-i", device.Name, "-a", "--json=c") deviceSMART := new(SMART) json.Unmarshal([]byte(rawInfo), &deviceSMART) SMARTInfo = append(SMARTInfo, *deviceSMART) } return SMARTInfo } 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("")) } func getBinary() string{ if runtime.GOOS == "windows" { return "./system/disk/smart/win/smartctl.exe" } else if runtime.GOOS == "linux" { if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" { return "./system/disk/smart/linux/smartctl_armv6" } if runtime.GOARCH == "386" || runtime.GOARCH == "amd64"{ return "./system/disk/smart/linux/smartctl_i386" } } return "" }