Files
finally-a-monolithic-linux-…/app/proc/proc.go
T
2026-08-07 14:49:21 -07:00

82 lines
1.4 KiB
Go

package proc
import (
"bufio"
"finally-a-monolithic-linux-hw-monitor/app/common"
"fmt"
"os"
"strings"
)
func (proc *Proc) Render(term common.Terminal) {
term.Header(proc.Model())
for _, driver := range proc.drivers {
driver.Render(term)
}
}
type Proc struct {
drivers []common.Driver
Info map[string]string
}
const (
VendorIntel string = "GenuineIntel"
VendorAMD string = "AuthenticAMD"
VendorUnknown string = "Unknown"
)
func GetProc() (Proc, error) {
// open and parse cpuinfo
proc := Proc{}
procFile, err := os.Open("/proc/cpuinfo")
if err != nil {
return proc, err
}
defer procFile.Close()
proc.Info = map[string]string{}
scanner := bufio.NewScanner(procFile)
if scanner.Err() != nil {
return proc, scanner.Err()
}
for scanner.Scan() {
line := scanner.Text()
line = strings.ReplaceAll(line, "\t", "")
if line != "" {
x := strings.Split(line, ": ")
if len(x) != 2 {
continue
}
key := x[0]
value := x[1]
proc.Info[key] = value
}
}
vendor := proc.Vendor()
switch vendor {
case VendorIntel:
drivers, err := NewIntelDrivers()
proc.drivers = drivers
return proc, err
case VendorAMD:
drivers, err := NewAMDDrivers()
proc.drivers = drivers
return proc, err
default:
return proc, fmt.Errorf("proc type %s is not supported", vendor)
}
}
func (proc *Proc) Vendor() string {
return proc.Info["vendor_id"]
}
func (proc *Proc) Model() string {
return proc.Info["model name"]
}