From 644cfd0c8f2843438c613bca61beffdafacedd13 Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Tue, 14 Jun 2022 14:35:40 +0200 Subject: [PATCH 01/10] fix: Regression introduced by gopsutil update. --- plugins/inputs/temp/README.md | 8 ++ plugins/inputs/temp/sample.conf | 8 +- plugins/inputs/temp/temp.go | 43 ++++++- plugins/inputs/temp/temp_test.go | 186 ++++++++++++++++++++++++++++++- 4 files changed, 236 insertions(+), 9 deletions(-) diff --git a/plugins/inputs/temp/README.md b/plugins/inputs/temp/README.md index b61c435fa17de..baaadd773eee2 100644 --- a/plugins/inputs/temp/README.md +++ b/plugins/inputs/temp/README.md @@ -32,9 +32,17 @@ wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature ## Example Output +For `output = "measurement" (default) the output will look like this + ```shell temp,sensor=coretemp_physicalid0_crit temp=100 1531298763000000000 temp,sensor=coretemp_physicalid0_critalarm temp=0 1531298763000000000 temp,sensor=coretemp_physicalid0_input temp=100 1531298763000000000 temp,sensor=coretemp_physicalid0_max temp=100 1531298763000000000 ``` + +For `output = "field" the output will look like this + +```shell +temp,sensor=coretemp_physicalid0 crit=100 temp=100 high=100 1531298763000000000 +``` diff --git a/plugins/inputs/temp/sample.conf b/plugins/inputs/temp/sample.conf index 938ce9d949734..9a2e8cd1e005e 100644 --- a/plugins/inputs/temp/sample.conf +++ b/plugins/inputs/temp/sample.conf @@ -1,3 +1,9 @@ # Read metrics about temperature [[inputs.temp]] - # no configuration + ## Sets the output scheme. + ## Available values are + ## measurement -- output one metric per type "critical", "high" and "temp" + ## with a concatenated sensor tag (default) + ## field -- output only one metric with "critical", "high" and "temp" + ## as fields + # output_scheme = "measurement" \ No newline at end of file diff --git a/plugins/inputs/temp/temp.go b/plugins/inputs/temp/temp.go index 0b6797cdc13fc..a222e8db10e04 100644 --- a/plugins/inputs/temp/temp.go +++ b/plugins/inputs/temp/temp.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/internal/choice" "github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs/system" ) @@ -16,6 +17,8 @@ import ( var sampleConfig string type Temperature struct { + Scheme string `toml:"output_scheme"` + ps system.PS } @@ -23,6 +26,13 @@ func (*Temperature) SampleConfig() string { return sampleConfig } +func (t *Temperature) Init() error { + if !choice.Contains(t.Scheme, []string{"", "measurement", "field"}) { + return fmt.Errorf("invalid output_scheme %q", t.Scheme) + } + return nil +} + func (t *Temperature) Gather(acc telegraf.Accumulator) error { temps, err := t.ps.Temperature() if err != nil { @@ -32,13 +42,34 @@ func (t *Temperature) Gather(acc telegraf.Accumulator) error { 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, + switch t.Scheme { + case "", "measurement": + 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"}, + ) + case "field": + acc.AddFields( + "temp", + map[string]interface{}{ + "crit": temp.Critical, + "high": temp.High, + "temp": temp.Temperature, + }, + map[string]string{"sensor": temp.SensorKey}, + ) } - acc.AddFields("temp", fields, tags) } return nil } diff --git a/plugins/inputs/temp/temp_test.go b/plugins/inputs/temp/temp_test.go index c73550f5d318c..ca9b70710e32a 100644 --- a/plugins/inputs/temp/temp_test.go +++ b/plugins/inputs/temp/temp_test.go @@ -1,11 +1,16 @@ package temp import ( + "os" + "path/filepath" "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" ) @@ -17,8 +22,8 @@ func TestTemperature(t *testing.T) { var acc testutil.Accumulator ts := host.TemperatureStat{ - SensorKey: "coretemp_sensor1_crit", - Temperature: 60.5, + SensorKey: "coretemp_sensor1", + Critical: 60.5, } mps.On("Temperature").Return([]host.TemperatureStat{ts}, nil) @@ -35,3 +40,180 @@ func TestTemperature(t *testing.T) { } acc.AssertContainsTaggedFields(t, "temp", expectedFields, expectedTags) } + +func TestTemperatureOutputInvalid(t *testing.T) { + plugin := &Temperature{Scheme: "garbage", ps: system.NewSystemPS()} + require.Error(t, plugin.Init(), "alal") +} + +func TestTemperatureOutputMeasurement(t *testing.T) { + tests := []struct { + name string + path string + expected []telegraf.Metric + }{ + { + name: "general", + path: filepath.Join("testdata", "general"), + 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), + ), + }, + }, + } + + for _, tt := range tests { + os.Setenv("HOST_SYS", filepath.Join(tt.path, "sys")) + + plugin := &Temperature{Scheme: "measurement", ps: system.NewSystemPS()} + require.NoError(t, plugin.Init()) + + var acc testutil.Accumulator + require.NoError(t, plugin.Gather(&acc)) + testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime()) + } +} + +func TestTemperatureOutputField(t *testing.T) { + tests := []struct { + name string + path string + expected []telegraf.Metric + }{ + { + name: "general", + path: filepath.Join("testdata", "general"), + expected: []telegraf.Metric{ + metric.New( + "temp", + map[string]string{"sensor": "nvme_composite"}, + map[string]interface{}{"crit": 84.85, "high": 81.85, "temp": 35.85}, + time.Unix(0, 0), + ), + metric.New( + "temp", + map[string]string{"sensor": "nvme_sensor_1"}, + map[string]interface{}{"crit": 0.0, "high": 65261.85, "temp": 35.85}, + time.Unix(0, 0), + ), + metric.New( + "temp", + map[string]string{"sensor": "nvme_sensor_2"}, + map[string]interface{}{"crit": 0.0, "high": 65261.85, "temp": 38.85}, + time.Unix(0, 0), + ), + metric.New( + "temp", + map[string]string{"sensor": "k10temp_tctl"}, + map[string]interface{}{"crit": 0.0, "high": 0.0, "temp": 33.25}, + time.Unix(0, 0), + ), + metric.New( + "temp", + map[string]string{"sensor": "k10temp_tccd1"}, + map[string]interface{}{"crit": 0.0, "high": 0.0, "temp": 33.25}, + time.Unix(0, 0), + ), + }, + }, + } + + for _, tt := range tests { + os.Setenv("HOST_SYS", filepath.Join(tt.path, "sys")) + + plugin := &Temperature{Scheme: "field", ps: system.NewSystemPS()} + require.NoError(t, plugin.Init()) + + var acc testutil.Accumulator + require.NoError(t, plugin.Gather(&acc)) + testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime()) + } +} From 71da7f0c6f31e6d341c567f61b3543d4b6bcd9b9 Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Tue, 14 Jun 2022 14:42:03 +0200 Subject: [PATCH 02/10] Add testdata for temp plugin. --- plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/name | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon0/temp1_alarm | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon0/temp1_crit | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon0/temp1_input | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon0/temp1_label | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon0/temp1_max | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon0/temp1_min | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon0/temp2_input | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon0/temp2_label | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon0/temp2_max | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon0/temp2_min | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon0/temp3_input | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon0/temp3_label | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon0/temp3_max | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon0/temp3_min | 1 + plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/name | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon1/temp1_input | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon1/temp1_label | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon1/temp3_input | 1 + .../temp/testdata/general/sys/class/hwmon/hwmon1/temp3_label | 1 + .../inputs/temp/testdata/general/sys/class/hwmon/hwmon1/uevent | 0 21 files changed, 20 insertions(+) create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/name create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_alarm create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_crit create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_input create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_label create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_max create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp1_min create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_input create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_label create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_max create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp2_min create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_input create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_label create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_max create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon0/temp3_min create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/name create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp1_input create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp1_label create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp3_input create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/temp3_label create mode 100644 plugins/inputs/temp/testdata/general/sys/class/hwmon/hwmon1/uevent 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 From ba410c66e0d73cc80797b1856aaef683960814a4 Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Tue, 14 Jun 2022 16:32:35 +0200 Subject: [PATCH 03/10] Update README. --- plugins/inputs/temp/README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plugins/inputs/temp/README.md b/plugins/inputs/temp/README.md index baaadd773eee2..8f6cfa7968359 100644 --- a/plugins/inputs/temp/README.md +++ b/plugins/inputs/temp/README.md @@ -10,7 +10,13 @@ Currently supports Linux and Windows. ```toml @sample.conf # Read metrics about temperature [[inputs.temp]] - # no configuration + ## Sets the output scheme. + ## Available values are + ## measurement -- output one metric per type "critical", "high" and "temp" + ## with a concatenated sensor tag (default) + ## field -- output only one metric with "critical", "high" and "temp" + ## as fields + # output_scheme = "measurement" ``` ## Metrics From 4d0d9f94715916c153b842e4a06463dfddd61fef Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Tue, 14 Jun 2022 16:52:27 +0200 Subject: [PATCH 04/10] Revert to previous state and only change Linux. --- plugins/inputs/temp/sample.conf | 8 +- plugins/inputs/temp/temp.go | 53 ------ plugins/inputs/temp/temp_test.go | 269 ++++++++++++------------------- 3 files changed, 101 insertions(+), 229 deletions(-) diff --git a/plugins/inputs/temp/sample.conf b/plugins/inputs/temp/sample.conf index 9a2e8cd1e005e..938ce9d949734 100644 --- a/plugins/inputs/temp/sample.conf +++ b/plugins/inputs/temp/sample.conf @@ -1,9 +1,3 @@ # Read metrics about temperature [[inputs.temp]] - ## Sets the output scheme. - ## Available values are - ## measurement -- output one metric per type "critical", "high" and "temp" - ## with a concatenated sensor tag (default) - ## field -- output only one metric with "critical", "high" and "temp" - ## as fields - # output_scheme = "measurement" \ No newline at end of file + # no configuration diff --git a/plugins/inputs/temp/temp.go b/plugins/inputs/temp/temp.go index a222e8db10e04..11a2e2e64d556 100644 --- a/plugins/inputs/temp/temp.go +++ b/plugins/inputs/temp/temp.go @@ -3,11 +3,8 @@ package temp import ( _ "embed" - "fmt" - "strings" "github.com/influxdata/telegraf" - "github.com/influxdata/telegraf/internal/choice" "github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs/system" ) @@ -17,8 +14,6 @@ import ( var sampleConfig string type Temperature struct { - Scheme string `toml:"output_scheme"` - ps system.PS } @@ -26,54 +21,6 @@ func (*Temperature) SampleConfig() string { return sampleConfig } -func (t *Temperature) Init() error { - if !choice.Contains(t.Scheme, []string{"", "measurement", "field"}) { - return fmt.Errorf("invalid output_scheme %q", t.Scheme) - } - return nil -} - -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 { - switch t.Scheme { - case "", "measurement": - 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"}, - ) - case "field": - acc.AddFields( - "temp", - map[string]interface{}{ - "crit": temp.Critical, - "high": temp.High, - "temp": temp.Temperature, - }, - map[string]string{"sensor": temp.SensorKey}, - ) - } - } - return nil -} - func init() { inputs.Add("temp", func() telegraf.Input { return &Temperature{ps: system.NewSystemPS()} diff --git a/plugins/inputs/temp/temp_test.go b/plugins/inputs/temp/temp_test.go index ca9b70710e32a..9e78ed7330b40 100644 --- a/plugins/inputs/temp/temp_test.go +++ b/plugins/inputs/temp/temp_test.go @@ -3,6 +3,7 @@ package temp import ( "os" "path/filepath" + "runtime" "testing" "time" @@ -41,179 +42,109 @@ func TestTemperature(t *testing.T) { acc.AssertContainsTaggedFields(t, "temp", expectedFields, expectedTags) } -func TestTemperatureOutputInvalid(t *testing.T) { - plugin := &Temperature{Scheme: "garbage", ps: system.NewSystemPS()} - require.Error(t, plugin.Init(), "alal") -} - -func TestTemperatureOutputMeasurement(t *testing.T) { - tests := []struct { - name string - path string - expected []telegraf.Metric - }{ - { - name: "general", - path: filepath.Join("testdata", "general"), - 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), - ), - }, - }, +func TestTemperatureLinux(t *testing.T) { + if runtime.GOOS != "linux" { + t.Skip("skipping test on non-Linux OS") } - for _, tt := range tests { - os.Setenv("HOST_SYS", filepath.Join(tt.path, "sys")) - - plugin := &Temperature{Scheme: "measurement", ps: system.NewSystemPS()} - require.NoError(t, plugin.Init()) - - var acc testutil.Accumulator - require.NoError(t, plugin.Gather(&acc)) - testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime()) + 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), + ), } -} -func TestTemperatureOutputField(t *testing.T) { - tests := []struct { - name string - path string - expected []telegraf.Metric - }{ - { - name: "general", - path: filepath.Join("testdata", "general"), - expected: []telegraf.Metric{ - metric.New( - "temp", - map[string]string{"sensor": "nvme_composite"}, - map[string]interface{}{"crit": 84.85, "high": 81.85, "temp": 35.85}, - time.Unix(0, 0), - ), - metric.New( - "temp", - map[string]string{"sensor": "nvme_sensor_1"}, - map[string]interface{}{"crit": 0.0, "high": 65261.85, "temp": 35.85}, - time.Unix(0, 0), - ), - metric.New( - "temp", - map[string]string{"sensor": "nvme_sensor_2"}, - map[string]interface{}{"crit": 0.0, "high": 65261.85, "temp": 38.85}, - time.Unix(0, 0), - ), - metric.New( - "temp", - map[string]string{"sensor": "k10temp_tctl"}, - map[string]interface{}{"crit": 0.0, "high": 0.0, "temp": 33.25}, - time.Unix(0, 0), - ), - metric.New( - "temp", - map[string]string{"sensor": "k10temp_tccd1"}, - map[string]interface{}{"crit": 0.0, "high": 0.0, "temp": 33.25}, - time.Unix(0, 0), - ), - }, - }, - } - - for _, tt := range tests { - os.Setenv("HOST_SYS", filepath.Join(tt.path, "sys")) + os.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys")) - plugin := &Temperature{Scheme: "field", ps: system.NewSystemPS()} - require.NoError(t, plugin.Init()) + plugin := &Temperature{ps: system.NewSystemPS()} - var acc testutil.Accumulator - require.NoError(t, plugin.Gather(&acc)) - testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime()) - } + var acc testutil.Accumulator + require.NoError(t, plugin.Gather(&acc)) + testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime()) } From 16897fe3f5e22dc254c1e11d10b3b0c3ed0eade7 Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Tue, 14 Jun 2022 16:54:12 +0200 Subject: [PATCH 05/10] Update README. --- plugins/inputs/temp/README.md | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/plugins/inputs/temp/README.md b/plugins/inputs/temp/README.md index 8f6cfa7968359..b61c435fa17de 100644 --- a/plugins/inputs/temp/README.md +++ b/plugins/inputs/temp/README.md @@ -10,13 +10,7 @@ Currently supports Linux and Windows. ```toml @sample.conf # Read metrics about temperature [[inputs.temp]] - ## Sets the output scheme. - ## Available values are - ## measurement -- output one metric per type "critical", "high" and "temp" - ## with a concatenated sensor tag (default) - ## field -- output only one metric with "critical", "high" and "temp" - ## as fields - # output_scheme = "measurement" + # no configuration ``` ## Metrics @@ -38,17 +32,9 @@ wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature ## Example Output -For `output = "measurement" (default) the output will look like this - ```shell temp,sensor=coretemp_physicalid0_crit temp=100 1531298763000000000 temp,sensor=coretemp_physicalid0_critalarm temp=0 1531298763000000000 temp,sensor=coretemp_physicalid0_input temp=100 1531298763000000000 temp,sensor=coretemp_physicalid0_max temp=100 1531298763000000000 ``` - -For `output = "field" the output will look like this - -```shell -temp,sensor=coretemp_physicalid0 crit=100 temp=100 high=100 1531298763000000000 -``` From f2b42d240a4b2f52fc215f28a055cee0a3b4d49d Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Tue, 14 Jun 2022 17:02:15 +0200 Subject: [PATCH 06/10] Fix linter issues. --- plugins/inputs/temp/temp_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/inputs/temp/temp_test.go b/plugins/inputs/temp/temp_test.go index 9e78ed7330b40..4e2e941aeff60 100644 --- a/plugins/inputs/temp/temp_test.go +++ b/plugins/inputs/temp/temp_test.go @@ -140,7 +140,8 @@ func TestTemperatureLinux(t *testing.T) { ), } - os.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys")) + err := os.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys")) + require.NoError(t, err) plugin := &Temperature{ps: system.NewSystemPS()} From 704fc83f658555dee67d4065c0b6fe1cdbff9c08 Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Tue, 14 Jun 2022 17:02:38 +0200 Subject: [PATCH 07/10] Add platform-specific Gather implementations. --- plugins/inputs/temp/temp_linux.go | 39 +++++++++++++++++++++++++++++ plugins/inputs/temp/temp_nolinux.go | 29 +++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 plugins/inputs/temp/temp_linux.go create mode 100644 plugins/inputs/temp/temp_nolinux.go 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..d1334949e0743 --- /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: %v", err) + } + return fmt.Errorf("error getting temperatures info: %s", err) + } + for _, temp := range temps { + acc.AddFields( + "temp", + map[string]interface{}{"temp": temp.Temperature}, + map[string]string{"sensor": temp.SensorKey}, + ) + } + return nil +} From a9b1c85d2ef25affa1d8c80cbf81ad710301803e Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Tue, 14 Jun 2022 17:15:16 +0200 Subject: [PATCH 08/10] Account for Linux sensor keys are now different in tests. --- plugins/inputs/temp/temp_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/inputs/temp/temp_test.go b/plugins/inputs/temp/temp_test.go index 4e2e941aeff60..c40c304738ae3 100644 --- a/plugins/inputs/temp/temp_test.go +++ b/plugins/inputs/temp/temp_test.go @@ -22,8 +22,12 @@ func TestTemperature(t *testing.T) { defer mps.AssertExpectations(t) var acc testutil.Accumulator + sensorkey := "coretemp_sensor1_crit" + if runtime.GOOS == "linux" { + sensorkey = "coretemp_sensor1" + } ts := host.TemperatureStat{ - SensorKey: "coretemp_sensor1", + SensorKey: sensorkey, Critical: 60.5, } From ae7536e994078409bd4cfd8bf051c8e014f3d4e8 Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Tue, 14 Jun 2022 17:33:08 +0200 Subject: [PATCH 09/10] Revert more test pieces. --- plugins/inputs/temp/temp_test.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/plugins/inputs/temp/temp_test.go b/plugins/inputs/temp/temp_test.go index c40c304738ae3..0026062509112 100644 --- a/plugins/inputs/temp/temp_test.go +++ b/plugins/inputs/temp/temp_test.go @@ -22,13 +22,17 @@ func TestTemperature(t *testing.T) { defer mps.AssertExpectations(t) var acc testutil.Accumulator - sensorkey := "coretemp_sensor1_crit" + var ts host.TemperatureStat if runtime.GOOS == "linux" { - sensorkey = "coretemp_sensor1" - } - ts := host.TemperatureStat{ - SensorKey: sensorkey, - Critical: 60.5, + 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) From 740cb36a11ad2dcc753cfb225811e176173f1a6c Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Tue, 14 Jun 2022 18:41:40 +0200 Subject: [PATCH 10/10] Update plugins/inputs/temp/temp_nolinux.go Co-authored-by: Thomas Casteleyn --- plugins/inputs/temp/temp_nolinux.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/temp/temp_nolinux.go b/plugins/inputs/temp/temp_nolinux.go index d1334949e0743..d4dc7e300af27 100644 --- a/plugins/inputs/temp/temp_nolinux.go +++ b/plugins/inputs/temp/temp_nolinux.go @@ -14,9 +14,9 @@ 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("plugin is not supported on this platform: %w", err) } - return fmt.Errorf("error getting temperatures info: %s", err) + return fmt.Errorf("error getting temperatures info: %w", err) } for _, temp := range temps { acc.AddFields(