mirror of
https://github.com/JustKato/drive-health.git
synced 2026-02-27 06:17:00 +02:00
Implemented the hardware service
+ Started working on web interface
This commit is contained in:
19
lib/config/config.go
Normal file
19
lib/config/config.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package config
|
||||
|
||||
type DHConfig struct {
|
||||
DiskFetchFrequency int `json:"diskFetchFrequency" comment:"How often should a snapshot be taken of the current state of the disks"`
|
||||
MemoryDumpFrequency int `json:"memoryDumpFrequency" comment:"How often should we save the snapshots from memory to disk"`
|
||||
MaxHistoryAge int
|
||||
}
|
||||
|
||||
func GetConfiguration() DHConfig {
|
||||
|
||||
// TODO: Read from os.environment or simply load the defaults
|
||||
|
||||
return DHConfig{
|
||||
DiskFetchFrequency: 5,
|
||||
MemoryDumpFrequency: 36,
|
||||
MaxHistoryAge: 2592000,
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetSystemHardDrives() ([]HardDrive, error) {
|
||||
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")
|
||||
@@ -20,7 +20,7 @@ func GetSystemHardDrives() ([]HardDrive, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var hardDrives []HardDrive
|
||||
var hardDrives []*HardDrive
|
||||
|
||||
// Scan the output line by line
|
||||
scanner := bufio.NewScanner(&out)
|
||||
@@ -40,7 +40,7 @@ func GetSystemHardDrives() ([]HardDrive, error) {
|
||||
|
||||
// Filter out nvme drives (M.2)
|
||||
if cols[1] != "nvme" {
|
||||
hd := HardDrive{
|
||||
hd := &HardDrive{
|
||||
Name: cols[0],
|
||||
Transport: cols[1],
|
||||
Size: cols[2],
|
||||
|
||||
131
lib/svc/service.go
Normal file
131
lib/svc/service.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
snapShot := &HardwareSnapshot{
|
||||
TimeStamp: time.Now(),
|
||||
HDD: []*hardware.HardDrive{},
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RunService() {
|
||||
|
||||
// Snapshot taking routine
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Duration(config.GetConfiguration().DiskFetchFrequency) * time.Second)
|
||||
data, err := TakeHardwareSnapshot()
|
||||
if err != nil {
|
||||
fmt.Printf("Hardware Fetch Error: %s", err)
|
||||
} else {
|
||||
fmt.Println("Got Snapshot for " + data.TimeStamp.Format("02/01/2006"))
|
||||
for _, hdd := range data.HDD {
|
||||
fmt.Printf("%s[%s]: %v\n", hdd.Model, hdd.Size, hdd.Temperature)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Periodic saving routine
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Duration(config.GetConfiguration().MemoryDumpFrequency) * time.Second)
|
||||
err := SaveSnapshotsToFile()
|
||||
if err != nil {
|
||||
fmt.Printf("Memory Dump Error: %s", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
34
lib/web/api.go
Normal file
34
lib/web/api.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"tea.chunkbyte.com/kato/drive-health/lib/hardware"
|
||||
)
|
||||
|
||||
func setupApi(r *gin.Engine) {
|
||||
api := r.Group("/v1/api")
|
||||
|
||||
api.GET("/disks", func(ctx *gin.Context) {
|
||||
// Fetch the disk list
|
||||
disks, err := hardware.GetSystemHardDrives()
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
|
||||
if ctx.Request.URL.Query().Get("temp") != "" {
|
||||
for _, d := range disks {
|
||||
temp := d.GetTemperature(true)
|
||||
fmt.Printf("Disk Temp: %v", temp)
|
||||
}
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "Disk List",
|
||||
"disks": disks,
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
7
lib/web/frontend.go
Normal file
7
lib/web/frontend.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package web
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func setupFrontend(r *gin.Engine) {
|
||||
|
||||
}
|
||||
16
lib/web/health.go
Normal file
16
lib/web/health.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func setupHealth(r *gin.Engine) {
|
||||
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": "Pong",
|
||||
})
|
||||
})
|
||||
}
|
||||
19
lib/web/net.go
Normal file
19
lib/web/net.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func SetupRouter() *gin.Engine {
|
||||
// Initialize the Gin engine
|
||||
r := gin.Default()
|
||||
|
||||
// Setup Health Pings
|
||||
setupHealth(r)
|
||||
// Setup Api
|
||||
setupApi(r)
|
||||
// Setup Frontend
|
||||
setupFrontend(r)
|
||||
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user