Files
deploy-felhom-compose/controller/internal/web/alerts.go
T
admin 45f75a916c fix: P2+P3 bug fixes, hardening, and cleanup (18 files)
Bug fixes:
- Add applyEnvOverrides to LoadFromBytes (M05)
- Set state=failed on compose-up failure in selfupdate (M16)
- Clamp usableMB to min 0 in memory check (M22)
- Remove "manual" schedule from triggerAllCrossBackups (M23)
- Add mmcblk device handling for partition paths (M21)
- Fix stripPartition for mmcblk devices (L25)
- Fix TruncateStr for UTF-8 and negative maxLen (L05/L06)
- Fix AllDone to return false for empty restore plans (L14)
- Fix PushOnce to return actual errors (L39)
- Restore pending events on save failure in DrainPendingEvents (M03)
- Add duplicate check in AddStoragePath (M04)
- Call CleanupTempMounts after drive scan (H13)
- Log SetStep save errors (M25)

Hardening:
- Guard scheduler Start() against double-start (M14)
- Acquire mutex in scheduler Stop() before reading cancel (L24)
- Cap log lines parameter to 10000 (L31)
- Require POST for logout (L32)
- Use sync.Once for Server.Close() (L49)
- Panic on crypto/rand.Read failure in setup CSRF (L40)
- Validate Bearer token against Hub API key in CSRF (H16 fix)
- Replace custom hasPrefix with strings.HasPrefix (L13)
- Replace simpleHash with crc32.ChecksumIEEE (L48)

Cleanup:
- Remove dead imageName function (L02)
- Remove dead detectHostIPViaRoute function (L03)
- Rename shadowed copy variable to cp (L07)
- Copy DefaultEnabledEvents in GetNotificationPrefs early return (L09)
- Update BUGHUNT.md with comprehensive audit results

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 13:47:52 +01:00

246 lines
6.6 KiB
Go

