86 lines
1.5 KiB
Go
86 lines
1.5 KiB
Go
package proc
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
type Proc struct {
|
|
Info map[string]string
|
|
SensorReader CPUSensorReader
|
|
}
|
|
|
|
type CPUSensorReader interface {
|
|
ReadTemperatureSensors() ([]TempSensor, error)
|
|
}
|
|
|
|
type TempSensor struct {
|
|
Name string
|
|
TempC float64
|
|
}
|
|
|
|
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)
|
|
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
|
|
}
|
|
}
|
|
|
|
return proc, proc.getSensorReader()
|
|
}
|
|
|
|
func (proc *Proc) getSensorReader() error {
|
|
// get vendor string and initialize sensor reader
|
|
|
|
vendor := proc.Vendor()
|
|
|
|
if vendor == VendorIntel {
|
|
intelReader, err := NewIntelReader()
|
|
proc.SensorReader = intelReader
|
|
return err
|
|
}
|
|
|
|
if vendor == VendorAMD {
|
|
amdReader, err := NewAMDReader()
|
|
proc.SensorReader = amdReader
|
|
return err
|
|
}
|
|
|
|
return 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"]
|
|
}
|