2024-01-20 00:21:59 +02:00
|
|
|
package hardware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2024-01-20 18:35:50 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
2024-01-20 00:21:59 +02:00
|
|
|
)
|
|
|
|
|
2024-01-20 18:35:50 +02:00
|
|
|
func GetSystemHardDrives(db *gorm.DB, olderThan *time.Time, newerThan *time.Time) ([]*HardDrive, error) {
|
2024-01-20 00:21:59 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-01-20 18:35:50 +02:00
|
|
|
var systemHardDrives []*HardDrive
|
2024-01-20 00:21:59 +02:00
|
|
|
|
|
|
|
// 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)
|
2024-01-21 19:14:34 +02:00
|
|
|
if cols[1] != "usb" {
|
2024-01-20 01:27:10 +02:00
|
|
|
hd := &HardDrive{
|
2024-01-20 18:35:50 +02:00
|
|
|
Name: cols[0],
|
|
|
|
Transport: cols[1],
|
|
|
|
Size: cols[2],
|
|
|
|
Model: cols[3],
|
|
|
|
Serial: cols[4],
|
|
|
|
Type: cols[5],
|
2024-01-20 00:21:59 +02:00
|
|
|
}
|
2024-01-20 18:35:50 +02:00
|
|
|
systemHardDrives = append(systemHardDrives, hd)
|
2024-01-20 00:21:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle error
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-20 18:35:50 +02:00
|
|
|
var updatedHardDrives []*HardDrive
|
|
|
|
|
|
|
|
for _, sysHDD := range systemHardDrives {
|
|
|
|
var existingHD HardDrive
|
2024-01-21 15:27:42 +02:00
|
|
|
q := db.Where("serial = ? AND model = ? AND type = ?", sysHDD.Serial, sysHDD.Model, sysHDD.Type)
|
2024-01-20 18:35:50 +02:00
|
|
|
|
|
|
|
if newerThan != nil && olderThan != nil {
|
|
|
|
q = q.Preload("Temperatures", "time_stamp < ? AND time_stamp > ?", newerThan, olderThan)
|
|
|
|
}
|
|
|
|
|
|
|
|
result := q.First(&existingHD)
|
|
|
|
|
|
|
|
if result.Error == gorm.ErrRecordNotFound {
|
|
|
|
// Hard drive not found, create new
|
|
|
|
db.Create(&sysHDD)
|
|
|
|
updatedHardDrives = append(updatedHardDrives, sysHDD)
|
|
|
|
} else {
|
|
|
|
// Hard drive found, update existing
|
|
|
|
existingHD.Name = sysHDD.Name
|
|
|
|
existingHD.Transport = sysHDD.Transport
|
|
|
|
existingHD.Size = sysHDD.Size
|
|
|
|
existingHD.Model = sysHDD.Model
|
|
|
|
existingHD.Type = sysHDD.Type
|
|
|
|
db.Save(&existingHD)
|
|
|
|
updatedHardDrives = append(updatedHardDrives, &existingHD)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return updatedHardDrives, nil
|
2024-01-20 00:21:59 +02:00
|
|
|
}
|