54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
//go:build linux
|
|
|
|
package processor
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var numberRegex *regexp.Regexp = regexp.MustCompile(`\d+`)
|
|
|
|
func getLinuxCpuInfo() (ProcessorInformation, error) {
|
|
procFile, err := os.Open("/proc/meminfo")
|
|
if err != nil {
|
|
return ProcessorInformation{}, fmt.Errorf("Could not find /proc/meminfo. Are you sure you are on linux?")
|
|
}
|
|
defer procFile.Close()
|
|
|
|
fileReader := bufio.NewScanner(procFile)
|
|
|
|
//var idleTime uint64
|
|
//var kernelTime uint64
|
|
//var userTime uint64
|
|
|
|
for fileReader.Scan() {
|
|
line := fileReader.Text()
|
|
|
|
fields := strings.Split(line, " ")
|
|
for _, field := range fields {
|
|
fmt.Print(field)
|
|
}
|
|
|
|
}
|
|
|
|
if fileReader.Err() != nil {
|
|
return ProcessorInformation{}, fmt.Errorf("There was an error while reading /proc/meninfo. Error: %v", fileReader.Err())
|
|
}
|
|
|
|
return ProcessorInformation{}, nil
|
|
}
|
|
|
|
func GetCPUInformation() ProcessorInformation {
|
|
info, err := getLinuxCpuInfo()
|
|
if err != nil {
|
|
log.Printf("Could not read processor info. Error: %v", err)
|
|
return ProcessorInformation{}
|
|
}
|
|
return info
|
|
}
|