package inspur import ( "fmt" "sort" "strings" "git.mchus.pro/mchus/logpile/internal/models" ) // applyMemoryStatusFromEvents explains DIMM state reported by the // component.log snapshot using the BMC journal (IDL/SEL Assert/Deassert). // The snapshot stays authoritative for the current status; the journal // supplies transition history, the fault description and when the // currently asserted fault started. Snapshot state is never duplicated // into the event log. func applyMemoryStatusFromEvents(hw *models.HardwareConfig, events []models.Event) { if hw == nil || len(hw.Memory) == 0 { return } bySlot := make(map[string][]models.Event) for _, event := range events { if !strings.EqualFold(strings.TrimSpace(event.SensorType), "memory") || event.Timestamp.IsZero() { continue } etype := strings.ToLower(strings.TrimSpace(event.EventType)) if etype != "assert" && etype != "deassert" { continue } slot := strings.ToUpper(strings.TrimSpace(event.SensorName)) if slot == "" { continue } bySlot[slot] = append(bySlot[slot], event) } for i := range hw.Memory { dimm := &hw.Memory[i] journal := bySlot[strings.ToUpper(strings.TrimSpace(dimm.Slot))] sort.SliceStable(journal, func(a, b int) bool { return journal[a].Timestamp.Before(journal[b].Timestamp) }) var active *models.Event lastStatus := "" for j := range journal { event := journal[j] status := "OK" if eventIsDeassert(event) { active = nil } else { status = "Warning" if event.Severity == models.SeverityCritical { status = "Critical" } active = &journal[j] } if status == lastStatus { continue } lastStatus = status dimm.StatusHistory = append(dimm.StatusHistory, models.StatusHistoryEntry{ Status: status, ChangedAt: event.Timestamp, Details: strings.TrimSpace(event.Description), }) } if !memoryStatusIsFault(dimm.Status) { continue } if active != nil { ts := active.Timestamp dimm.StatusChangedAt = &ts if strings.TrimSpace(dimm.ErrorDescription) == "" { dimm.ErrorDescription = strings.Join(strings.Fields(active.Description), " ") } continue } if strings.TrimSpace(dimm.ErrorDescription) == "" { dimm.ErrorDescription = fmt.Sprintf("BMC reported %s at log collection; no matching journal entry", dimm.Status) } } } func memoryStatusIsFault(status string) bool { switch strings.ToLower(strings.TrimSpace(status)) { case "warning", "error", "critical": return true default: return false } }