From 1bcdc124ab50c9efdef1dc9c610923c5da3ec029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 5 Feb 2022 22:27:18 +0200 Subject: [PATCH 1/2] [common] add ability to pass env to invoker commands via context --- internal/common/common.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/common/common.go b/internal/common/common.go index 4f6df9a..9c21adf 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -26,6 +26,11 @@ import ( "time" ) +const ( + // InvokerCtxKeyEnv is a Invoker context value key for setting the command environment, a slice of strings + InvokerCtxKeyEnv = "invoker-env" +) + var ( Timeout = 3 * time.Second ErrTimeout = errors.New("command timed out") @@ -47,6 +52,11 @@ func (i Invoke) Command(name string, arg ...string) ([]byte, error) { func (i Invoke) CommandWithContext(ctx context.Context, name string, arg ...string) ([]byte, error) { cmd := exec.CommandContext(ctx, name, arg...) + env := ctx.Value(InvokerCtxKeyEnv) + if cmdEnv, ok := env.([]string); ok { + cmd.Env = cmdEnv + } + var buf bytes.Buffer cmd.Stdout = &buf cmd.Stderr = &buf From d5017bbd7e7e64c3272f43e405f200f5072df486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 5 Feb 2022 22:28:31 +0200 Subject: [PATCH 2/2] [host][freebsd] implement SensorsTemperaturesWithContext --- host/host_freebsd.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/host/host_freebsd.go b/host/host_freebsd.go index 2c9aa9d..39db11c 100644 --- a/host/host_freebsd.go +++ b/host/host_freebsd.go @@ -4,12 +4,15 @@ package host import ( + "bufio" "bytes" "context" "encoding/binary" "io/ioutil" "math" "os" + "os/exec" + "strconv" "strings" "unsafe" @@ -142,7 +145,59 @@ func getUsersFromUtmp(utmpfile string) ([]UserStat, error) { } func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { - return []TemperatureStat{}, common.ErrNotImplementedError + sysctl, err := exec.LookPath("sysctl") + var ret []TemperatureStat + if err != nil { + return ret, err + } + + out, err := invoke.CommandWithContext(context.WithValue(ctx, common.InvokerCtxKeyEnv, []string{"LC_ALL=C"}), sysctl, "-a") + if err != nil { + return ret, err + } + + tjmaxs := make(map[string]float64) + + var warns Warnings + + sc := bufio.NewScanner(bytes.NewReader(out)) + for sc.Scan() { + // hw.acpi.thermal.tz1.temperature: 29.9C + // dev.cpu.7.temperature: 28.0C + // dev.cpu.7.coretemp.tjmax: 100.0C + flds := strings.SplitN(sc.Text(), ":", 2) + if len(flds) != 2 || + !(strings.HasSuffix(flds[0], ".temperature") || + strings.HasSuffix(flds[0], ".coretemp.tjmax")) || + !strings.HasSuffix(flds[1], "C") { + continue + } + v, err := strconv.ParseFloat(strings.TrimSuffix(strings.TrimSpace(flds[1]), "C"), 64) + if err != nil { + warns.Add(err) + continue + } + k := strings.TrimSuffix(flds[0], ".temperature") + if k == flds[0] { + k = strings.TrimSuffix(flds[0], ".coretemp.tjmax") + tjmaxs[k] = v + continue + } + ts := TemperatureStat{ + SensorKey: k, + Temperature: v, + } + ret = append(ret, ts) + } + + for i, ts := range ret[:] { + if tjmax, ok := tjmaxs[ts.SensorKey]; ok { + ts.Critical = tjmax + ret[i] = ts + } + } + + return ret, warns.Reference() } func KernelVersionWithContext(ctx context.Context) (string, error) {