Created
January 13, 2015 00:08
-
-
Save lykkin/a03638c2a9d87c160f97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +build darwin | |
package plugins | |
import ( | |
"os/exec" | |
"log" | |
"strconv" | |
) | |
type Cpu struct { | |
Name string | |
} | |
func GetCpuMetric(metric string) (string, error) { | |
cmd := exec.Command("sysctl", "-n", metric) | |
result, err := cmd.Output() | |
return string(result), err | |
} | |
// Cpu.CollectData returns cpu information about the system. | |
// Returns a string and a map[string]MetricValue. | |
func (m *Cpu) CollectData() (string, map[string]*MetricValue, error) { | |
identifiers := map[string]string{ | |
"real": "hw.physicalcpu", | |
"total": "hw.logicalcpu", | |
"mhz": "hw.cpufrequency", | |
"vendor_id": "machdep.cpu.brand_string", | |
"model_name": "machdep.cpu.model", | |
"model": "machdep.cpu.family", | |
"family": "machdep.cpu.stepping", | |
"stepping": "machdep.cpu.features", | |
} | |
data := make(map[string]*MetricValue) | |
for metric, sysMetric := range identifiers { | |
val, err := GetCpuMetric(sysMetric) | |
if err != nil { | |
log.Fatal(err) | |
} | |
data[metric] = &MetricValue{val} | |
} | |
// Convert the mhz field from hertz to mhz | |
mhz := strconv.Atoi(*data["mhz"].Val)/1000 | |
*data["mhz"].Val = string(mhz) | |
return m.Name, data, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment