diff --git a/plugins/inputs/temp/temp.go b/plugins/inputs/temp/temp.go index 0b6797cdc13fc..11a2e2e64d556 100644 --- a/plugins/inputs/temp/temp.go +++ b/plugins/inputs/temp/temp.go @@ -3,8 +3,6 @@ package temp import ( _ "embed" - "fmt" - "strings" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/inputs" @@ -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()} diff --git a/plugins/inputs/temp/temp_linux.go b/plugins/inputs/temp/temp_linux.go new file mode 100644 index 0000000000000..58d9c3ed7fad3 --- /dev/null +++ b/plugins/inputs/temp/temp_linux.go @@ -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 +} diff --git a/plugins/inputs/temp/temp_nolinux.go b/plugins/inputs/temp/temp_nolinux.go new file mode 100644 index 0000000000000..d4dc7e300af27 --- /dev/null +++ b/plugins/inputs/temp/temp_nolinux.go @@ -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 +} diff --git a/plugins/inputs/temp/temp_test.go b/plugins/inputs/temp/temp_test.go index c73550f5d318c..0026062509112 100644 --- a/plugins/inputs/temp/temp_test.go +++ b/plugins/inputs/temp/temp_test.go @@ -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" ) @@ -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) @@ -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()) +} diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/name b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/name new file mode 100644 index 0000000000000..9158fff478b2a --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/name @@ -0,0 +1 @@ +nvme diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_alarm b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_alarm new file mode 100644 index 0000000000000..573541ac9702d --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_alarm @@ -0,0 +1 @@ +0 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_crit b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_crit new file mode 100644 index 0000000000000..360ca8bdb9c85 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_crit @@ -0,0 +1 @@ +84850 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_input b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_input new file mode 100644 index 0000000000000..fcfd937252845 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_input @@ -0,0 +1 @@ +35850 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_label b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_label new file mode 100644 index 0000000000000..3636921683713 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_label @@ -0,0 +1 @@ +Composite diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_max b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_max new file mode 100644 index 0000000000000..4b1e1221005fc --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_max @@ -0,0 +1 @@ +81850 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_min b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_min new file mode 100644 index 0000000000000..526f8b2338f95 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_min @@ -0,0 +1 @@ +-273150 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_input b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_input new file mode 100644 index 0000000000000..fcfd937252845 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_input @@ -0,0 +1 @@ +35850 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_label b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_label new file mode 100644 index 0000000000000..19fc4b96f8184 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_label @@ -0,0 +1 @@ +Sensor 1 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_max b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_max new file mode 100644 index 0000000000000..bce5872a7fb59 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_max @@ -0,0 +1 @@ +65261850 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_min b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_min new file mode 100644 index 0000000000000..526f8b2338f95 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_min @@ -0,0 +1 @@ +-273150 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_input b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_input new file mode 100644 index 0000000000000..7f07fd83f9b48 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_input @@ -0,0 +1 @@ +38850 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_label b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_label new file mode 100644 index 0000000000000..35141d68738e6 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_label @@ -0,0 +1 @@ +Sensor 2 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_max b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_max new file mode 100644 index 0000000000000..bce5872a7fb59 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_max @@ -0,0 +1 @@ +65261850 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_min b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_min new file mode 100644 index 0000000000000..526f8b2338f95 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_min @@ -0,0 +1 @@ +-273150 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/name b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/name new file mode 100644 index 0000000000000..02275a5f2fda1 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/name @@ -0,0 +1 @@ +k10temp diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp1_input b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp1_input new file mode 100644 index 0000000000000..152491c03fc16 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp1_input @@ -0,0 +1 @@ +33250 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp1_label b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp1_label new file mode 100644 index 0000000000000..13e1919d4abd9 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp1_label @@ -0,0 +1 @@ +Tctl diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp3_input b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp3_input new file mode 100644 index 0000000000000..152491c03fc16 --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp3_input @@ -0,0 +1 @@ +33250 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp3_label b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp3_label new file mode 100644 index 0000000000000..06b4c97a4978f --- /dev/null +++ b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp3_label @@ -0,0 +1 @@ +Tccd1 diff --git a/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/uevent b/plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/uevent new file mode 100644 index 0000000000000..e69de29bb2d1d