abe4e8e619
Retired (~12.3k LOC): internal/storage/* (scan/format/attach/migrate/safety), backup restic/crossdrive/restore_drives/disk_layout/local_infra/restore_scan/ paths + restore_app, report/infra_backup*/infra_pull, setup/scanner, monitor/watchdog+pinger, web/storage_handlers+handler_restore. Surgically split backup.Manager to app-data only (DB dumps + volume tars + app restore; dropped restic + cross-drive + snapshot history). Fixed router/main/web wiring. Added agent-backed disk API (web/agent_disk_handlers.go): /api/disks list/ assign/eject/format proxying agentapi; data-bearing format refusal -> HTTP 409 'operator authorization required'. report/config_pull.go keeps the setup fresh-install config download. go build + go test green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
281 lines
7.5 KiB
Go
281 lines
7.5 KiB
Go
package report
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"gitea.dooplex.hu/admin/felhom-controller/internal/backup"
|
|
"gitea.dooplex.hu/admin/felhom-controller/internal/config"
|
|
"gitea.dooplex.hu/admin/felhom-controller/internal/metrics"
|
|
"gitea.dooplex.hu/admin/felhom-controller/internal/monitor"
|
|
"gitea.dooplex.hu/admin/felhom-controller/internal/scheduler"
|
|
"gitea.dooplex.hu/admin/felhom-controller/internal/settings"
|
|
"gitea.dooplex.hu/admin/felhom-controller/internal/stacks"
|
|
"gitea.dooplex.hu/admin/felhom-controller/internal/system"
|
|
)
|
|
|
|
// BuildReport collects current state from all subsystems and returns a Report.
|
|
func BuildReport(
|
|
cfg *config.Config,
|
|
configPath string,
|
|
stackMgr *stacks.Manager,
|
|
backupMgr *backup.Manager,
|
|
cpuCollector *system.CPUCollector,
|
|
metricsStore *metrics.MetricsStore,
|
|
version string,
|
|
storagePaths []settings.StoragePath,
|
|
geoRestriction *settings.GeoRestriction,
|
|
logger *log.Logger,
|
|
) *Report {
|
|
debug := cfg.Logging.Level == "debug"
|
|
if logger != nil {
|
|
logger.Printf("[INFO] [report] Building system report")
|
|
}
|
|
if debug && logger != nil {
|
|
logger.Printf("[DEBUG] [report] BuildReport: starting — version=%s, storagePaths=%d", version, len(storagePaths))
|
|
}
|
|
|
|
r := &Report{
|
|
Version: 1,
|
|
CustomerID: cfg.Customer.ID,
|
|
CustomerName: cfg.Customer.Name,
|
|
ControllerVersion: version,
|
|
Timestamp: time.Now().UTC(),
|
|
}
|
|
|
|
// Controller URL for hub callbacks (self-update trigger, etc.)
|
|
if cfg.Customer.Domain != "" {
|
|
r.ControllerURL = fmt.Sprintf("https://felhom.%s", cfg.Customer.Domain)
|
|
}
|
|
|
|
// Config hash for Hub comparison
|
|
if configPath != "" {
|
|
if data, err := os.ReadFile(configPath); err == nil {
|
|
h := sha256.Sum256(data)
|
|
r.ConfigHash = hex.EncodeToString(h[:])
|
|
if debug && logger != nil {
|
|
logger.Printf("[DEBUG] [report] BuildReport: configHash=%s (%d bytes)", r.ConfigHash[:12]+"...", len(data))
|
|
}
|
|
}
|
|
}
|
|
|
|
// System info
|
|
staticInfo := metrics.GetStaticInfo()
|
|
hddPath := cfg.Paths.HDDPath
|
|
if len(storagePaths) > 0 {
|
|
hddPath = storagePaths[0].Path
|
|
}
|
|
sysInfo := system.GetInfo(hddPath, cpuCollector)
|
|
|
|
r.System = SystemReport{
|
|
Hostname: staticInfo.Hostname,
|
|
OS: staticInfo.OS,
|
|
Kernel: staticInfo.Kernel,
|
|
CPUModel: staticInfo.CPUModel,
|
|
CPUCores: staticInfo.CPUCores,
|
|
UptimeSeconds: staticInfo.UptimeSeconds,
|
|
CPUPercent: sysInfo.CPUPercent,
|
|
MemoryTotalMB: sysInfo.TotalMemMB,
|
|
MemoryUsedMB: sysInfo.UsedMemMB,
|
|
MemoryPercent: sysInfo.MemPercent,
|
|
TemperatureCelsius: sysInfo.TemperatureCelsius,
|
|
LoadAvg1: sysInfo.LoadAvg1,
|
|
LoadAvg5: sysInfo.LoadAvg5,
|
|
LoadAvg15: sysInfo.LoadAvg15,
|
|
}
|
|
|
|
// Storage — root filesystem + all registered storage paths
|
|
r.Storage = []StorageReport{
|
|
{Mount: "/", Label: "SSD", TotalGB: sysInfo.DiskTotalGB, UsedGB: sysInfo.DiskUsedGB, Percent: sysInfo.DiskPercent},
|
|
}
|
|
for _, sp := range storagePaths {
|
|
if sp.Decommissioned {
|
|
r.Storage = append(r.Storage, StorageReport{
|
|
Mount: sp.Path,
|
|
Label: sp.Label,
|
|
Decommissioned: true,
|
|
MigratedTo: sp.MigratedTo,
|
|
})
|
|
continue
|
|
}
|
|
if sp.Disconnected {
|
|
r.Storage = append(r.Storage, StorageReport{
|
|
Mount: sp.Path,
|
|
Label: sp.Label,
|
|
Disconnected: true,
|
|
})
|
|
continue
|
|
}
|
|
di := system.GetDiskUsage(sp.Path)
|
|
if di == nil {
|
|
continue
|
|
}
|
|
r.Storage = append(r.Storage, StorageReport{
|
|
Mount: sp.Path,
|
|
Label: sp.Label,
|
|
TotalGB: di.TotalGB,
|
|
UsedGB: di.UsedGB,
|
|
Percent: di.UsedPercent,
|
|
})
|
|
}
|
|
|
|
if debug && logger != nil {
|
|
logger.Printf("[DEBUG] [report] BuildReport: system info collected — cpu=%.1f%%, mem=%d/%dMB, temp=%.1fC",
|
|
sysInfo.CPUPercent, sysInfo.UsedMemMB, sysInfo.TotalMemMB, sysInfo.TemperatureCelsius)
|
|
logger.Printf("[DEBUG] [report] BuildReport: storage entries=%d", len(r.Storage))
|
|
}
|
|
|
|
// Containers
|
|
r.Containers = buildContainerReport(stackMgr, metricsStore)
|
|
|
|
// Backup
|
|
r.Backup = buildBackupReport(cfg, backupMgr)
|
|
|
|
// Health
|
|
healthReport := monitor.RunHealthCheck(cfg, cpuCollector, storagePaths, logger)
|
|
r.Health = HealthReport{
|
|
Status: healthReport.Status,
|
|
Issues: healthReport.Issues,
|
|
Warnings: healthReport.Warnings,
|
|
}
|
|
if r.Health.Issues == nil {
|
|
r.Health.Issues = []string{}
|
|
}
|
|
if r.Health.Warnings == nil {
|
|
r.Health.Warnings = []string{}
|
|
}
|
|
|
|
// Stacks
|
|
r.Stacks = buildStacksReport(stackMgr)
|
|
|
|
// App telemetry (metrics + log scan)
|
|
r.AppTelemetry = buildAppTelemetrySection(stackMgr, metricsStore, logger)
|
|
|
|
// Geo-restriction status
|
|
if geoRestriction != nil {
|
|
gr := &GeoRestrictionReport{
|
|
Enabled: geoRestriction.Enabled,
|
|
AllowedCountries: geoRestriction.AllowedCountries,
|
|
LastSync: geoRestriction.LastSync,
|
|
LastSyncError: geoRestriction.LastSyncError,
|
|
}
|
|
if len(geoRestriction.AppOverrides) > 0 {
|
|
gr.AppOverrides = make(map[string]GeoAppOverrideReport, len(geoRestriction.AppOverrides))
|
|
for k, v := range geoRestriction.AppOverrides {
|
|
gr.AppOverrides[k] = GeoAppOverrideReport{AllowedCountries: v.AllowedCountries}
|
|
}
|
|
}
|
|
r.GeoRestriction = gr
|
|
}
|
|
|
|
if debug && logger != nil {
|
|
logger.Printf("[DEBUG] [report] BuildReport: complete — containers=%d, health=%s, deployed=%d, available=%d, app_telemetry=%d",
|
|
r.Containers.Total, r.Health.Status, len(r.Stacks.Deployed), len(r.Stacks.Available), len(r.AppTelemetry))
|
|
}
|
|
|
|
return r
|
|
}
|
|
|
|
func buildContainerReport(stackMgr *stacks.Manager, metricsStore *metrics.MetricsStore) ContainerReport {
|
|
cr := ContainerReport{}
|
|
|
|
allStacks := stackMgr.GetStacks()
|
|
|
|
// Build a map of container stats from metrics store
|
|
statsMap := make(map[string]metrics.ContainerCurrentStats)
|
|
if metricsStore != nil {
|
|
if stats, err := metricsStore.QueryContainerSummary(); err == nil {
|
|
for _, s := range stats {
|
|
statsMap[s.ContainerName] = s
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, s := range allStacks {
|
|
if !s.Deployed {
|
|
continue
|
|
}
|
|
for _, c := range s.Containers {
|
|
cr.Total++
|
|
switch c.State {
|
|
case stacks.StateRunning, stacks.StateStarting:
|
|
cr.Running++
|
|
case stacks.StateUnhealthy:
|
|
cr.Unhealthy++
|
|
cr.Running++ // unhealthy containers are still running
|
|
default:
|
|
cr.Stopped++
|
|
}
|
|
|
|
detail := ContainerDetailReport{
|
|
Name: c.Name,
|
|
State: string(c.State),
|
|
}
|
|
if cs, ok := statsMap[c.Name]; ok {
|
|
detail.CPUPercent = cs.CPUPercent
|
|
detail.MemoryMB = cs.MemUsageMB
|
|
}
|
|
cr.List = append(cr.List, detail)
|
|
}
|
|
}
|
|
|
|
if cr.List == nil {
|
|
cr.List = []ContainerDetailReport{}
|
|
}
|
|
|
|
return cr
|
|
}
|
|
|
|
func buildBackupReport(cfg *config.Config, backupMgr *backup.Manager) BackupReport {
|
|
br := BackupReport{
|
|
Enabled: cfg.Backup.Enabled,
|
|
}
|
|
|
|
if backupMgr == nil {
|
|
return br
|
|
}
|
|
|
|
// Disk-tier backup (restic snapshots, integrity check, repo stats) has moved to
|
|
// the host agent (slice 8C). The controller report now covers only app-data backup
|
|
// (database dumps); restic/snapshot/integrity fields are left zero.
|
|
nextDBDump := scheduler.NextDailyRun(cfg.Backup.DBDumpSchedule)
|
|
status := backupMgr.GetFullStatus(nextDBDump)
|
|
|
|
if status.LastDBDump != nil {
|
|
t := status.LastDBDump.LastRun
|
|
br.LastDBDump = &t
|
|
}
|
|
|
|
return br
|
|
}
|
|
|
|
func buildStacksReport(stackMgr *stacks.Manager) StacksReport {
|
|
sr := StacksReport{}
|
|
allStacks := stackMgr.GetStacks()
|
|
|
|
for _, s := range allStacks {
|
|
if s.Protected {
|
|
continue
|
|
}
|
|
if s.Deployed {
|
|
sr.Deployed = append(sr.Deployed, s.Name)
|
|
} else {
|
|
sr.Available = append(sr.Available, s.Name)
|
|
}
|
|
}
|
|
|
|
if sr.Deployed == nil {
|
|
sr.Deployed = []string{}
|
|
}
|
|
if sr.Available == nil {
|
|
sr.Available = []string{}
|
|
}
|
|
|
|
return sr
|
|
}
|
|
|