82 lines
1.4 KiB
Go
82 lines
1.4 KiB
Go
package proc
|
|
|
|
import (
|
|
"bufio"
|
|
"finally-a-monolithic-linux-hw-monitor/app/common"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
VendorIntel string = "GenuineIntel"
|
|
VendorAMD string = "AuthenticAMD"
|
|
VendorUnknown string = "Unknown"
|
|
)
|
|
|
|
type Proc struct {
|
|
drivers []common.Driver
|
|
Info map[string]string
|
|
}
|
|
|
|
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) Render(term common.Terminal) {
|
|
term.Header(proc.model())
|
|
for _, driver := range proc.drivers {
|
|
driver.Render(term)
|
|
}
|
|
}
|
|
|
|
func (proc *Proc) vendor() string {
|
|
return proc.Info["vendor_id"]
|
|
}
|
|
|
|
func (proc *Proc) model() string {
|
|
return proc.Info["model name"]
|
|
}
|