12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 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"
- "strings"
- //"os/exec"
- "errors"
- "runtime"
- //"time"
- )
- type SMARTListener struct {
- SystemSmartExecutable string
- DriveList DevicesList `json:"driveList"`
- }
- // 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)
- readSMARTDevices(smartExec, &driveList)
- return &SMARTListener{
- SystemSmartExecutable: smartExec,
- DriveList: driveList,
- }, nil
- }
- func scanAvailableDevices(smartExec string) DevicesList {
- rawInfo := execCommand(smartExec, "--scan", "--json=c")
- devicesList := new(DevicesList)
- json.Unmarshal([]byte(rawInfo), &devicesList)
- //used to remove csmi devices (Intel RAID Devices)
- numOfRemoved := 0
- for i, device := range devicesList.Devices {
- if strings.Contains(device.Name, "/dev/csmi") {
- devicesList.Devices = append(devicesList.Devices[:i-numOfRemoved], devicesList.Devices[i+1-numOfRemoved:]...)
- numOfRemoved++
- }
- }
- return *devicesList
- }
- func readSMARTDevices(smartExec string, devicesList *DevicesList) {
- for i, device := range devicesList.Devices {
- rawInfo := execCommand(smartExec, device.Name, "--info", "--all", "--json=c")
- deviceSMART := new(DeviceSMART)
- json.Unmarshal([]byte(rawInfo), &deviceSMART)
- devicesList.Devices[i].Smart = *deviceSMART
- }
- }
- func (s *SMARTListener) GetSMART(w http.ResponseWriter, r *http.Request) {
- jsonText, _ := json.Marshal(s.DriveList)
- sendJSONResponse(w, string(jsonText))
- }
- 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 ""
- }
|