4 Commits

Author SHA1 Message Date
2776ad8e52 * Fixed config defaults 2024-01-21 21:43:24 +02:00
f3905bb822 Cleanup Service
* Updated .env
+ Implemented automatical removal of old logs
2024-01-21 21:42:58 +02:00
92baa56a1c Re-Added NVME 2024-01-21 19:14:34 +02:00
4c877e7162 Styling and fixes 2024-01-21 19:12:40 +02:00
9 changed files with 123 additions and 36 deletions

View File

@@ -1,11 +1,25 @@
#
# All time references are in seconds
#
#
############################################################
# The frequency at which to fetch the temperature of ALL disks and add it to the database # The frequency at which to fetch the temperature of ALL disks and add it to the database
DISK_FETCH_FREQUENCY=5 DISK_FETCH_FREQUENCY=5
# How ofthen should the program clean the database of old logs
CLEANUP_SERVICE_FREQUENCY=3600
# The maximum age of logs in seconds # The maximum age of logs in seconds
# 1 Day = 86400
# 1 Week = 604800
# 1 Month ~= 2592000
# Recommended 1 week
MAX_HISTORY_AGE=2592000 MAX_HISTORY_AGE=2592000
# The ip:port to listen to for the application # The ip:port to listen to for the application
LISTEN=":8080" LISTEN=":8080"
# Basic Security # Basic Security, these are required to view the data
ACCESS_PASSWORD= IDENTITY_USERNAME=admin
IDENTITY_PASSWORD=admin

View File

