summaryrefslogtreecommitdiffstats
path: root/devices/temp_freebsd.go
blob: 3e659be14607835423c7e0872f2bce343d97fe72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// +build freebsd

package devices

import (
	"log"
	"os/exec"
	"strconv"
	"strings"

	"github.com/xxxserxxx/gotop/v4/utils"
)

func init() {
	if len(devs()) == 0 {
		log.Println("temp: no thermal sensors found")
		return
	}
	RegisterTemp(update)
	RegisterDeviceList(Temperatures, devs, devs)
}

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 {
	errors := make(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
			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
}

func devs() []string {
	rv := make([]string, 0, len(sensorOIDS))
	// Check that thermal sensors are really available; they aren't in VMs
	bs, err := exec.Command("sysctl", "-a").Output()
	if err != nil {
		log.Printf("temp: failure to get system information %s", err.Error())
		return []string{}
	}
	for k, _ := range sensorOIDS {
		idx := strings.Index(string(bs), k)
		if idx < 0 {
			log.Printf("temp: no device %s found", k)
		} else {
			rv = append(rv, k)
		}
	}
	return rv
}