Răsfoiți Sursa

Updated SMART

If server is using windows, the smartctl will attempt using wmic to obtain capacity
Alanyeung's Windows Laptop 4 ani în urmă
părinte
comite
505c73d537
2 a modificat fișierele cu 57 adăugiri și 0 ștergeri
  1. 36 0
      mod/disk/smart/common.go
  2. 21 0
      mod/disk/smart/smart.go

+ 36 - 0
mod/disk/smart/common.go

@@ -205,3 +205,39 @@ func execCommand(executable string, args ...string) string {
 
 	return string(output)
 }
+
+func wmicGetinfo(wmicName string, itemName string) []string {
+	//get systeminfo
+	var InfoStorage []string
+
+	cmd := exec.Command("chcp", "65001")
+
+	cmd = exec.Command("wmic", wmicName, "list", "full", "/format:list")
+	if wmicName == "os" {
+		cmd = exec.Command("wmic", wmicName, "get", "*", "/format:list")
+	}
+
+	if len(wmicName) > 6 {
+		if wmicName[0:6] == "Win32_" {
+			cmd = exec.Command("wmic", "path", wmicName, "get", "*", "/format:list")
+		}
+	}
+	out, _ := cmd.CombinedOutput()
+	strOut := string(out)
+
+	strSplitedOut := strings.Split(strOut, "\n")
+	for _, strConfig := range strSplitedOut {
+		if strings.Contains(strConfig, "=") {
+			strSplitedConfig := strings.SplitN(strConfig, "=", 2)
+			if strSplitedConfig[0] == itemName {
+				strSplitedConfigReplaced := strings.Replace(strSplitedConfig[1], "\r", "", -1)
+				InfoStorage = append(InfoStorage, strSplitedConfigReplaced)
+			}
+		}
+
+	}
+	if len(InfoStorage) == 0 {
+		InfoStorage = append(InfoStorage, "Undefined")
+	}
+	return InfoStorage
+}

+ 21 - 0
mod/disk/smart/smart.go

@@ -14,6 +14,7 @@ import (
 	"encoding/json"
 	"log"
 	"net/http"
+	"strconv"
 	"strings"
 
 	//"os/exec"
@@ -44,12 +45,14 @@ func NewSmartListener() (*SMARTListener, error) {
 	driveList := scanAvailableDevices(smartExec)
 	readSMARTDevices(smartExec, &driveList)
 	fillHealthyStatus(&driveList)
+	fillCapacity(&driveList)
 	return &SMARTListener{
 		SystemSmartExecutable: smartExec,
 		DriveList:             driveList,
 	}, nil
 }
 
+//this function used for fetch available devices by using smartctl
 func scanAvailableDevices(smartExec string) DevicesList {
 	rawInfo := execCommand(smartExec, "--scan", "--json=c")
 	devicesList := new(DevicesList)
@@ -65,6 +68,7 @@ func scanAvailableDevices(smartExec string) DevicesList {
 	return *devicesList
 }
 
+//this function used for merge SMART Information into devicesList
 func readSMARTDevices(smartExec string, devicesList *DevicesList) {
 	for i, device := range devicesList.Devices {
 		rawInfo := execCommand(smartExec, device.Name, "--info", "--all", "--json=c")
@@ -74,6 +78,7 @@ func readSMARTDevices(smartExec string, devicesList *DevicesList) {
 	}
 }
 
+//used for fill the healthy status to the array
 func fillHealthyStatus(devicesList *DevicesList) {
 	devicesList.Healthy = "Normal"
 	for i, device := range devicesList.Devices {
@@ -96,6 +101,22 @@ func fillHealthyStatus(devicesList *DevicesList) {
 	}
 }
 
+//fill the capacity if windows
+func fillCapacity(devicesList *DevicesList) {
+	if runtime.GOOS == "windows" {
+		DiskNames := wmicGetinfo("diskdrive", "Model")
+		DiskSizes := wmicGetinfo("diskdrive", "Size")
+		for i, device := range devicesList.Devices {
+			for j := range DiskNames {
+				if device.Smart.ModelName == DiskNames[j] && devicesList.Devices[i].Smart.UserCapacity.Bytes == 0 {
+					capacity, _ := strconv.ParseInt(DiskSizes[j], 10, 64)
+					devicesList.Devices[i].Smart.UserCapacity.Bytes = capacity
+				}
+			}
+		}
+	}
+}
+
 func (s *SMARTListener) GetSMART(w http.ResponseWriter, r *http.Request) {
 	jsonText, _ := json.Marshal(s.DriveList)
 	sendJSONResponse(w, string(jsonText))