75 lines
1.5 KiB
Go
75 lines
1.5 KiB
Go
package syscheck
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"golang.org/x/sys/cpu"
|
|
)
|
|
|
|
type CPUInfo struct {
|
|
Cores int64
|
|
FrequencyMHz int64
|
|
SupportAES bool
|
|
IsX86V2 bool
|
|
}
|
|
|
|
func GetCPUInfo(ctx context.Context) (CPUInfo, error) {
|
|
info := CPUInfo{
|
|
Cores: int64(runtime.NumCPU()),
|
|
}
|
|
|
|
// Parse /proc/cpuinfo to get CPU frequency and model info
|
|
file, err := os.Open("/proc/cpuinfo")
|
|
if err != nil {
|
|
return info, fmt.Errorf("failed to open /proc/cpuinfo: %w", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
|
|
// Parse CPU MHz
|
|
if strings.HasPrefix(line, "cpu MHz") {
|
|
parts := strings.Split(line, ":")
|
|
if len(parts) == 2 {
|
|
freqStr := strings.TrimSpace(parts[1])
|
|
if freq, err := strconv.ParseFloat(freqStr, 64); err == nil {
|
|
info.FrequencyMHz = int64(freq)
|
|
break // Get first CPU frequency
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
return info, fmt.Errorf("failed to read /proc/cpuinfo: %w", err)
|
|
}
|
|
|
|
// Check CPU features using x/sys/cpu package
|
|
if runtime.GOARCH == "amd64" || runtime.GOARCH == "386" {
|
|
// Check AES-NI support
|
|
info.SupportAES = cpu.X86.HasAES
|
|
|
|
// Check x86-64-v2 support
|
|
// x86-64-v2 requires: SSE3, SSSE3, SSE4.1, SSE4.2, POPCNT
|
|
info.IsX86V2 = cpu.X86.HasSSE3 &&
|
|
cpu.X86.HasSSSE3 &&
|
|
cpu.X86.HasSSE41 &&
|
|
cpu.X86.HasSSE42 &&
|
|
cpu.X86.HasPOPCNT
|
|
} else {
|
|
// For ARM or other architectures
|
|
info.SupportAES = false
|
|
info.IsX86V2 = false
|
|
}
|
|
|
|
return info, nil
|
|
}
|