diff --git a/pkg/analyze/host_cpu.go b/pkg/analyze/host_cpu.go index 97d9f04e2..cb24c43f2 100644 --- a/pkg/analyze/host_cpu.go +++ b/pkg/analyze/host_cpu.go @@ -2,6 +2,7 @@ package analyzer import ( "encoding/json" + "fmt" "slices" "strconv" "strings" @@ -33,94 +34,40 @@ func (a *AnalyzeHostCPU) IsExcluded() (bool, error) { return isExcluded(a.hostAnalyzer.Exclude) } -func (a *AnalyzeHostCPU) Analyze( - getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents, -) ([]*AnalyzeResult, error) { - hostAnalyzer := a.hostAnalyzer - - contents, err := getCollectedFileContents(collect.HostCPUPath) - if err != nil { - return nil, errors.Wrap(err, "failed to get collected file") - } +func (a *AnalyzeHostCPU) CheckCondition(when string, data []byte) (bool, error) { cpuInfo := collect.CPUInfo{} - if err := json.Unmarshal(contents, &cpuInfo); err != nil { - return nil, errors.Wrap(err, "failed to unmarshal cpu info") - } - - result := AnalyzeResult{ - Title: a.Title(), + if err := json.Unmarshal(data, &cpuInfo); err != nil { + return false, fmt.Errorf("failed to unmarshal data into CPUInfo: %v", err) } - for _, outcome := range hostAnalyzer.Outcomes { - - if outcome.Fail != nil { - if outcome.Fail.When == "" { - result.IsFail = true - result.Message = outcome.Fail.Message - result.URI = outcome.Fail.URI - - return []*AnalyzeResult{&result}, nil - } - - isMatch, err := compareHostCPUConditionalToActual(outcome.Fail.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch) - if err != nil { - return nil, errors.Wrap(err, "failed to compare") - } - - if isMatch { - result.IsFail = true - result.Message = outcome.Fail.Message - result.URI = outcome.Fail.URI - - return []*AnalyzeResult{&result}, nil - } - } else if outcome.Warn != nil { - if outcome.Warn.When == "" { - result.IsWarn = true - result.Message = outcome.Warn.Message - result.URI = outcome.Warn.URI - - return []*AnalyzeResult{&result}, nil - } + return compareHostCPUConditionalToActual(when, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch) - isMatch, err := compareHostCPUConditionalToActual(outcome.Warn.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch) - if err != nil { - return nil, errors.Wrap(err, "failed to compare") - } - - if isMatch { - result.IsWarn = true - result.Message = outcome.Warn.Message - result.URI = outcome.Warn.URI - - return []*AnalyzeResult{&result}, nil - } - } else if outcome.Pass != nil { - if outcome.Pass.When == "" { - result.IsPass = true - result.Message = outcome.Pass.Message - result.URI = outcome.Pass.URI - - return []*AnalyzeResult{&result}, nil - } - - isMatch, err := compareHostCPUConditionalToActual(outcome.Pass.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch) - if err != nil { - return nil, errors.Wrap(err, "failed to compare") - } +} - if isMatch { - result.IsPass = true - result.Message = outcome.Pass.Message - result.URI = outcome.Pass.URI +func (a *AnalyzeHostCPU) Analyze( + getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents, +) ([]*AnalyzeResult, error) { + result := AnalyzeResult{Title: a.Title()} + + // Use the generic function to collect both local and remote data + collectedContents, err := retrieveCollectedContents( + getCollectedFileContents, + collect.HostCPUPath, // Local path + collect.NodeInfoBaseDir, // Remote base directory + collect.HostCPUFileName, // Remote file name + ) + if err != nil { + return []*AnalyzeResult{&result}, err + } - return []*AnalyzeResult{&result}, nil - } - } + results, err := analyzeHostCollectorResults(collectedContents, a.hostAnalyzer.Outcomes, a.CheckCondition, a.Title()) + if err != nil { + return nil, errors.Wrap(err, "failed to analyze OS version") } - return []*AnalyzeResult{&result}, nil + return results, nil + } func doCompareHostCPUMicroArchitecture(microarch string, flags []string) (res bool, err error) { diff --git a/pkg/collect/host_cpu.go b/pkg/collect/host_cpu.go index 4e7bb1ed8..20ab58eda 100644 --- a/pkg/collect/host_cpu.go +++ b/pkg/collect/host_cpu.go @@ -18,6 +18,7 @@ type CPUInfo struct { } const HostCPUPath = `host-collectors/system/cpu.json` +const HostCPUFileName = `cpu.json` type CollectHostCPU struct { hostCollector *troubleshootv1beta2.CPU