skeleton for testing purposes
This commit is contained in:
1 parent
8076b30e3f
commit
71e4f1294e
11 files changed
+273
No files matched your search
@@ -0,0 +1,49 @@
|
||||
package system_check
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var numberRegex *regexp.Regexp = regexp.MustCompile(`\d+`)
|
||||
|
||||
func getLinuxRamInfo() (MemoryInformation, error) {
|
||||
memoryFile, err := os.Open("/proc/meminfo")
|
||||
if err != nil {
|
||||
return MemoryInformation{}, fmt.Errorf("Could not find /proc/meminfo. Are you sure you are on linux?")
|
||||
}
|
||||
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") {
|
||||
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") {
|
||||
AvailMemory, err = strconv.ParseUint(numberRegex.FindString(line), 10, 64)
|
||||
if err != nil {
|
||||
return MemoryInformation{}, fmt.Errorf("Could not parse available memory from /proc/meminfo")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return MemoryInformation{
|
||||
SystemMemory: TotalMemory,
|
||||
AvailableMemory: AvailMemory,
|
||||
}, nil
|
||||
}
|
||||
Reference in new issue
Block a user