package web
import (
"fmt"
"hash/crc32"
"log"
"strings"
"sync"
"time"
"gitea.dooplex.hu/admin/felhom-controller/internal/backup"
"gitea.dooplex.hu/admin/felhom-controller/internal/config"
"gitea.dooplex.hu/admin/felhom-controller/internal/monitor"
"gitea.dooplex.hu/admin/felhom-controller/internal/settings"
)
// Alert represents a persistent dashboard alert banner.
type Alert struct {
ID string // unique identifier for filtering
Level string // "error", "warning", "info"
Message string // Hungarian display text
Link string // optional link to relevant page
LinkText string // link display text
PageOnly []string // if non-empty, only show on these pages (e.g., ["dashboard", "monitoring"])
Inline bool // if true, rendered by page template inline, not in layout banner
}
// AlertManager generates and stores dashboard alerts from health check results.
// Alerts are state-based (not event-based) — they reflect current system state
// and are regenerated after each health check cycle.
type AlertManager struct {
mu sync.RWMutex
alerts []Alert
logger *log.Logger
hubPushStatusFn func() HubPushStatusData
}
// NewAlertManager creates a new AlertManager.
func NewAlertManager(logger *log.Logger) *AlertManager {
return &AlertManager{
logger: logger,
}
}
// SetHubPushStatus sets the hub push status callback for generating hub alerts.
func (am *AlertManager) SetHubPushStatus(fn func() HubPushStatusData) {
am.mu.Lock()
am.hubPushStatusFn = fn
am.mu.Unlock()
}
// Refresh regenerates alerts from the latest health check report and config state.
// Called after each health check cycle (every 5 minutes) and on storage state changes.
func (am *AlertManager) Refresh(report *monitor.HealthReport, cfg *config.Config, backupMgr *backup.Manager, updateAvailable bool, latestVersion string, storagePaths ...[]settings.StoragePath) {
var alerts []Alert
// Disconnected storage alerts (top-level error banners on all pages)
if len(storagePaths) > 0 {
for _, sp := range storagePaths[0] {
if sp.Disconnected {
label := sp.Label
if label == "" {
label = sp.Path
}
alerts = append(alerts, Alert{
ID: "storage-disconnected-" + simpleHash(sp.Path),
Level: "error",
Message: fmt.Sprintf("Meghajtó leválasztva: %s (%s)", label, sp.Path),
Link: "/settings",
LinkText: "Beállítások",
})
}
}
}
// From health check issues (critical)
for _, issue := range report.Issues {
alerts = append(alerts, Alert{
ID: "health-" + simpleHash(issue),
Level: "error",
Message: issue,
Link: "/monitoring",
LinkText: "Rendszermonitor",
})
}
// From health check warnings
for _, w := range report.Warnings {
alert := Alert{
ID: "health-" + simpleHash(w),
Level: "warning",
Message: w,
Link: "/monitoring",
LinkText: "Rendszermonitor",
}
// Disk-related warnings rendered inline under storage bars, not in top banner
if strings.Contains(w, "meghajtón") || strings.Contains(w, "adattároló") || strings.Contains(w, "meghajtó") {
alert.ID = "disk-not-separate"
alert.PageOnly = []string{"dashboard", "monitoring"}
alert.Inline = true
}
alerts = append(alerts, alert)
}
// Hub connection status
if !cfg.Hub.Enabled || cfg.Hub.URL == "" {
alerts = append(alerts, Alert{
ID: "hub-disabled",
Level: "warning",
Message: "Hub kapcsolat kikapcsolva — a központi monitoring nem aktív",
Link: "/monitoring",
LinkText: "Rendszermonitor",
})
} else if am.hubPushStatusFn != nil {
ps := am.hubPushStatusFn()
if ps.LastError != "" && (ps.LastSuccess.IsZero() || time.Since(ps.LastSuccess) > 30*time.Minute) {
alerts = append(alerts, Alert{
ID: "hub-unreachable",
Level: "error",
Message: fmt.Sprintf("Hub nem elérhető — utolsó hiba: %s", ps.LastError),
Link: "/monitoring",
LinkText: "Rendszermonitor",
})
}
}
// Backup disabled
if !cfg.Backup.Enabled {
alerts = append(alerts, Alert{
ID: "backup-disabled",
Level: "warning",
Message: "A biztonsági mentés nincs bekapcsolva",
Link: "/settings",
LinkText: "Beállítások",
})
}
// Update available
if updateAvailable && latestVersion != "" {
alerts = append(alerts, Alert{
ID: "update-available",
Level: "info",
Message: fmt.Sprintf("Új controller verzió elérhető: %s", latestVersion),
Link: "/settings",
LinkText: "Frissítés",
})
}
// Sort: errors first, then warnings, then info
sortAlerts(alerts)
am.mu.Lock()
am.alerts = alerts
am.mu.Unlock()
am.logger.Printf("[DEBUG] AlertManager refreshed: %d alerts (%d error, %d warning)",
len(alerts), countLevel(alerts, "error"), countLevel(alerts, "warning"))
}
// GetAlerts returns a copy of the current alerts, optionally excluding specific IDs.
func (am *AlertManager) GetAlerts(excludeIDs ...string) []Alert {
am.mu.RLock()
defer am.mu.RUnlock()
if len(am.alerts) == 0 {
return nil
}
exclude := make(map[string]bool, len(excludeIDs))
for _, id := range excludeIDs {
exclude[id] = true
}
var result []Alert
for _, a := range am.alerts {
if exclude[a.ID] {
continue
}
result = append(result, a)
}
// Cap at 5 visible alerts
if len(result) > 5 {
overflow := len(result) - 5
result = result[:5]
result = append(result, Alert{
ID: "overflow",
Level: "info",
Message: fmt.Sprintf("+ %d további figyelmeztetés", overflow),
Link: "/monitoring",
})
}
return result
}
// GetInlineAlerts returns alerts marked as Inline for a specific page.
func (am *AlertManager) GetInlineAlerts(page string) []Alert {
am.mu.RLock()
defer am.mu.RUnlock()
var result []Alert
for _, a := range am.alerts {
if !a.Inline {
continue
}
if len(a.PageOnly) == 0 {
result = append(result, a)
continue
}
for _, p := range a.PageOnly {
if p == page {
result = append(result, a)
break
}
}
}
return result
}
// simpleHash returns a short deterministic hash for deduplication.
func simpleHash(s string) string {
return fmt.Sprintf("%08x", crc32.ChecksumIEEE([]byte(s)))
}
// sortAlerts sorts alerts by severity: error > warning > info.
func sortAlerts(alerts []Alert) {
levelOrder := map[string]int{"error": 0, "warning": 1, "info": 2}
for i := 1; i < len(alerts); i++ {
for j := i; j > 0 && levelOrder[alerts[j].Level] < levelOrder[alerts[j-1].Level]; j-- {
alerts[j], alerts[j-1] = alerts[j-1], alerts[j]
}
}
}
func countLevel(alerts []Alert, level string) int {
n := 0
for _, a := range alerts {
if a.Level == level {
n++
}
}
return n
}