diff options
Diffstat (limited to 'devices')
-rw-r--r-- | devices/devices.go | 21 | ||||
-rw-r--r-- | devices/temp_darwin.go | 15 | ||||
-rw-r--r-- | devices/temp_freebsd.go | 12 | ||||
-rw-r--r-- | devices/temp_linux.go | 24 | ||||
-rw-r--r-- | devices/temp_openbsd.go | 5 | ||||
-rw-r--r-- | devices/temp_windows.go | 19 |
6 files changed, 90 insertions, 6 deletions
diff --git a/devices/devices.go b/devices/devices.go index 91a8815..efdc404 100644 --- a/devices/devices.go +++ b/devices/devices.go @@ -2,7 +2,13 @@ package devices import "log" +const ( + Temperatures = "Temperatures" +) + +var Domains []string = []string{Temperatures} var shutdownFuncs []func() error +var _devs map[string][]string // RegisterShutdown stores a function to be called by gotop on exit, allowing // extensions to properly release resources. Extensions should register a @@ -24,3 +30,18 @@ func Shutdown() { } } } + +func RegisterDeviceList(typ string, f func() []string) { + if _devs == nil { + _devs = make(map[string][]string) + } + if ls, ok := _devs[typ]; ok { + _devs[typ] = append(ls, f()...) + return + } + _devs[typ] = f() +} + +func Devices(domain string) []string { + return _devs[domain] +} diff --git a/devices/temp_darwin.go b/devices/temp_darwin.go index 4e068a4..fe98fc2 100644 --- a/devices/temp_darwin.go +++ b/devices/temp_darwin.go @@ -6,6 +6,7 @@ import smc "github.com/xxxserxxx/iSMC" func init() { RegisterTemp(update) + RegisterDeviceList(Temperatures, devs) ts = make(map[string]float32) } @@ -17,7 +18,19 @@ func update(temps map[string]int) map[string]error { return map[string]error{"temps": err} } for k, v := range ts { - temps[k] = int(v + 0.5) + if _, ok := temps[k]; ok { + temps[k] = int(v + 0.5) + } } return nil } + +// TODO: Set reasonable default devices +// CPU (TC[01]P), GPU (TG0P), Memory (Ts0S) and Disk (TH0P) +func devs() []string { + rv := make([]string, len(smc.AppleTemp)) + for i, v := range smc.AppleTemp { + rv[i] = v.Desc + } + return rv +} diff --git a/devices/temp_freebsd.go b/devices/temp_freebsd.go index 54355fc..a0277a6 100644 --- a/devices/temp_freebsd.go +++ b/devices/temp_freebsd.go @@ -12,6 +12,7 @@ import ( func init() { RegisterTemp(update) + RegisterDeviceList(Temperatures, devs) } var sensorOIDS = map[string]string{ @@ -23,6 +24,9 @@ func update(temps map[string]int) map[string]error { var errors map[string]error for k, v := range sensorOIDS { + if _, ok := temps[k]; !ok { + continue + } output, err := exec.Command("sysctl", "-n", k).Output() if err != nil { errors[v] = err @@ -43,3 +47,11 @@ func update(temps map[string]int) map[string]error { return errors } + +func devs() []string { + rv := make([]string, 0, len(sensorOIDS)) + for k, _ := range sensorOIDS { + rv = append(rv, k) + } + return rv +} diff --git a/devices/temp_linux.go b/devices/temp_linux.go index ec4ff95..2ca01f5 100644 --- a/devices/temp_linux.go +++ b/devices/temp_linux.go @@ -10,6 +10,7 @@ import ( func init() { RegisterTemp(getTemps) + RegisterDeviceList(Temperatures, devs) } func getTemps(temps map[string]int) map[string]error { @@ -18,12 +19,31 @@ func getTemps(temps map[string]int) map[string]error { return map[string]error{"psHost": err} } for _, sensor := range sensors { + // removes '_input' from the end of the sensor name + idx := strings.Index(sensor.SensorKey, "_input") + if idx >= 0 { + label := sensor.SensorKey[:idx] + if _, ok := temps[label]; ok { + temps[label] = int(sensor.Temperature) + } + } + } + return nil +} + +func devs() []string { + sensors, err := psHost.SensorsTemperatures() + if err != nil { + return []string{} + } + rv := make([]string, 0, len(sensors)) + for _, sensor := range sensors { // only sensors with input in their name are giving us live temp info if strings.Contains(sensor.SensorKey, "input") && sensor.Temperature != 0 { // removes '_input' from the end of the sensor name label := sensor.SensorKey[:strings.Index(sensor.SensorKey, "_input")] - temps[label] = int(sensor.Temperature) + rv = append(rv, label) } } - return nil + return rv } diff --git a/devices/temp_openbsd.go b/devices/temp_openbsd.go index 410cc4a..d889d78 100644 --- a/devices/temp_openbsd.go +++ b/devices/temp_openbsd.go @@ -15,6 +15,7 @@ import ( "unsafe" ) +// TODO: Add filtering. Getting the temperature sensor names is non-trivial for OpenBSD, and until I can test it, leave it unimplemented func init() { RegisterTemp(update) } @@ -66,7 +67,9 @@ func getTemp(temps map[string]int, mib []C.int, mlen int, snsrdev *C.struct_sens key := C.GoString(&snsrdev.xname[0]) + ".temp" + strconv.Itoa(index) temp := int((snsr.value - 273150000.0) / 1000000.0) - temps[key] = temp + if _, ok := temps[key]; ok { + temps[key] = temp + } } } } diff --git a/devices/temp_windows.go b/devices/temp_windows.go index 230e902..54fe0eb 100644 --- a/devices/temp_windows.go +++ b/devices/temp_windows.go @@ -8,6 +8,7 @@ import ( func init() { RegisterTemp(update) + RegisterDeviceList(Temperatures, devs) } func update(temps map[string]int) map[string]error { @@ -16,9 +17,23 @@ func update(temps map[string]int) map[string]error { return map[string]error{"gopsutil": err} } for _, sensor := range sensors { - if sensor.Temperature != 0 { - temps[sensor.SensorKey] = int(sensor.Temperature) + if _, ok := temps[sensor.SensorKey]; ok { + temps[sensor.SensorKey] = int(sensor.Temperature + 0.5) } } return nil } + +func devs() []string { + sensors, err := psHost.SensorsTemperatures() + if err != nil { + return []string{} + } + rv := make([]string, 0, len(sensors)) + for _, sensor := range sensors { + if sensor.Temperature != 0 { + rv = append(rv, sensor.SensorKey) + } + } + return rv +} |