2020-02-28 08:51:28 +08:00
|
|
|
// +build freebsd
|
|
|
|
|
|
|
|
package devices
|
|
|
|
|
|
|
|
import (
|
2020-05-30 18:35:27 +08:00
|
|
|
"log"
|
2020-02-28 08:51:28 +08:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2020-04-24 03:07:08 +08:00
|
|
|
"github.com/xxxserxxx/gotop/v4/utils"
|
2020-02-28 08:51:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-05-30 18:35:27 +08:00
|
|
|
if len(devs()) == 0 {
|
2020-06-21 05:47:48 +08:00
|
|
|
log.Println(tr.Value("error.nodevfound", "thermal sensors"))
|
2020-05-30 18:35:27 +08:00
|
|
|
return
|
|
|
|
}
|
2020-02-28 08:51:28 +08:00
|
|
|
RegisterTemp(update)
|
2020-04-24 02:01:13 +08:00
|
|
|
RegisterDeviceList(Temperatures, devs, devs)
|
2020-02-28 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var sensorOIDS = map[string]string{
|
|
|
|
"dev.cpu.0.temperature": "CPU 0 ",
|
|
|
|
"hw.acpi.thermal.tz0.temperature": "Thermal zone 0",
|
|
|
|
}
|
|
|
|
|
|
|
|
func update(temps map[string]int) map[string]error {
|
2020-04-29 01:58:33 +08:00
|
|
|
errors := make(map[string]error)
|
2020-02-28 08:51:28 +08:00
|
|
|
|
|
|
|
for k, v := range sensorOIDS {
|
2020-04-23 23:41:20 +08:00
|
|
|
if _, ok := temps[k]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-28 08:51:28 +08:00
|
|
|
output, err := exec.Command("sysctl", "-n", k).Output()
|
|
|
|
if err != nil {
|
|
|
|
errors[v] = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s1 := strings.Replace(string(output), "C", "", 1)
|
|
|
|
s2 := strings.TrimSuffix(s1, "\n")
|
|
|
|
convertedOutput := utils.ConvertLocalizedString(s2)
|
|
|
|
value, err := strconv.ParseFloat(convertedOutput, 64)
|
|
|
|
if err != nil {
|
|
|
|
errors[v] = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
temps[v] = int(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors
|
|
|
|
}
|
2020-04-23 23:41:20 +08:00
|
|
|
|
|
|
|
func devs() []string {
|
|
|
|
rv := make([]string, 0, len(sensorOIDS))
|
2020-05-30 18:35:27 +08:00
|
|
|
// Check that thermal sensors are really available; they aren't in VMs
|
|
|
|
bs, err := exec.Command("sysctl", "-a").Output()
|
|
|
|
if err != nil {
|
2020-06-21 05:47:48 +08:00
|
|
|
log.Printf(tr.Value("error.fatalfetch", "temp", err.Error()))
|
2020-05-30 18:35:27 +08:00
|
|
|
return []string{}
|
|
|
|
}
|
2020-04-23 23:41:20 +08:00
|
|
|
for k, _ := range sensorOIDS {
|
2020-05-30 18:35:27 +08:00
|
|
|
idx := strings.Index(string(bs), k)
|
|
|
|
if idx < 0 {
|
2020-06-21 05:47:48 +08:00
|
|
|
log.Printf(tr.Value("error.nodevfound", k))
|
2020-05-30 18:35:27 +08:00
|
|
|
} else {
|
|
|
|
rv = append(rv, k)
|
|
|
|
}
|
2020-04-23 23:41:20 +08:00
|
|
|
}
|
|
|
|
return rv
|
|
|
|
}
|