mirror of
https://github.com/JustKato/drive-health.git
synced 2026-03-04 08:29:45 +02:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 40e02abe87 | |||
| c8fa24f11c | |||
| 2776ad8e52 | |||
| f3905bb822 |
13
.env.example
13
.env.example
@@ -1,7 +1,20 @@
|
||||
#
|
||||
# All time references are in seconds
|
||||
#
|
||||
#
|
||||
############################################################
|
||||
|
||||
# The frequency at which to fetch the temperature of ALL disks and add it to the database
|
||||
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
|
||||
# 1 Day = 86400
|
||||
# 1 Week = 604800
|
||||
# 1 Month ~= 2592000
|
||||
# Recommended 1 week
|
||||
MAX_HISTORY_AGE=2592000
|
||||
|
||||
# The ip:port to listen to for the application
|
||||
|
||||
@@ -1,34 +1,31 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type DHConfig struct {
|
||||
DiskFetchFrequency int `json:"diskFetchFrequency"`
|
||||
MaxHistoryAge int `json:"maxHistoryAge"`
|
||||
DatabaseFilePath string
|
||||
Listen string
|
||||
IdentityUsername string
|
||||
IdentityPassword string
|
||||
CleanupServiceFrequency int `json:"cleanupServiceFrequency"`
|
||||
DiskFetchFrequency int `json:"diskFetchFrequency"`
|
||||
MaxHistoryAge int `json:"maxHistoryAge"`
|
||||
|
||||
DatabaseFilePath string `json:"databaseFilePath"`
|
||||
|
||||
Listen string `json:"listen"`
|
||||
|
||||
IdentityUsername string `json:"identityUsername"`
|
||||
IdentityPassword string `json:"identityPassword"`
|
||||
}
|
||||
|
||||
func GetConfiguration() DHConfig {
|
||||
// Load .env file if it exists
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Println("No .env file found")
|
||||
}
|
||||
|
||||
config := DHConfig{
|
||||
DiskFetchFrequency: 5, // default value
|
||||
MaxHistoryAge: 2592000, // default value
|
||||
DatabaseFilePath: "./data.sqlite",
|
||||
IdentityUsername: "admin",
|
||||
IdentityPassword: "admin",
|
||||
DiskFetchFrequency: 5,
|
||||
CleanupServiceFrequency: 3600,
|
||||
MaxHistoryAge: 2592000,
|
||||
DatabaseFilePath: "./data.sqlite",
|
||||
IdentityUsername: "admin",
|
||||
IdentityPassword: "admin",
|
||||
|
||||
Listen: ":8080",
|
||||
}
|
||||
@@ -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 intValue, err := strconv.Atoi(val); err == nil {
|
||||
config.MaxHistoryAge = intValue
|
||||
|
||||
@@ -14,15 +14,16 @@ import (
|
||||
|
||||
var db *gorm.DB
|
||||
|
||||
// Initialize the database connection
|
||||
func InitDB() {
|
||||
var err error
|
||||
dbPath := config.GetConfiguration().DatabaseFilePath
|
||||
if dbPath == "" {
|
||||
dbPath = "./data.sqlite"
|
||||
}
|
||||
|
||||
db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
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")
|
||||
}
|
||||
|
||||
@@ -30,10 +31,12 @@ func InitDB() {
|
||||
db.AutoMigrate(&hardware.HardDrive{}, &hardware.HardDriveTemperature{})
|
||||
}
|
||||
|
||||
// Fetch the open database pointer
|
||||
func GetDatabaseRef() *gorm.DB {
|
||||
return db
|
||||
}
|
||||
|
||||
// Log the temperature of the disks
|
||||
func LogDriveTemps() error {
|
||||
drives, err := hardware.GetSystemHardDrives(db, nil, nil)
|
||||
if err != nil {
|
||||
@@ -52,8 +55,9 @@ func LogDriveTemps() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Run the logging service, this will periodically log the temperature of the disks with the LogDriveTemps function
|
||||
func RunLoggerService() {
|
||||
fmt.Println("Initializing Temperature Logging Service...")
|
||||
fmt.Println("[🦝] Initializing Temperature Logging Service...")
|
||||
|
||||
tickTime := time.Duration(config.GetConfiguration().DiskFetchFrequency) * time.Second
|
||||
|
||||
@@ -63,18 +67,19 @@ func RunLoggerService() {
|
||||
time.Sleep(tickTime)
|
||||
err := LogDriveTemps()
|
||||
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) {
|
||||
var hdd hardware.HardDrive
|
||||
// Fetch by a combination of fields
|
||||
q := db.Where("id = ?", hddID)
|
||||
|
||||
if newerThan == nil && olderThan == nil {
|
||||
if newerThan == nil || olderThan == nil {
|
||||
q = q.Preload("Temperatures")
|
||||
} else {
|
||||
q = q.Preload("Temperatures", "time_stamp < ? AND time_stamp > ?", newerThan, olderThan)
|
||||
|
||||
45
lib/svc/service_cleanup.go
Normal file
45
lib/svc/service_cleanup.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
func setupApi(r *gin.Engine) {
|
||||
api := r.Group("/api/v1")
|
||||
|
||||
// Fetch the chart image for the disk's temperature
|
||||
api.GET("/disks/:diskid/chart", func(ctx *gin.Context) {
|
||||
diskIDString := ctx.Param("diskid")
|
||||
diskId, err := strconv.Atoi(diskIDString)
|
||||
@@ -25,7 +27,28 @@ func setupApi(r *gin.Engine) {
|
||||
return
|
||||
}
|
||||
|
||||
graphData, err := svc.GetDiskGraphImage(diskId, nil, nil)
|
||||
var olderThan, newerThan *time.Time
|
||||
|
||||
if ot := ctx.Query("older"); ot != "" {
|
||||
fmt.Printf("ot = %s\n", ot)
|
||||
if otInt, err := strconv.ParseInt(ot, 10, 64); err == nil {
|
||||
otTime := time.UnixMilli(otInt)
|
||||
olderThan = &otTime
|
||||
}
|
||||
}
|
||||
|
||||
if nt := ctx.Query("newer"); nt != "" {
|
||||
fmt.Printf("nt = %s\n", nt)
|
||||
if ntInt, err := strconv.ParseInt(nt, 10, 64); err == nil {
|
||||
ntTime := time.UnixMilli(ntInt)
|
||||
newerThan = &ntTime
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("olderThan = %s\n", olderThan)
|
||||
fmt.Printf("newerThan = %s\n", newerThan)
|
||||
|
||||
graphData, err := svc.GetDiskGraphImage(diskId, newerThan, olderThan)
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(500, gin.H{
|
||||
"error": err.Error(),
|
||||
@@ -51,6 +74,7 @@ func setupApi(r *gin.Engine) {
|
||||
}
|
||||
})
|
||||
|
||||
// Get a list of all the disks
|
||||
api.GET("/disks", func(ctx *gin.Context) {
|
||||
|
||||
olderThan := time.Now().Add(time.Minute * time.Duration(10) * -1)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -14,22 +16,51 @@ func setupFrontend(r *gin.Engine) {
|
||||
r.Static("/static", "./static")
|
||||
|
||||
// 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(svc.GetDatabaseRef(), &olderThan, &newerThan)
|
||||
r.GET("/", func(ctx *gin.Context) {
|
||||
hardDrives, err := hardware.GetSystemHardDrives(svc.GetDatabaseRef(), nil, nil)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(500)
|
||||
ctx.AbortWithStatus(500)
|
||||
}
|
||||
|
||||
for _, hdd := range hardDrives {
|
||||
hdd.GetTemperature()
|
||||
}
|
||||
|
||||
var olderThan, newerThan *time.Time
|
||||
|
||||
if ot := ctx.Query("older"); ot != "" {
|
||||
fmt.Printf("ot = %s\n", ot)
|
||||
if otInt, err := strconv.ParseInt(ot, 10, 64); err == nil {
|
||||
otTime := time.UnixMilli(otInt)
|
||||
olderThan = &otTime
|
||||
}
|
||||
}
|
||||
|
||||
if nt := ctx.Query("newer"); nt != "" {
|
||||
fmt.Printf("nt = %s\n", nt)
|
||||
if ntInt, err := strconv.ParseInt(nt, 10, 64); err == nil {
|
||||
ntTime := time.UnixMilli(ntInt)
|
||||
newerThan = &ntTime
|
||||
}
|
||||
}
|
||||
|
||||
if olderThan == nil {
|
||||
genTime := time.Now().Add(time.Hour * -1)
|
||||
|
||||
olderThan = &genTime
|
||||
}
|
||||
|
||||
if newerThan == nil {
|
||||
genTime := time.Now()
|
||||
|
||||
newerThan = &genTime
|
||||
}
|
||||
|
||||
// Render the HTML template
|
||||
c.HTML(http.StatusOK, "index.html", gin.H{
|
||||
ctx.HTML(http.StatusOK, "index.html", gin.H{
|
||||
"drives": hardDrives,
|
||||
"older": olderThan.UnixMilli(),
|
||||
"newer": newerThan.UnixMilli(),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
16
main.go
16
main.go
@@ -9,12 +9,18 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"tea.chunkbyte.com/kato/drive-health/lib/config"
|
||||
"tea.chunkbyte.com/kato/drive-health/lib/svc"
|
||||
"tea.chunkbyte.com/kato/drive-health/lib/web"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Load .env file if it exists
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Println("[🟨] No .env file found")
|
||||
}
|
||||
|
||||
// Init the database
|
||||
svc.InitDB()
|
||||
cfg := config.GetConfiguration()
|
||||
@@ -29,12 +35,14 @@ func main() {
|
||||
// Run the server in a goroutine
|
||||
go func() {
|
||||
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
|
||||
svc.RunLoggerService()
|
||||
// Run the cleanup service
|
||||
svc.RunCleanupService()
|
||||
|
||||
// Setting up signal capturing
|
||||
quit := make(chan os.Signal, 1)
|
||||
@@ -42,14 +50,14 @@ func main() {
|
||||
|
||||
// Block until a signal is received
|
||||
<-quit
|
||||
log.Println("Shutting down server...")
|
||||
log.Println("[🦝] Shutting down server...")
|
||||
|
||||
// Graceful shutdown
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
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")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @typedef {number}
|
||||
*/
|
||||
var initialInputOlder = 0; // miliseconds Unix TimeStamp
|
||||
/**
|
||||
* @typedef {number}
|
||||
*/
|
||||
var initialInputNewer = 0; // miliseconds Unix TimeStamp
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {HTMLInputElement}
|
||||
*/
|
||||
var olderThanInputElement;
|
||||
/**
|
||||
* @typedef {HTMLInputElement}
|
||||
*/
|
||||
var newerThanInputElement;
|
||||
|
||||
document.addEventListener(`DOMContentLoaded`, initializePage)
|
||||
|
||||
function initializePage() {
|
||||
|
||||
// Update the page's time filter
|
||||
initialInputOlder = Number(document.getElementById(`inp-older`).textContent.trim())
|
||||
initialInputNewer = Number(document.getElementById(`inp-newer`).textContent.trim())
|
||||
|
||||
// Bind the date elements
|
||||
olderThanInputElement = document.getElementById(`olderThan`);
|
||||
newerThanInputElement = document.getElementById(`newerThan`);
|
||||
|
||||
olderThanInputElement.value = convertTimestampToDateTimeLocal(initialInputOlder);
|
||||
newerThanInputElement.value = convertTimestampToDateTimeLocal(initialInputNewer);
|
||||
|
||||
}
|
||||
|
||||
// Handle one of the date elements having their value changed.
|
||||
function applyDateInterval() {
|
||||
const olderTimeStamp = new Date(olderThanInputElement.value).getTime()
|
||||
const newerTimeStamp = new Date(newerThanInputElement.value).getTime()
|
||||
|
||||
window.location.href = `/?older=${olderTimeStamp}&newer=${newerTimeStamp}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a Unix timestamp to a standard datetime string
|
||||
* @param {number} timestamp - The Unix timestamp in milliseconds.
|
||||
* @returns {string} - A normal string with Y-m-d H:i:s format
|
||||
*/
|
||||
function convertTimestampToDateTimeLocal(timestamp) {
|
||||
const date = new Date(timestamp);
|
||||
const offset = date.getTimezoneOffset() * 60000; // offset in milliseconds
|
||||
const localDate = new Date(date.getTime() - offset);
|
||||
return localDate.toISOString().slice(0, 19);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
--bg1: #282d33;
|
||||
--bg2: #31373f;
|
||||
--bg3: #3e4248;
|
||||
--bg4: #1a1c1f;
|
||||
|
||||
--fg0: #bbc0ca;
|
||||
--fg1: #434c56;
|
||||
@@ -76,4 +77,67 @@ table thead tr {
|
||||
border-radius: 8px;
|
||||
|
||||
padding: .3rem .5rem;
|
||||
}
|
||||
|
||||
|
||||
/* Controls */
|
||||
|
||||
input {
|
||||
padding: .25rem .5rem;
|
||||
font-size: 16px;
|
||||
background-color: var(--bg3);
|
||||
color: var(--fg0);
|
||||
|
||||
border: 1px solid var(--fg1);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.btn {
|
||||
font-size: 16px;
|
||||
|
||||
padding: .5rem 1rem;
|
||||
|
||||
border: 1px solid var(--fg1);
|
||||
border-radius: 6px;
|
||||
|
||||
background-color: var(--bg4);
|
||||
color: var(--fg0);
|
||||
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
background-color: var(--bg3);
|
||||
}
|
||||
|
||||
.input-grp {
|
||||
display: flex;
|
||||
flex-flow: column;
|
||||
}
|
||||
|
||||
.input-grp label {
|
||||
margin-bottom: .25rem;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.graph-controls {
|
||||
display: flex;
|
||||
flex-flow: wrap;
|
||||
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.graph-controls input:nth-child(0) {
|
||||
margin-right: 1rem;
|
||||
}
|
||||
|
||||
.controls-panel {
|
||||
display: flex;
|
||||
flex-flow: column;
|
||||
|
||||
padding: 1rem 0;
|
||||
|
||||
border-bottom: 1px solid var(--fg1);
|
||||
margin-bottom: 1rem;
|
||||
|
||||
}
|
||||
@@ -6,6 +6,10 @@
|
||||
<link rel="stylesheet" href="/static/style.css">
|
||||
<title>Drive Health Dashboard</title>
|
||||
</head>
|
||||
|
||||
{{ $older := .older }}
|
||||
{{ $newer := .newer }}
|
||||
|
||||
<body>
|
||||
<div class="container bordered">
|
||||
|
||||
@@ -66,12 +70,39 @@
|
||||
</div>
|
||||
<div class="container-body">
|
||||
<div class="pad">
|
||||
|
||||
<!-- Controls -->
|
||||
<div class="controls-panel">
|
||||
<div class="graph-controls">
|
||||
<span id="inp-older" style="display: none !important" hidden="true">{{ .older }}</span>
|
||||
<span id="inp-newer" style="display: none !important" hidden="true">{{ .newer }}</span>
|
||||
|
||||
<div class="input-grp" style="margin-right: 1rem;">
|
||||
<label for="olderThan">From Date</label>
|
||||
<input id="olderThan" type="datetime-local" class="date-change-inp">
|
||||
</div>
|
||||
|
||||
<div class="input-grp">
|
||||
<label for="newerThan">To Date</label>
|
||||
<input id="newerThan" type="datetime-local" class="date-change-inp">
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="btn-group">
|
||||
<button type="button" class="btn" onclick="applyDateInterval()" style="margin-top: 1rem;">
|
||||
Filter
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Drives -->
|
||||
{{ if len .drives }}
|
||||
{{ range .drives }}
|
||||
<div class="disk-graph-entry bordered" id="disk-temp-{{ .ID }}">
|
||||
<h4>{{.Name}}:{{.Serial}} [{{.Size}}]</h4>
|
||||
<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?older={{ $older }}&newer={{ $newer }}" alt="{{ .Model }} Image">
|
||||
</a>
|
||||
</div>
|
||||
<br>
|
||||
|
||||
Reference in New Issue
Block a user