From ad2a44d36dd602b770595daef7e46a9d077c41a1 Mon Sep 17 00:00:00 2001 From: Dottere Date: Mon, 5 Oct 2026 14:53:31 +0200 Subject: [PATCH] fixed bugs in linux_ram:getLinuxRamInfo --- internal/system_check/linux_ram.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/internal/system_check/linux_ram.go b/internal/system_check/linux_ram.go index f004788..d0465a6 100644 --- a/internal/system_check/linux_ram.go +++ b/internal/system_check/linux_ram.go @@ -18,35 +18,40 @@ func getLinuxRamInfo() (MemoryInformation, error) { if err != nil { return MemoryInformation{}, fmt.Errorf("Could not find /proc/meminfo. Are you sure you are on linux?") } + defer memoryFile.Close() + fileReader := bufio.NewScanner(memoryFile) var TotalMemory uint64 var AvailMemory uint64 for fileReader.Scan() { - - if fileReader.Err() != nil { - return MemoryInformation{}, fmt.Errorf("There was an error while reading /proc/meninfo. Error: %v", fileReader.Err()) - } - line := fileReader.Text() - if strings.Contains(line, "MemTotal") { + if strings.HasPrefix(line, "MemTotal:") { TotalMemory, err = strconv.ParseUint(numberRegex.FindString(line), 10, 64) if err != nil { return MemoryInformation{}, fmt.Errorf("Could not parse total memory from /proc/meminfo") } - } else if strings.Contains(line, "MemAvailable") { + } else if strings.HasPrefix(line, "MemAvailable:") { AvailMemory, err = strconv.ParseUint(numberRegex.FindString(line), 10, 64) if err != nil { return MemoryInformation{}, fmt.Errorf("Could not parse available memory from /proc/meminfo") } } + + if TotalMemory > 0 && AvailMemory > 0 { + break + } + } + + if fileReader.Err() != nil { + return MemoryInformation{}, fmt.Errorf("There was an error while reading /proc/meninfo. Error: %v", fileReader.Err()) } return MemoryInformation{ - SystemMemory: TotalMemory, - AvailableMemory: AvailMemory, + SystemMemory: TotalMemory * 1024, + AvailableMemory: AvailMemory * 1024, }, nil }