@@ -1,31 +1,28 @@
package config package config
import ( import (
"log"
"os" "os"
"strconv" "strconv"
"github.com/joho/godotenv"
) )
type DHConfig struct { type DHConfig struct {
CleanupServiceFrequency int `json:"cleanupServiceFrequency"`
DiskFetchFrequency int `json:"diskFetchFrequency"` DiskFetchFrequency int `json:"diskFetchFrequency"`
MaxHistoryAge int `json:"maxHistoryAge"` MaxHistoryAge int `json:"maxHistoryAge"`
DatabaseFilePath string
Listen string DatabaseFilePath string `json:"databaseFilePath"`
IdentityUsername string
IdentityPassword string Listen string `json:"listen"`
IdentityUsername string `json:"identityUsername"`
IdentityPassword string `json:"identityPassword"`
} }
func GetConfiguration() DHConfig { func GetConfiguration() DHConfig {
// Load .env file if it exists
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
config := DHConfig{ config := DHConfig{
DiskFetchFrequency: 5, // default value DiskFetchFrequency: 5,
MaxHistoryAge: 2592000, // default value CleanupServiceFrequency: 3600,
MaxHistoryAge: 2592000,
DatabaseFilePath: "./data.sqlite", DatabaseFilePath: "./data.sqlite",
IdentityUsername: "admin", IdentityUsername: "admin",
IdentityPassword: "admin", IdentityPassword: "admin",
@@ -39,6 +36,12 @@ func GetConfiguration() DHConfig {
} }
} }
if val, exists := os.LookupEnv("CLEANUP_SERVICE_FREQUENCY"); exists {
if intValue, err := strconv.Atoi(val); err == nil {
config.CleanupServiceFrequency = intValue
}
}
if val, exists := os.LookupEnv("MAX_HISTORY_AGE"); exists { if val, exists := os.LookupEnv("MAX_HISTORY_AGE"); exists {
if intValue, err := strconv.Atoi(val); err == nil { if intValue, err := strconv.Atoi(val); err == nil {
config.MaxHistoryAge = intValue config.MaxHistoryAge = intValue

View File

@@ -42,7 +42,7 @@ func GetSystemHardDrives(db *gorm.DB, olderThan *time.Time, newerThan *time.Time
} }
// Filter out nvme drives (M.2) // Filter out nvme drives (M.2)
if cols[1] != "nvme" && cols[5] != "Device" && cols[1] != "usb" { if cols[1] != "usb" {
hd := &HardDrive{ hd := &HardDrive{
Name: cols[0], Name: cols[0],
Transport: cols[1], Transport: cols[1],

View File

@@ -14,15 +14,16 @@ import (
var db *gorm.DB var db *gorm.DB
// Initialize the database connection
func InitDB() { func InitDB() {
var err error var err error
dbPath := config.GetConfiguration().DatabaseFilePath dbPath := config.GetConfiguration().DatabaseFilePath
if dbPath == "" {
dbPath = "./data.sqlite"
}
db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{}) db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
if err != nil { if err != nil {
// This should basically never happen, unless the path to the database
// is inaccessible, doesn't exist or there's no permission to it, which
// should and will crash the program
panic("failed to connect database") panic("failed to connect database")
} }
@@ -30,10 +31,12 @@ func InitDB() {
db.AutoMigrate(&hardware.HardDrive{}, &hardware.HardDriveTemperature{}) db.AutoMigrate(&hardware.HardDrive{}, &hardware.HardDriveTemperature{})
} }
// Fetch the open database pointer
func GetDatabaseRef() *gorm.DB { func GetDatabaseRef() *gorm.DB {
return db return db
} }
// Log the temperature of the disks
func LogDriveTemps() error { func LogDriveTemps() error {
drives, err := hardware.GetSystemHardDrives(db, nil, nil) drives, err := hardware.GetSystemHardDrives(db, nil, nil)
if err != nil { if err != nil {
@@ -52,8 +55,9 @@ func LogDriveTemps() error {
return nil return nil
} }
// Run the logging service, this will periodically log the temperature of the disks with the LogDriveTemps function
func RunLoggerService() { func RunLoggerService() {
fmt.Println("Initializing Temperature Logging Service...") fmt.Println("[🦝] Initializing Temperature Logging Service...")
tickTime := time.Duration(config.GetConfiguration().DiskFetchFrequency) * time.Second tickTime := time.Duration(config.GetConfiguration().DiskFetchFrequency) * time.Second
@@ -63,12 +67,13 @@ func RunLoggerService() {
time.Sleep(tickTime) time.Sleep(tickTime)
err := LogDriveTemps() err := LogDriveTemps()
if err != nil { if err != nil {
fmt.Printf("🛑 Temperature logging failed: %s\n", err) fmt.Printf("[🛑] Temperature logging failed: %s\n", err)
} }
} }
}() }()
} }
// Generate a PNG based upon a HDD id and a date range
func GetDiskGraphImage(hddID int, newerThan *time.Time, olderThan *time.Time) (*bytes.Buffer, error) { func GetDiskGraphImage(hddID int, newerThan *time.Time, olderThan *time.Time) (*bytes.Buffer, error) {
var hdd hardware.HardDrive var hdd hardware.HardDrive
// Fetch by a combination of fields // Fetch by a combination of fields

View File

@@ -0,0 +1,45 @@
package svc
import (
"fmt"
"time"
"tea.chunkbyte.com/kato/drive-health/lib/config"
"tea.chunkbyte.com/kato/drive-health/lib/hardware"
)
// Delete all thermal entries that are older than X amount of seconds
func CleanupOldData() error {
cfg := config.GetConfiguration()
beforeDate := time.Now().Add(-1 * time.Duration(cfg.MaxHistoryAge) * time.Second)
deleteResult := db.Where("time_stamp < ?", beforeDate).Delete(&hardware.HardDriveTemperature{})
if deleteResult.Error != nil {
fmt.Printf("[🛑] Error during cleanup: %s\n", deleteResult.Error)
return db.Error
}
if deleteResult.RowsAffected > 0 {
fmt.Printf("[🛑] Cleaned up %v entries before %s\n", deleteResult.RowsAffected, beforeDate)
}
return nil
}
func RunCleanupService() {
fmt.Println("[🦝] Initializing Log Cleanup Service...")
tickTime := time.Duration(config.GetConfiguration().CleanupServiceFrequency) * time.Second
// Snapshot taking routine
go func() {
for {
time.Sleep(tickTime)
err := CleanupOldData()
if err != nil {
fmt.Printf("🛑 Cleanup process failed: %s\n", err)
}
}
}()
}

View File

@@ -13,6 +13,7 @@ import (
func setupApi(r *gin.Engine) { func setupApi(r *gin.Engine) {
api := r.Group("/api/v1") api := r.Group("/api/v1")
// Fetch the chart image for the disk's temperature
api.GET("/disks/:diskid/chart", func(ctx *gin.Context) { api.GET("/disks/:diskid/chart", func(ctx *gin.Context) {
diskIDString := ctx.Param("diskid") diskIDString := ctx.Param("diskid")
diskId, err := strconv.Atoi(diskIDString) diskId, err := strconv.Atoi(diskIDString)
@@ -51,6 +52,7 @@ func setupApi(r *gin.Engine) {
} }
}) })
// Get a list of all the disks
api.GET("/disks", func(ctx *gin.Context) { api.GET("/disks", func(ctx *gin.Context) {
olderThan := time.Now().Add(time.Minute * time.Duration(10) * -1) olderThan := time.Now().Add(time.Minute * time.Duration(10) * -1)

16
main.go
View File

@@ -9,12 +9,18 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/joho/godotenv"
"tea.chunkbyte.com/kato/drive-health/lib/config" "tea.chunkbyte.com/kato/drive-health/lib/config"
"tea.chunkbyte.com/kato/drive-health/lib/svc" "tea.chunkbyte.com/kato/drive-health/lib/svc"
"tea.chunkbyte.com/kato/drive-health/lib/web" "tea.chunkbyte.com/kato/drive-health/lib/web"
) )
func main() { func main() {
// Load .env file if it exists
if err := godotenv.Load(); err != nil {
log.Println("[🟨] No .env file found")
}
// Init the database // Init the database
svc.InitDB() svc.InitDB()
cfg := config.GetConfiguration() cfg := config.GetConfiguration()
@@ -29,12 +35,14 @@ func main() {
// Run the server in a goroutine // Run the server in a goroutine
go func() { go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err) log.Fatalf("[🛑] listening failed: %s\n", err)
} }
}() }()
// Run the hardware service // Run the hardware service
svc.RunLoggerService() svc.RunLoggerService()
// Run the cleanup service
svc.RunCleanupService()
// Setting up signal capturing // Setting up signal capturing
quit := make(chan os.Signal, 1) quit := make(chan os.Signal, 1)
@@ -42,14 +50,14 @@ func main() {
// Block until a signal is received // Block until a signal is received
<-quit <-quit
log.Println("Shutting down server...") log.Println("[🦝] Shutting down server...")
// Graceful shutdown // Graceful shutdown
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
if err := srv.Shutdown(ctx); err != nil { if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown:", err) log.Fatal("[🛑] Server forced to shutdown:", err)
} }
log.Println("Server exiting") log.Println("[🦝] Server exiting")
} }

View File

@@ -4,9 +4,10 @@
--bg0: #202327; --bg0: #202327;
--bg1: #282d33; --bg1: #282d33;
--bg2: #31373f; --bg2: #31373f;
--bg3: #3e4248;
--fg0: #bbc0ca; --fg0: #bbc0ca;
--fg0: #bbc0ca; --fg1: #434c56;
--acc: #bbc0ca; --acc: #bbc0ca;
} }
@@ -21,7 +22,6 @@ html, body {
padding: 0; padding: 0;
width: 100vw; width: 100vw;
height: 100vw;
overflow: auto; overflow: auto;
@@ -69,3 +69,11 @@ table thead tr {
.graph-image { .graph-image {
max-width: 100%; max-width: 100%;
} }
.disk-graph-entry {
background-color: var(--bg3);
border-radius: 8px;
padding: .3rem .5rem;
}

View File

@@ -7,7 +7,7 @@
<title>Drive Health Dashboard</title> <title>Drive Health Dashboard</title>
</head> </head>
<body> <body>
<div class="container"> <div class="container bordered">
<div class="container-titlebar"> <div class="container-titlebar">
<div class="pad"> <div class="pad">
@@ -57,7 +57,7 @@
</div> </div>
<div class="container"> <div class="container bordered">
<div class="container-titlebar"> <div class="container-titlebar">
<div class="pad"> <div class="pad">
@@ -68,11 +68,13 @@
<div class="pad"> <div class="pad">
{{ if len .drives }} {{ if len .drives }}
{{ range .drives }} {{ range .drives }}
<div id="disk-temp-{{ .ID }}"> <div class="disk-graph-entry bordered" id="disk-temp-{{ .ID }}">
<h4>{{.Name}}:{{.Serial}} [{{.Size}}]</h4>
<a href="/api/v1/disks/{{.ID}}/chart" target="_blank"> <a href="/api/v1/disks/{{.ID}}/chart" target="_blank">
<img class="graph-image" src="/api/v1/disks/{{.ID}}/chart" alt="{{ .Model }} Image"> <img class="graph-image" src="/api/v1/disks/{{.ID}}/chart" alt="{{ .Model }} Image">
</a> </a>
</div> </div>
<br>
{{ end }} {{ end }}
{{ else }} {{ else }}
<p>No hard drives found.</p> <p>No hard drives found.</p>