mirror of
https://github.com/JustKato/drive-health.git
synced 2026-02-27 06:17:00 +02:00
Started moving to a sqlite3 implementation
This commit is contained in:
@@ -9,10 +9,10 @@ import (
|
||||
)
|
||||
|
||||
type DHConfig struct {
|
||||
DiskFetchFrequency int `json:"diskFetchFrequency"`
|
||||
MemoryDumpFrequency int `json:"memoryDumpFrequency"`
|
||||
MaxHistoryAge int `json:"maxHistoryAge"`
|
||||
Listen string
|
||||
DiskFetchFrequency int `json:"diskFetchFrequency"`
|
||||
MaxHistoryAge int `json:"maxHistoryAge"`
|
||||
DatabaseFilePath string
|
||||
Listen string
|
||||
}
|
||||
|
||||
func GetConfiguration() DHConfig {
|
||||
@@ -22,9 +22,9 @@ func GetConfiguration() DHConfig {
|
||||
}
|
||||
|
||||
config := DHConfig{
|
||||
DiskFetchFrequency: 5, // default value
|
||||
MemoryDumpFrequency: 60, // default value
|
||||
MaxHistoryAge: 2592000, // default value
|
||||
DiskFetchFrequency: 5, // default value
|
||||
MaxHistoryAge: 2592000, // default value
|
||||
DatabaseFilePath: "./data.db",
|
||||
|
||||
Listen: ":8080",
|
||||
}
|
||||
@@ -35,12 +35,6 @@ func GetConfiguration() DHConfig {
|
||||
}
|
||||
}
|
||||
|
||||
if val, exists := os.LookupEnv("MEMORY_DUMP_FREQUENCY"); exists {
|
||||
if intValue, err := strconv.Atoi(val); err == nil {
|
||||
config.MemoryDumpFrequency = intValue
|
||||
}
|
||||
}
|
||||
|
||||
if val, exists := os.LookupEnv("MAX_HISTORY_AGE"); exists {
|
||||
if intValue, err := strconv.Atoi(val); err == nil {
|
||||
config.MaxHistoryAge = intValue
|
||||
@@ -51,5 +45,9 @@ func GetConfiguration() DHConfig {
|
||||
config.Listen = val
|
||||
}
|
||||
|
||||
if val, exists := os.LookupEnv("DATABASE_FILE_PATH"); exists {
|
||||
config.DatabaseFilePath = val
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
@@ -6,9 +6,12 @@ import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func GetSystemHardDrives() ([]*HardDrive, error) {
|
||||
func GetSystemHardDrives(db *gorm.DB, olderThan *time.Time, newerThan *time.Time) ([]*HardDrive, error) {
|
||||
|
||||
// Execute the lsblk command to get detailed block device information
|
||||
cmd := exec.Command("lsblk", "-d", "-o", "NAME,TRAN,SIZE,MODEL,SERIAL,TYPE")
|
||||
@@ -20,7 +23,7 @@ func GetSystemHardDrives() ([]*HardDrive, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var hardDrives []*HardDrive
|
||||
var systemHardDrives []*HardDrive
|
||||
|
||||
// Scan the output line by line
|
||||
scanner := bufio.NewScanner(&out)
|
||||
@@ -39,17 +42,16 @@ func GetSystemHardDrives() ([]*HardDrive, error) {
|
||||
}
|
||||
|
||||
// Filter out nvme drives (M.2)
|
||||
if cols[1] != "nvme" && cols[5] != "Device" {
|
||||
if cols[1] != "nvme" && cols[5] != "Device" && cols[1] != "usb" {
|
||||
hd := &HardDrive{
|
||||
Name: cols[0],
|
||||
Transport: cols[1],
|
||||
Size: cols[2],
|
||||
Model: cols[3],
|
||||
Serial: cols[4],
|
||||
Type: cols[5],
|
||||
Temperature: 0,
|
||||
Name: cols[0],
|
||||
Transport: cols[1],
|
||||
Size: cols[2],
|
||||
Model: cols[3],
|
||||
Serial: cols[4],
|
||||
Type: cols[5],
|
||||
}
|
||||
hardDrives = append(hardDrives, hd)
|
||||
systemHardDrives = append(systemHardDrives, hd)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,5 +60,35 @@ func GetSystemHardDrives() ([]*HardDrive, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return hardDrives, nil
|
||||
var updatedHardDrives []*HardDrive
|
||||
|
||||
for _, sysHDD := range systemHardDrives {
|
||||
var existingHD HardDrive
|
||||
q := db.Where("serial = ?", sysHDD.Serial)
|
||||
|
||||
if newerThan != nil && olderThan != nil {
|
||||
fmt.Printf("\nNewer Than: %s\n", newerThan)
|
||||
fmt.Printf("Older Than: %s\n\n", olderThan)
|
||||
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
|
||||
}
|
||||
|
||||
64
lib/hardware/models.go
Normal file
64
lib/hardware/models.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package hardware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/anatol/smart.go"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type HardDrive struct {
|
||||
ID uint `gorm:"primarykey"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
Name string
|
||||
Transport string
|
||||
Size string
|
||||
Model string
|
||||
Serial string
|
||||
Type string
|
||||
Temperatures []HardDriveTemperature `gorm:"foreignKey:HardDriveID"`
|
||||
}
|
||||
|
||||
type HardDriveTemperature struct {
|
||||
gorm.Model
|
||||
HardDriveID uint
|
||||
TimeStamp time.Time
|
||||
Temperature int
|
||||
}
|
||||
|
||||
// A snapshot in time of the current state of the harddrives
|
||||
type HardwareSnapshot struct {
|
||||
TimeStamp time.Time
|
||||
HDD []*HardDrive
|
||||
}
|
||||
|
||||
type Snapshots struct {
|
||||
List []*HardwareSnapshot
|
||||
}
|
||||
|
||||
// Fetch the temperature of the device, optinally update the reference object
|
||||
func (h *HardDrive) GetTemperature() 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)
|
||||
|
||||
// Return the found value
|
||||
return temperature
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,127 +1,67 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"tea.chunkbyte.com/kato/drive-health/lib/config"
|
||||
"tea.chunkbyte.com/kato/drive-health/lib/hardware"
|
||||
)
|
||||
|
||||
// The path to where the snapshot database is located
|
||||
const SNAPSHOT_LIST_PATH = "./snapshots.dat"
|
||||
var db *gorm.DB
|
||||
|
||||
// A simple in-memory buffer for the history of snapshots
|
||||
var snapShotBuffer []*HardwareSnapshot
|
||||
|
||||
// A snapshot in time of the current state of the harddrives
|
||||
type HardwareSnapshot struct {
|
||||
TimeStamp time.Time
|
||||
HDD []*hardware.HardDrive
|
||||
}
|
||||
|
||||
type Snapshots struct {
|
||||
List []*HardwareSnapshot
|
||||
}
|
||||
|
||||
// The function itterates through all hard disks and takes a snapshot of their state,
|
||||
// returns a struct which contains metadata as well as the harddrives themselves.
|
||||
func TakeHardwareSnapshot() (*HardwareSnapshot, error) {
|
||||
drives, err := hardware.GetSystemHardDrives()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
func InitDB() {
|
||||
var err error
|
||||
dbPath := config.GetConfiguration().DatabaseFilePath
|
||||
if dbPath == "" {
|
||||
dbPath = "./data.db"
|
||||
}
|
||||
|
||||
snapShot := &HardwareSnapshot{
|
||||
TimeStamp: time.Now(),
|
||||
HDD: []*hardware.HardDrive{},
|
||||
db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
|
||||
// Migrate the schema
|
||||
db.AutoMigrate(&hardware.HardDrive{}, &hardware.HardDriveTemperature{})
|
||||
}
|
||||
|
||||
func GetDatabaseRef() *gorm.DB {
|
||||
return db
|
||||
}
|
||||
|
||||
func LogDriveTemps() error {
|
||||
drives, err := hardware.GetSystemHardDrives(db, nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, hdd := range drives {
|
||||
hdd.GetTemperature(true)
|
||||
snapShot.HDD = append(snapShot.HDD, hdd)
|
||||
}
|
||||
|
||||
// Append to the in-memory listing
|
||||
snapShotBuffer = append(snapShotBuffer, snapShot)
|
||||
|
||||
// Return the snapshot just in case there is any need to modify it,
|
||||
// any modification to it will also affect the current buffer from memory.
|
||||
return snapShot, nil
|
||||
}
|
||||
|
||||
// The function wil check if the `.dat` file is present, if it is then it will load it into memory
|
||||
func UpdateHardwareSnapshotsFromFile() {
|
||||
file, err := os.Open(SNAPSHOT_LIST_PATH)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return // File does not exist, no snapshots to load
|
||||
}
|
||||
panic(err) // Handle error according to your error handling policy
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
decoder := gob.NewDecoder(file)
|
||||
var snapshots Snapshots
|
||||
if err := decoder.Decode(&snapshots); err != nil {
|
||||
if err == io.EOF {
|
||||
return // End of file reached
|
||||
}
|
||||
panic(err) // Handle error according to your error handling policy
|
||||
}
|
||||
|
||||
snapShotBuffer = snapshots.List
|
||||
|
||||
fmt.Printf("Loaded %v snapshots from .dat", len(snapShotBuffer))
|
||||
}
|
||||
|
||||
// Get the list of snapshots that have been buffered in memory
|
||||
func GetHardwareSnapshot() []*HardwareSnapshot {
|
||||
return snapShotBuffer
|
||||
}
|
||||
|
||||
// Dump the current snapshot history from memory to file
|
||||
func SaveSnapshotsToFile() error {
|
||||
file, err := os.Create(SNAPSHOT_LIST_PATH)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
encoder := gob.NewEncoder(file)
|
||||
snapshots := Snapshots{List: snapShotBuffer}
|
||||
if err := encoder.Encode(snapshots); err != nil {
|
||||
return err
|
||||
temp := hdd.GetTemperature()
|
||||
db.Create(&hardware.HardDriveTemperature{
|
||||
HardDriveID: hdd.ID,
|
||||
TimeStamp: time.Now(),
|
||||
Temperature: temp,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RunService() {
|
||||
func RunLoggerService() {
|
||||
fmt.Println("Initializing Temperature Logging Service...")
|
||||
|
||||
tickTime := time.Duration(config.GetConfiguration().DiskFetchFrequency) * time.Second
|
||||
|
||||
// Snapshot taking routine
|
||||
go func() {
|
||||
waitTime := time.Duration(config.GetConfiguration().DiskFetchFrequency) * time.Second
|
||||
for {
|
||||
time.Sleep(waitTime)
|
||||
_, err := TakeHardwareSnapshot()
|
||||
time.Sleep(tickTime)
|
||||
err := LogDriveTemps()
|
||||
if err != nil {
|
||||
fmt.Printf("Hardware Fetch Error: %s", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Periodic saving routine
|
||||
go func() {
|
||||
for {
|
||||
waitTime := time.Duration(config.GetConfiguration().MemoryDumpFrequency) * time.Second
|
||||
time.Sleep(waitTime)
|
||||
err := SaveSnapshotsToFile()
|
||||
if err != nil {
|
||||
fmt.Printf("Memory Dump Error: %s", err)
|
||||
fmt.Printf("🛑 Temperature logging failed: %s\n", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -2,6 +2,7 @@ package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"tea.chunkbyte.com/kato/drive-health/lib/hardware"
|
||||
@@ -9,18 +10,22 @@ import (
|
||||
)
|
||||
|
||||
func setupApi(r *gin.Engine) {
|
||||
api := r.Group("/v1/api")
|
||||
api := r.Group("/api/v1")
|
||||
|
||||
api.GET("/disks", func(ctx *gin.Context) {
|
||||
|
||||
olderThan := time.Now().Add(time.Minute * time.Duration(10) * -1)
|
||||
newerThan := time.Now()
|
||||
|
||||
// Fetch the disk list
|
||||
disks, err := hardware.GetSystemHardDrives()
|
||||
disks, err := hardware.GetSystemHardDrives(svc.GetDatabaseRef(), &olderThan, &newerThan)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
|
||||
if ctx.Request.URL.Query().Get("temp") != "" {
|
||||
for _, d := range disks {
|
||||
d.GetTemperature(true)
|
||||
d.GetTemperature()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,10 +35,4 @@ func setupApi(r *gin.Engine) {
|
||||
})
|
||||
})
|
||||
|
||||
api.GET("/snapshots", func(ctx *gin.Context) {
|
||||
snapshots := svc.GetHardwareSnapshot()
|
||||
|
||||
ctx.JSON(http.StatusOK, snapshots)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
@@ -2,9 +2,11 @@ package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"tea.chunkbyte.com/kato/drive-health/lib/hardware"
|
||||
"tea.chunkbyte.com/kato/drive-health/lib/svc"
|
||||
)
|
||||
|
||||
func setupFrontend(r *gin.Engine) {
|
||||
@@ -13,14 +15,16 @@ func setupFrontend(r *gin.Engine) {
|
||||
|
||||
// Set up a route for the root URL
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
olderThan := time.Now().Add(time.Minute * time.Duration(10) * -1)
|
||||
newerThan := time.Now()
|
||||
|
||||
hardDrives, err := hardware.GetSystemHardDrives()
|
||||
hardDrives, err := hardware.GetSystemHardDrives(svc.GetDatabaseRef(), &olderThan, &newerThan)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(500)
|
||||
}
|
||||
|
||||
for _, hdd := range hardDrives {
|
||||
hdd.GetTemperature(true)
|
||||
hdd.GetTemperature()
|
||||
}
|
||||
|
||||
// Render the HTML template
|
||||
|
||||
Reference in New Issue
Block a user