Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Regression in temps introduced by gopsutil update #11301

Closed
wants to merge 10 commits into from
22 changes: 0 additions & 22 deletions plugins/inputs/temp/temp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package temp

import (
_ "embed"
"fmt"
"strings"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
Expand All @@ -23,26 +21,6 @@ func (*Temperature) SampleConfig() string {
return sampleConfig
}

func (t *Temperature) Gather(acc telegraf.Accumulator) error {
temps, err := t.ps.Temperature()
if err != nil {
if strings.Contains(err.Error(), "not implemented yet") {
return fmt.Errorf("plugin is not supported on this platform: %v", err)
}
return fmt.Errorf("error getting temperatures info: %s", err)
}
for _, temp := range temps {
tags := map[string]string{
"sensor": temp.SensorKey,
}
fields := map[string]interface{}{
"temp": temp.Temperature,
}
acc.AddFields("temp", fields, tags)
}
return nil
}

func init() {
inputs.Add("temp", func() telegraf.Input {
return &Temperature{ps: system.NewSystemPS()}
Expand Down
39 changes: 39 additions & 0 deletions plugins/inputs/temp/temp_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//go:build linux
// +build linux

package temp

import (
"fmt"
"strings"

"github.com/influxdata/telegraf"
)

func (t *Temperature) Gather(acc telegraf.Accumulator) error {
temps, err := t.ps.Temperature()
if err != nil {
if strings.Contains(err.Error(), "not implemented yet") {
return fmt.Errorf("plugin is not supported on this platform: %v", err)
}
return fmt.Errorf("error getting temperatures info: %s", err)
}
for _, temp := range temps {
acc.AddFields(
"temp",
map[string]interface{}{"temp": temp.Critical},
map[string]string{"sensor": temp.SensorKey + "_crit"},
)
acc.AddFields(
"temp",
map[string]interface{}{"temp": temp.High},
map[string]string{"sensor": temp.SensorKey + "_max"},
)
acc.AddFields(
"temp",
map[string]interface{}{"temp": temp.Temperature},
map[string]string{"sensor": temp.SensorKey + "_input"},
)
}
return nil
}
29 changes: 29 additions & 0 deletions plugins/inputs/temp/temp_nolinux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !linux
// +build !linux

package temp

import (
"fmt"
"strings"

"github.com/influxdata/telegraf"
)

func (t *Temperature) Gather(acc telegraf.Accumulator) error {
temps, err := t.ps.Temperature()
if err != nil {
if strings.Contains(err.Error(), "not implemented yet") {
return fmt.Errorf("plugin is not supported on this platform: %w", err)
}
return fmt.Errorf("error getting temperatures info: %w", err)
}
for _, temp := range temps {
acc.AddFields(
"temp",
map[string]interface{}{"temp": temp.Temperature},
map[string]string{"sensor": temp.SensorKey},
)
}
return nil
}
128 changes: 125 additions & 3 deletions plugins/inputs/temp/temp_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package temp

import (
"os"
"path/filepath"
"runtime"
"testing"
"time"

"github.com/shirou/gopsutil/v3/host"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/inputs/system"
"github.com/influxdata/telegraf/testutil"
)
Expand All @@ -16,9 +22,17 @@ func TestTemperature(t *testing.T) {
defer mps.AssertExpectations(t)
var acc testutil.Accumulator

ts := host.TemperatureStat{
SensorKey: "coretemp_sensor1_crit",
Temperature: 60.5,
var ts host.TemperatureStat
if runtime.GOOS == "linux" {
ts = host.TemperatureStat{
SensorKey: "coretemp_sensor1",
Critical: 60.5,
}
} else {
ts = host.TemperatureStat{
SensorKey: "coretemp_sensor1_crit",
Temperature: 60.5,
}
}

mps.On("Temperature").Return([]host.TemperatureStat{ts}, nil)
Expand All @@ -35,3 +49,111 @@ func TestTemperature(t *testing.T) {
}
acc.AssertContainsTaggedFields(t, "temp", expectedFields, expectedTags)
}

func TestTemperatureLinux(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("skipping test on non-Linux OS")
}

expected := []telegraf.Metric{
metric.New(
"temp",
map[string]string{"sensor": "nvme_composite_crit"},
map[string]interface{}{"temp": 84.85},
time.Unix(0, 0),
),
metric.New(
"temp",
map[string]string{"sensor": "nvme_composite_max"},
map[string]interface{}{"temp": 81.85},
time.Unix(0, 0),
),
metric.New(
"temp",
map[string]string{"sensor": "nvme_composite_input"},
map[string]interface{}{"temp": 35.85},
time.Unix(0, 0),
),
metric.New(
"temp",
map[string]string{"sensor": "nvme_sensor_1_crit"},
map[string]interface{}{"temp": 0.0},
time.Unix(0, 0),
),
metric.New(
"temp",
map[string]string{"sensor": "nvme_sensor_1_max"},
map[string]interface{}{"temp": 65261.85},
time.Unix(0, 0),
),
metric.New(
"temp",
map[string]string{"sensor": "nvme_sensor_1_input"},
map[string]interface{}{"temp": 35.85},
time.Unix(0, 0),
),
metric.New(
"temp",
map[string]string{"sensor": "nvme_sensor_2_crit"},
map[string]interface{}{"temp": 0.0},
time.Unix(0, 0),
),
metric.New(
"temp",
map[string]string{"sensor": "nvme_sensor_2_max"},
map[string]interface{}{"temp": 65261.85},
time.Unix(0, 0),
),
metric.New(
"temp",
map[string]string{"sensor": "nvme_sensor_2_input"},
map[string]interface{}{"temp": 38.85},
time.Unix(0, 0),
),
metric.New(
"temp",
map[string]string{"sensor": "k10temp_tctl_crit"},
map[string]interface{}{"temp": 0.0},
time.Unix(0, 0),
),
metric.New(
"temp",
map[string]string{"sensor": "k10temp_tctl_max"},
map[string]interface{}{"temp": 0.0},
time.Unix(0, 0),
),
metric.New(
"temp",
map[string]string{"sensor": "k10temp_tctl_input"},
map[string]interface{}{"temp": 33.25},
time.Unix(0, 0),
),
metric.New(
"temp",
map[string]string{"sensor": "k10temp_tccd1_crit"},
map[string]interface{}{"temp": 0.0},
time.Unix(0, 0),
),
metric.New(
"temp",
map[string]string{"sensor": "k10temp_tccd1_max"},
map[string]interface{}{"temp": 0.0},
time.Unix(0, 0),
),
metric.New(
"temp",
map[string]string{"sensor": "k10temp_tccd1_input"},
map[string]interface{}{"temp": 33.25},
time.Unix(0, 0),
),
}

err := os.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys"))
require.NoError(t, err)

plugin := &Temperature{ps: system.NewSystemPS()}

var acc testutil.Accumulator
require.NoError(t, plugin.Gather(&acc))
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
84850
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
35850
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Composite
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
81850
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-273150
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
35850
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sensor 1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
65261850
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-273150
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
38850
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sensor 2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
65261850
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-273150
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
k10temp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
33250
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tctl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
33250
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tccd1
Empty file.