diff --git a/README.rst b/README.rst index 7d8143c..7360da0 100644 --- a/README.rst +++ b/README.rst @@ -72,6 +72,15 @@ To becomes more useful, add more information. - Platform family ex: debian - Platform Version ex: Ubuntu 13.10 +- CPUInfoStat() + + - Processer + - Vendor ID + - Model name + - cores + - Mhz + - etc... + Some codes are ported some functions from Ohai. very thanks. diff --git a/cpu.go b/cpu.go index ec139bd..708b3ba 100644 --- a/cpu.go +++ b/cpu.go @@ -20,6 +20,21 @@ type CPUTimesStat struct { Stolen float32 `json:"stolen"` } +type CPUInfoStat struct { + CPU int32 `json:"cpu"` + VendorId string `json:"vendorId"` + Family string `json:"family"` + Model string `json:"model"` + Stepping int32 `json:"stepping"` + PhysicalID string `json:"physicalId"` + CoreID string `json:"coreId"` + Cores int32 `json:"cores"` + ModelName string `json:"modelName"` + Mhz float64 `json:"mhz"` + CacheSize int32 `json:"cacheSize"` + Flags []string `json:"flags"` +} + func CPUCounts(logical bool) (int, error) { return runtime.NumCPU(), nil } @@ -28,3 +43,8 @@ func (c CPUTimesStat) String() string { s, _ := json.Marshal(c) return string(s) } + +func (c CPUInfoStat) String() string { + s, _ := json.Marshal(c) + return string(s) +} diff --git a/cpu_linux.go b/cpu_linux.go index 58e69b9..25f165a 100644 --- a/cpu_linux.go +++ b/cpu_linux.go @@ -25,6 +25,55 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) { return ret, nil } +func CPUInfo() ([]CPUInfoStat, error) { + filename := "/proc/cpuinfo" + lines, _ := readLines(filename) + + var ret []CPUInfoStat + + var c CPUInfoStat + for _, line := range lines { + fields := strings.Split(line, ":") + if len(fields) < 2{ + if c.VendorId != "" { + ret = append(ret, c) + } + continue + } + key := strings.TrimSpace(fields[0]) + value := strings.TrimSpace(fields[1]) + + switch key { + case "processor": + c = CPUInfoStat{} + c.CPU = parseInt32(value) + case "vendor_id": + c.VendorId = value + case "cpu family": + c.Family = value + case "model": + c.Model = value + case "model name": + c.ModelName = value + case "stepping": + c.Stepping = parseInt32(value) + case "cpu MHz": + c.Mhz = parseFloat64(value) + case "cache size": + c.CacheSize = parseInt32(strings.Replace(value, " KB", "", 1)) + case "physical id": + c.PhysicalID = value + case "core id": + c.CoreID = value + case "cpu cores": + c.Cores = parseInt32(value) + case "flags": + c.Flags = strings.Split(value, ",") + } + } + return ret, nil +} + func parseStatLine(line string) (*CPUTimesStat, error) { fields := strings.Fields(line) diff --git a/cpu_test.go b/cpu_test.go index e2e6b2e..0463bcf 100644 --- a/cpu_test.go +++ b/cpu_test.go @@ -43,3 +43,15 @@ func TestCPUTimeStat_String(t *testing.T) { t.Errorf("CPUTimesStat string is invalid: %v", v) } } + +func TestCpuInfo(t *testing.T) { + v, err := CPUInfo() + if err != nil { + t.Errorf("error %v", err) + } + for _, vv := range v { + if vv.ModelName == ""{ + t.Errorf("could not get CPU Info: %v", vv) + } + } +}