ソースを参照

Added supported disk type detections

Toby Chui 1 ヶ月 前
コミット
6029f4d7f4
2 ファイル変更43 行追加15 行削除
  1. 4 0
      main.go
  2. 39 15
      mod/middleware/SMART/smart.go

+ 4 - 0
main.go

@@ -27,6 +27,10 @@ func main() {
 			fullPath := "/dev/" + deviceFile.Name()
 			fmt.Println(fullPath)
 
+			if !smart.IsNVMeDevice(fullPath) && !smart.IsSATADevice(fullPath) {
+				fmt.Println("Unsupported disk type")
+				continue
+			}
 			//Get the SMART data printout in json
 			smartdata, err := smart.GetSMARTData(fullPath)
 			if err != nil {

+ 39 - 15
mod/middleware/SMART/smart.go

@@ -27,11 +27,11 @@ type SMARTData struct {
 	Temperature  int
 
 	/* NVME specific fields */
-	NVMENameSpace   []smart.NvmeIdentNamespace
-	PowerOnHours    uint64
-	PowerCycles     uint64
-	UnsafeShutdowns uint64
-	MediaErrors     uint64
+	NameSpaceUtilizations []uint64
+	PowerOnHours          uint64
+	PowerCycles           uint64
+	UnsafeShutdowns       uint64
+	MediaErrors           uint64
 
 	/* SATA specific fields */
 	SATAAttrs []*SATAAttrData
@@ -66,6 +66,26 @@ func GetSMARTData(disk string) (*SMARTData, error) {
 	return nil, errors.New("unsupported disk type")
 }
 
+// Check if the disk is a SATA device
+func IsSATADevice(disk string) bool {
+	if !strings.HasPrefix(disk, "/dev/") {
+		disk = filepath.Join("/dev", disk)
+	}
+
+	_, err := smart.OpenSata(disk)
+	return err == nil
+}
+
+// Check if the disk is a NVMe device
+func IsNVMeDevice(disk string) bool {
+	if !strings.HasPrefix(disk, "/dev/") {
+		disk = filepath.Join("/dev", disk)
+	}
+
+	_, err := smart.OpenNVMe(disk)
+	return err == nil
+}
+
 func getSATASMART(diskpath string) (*SMARTData, error) {
 	dev, err := smart.OpenSata(diskpath)
 	if err != nil {
@@ -131,18 +151,22 @@ func getNVMESMART(diskpath string) (*SMARTData, error) {
 		return nil, err
 	}
 
+	NameSpaceUtilizations := []uint64{}
+	for _, ns := range nss {
+		NameSpaceUtilizations = append(NameSpaceUtilizations, ns.Nuse*ns.LbaSize())
+	}
+
 	sm, _ := dev.ReadSMART()
 	smartData := SMARTData{
-		ModelNumber:     c.ModelNumber(),
-		SerialNumber:    c.SerialNumber(),
-		Size:            c.Tnvmcap.Val[0],
-		Temperature:     int(sm.Temperature),
-		PowerOnHours:    sm.PowerOnHours.Val[0],
-		PowerCycles:     sm.PowerCycles.Val[0],
-		UnsafeShutdowns: sm.UnsafeShutdowns.Val[0],
-		MediaErrors:     sm.MediaErrors.Val[0],
-
-		NVMENameSpace: nss,
+		ModelNumber:           c.ModelNumber(),
+		SerialNumber:          c.SerialNumber(),
+		Size:                  c.Tnvmcap.Val[0],
+		NameSpaceUtilizations: NameSpaceUtilizations,
+		Temperature:           int(sm.Temperature),
+		PowerOnHours:          sm.PowerOnHours.Val[0],
+		PowerCycles:           sm.PowerCycles.Val[0],
+		UnsafeShutdowns:       sm.UnsafeShutdowns.Val[0],
+		MediaErrors:           sm.MediaErrors.Val[0],
 	}
 
 	return &smartData, nil