aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/host/machine_info_linux.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/host/machine_info_linux.go')
-rw-r--r--pkg/host/machine_info_linux.go157
1 files changed, 0 insertions, 157 deletions
diff --git a/pkg/host/machine_info_linux.go b/pkg/host/machine_info_linux.go
index 8bb5e8188..fceb3ab78 100644
--- a/pkg/host/machine_info_linux.go
+++ b/pkg/host/machine_info_linux.go
@@ -4,171 +4,14 @@
package host
import (
- "bufio"
- "bytes"
- "fmt"
- "os"
"path/filepath"
- "regexp"
- "strconv"
"strings"
)
func init() {
- machineInfoFuncs = []machineInfoFunc{
- {"CPU Info", readCPUInfo},
- {"KVM", readKVMInfo},
- }
- machineModulesInfo = getModulesInfo
machineGlobsInfo = getGlobsInfo
}
-func readCPUInfo(buffer *bytes.Buffer) error {
- file, err := os.Open("/proc/cpuinfo")
- if err != nil {
- return err
- }
- defer file.Close()
-
- scanner := bufio.NewScanner(file)
- scanCPUInfo(buffer, scanner)
- return nil
-}
-
-func scanCPUInfo(buffer *bytes.Buffer, scanner *bufio.Scanner) {
- keyIndices := make(map[string]int)
- type keyValues struct {
- key string
- values []string
- }
- var info []keyValues
-
- for scanner.Scan() {
- splitted := strings.Split(scanner.Text(), ":")
- if len(splitted) != 2 {
- continue
- }
- key := strings.TrimSpace(splitted[0])
- val := strings.TrimSpace(splitted[1])
-
- if idx, ok := keyIndices[key]; !ok {
- idx = len(keyIndices)
- keyIndices[key] = idx
- info = append(info, keyValues{key, []string{val}})
- } else {
- info[idx].values = append(info[idx].values, val)
- }
- }
-
- for _, kv := range info {
- // It is guaranteed that len(vals) >= 1
- key := kv.key
- vals := kv.values
- if allEqual(vals) {
- fmt.Fprintf(buffer, "%-20s: %s\n", key, vals[0])
- } else {
- fmt.Fprintf(buffer, "%-20s: %s\n", key, strings.Join(vals, ", "))
- }
- }
-}
-
-func allEqual(slice []string) bool {
- if len(slice) == 0 {
- return true
- }
- for i := 1; i < len(slice); i++ {
- if slice[i] != slice[0] {
- return false
- }
- }
- return true
-}
-
-func readKVMInfo(buffer *bytes.Buffer) error {
- files, err := os.ReadDir("/sys/module/")
- if err != nil {
- return err
- }
-
- for _, file := range files {
- name := file.Name()
- if !strings.HasPrefix(name, "kvm") {
- continue
- }
-
- paramPath := filepath.Join("/sys", "module", name, "parameters")
- params, err := os.ReadDir(paramPath)
- if err != nil {
- if os.IsNotExist(err) {
- continue
- }
- return err
- }
-
- if len(params) == 0 {
- continue
- }
-
- fmt.Fprintf(buffer, "/sys/module/%s:\n", name)
- for _, key := range params {
- keyName := key.Name()
- data, err := os.ReadFile(filepath.Join(paramPath, keyName))
- if err != nil {
- return err
- }
- fmt.Fprintf(buffer, "\t%s: ", keyName)
- buffer.Write(data)
- }
- buffer.WriteByte('\n')
- }
- return nil
-}
-
-func getModuleTextAddr(moduleName string) (uint64, error) {
- addrPath := filepath.Join("/sys", "module", moduleName, "sections", ".text")
- addrContent, err := os.ReadFile(addrPath)
- if err != nil {
- return 0, fmt.Errorf("could not read module .text address file: %w", err)
- }
- addrString := strings.TrimSpace(string(addrContent))
- addr, err := strconv.ParseUint(addrString, 0, 64)
- if err != nil {
- return 0, fmt.Errorf("address parsing error in %v: %w", moduleName, err)
- }
- return addr, nil
-}
-
-func getModulesInfo() ([]KernelModule, error) {
- var modules []KernelModule
- re := regexp.MustCompile(`(\w+) ([0-9]+) .*(0[x|X][a-fA-F0-9]+)[^\n]*`)
- modulesText, _ := os.ReadFile("/proc/modules")
- for _, m := range re.FindAllSubmatch(modulesText, -1) {
- name := string(m[1])
- modAddr, err := strconv.ParseUint(string(m[3]), 0, 64)
- if err != nil {
- // /proc/modules is broken, bail out.
- return nil, fmt.Errorf("address parsing error in /proc/modules: %w", err)
- }
- textAddr, err := getModuleTextAddr(name)
- if err != nil {
- // Module address unavailable, .text is probably 0. Skip this module.
- continue
- }
- modSize, err := strconv.ParseUint(string(m[2]), 0, 64)
- if err != nil {
- // /proc/modules is broken, bail out.
- return nil, fmt.Errorf("module size parsing error in /proc/modules: %w", err)
- }
- offset := modAddr - textAddr
- modules = append(modules, KernelModule{
- Name: string(m[1]),
- Addr: textAddr,
- Size: modSize - offset,
- })
- }
- return modules, nil
-}
-
func getGlobsInfo(globs map[string]bool) (map[string][]string, error) {
var err error
files := make(map[string][]string, len(globs))