mirror of
https://github.com/JustKato/drive-health.git
synced 2026-02-27 06:17:00 +02:00
Initial Commit
This commit is contained in:
62
lib/hardware/logic.go
Normal file
62
lib/hardware/logic.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package hardware
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetSystemHardDrives() ([]HardDrive, error) {
|
||||
|
||||
// Execute the lsblk command to get detailed block device information
|
||||
cmd := exec.Command("lsblk", "-d", "-o", "NAME,TRAN,SIZE,MODEL,SERIAL,TYPE")
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println("Failed to execute command:", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var hardDrives []HardDrive
|
||||
|
||||
// Scan the output line by line
|
||||
scanner := bufio.NewScanner(&out)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
// Skip the header line
|
||||
if strings.Contains(line, "NAME") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Split the line into columns
|
||||
cols := strings.Fields(line)
|
||||
if len(cols) < 6 {
|
||||
continue
|
||||
}
|
||||
|
||||
// Filter out nvme drives (M.2)
|
||||
if cols[1] != "nvme" {
|
||||
hd := HardDrive{
|
||||
Name: cols[0],
|
||||
Transport: cols[1],
|
||||
Size: cols[2],
|
||||
Model: cols[3],
|
||||
Serial: cols[4],
|
||||
Type: cols[5],
|
||||
Temperature: 0,
|
||||
}
|
||||
hardDrives = append(hardDrives, hd)
|
||||
}
|
||||
}
|
||||
|
||||
// Handle error
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return hardDrives, nil
|
||||
}
|
||||
46
lib/hardware/type.go
Normal file
46
lib/hardware/type.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package hardware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/anatol/smart.go"
|
||||
)
|
||||
|
||||
type HardDrive struct {
|
||||
Name string
|
||||
Transport string
|
||||
Size string
|
||||
Model string
|
||||
Serial string
|
||||
Type string
|
||||
Temperature int
|
||||
}
|
||||
|
||||
// Fetch the temperature of the device, optinally update the reference object
|
||||
func (h *HardDrive) GetTemperature(updateTemp bool) int {
|
||||
// Fetch the device by name
|
||||
disk, err := smart.Open("/dev/" + h.Name)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to open device %s: %s\n", h.Name, err)
|
||||
return -1
|
||||
}
|
||||
defer disk.Close()
|
||||
|
||||
// Fetch SMART data
|
||||
smartInfo, err := disk.ReadGenericAttributes()
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to get SMART data for %s: %s\n", h.Name, err)
|
||||
return -1
|
||||
}
|
||||
|
||||
// Parse the temperature
|
||||
temperature := int(smartInfo.Temperature)
|
||||
|
||||
// Optionally update the reference object's temperature
|
||||
if updateTemp {
|
||||
h.Temperature = temperature
|
||||
}
|
||||
|
||||
// Return the found value
|
||||
return temperature
|
||||
}
|
||||
Reference in New Issue
Block a user