package inspur import ( "bytes" "compress/gzip" "fmt" "io" "regexp" "strings" "git.mchus.pro/mchus/logpile/internal/models" ) const componentReplaceEventType = "Component Replace" var ( reComponentReplace = regexp.MustCompile(`(?i)^Component Replace\s+(\S+)\s+(?:SN\s+)?changed from\s+(\S+)\s+to\s+(\S+?)!?(?:\s+-\s+(?:Assert|Deassert))?\s*$`) reInspurDIMMCode = regexp.MustCompile(`(?i)^DIMM(\d)(\d)(\d)$`) ) // ParseMaintenanceLog extracts BMC component replacement records // ("Component Replace DIMM021 SN changed from NULL to …") from // maintenance.log. The BMC writes them only there, not to IDL/SEL, and they // are the only trace of when a DIMM, CPU or PSU was installed or swapped. // Other maintenance.log lines are daemon chatter and are ignored. func ParseMaintenanceLog(content []byte) []models.Event { var events []models.Event for _, event := range ParseIDLLog(content) { match := reComponentReplace.FindStringSubmatch(strings.TrimSpace(event.Description)) if match == nil { continue } device, from, to := match[1], match[2], match[3] sensorName := device if slot, ok := inspurDIMMCodeToSlot(device); ok { sensorName = slot } action := "replaced" switch { case strings.EqualFold(from, "NULL"): action = "installed" case strings.EqualFold(to, "NULL"): action = "removed" } description := fmt.Sprintf("%s %s: SN %s → %s", sensorName, action, from, to) if sensorName != device { description = fmt.Sprintf("%s (%s) %s: SN %s → %s", sensorName, device, action, from, to) } event.EventType = componentReplaceEventType event.SensorName = sensorName event.Severity = models.SeverityInfo event.Description = description event.RawData = strings.TrimSpace(match[0]) events = append(events, event) } return events } // inspurDIMMCodeToSlot maps the BMC asset code DIMM // (e.g. DIMM021) to the inventory slot name CPU0_C2D1. func inspurDIMMCodeToSlot(code string) (string, bool) { match := reInspurDIMMCode.FindStringSubmatch(strings.TrimSpace(code)) if match == nil { return "", false } return fmt.Sprintf("CPU%s_C%sD%s", match[1], match[2], match[3]), true } // decodeGzipLog returns the plain text of a rotated "*.log.N.gz" member. func decodeGzipLog(content []byte) []byte { gz, err := gzip.NewReader(bytes.NewReader(content)) if err != nil { return nil } defer gz.Close() out, err := io.ReadAll(io.LimitReader(gz, 64<<20)) if err != nil { return nil } return out }