refactor: alerts package compatiblity with new systems package

This commit is contained in:
henrygd
2025-03-03 23:52:27 -05:00
parent f8a1d9fc5d
commit a1f6eeb9eb
2 changed files with 16 additions and 26 deletions

View File

@@ -26,8 +26,7 @@ type alertInfo struct {
// startWorker is a long-running goroutine that processes alert tasks // startWorker is a long-running goroutine that processes alert tasks
// every x seconds. It must be running to process status alerts. // every x seconds. It must be running to process status alerts.
func (am *AlertManager) startWorker() { func (am *AlertManager) startWorker() {
// no special reason for 13 seconds tick := time.Tick(15 * time.Second)
tick := time.Tick(13 * time.Second)
for { for {
select { select {
case <-am.stopChan: case <-am.stopChan:
@@ -64,21 +63,12 @@ func (am *AlertManager) StopWorker() {
} }
// HandleStatusAlerts manages the logic when system status changes. // HandleStatusAlerts manages the logic when system status changes.
func (am *AlertManager) HandleStatusAlerts(newStatus string, oldSystemRecord *core.Record) error { func (am *AlertManager) HandleStatusAlerts(newStatus string, systemRecord *core.Record) error {
switch newStatus { if newStatus != "up" && newStatus != "down" {
case "up":
if oldSystemRecord.GetString("status") != "down" {
return nil
}
case "down":
if oldSystemRecord.GetString("status") != "up" {
return nil
}
default:
return nil return nil
} }
alertRecords, err := am.getSystemStatusAlerts(oldSystemRecord.Id) alertRecords, err := am.getSystemStatusAlerts(systemRecord.Id)
if err != nil { if err != nil {
return err return err
} }
@@ -86,7 +76,7 @@ func (am *AlertManager) HandleStatusAlerts(newStatus string, oldSystemRecord *co
return nil return nil
} }
systemName := oldSystemRecord.GetString("name") systemName := systemRecord.GetString("name")
if newStatus == "down" { if newStatus == "down" {
am.handleSystemDown(systemName, alertRecords) am.handleSystemDown(systemName, alertRecords)
} else { } else {

View File

@@ -15,7 +15,7 @@ import (
"github.com/spf13/cast" "github.com/spf13/cast"
) )
func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, systemInfo system.Info, temperatures map[string]float64, extraFs map[string]*system.FsStats) error { func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, data *system.CombinedData) error {
alertRecords, err := am.app.FindAllRecords("alerts", alertRecords, err := am.app.FindAllRecords("alerts",
dbx.NewExp("system={:system}", dbx.Params{"system": systemRecord.Id}), dbx.NewExp("system={:system}", dbx.Params{"system": systemRecord.Id}),
) )
@@ -35,15 +35,15 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, systemInfo
switch name { switch name {
case "CPU": case "CPU":
val = systemInfo.Cpu val = data.Info.Cpu
case "Memory": case "Memory":
val = systemInfo.MemPct val = data.Info.MemPct
case "Bandwidth": case "Bandwidth":
val = systemInfo.Bandwidth val = data.Info.Bandwidth
unit = " MB/s" unit = " MB/s"
case "Disk": case "Disk":
maxUsedPct := systemInfo.DiskPct maxUsedPct := data.Info.DiskPct
for _, fs := range extraFs { for _, fs := range data.Stats.ExtraFs {
usedPct := fs.DiskUsed / fs.DiskTotal * 100 usedPct := fs.DiskUsed / fs.DiskTotal * 100
if usedPct > maxUsedPct { if usedPct > maxUsedPct {
maxUsedPct = usedPct maxUsedPct = usedPct
@@ -51,10 +51,10 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, systemInfo
} }
val = maxUsedPct val = maxUsedPct
case "Temperature": case "Temperature":
if temperatures == nil { if data.Stats.Temperatures == nil {
continue continue
} }
for _, temp := range temperatures { for _, temp := range data.Stats.Temperatures {
if temp > val { if temp > val {
val = temp val = temp
} }
@@ -111,7 +111,7 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, systemInfo
)). )).
OrderBy("created"). OrderBy("created").
All(&systemStats) All(&systemStats)
if err != nil { if err != nil || len(systemStats) == 0 {
return err return err
} }
@@ -163,7 +163,7 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, systemInfo
alert.val += stats.NetSent + stats.NetRecv alert.val += stats.NetSent + stats.NetRecv
case "Disk": case "Disk":
if alert.mapSums == nil { if alert.mapSums == nil {
alert.mapSums = make(map[string]float32, len(extraFs)+1) alert.mapSums = make(map[string]float32, len(data.Stats.ExtraFs)+1)
} }
// add root disk // add root disk
if _, ok := alert.mapSums["root"]; !ok { if _, ok := alert.mapSums["root"]; !ok {
@@ -171,7 +171,7 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, systemInfo
} }
alert.mapSums["root"] += float32(stats.Disk) alert.mapSums["root"] += float32(stats.Disk)
// add extra disks // add extra disks
for key, fs := range extraFs { for key, fs := range data.Stats.ExtraFs {
if _, ok := alert.mapSums[key]; !ok { if _, ok := alert.mapSums[key]; !ok {
alert.mapSums[key] = 0.0 alert.mapSums[key] = 0.0
} }