fix(inputs.temp): Fix regression in metric formats (#14649)
This commit is contained in:
parent
375caaa1d8
commit
043ae3e8a0
|
|
@ -56,26 +56,55 @@ func (t *Temperature) Gather(acc telegraf.Accumulator) error {
|
|||
}
|
||||
}
|
||||
|
||||
for _, temp := range temperatures {
|
||||
acc.AddFields(
|
||||
"temp",
|
||||
map[string]interface{}{"temp": temp.Temperature},
|
||||
t.getTagsForTemperature(temp, "_input"),
|
||||
)
|
||||
switch t.MetricFormat {
|
||||
case "v1":
|
||||
t.createMetricsV1(acc, temperatures)
|
||||
case "v2":
|
||||
t.createMetricsV2(acc, temperatures)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Temperature) createMetricsV1(acc telegraf.Accumulator, temperatures []TemperatureStat) {
|
||||
for _, temp := range temperatures {
|
||||
sensor := temp.Name
|
||||
if temp.Label != "" {
|
||||
sensor += "_" + strings.ReplaceAll(temp.Label, " ", "")
|
||||
}
|
||||
|
||||
// Mandatory measurement value
|
||||
tags := map[string]string{"sensor": sensor + "_input"}
|
||||
if t.DeviceTag {
|
||||
tags["device"] = temp.Device
|
||||
}
|
||||
acc.AddFields("temp", map[string]interface{}{"temp": temp.Temperature}, tags)
|
||||
|
||||
// Optional values values
|
||||
for measurement, value := range temp.Additional {
|
||||
fieldname := "temp"
|
||||
if measurement == "alarm" {
|
||||
fieldname = "active"
|
||||
tags := map[string]string{"sensor": sensor + "_" + measurement}
|
||||
if t.DeviceTag {
|
||||
tags["device"] = temp.Device
|
||||
}
|
||||
acc.AddFields(
|
||||
"temp",
|
||||
map[string]interface{}{fieldname: value},
|
||||
t.getTagsForTemperature(temp, "_"+measurement),
|
||||
)
|
||||
acc.AddFields("temp", map[string]interface{}{"temp": value}, tags)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Temperature) createMetricsV2(acc telegraf.Accumulator, temperatures []TemperatureStat) {
|
||||
for _, temp := range temperatures {
|
||||
sensor := temp.Name
|
||||
if temp.Label != "" {
|
||||
sensor += "_" + strings.ReplaceAll(temp.Label, " ", "_")
|
||||
}
|
||||
|
||||
// Mandatory measurement value
|
||||
tags := map[string]string{"sensor": sensor}
|
||||
if t.DeviceTag {
|
||||
tags["device"] = temp.Device
|
||||
}
|
||||
acc.AddFields("temp", map[string]interface{}{"temp": temp.Temperature}, tags)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Temperature) gatherHwmon(syspath string) ([]TemperatureStat, error) {
|
||||
|
|
@ -144,15 +173,6 @@ func (t *Temperature) gatherHwmon(syspath string) ([]TemperatureStat, error) {
|
|||
temp.Temperature = v / scalingFactor
|
||||
}
|
||||
|
||||
// Alarm (optional)
|
||||
fn = filepath.Join(path, prefix+"_alarm")
|
||||
buf, err = os.ReadFile(fn)
|
||||
if err == nil {
|
||||
if a, err := strconv.ParseBool(strings.TrimSpace(string(buf))); err == nil {
|
||||
temp.Additional["alarm"] = a
|
||||
}
|
||||
}
|
||||
|
||||
// Read all possible values of the sensor
|
||||
matches, err := filepath.Glob(filepath.Join(path, prefix+"_*"))
|
||||
if err != nil {
|
||||
|
|
@ -172,7 +192,7 @@ func (t *Temperature) gatherHwmon(syspath string) ([]TemperatureStat, error) {
|
|||
|
||||
// Skip already added values
|
||||
switch measurement {
|
||||
case "label", "input", "alarm":
|
||||
case "label", "input":
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -229,21 +249,3 @@ func (t *Temperature) gatherThermalZone(syspath string) ([]TemperatureStat, erro
|
|||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (t *Temperature) getTagsForTemperature(temp TemperatureStat, suffix string) map[string]string {
|
||||
sensor := temp.Name
|
||||
if temp.Label != "" && suffix != "" {
|
||||
switch t.MetricFormat {
|
||||
case "v1":
|
||||
sensor += "_" + strings.ReplaceAll(temp.Label, " ", "") + suffix
|
||||
case "v2":
|
||||
sensor += "_" + strings.ReplaceAll(temp.Label, " ", "_") + suffix
|
||||
}
|
||||
}
|
||||
|
||||
tags := map[string]string{"sensor": sensor}
|
||||
if t.DeviceTag {
|
||||
tags["device"] = temp.Device
|
||||
}
|
||||
return tags
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,19 +3,33 @@
|
|||
package temp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/shirou/gopsutil/v3/host"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/metric"
|
||||
"github.com/influxdata/telegraf/config"
|
||||
"github.com/influxdata/telegraf/plugins/inputs"
|
||||
"github.com/influxdata/telegraf/plugins/parsers/influx"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
)
|
||||
|
||||
func TestTemperatureInvaldiMetricFormat(t *testing.T) {
|
||||
func TestDefaultMetricFormat(t *testing.T) {
|
||||
plugin := &Temperature{
|
||||
Log: &testutil.Logger{},
|
||||
}
|
||||
require.NoError(t, plugin.Init())
|
||||
require.Equal(t, "v2", plugin.MetricFormat)
|
||||
}
|
||||
|
||||
func TestInvalidMetricFormat(t *testing.T) {
|
||||
plugin := &Temperature{
|
||||
MetricFormat: "foo",
|
||||
Log: &testutil.Logger{},
|
||||
|
|
@ -23,451 +37,306 @@ func TestTemperatureInvaldiMetricFormat(t *testing.T) {
|
|||
require.ErrorContains(t, plugin.Init(), "invalid 'metric_format'")
|
||||
}
|
||||
|
||||
func TestTemperatureMetricV1(t *testing.T) {
|
||||
expected := []telegraf.Metric{
|
||||
// hwmon0 / temp1
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{"sensor": "nvme_composite_alarm"},
|
||||
map[string]interface{}{"active": false},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
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_input"},
|
||||
map[string]interface{}{"temp": 35.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_min"},
|
||||
map[string]interface{}{"temp": -273.15},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon0 / temp2
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{"sensor": "nvme_sensor1_input"},
|
||||
map[string]interface{}{"temp": 35.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{"sensor": "nvme_sensor1_max"},
|
||||
map[string]interface{}{"temp": 65261.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{"sensor": "nvme_sensor1_min"},
|
||||
map[string]interface{}{"temp": -273.15},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon0 / temp3
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{"sensor": "nvme_sensor2_input"},
|
||||
map[string]interface{}{"temp": 38.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{"sensor": "nvme_sensor2_max"},
|
||||
map[string]interface{}{"temp": 65261.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{"sensor": "nvme_sensor2_min"},
|
||||
map[string]interface{}{"temp": -273.15},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon1 / temp1
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{"sensor": "k10temp_tctl_input"},
|
||||
map[string]interface{}{"temp": 33.25},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon1 / temp3
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{"sensor": "k10temp_tccd1_input"},
|
||||
map[string]interface{}{"temp": 33.25},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
}
|
||||
|
||||
require.NoError(t, os.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys")))
|
||||
|
||||
plugin := &Temperature{
|
||||
MetricFormat: "v1",
|
||||
Log: &testutil.Logger{},
|
||||
}
|
||||
require.NoError(t, plugin.Init())
|
||||
|
||||
var acc testutil.Accumulator
|
||||
require.NoError(t, plugin.Gather(&acc))
|
||||
actual := acc.GetTelegrafMetrics()
|
||||
testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime(), testutil.SortMetrics())
|
||||
}
|
||||
|
||||
func TestTemperature(t *testing.T) {
|
||||
expected := []telegraf.Metric{
|
||||
// hwmon0 / temp1
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{"sensor": "nvme_composite_alarm"},
|
||||
map[string]interface{}{"active": false},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
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_input"},
|
||||
map[string]interface{}{"temp": 35.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_min"},
|
||||
map[string]interface{}{"temp": -273.15},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon0 / temp2
|
||||
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_1_max"},
|
||||
map[string]interface{}{"temp": 65261.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{"sensor": "nvme_sensor_1_min"},
|
||||
map[string]interface{}{"temp": -273.15},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon0 / temp3
|
||||
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": "nvme_sensor_2_max"},
|
||||
map[string]interface{}{"temp": 65261.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{"sensor": "nvme_sensor_2_min"},
|
||||
map[string]interface{}{"temp": -273.15},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon1 / temp1
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{"sensor": "k10temp_tctl_input"},
|
||||
map[string]interface{}{"temp": 33.25},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon1 / temp3
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{"sensor": "k10temp_tccd1_input"},
|
||||
map[string]interface{}{"temp": 33.25},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
}
|
||||
|
||||
require.NoError(t, os.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys")))
|
||||
func TestNameCollisions(t *testing.T) {
|
||||
require.NoError(t, os.Setenv("HOST_SYS", filepath.Join("testcases", "with_name", "sys")))
|
||||
plugin := &Temperature{Log: &testutil.Logger{}}
|
||||
require.NoError(t, plugin.Init())
|
||||
|
||||
var acc testutil.Accumulator
|
||||
require.NoError(t, plugin.Gather(&acc))
|
||||
actual := acc.GetTelegrafMetrics()
|
||||
testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime(), testutil.SortMetrics())
|
||||
require.Len(t, acc.GetTelegrafMetrics(), 8)
|
||||
}
|
||||
|
||||
func TestTemperatureNameCollisions(t *testing.T) {
|
||||
require.NoError(t, os.Setenv("HOST_SYS", filepath.Join("testdata", "with_name", "sys")))
|
||||
plugin := &Temperature{Log: &testutil.Logger{}}
|
||||
require.NoError(t, plugin.Init())
|
||||
func TestCases(t *testing.T) {
|
||||
// Get all directories in testdata
|
||||
folders, err := os.ReadDir("testcases")
|
||||
require.NoError(t, err)
|
||||
|
||||
var acc testutil.Accumulator
|
||||
require.NoError(t, plugin.Gather(&acc))
|
||||
require.Len(t, acc.GetTelegrafMetrics(), 24)
|
||||
// Register the plugin
|
||||
inputs.Add("temp", func() telegraf.Input {
|
||||
return &Temperature{}
|
||||
})
|
||||
|
||||
// Prepare the influx parser for expectations
|
||||
parser := &influx.Parser{}
|
||||
require.NoError(t, parser.Init())
|
||||
|
||||
for _, f := range folders {
|
||||
// Only handle folders
|
||||
if !f.IsDir() {
|
||||
continue
|
||||
}
|
||||
// Compare options
|
||||
options := []cmp.Option{
|
||||
testutil.IgnoreTime(),
|
||||
testutil.SortMetrics(),
|
||||
}
|
||||
|
||||
// Test v1
|
||||
t.Run(f.Name()+"_v1", func(t *testing.T) {
|
||||
testcasePath := filepath.Join("testcases", f.Name())
|
||||
configFilename := filepath.Join(testcasePath, "telegraf.conf")
|
||||
expectedFilename := filepath.Join(testcasePath, "expected_v1.out")
|
||||
|
||||
// Read the expected output if any
|
||||
var expected []telegraf.Metric
|
||||
if _, err := os.Stat(expectedFilename); err == nil {
|
||||
var err error
|
||||
expected, err = testutil.ParseMetricsFromFile(expectedFilename, parser)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Prepare the environment
|
||||
require.NoError(t, os.Setenv("HOST_SYS", filepath.Join(testcasePath, "sys")))
|
||||
|
||||
// Configure the plugin
|
||||
cfg := config.NewConfig()
|
||||
require.NoError(t, cfg.LoadConfig(configFilename))
|
||||
require.Len(t, cfg.Inputs, 1)
|
||||
plugin := cfg.Inputs[0].Input.(*Temperature)
|
||||
plugin.MetricFormat = "v1"
|
||||
require.NoError(t, plugin.Init())
|
||||
|
||||
var acc testutil.Accumulator
|
||||
require.NoError(t, plugin.Gather(&acc))
|
||||
|
||||
// Check the metric nevertheless as we might get some metrics despite errors.
|
||||
actual := acc.GetTelegrafMetrics()
|
||||
testutil.RequireMetricsEqual(t, expected, actual, options...)
|
||||
})
|
||||
|
||||
// Test v2
|
||||
t.Run(f.Name()+"_v2", func(t *testing.T) {
|
||||
testcasePath := filepath.Join("testcases", f.Name())
|
||||
configFilename := filepath.Join(testcasePath, "telegraf.conf")
|
||||
expectedFilename := filepath.Join(testcasePath, "expected_v2.out")
|
||||
|
||||
// Read the expected output if any
|
||||
var expected []telegraf.Metric
|
||||
if _, err := os.Stat(expectedFilename); err == nil {
|
||||
var err error
|
||||
expected, err = testutil.ParseMetricsFromFile(expectedFilename, parser)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Prepare the environment
|
||||
require.NoError(t, os.Setenv("HOST_SYS", filepath.Join(testcasePath, "sys")))
|
||||
|
||||
// Configure the plugin
|
||||
cfg := config.NewConfig()
|
||||
require.NoError(t, cfg.LoadConfig(configFilename))
|
||||
require.Len(t, cfg.Inputs, 1)
|
||||
plugin := cfg.Inputs[0].Input.(*Temperature)
|
||||
plugin.MetricFormat = "v2"
|
||||
require.NoError(t, plugin.Init())
|
||||
|
||||
var acc testutil.Accumulator
|
||||
require.NoError(t, plugin.Gather(&acc))
|
||||
|
||||
// Check the metric nevertheless as we might get some metrics despite errors.
|
||||
actual := acc.GetTelegrafMetrics()
|
||||
testutil.RequireMetricsEqual(t, expected, actual, options...)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTemperatureWithDeviceTag(t *testing.T) {
|
||||
expected := []telegraf.Metric{
|
||||
// hwmon0 / temp1
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_composite_input",
|
||||
"device": "nvme0",
|
||||
},
|
||||
map[string]interface{}{"temp": 32.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_composite_alarm",
|
||||
"device": "nvme0",
|
||||
},
|
||||
map[string]interface{}{"active": false},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_composite_crit",
|
||||
"device": "nvme0",
|
||||
},
|
||||
map[string]interface{}{"temp": 84.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_composite_min",
|
||||
"device": "nvme0",
|
||||
},
|
||||
map[string]interface{}{"temp": -273.15},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_composite_max",
|
||||
"device": "nvme0",
|
||||
},
|
||||
map[string]interface{}{"temp": 81.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon0 / temp2
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_sensor_1_input",
|
||||
"device": "nvme0",
|
||||
},
|
||||
map[string]interface{}{"temp": 32.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_sensor_1_min",
|
||||
"device": "nvme0",
|
||||
},
|
||||
map[string]interface{}{"temp": -273.15},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_sensor_1_max",
|
||||
"device": "nvme0",
|
||||
},
|
||||
map[string]interface{}{"temp": 65261.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon0 / temp3
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_sensor_2_input",
|
||||
"device": "nvme0",
|
||||
},
|
||||
map[string]interface{}{"temp": 36.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_sensor_2_min",
|
||||
"device": "nvme0",
|
||||
},
|
||||
map[string]interface{}{"temp": -273.15},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_sensor_2_max",
|
||||
"device": "nvme0",
|
||||
},
|
||||
map[string]interface{}{"temp": 65261.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon1 / temp1
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_composite_input",
|
||||
"device": "nvme1",
|
||||
},
|
||||
map[string]interface{}{"temp": 35.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_composite_alarm",
|
||||
"device": "nvme1",
|
||||
},
|
||||
map[string]interface{}{"active": false},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_composite_crit",
|
||||
"device": "nvme1",
|
||||
},
|
||||
map[string]interface{}{"temp": 84.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_composite_min",
|
||||
"device": "nvme1",
|
||||
},
|
||||
map[string]interface{}{"temp": -273.15},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_composite_max",
|
||||
"device": "nvme1",
|
||||
},
|
||||
map[string]interface{}{"temp": 81.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon1 / temp2
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_sensor_1_input",
|
||||
"device": "nvme1",
|
||||
},
|
||||
map[string]interface{}{"temp": 35.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_sensor_1_min",
|
||||
"device": "nvme1",
|
||||
},
|
||||
map[string]interface{}{"temp": -273.15},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_sensor_1_max",
|
||||
"device": "nvme1",
|
||||
},
|
||||
map[string]interface{}{"temp": 65261.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon1 / temp3
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_sensor_2_input",
|
||||
"device": "nvme1",
|
||||
},
|
||||
map[string]interface{}{"temp": 37.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_sensor_2_min",
|
||||
"device": "nvme1",
|
||||
},
|
||||
map[string]interface{}{"temp": -273.15},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "nvme_sensor_2_max",
|
||||
"device": "nvme1",
|
||||
},
|
||||
map[string]interface{}{"temp": 65261.85},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon2 / temp1
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "k10temp_tctl_input",
|
||||
"device": "0000:00:18.3",
|
||||
},
|
||||
map[string]interface{}{"temp": 31.875},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
// hwmon2 / temp3
|
||||
metric.New(
|
||||
"temp",
|
||||
map[string]string{
|
||||
"sensor": "k10temp_tccd1_input",
|
||||
"device": "0000:00:18.3",
|
||||
},
|
||||
map[string]interface{}{"temp": 30.75},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
func TestRegression(t *testing.T) {
|
||||
// Get all directories in testdata
|
||||
folders, err := os.ReadDir("testcases")
|
||||
require.NoError(t, err)
|
||||
|
||||
// Register the plugin
|
||||
inputs.Add("temp", func() telegraf.Input {
|
||||
return &Temperature{}
|
||||
})
|
||||
|
||||
// Prepare the influx parser for expectations
|
||||
parser := &influx.Parser{}
|
||||
require.NoError(t, parser.Init())
|
||||
|
||||
for _, f := range folders {
|
||||
// Only handle folders
|
||||
if !f.IsDir() {
|
||||
continue
|
||||
}
|
||||
// Compare options
|
||||
options := []cmp.Option{
|
||||
testutil.IgnoreTime(),
|
||||
testutil.SortMetrics(),
|
||||
}
|
||||
|
||||
// Test v1 metrics
|
||||
t.Run(f.Name()+"_v1", func(t *testing.T) {
|
||||
testcasePath := filepath.Join("testcases", f.Name())
|
||||
actualFilename := filepath.Join(testcasePath, "expected_v1.out")
|
||||
|
||||
// Read the expected output if any
|
||||
var actual []telegraf.Metric
|
||||
if _, err := os.Stat(actualFilename); err == nil {
|
||||
var err error
|
||||
actual, err = testutil.ParseMetricsFromFile(actualFilename, parser)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Remove potential device-tags
|
||||
for i := range actual {
|
||||
actual[i].RemoveTag("device")
|
||||
}
|
||||
|
||||
// Use the <v1.22.4 code to compare against
|
||||
temps, err := sensorsTemperaturesOld(filepath.Join(testcasePath, "sys"))
|
||||
require.NoError(t, err)
|
||||
|
||||
var acc testutil.Accumulator
|
||||
for _, temp := range temps {
|
||||
tags := map[string]string{
|
||||
"sensor": temp.SensorKey,
|
||||
}
|
||||
fields := map[string]interface{}{
|
||||
"temp": temp.Temperature,
|
||||
}
|
||||
acc.AddFields("temp", fields, tags)
|
||||
}
|
||||
|
||||
expected := acc.GetTelegrafMetrics()
|
||||
testutil.RequireMetricsEqual(t, expected, actual, options...)
|
||||
})
|
||||
|
||||
// Test v2 metrics
|
||||
t.Run(f.Name()+"_v2", func(t *testing.T) {
|
||||
testcasePath := filepath.Join("testcases", f.Name())
|
||||
actualFilename := filepath.Join(testcasePath, "expected_v2.out")
|
||||
|
||||
// Read the expected output if any
|
||||
var actual []telegraf.Metric
|
||||
if _, err := os.Stat(actualFilename); err == nil {
|
||||
var err error
|
||||
actual, err = testutil.ParseMetricsFromFile(actualFilename, parser)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Remove potential device-tags
|
||||
for i := range actual {
|
||||
actual[i].RemoveTag("device")
|
||||
}
|
||||
|
||||
// Prepare the environment
|
||||
require.NoError(t, os.Setenv("HOST_SYS", filepath.Join(testcasePath, "sys")))
|
||||
|
||||
// Use the v1.28.x code to compare against
|
||||
var acc testutil.Accumulator
|
||||
temps, err := host.SensorsTemperatures()
|
||||
require.NoError(t, err)
|
||||
for _, temp := range temps {
|
||||
tags := map[string]string{
|
||||
"sensor": temp.SensorKey,
|
||||
}
|
||||
fields := map[string]interface{}{
|
||||
"temp": temp.Temperature,
|
||||
}
|
||||
acc.AddFields("temp", fields, tags)
|
||||
}
|
||||
|
||||
expected := acc.GetTelegrafMetrics()
|
||||
testutil.RequireMetricsEqual(t, expected, actual, options...)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func sensorsTemperaturesOld(syspath string) ([]host.TemperatureStat, error) {
|
||||
files, err := filepath.Glob(syspath + "/class/hwmon/hwmon*/temp*_*")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(files) == 0 {
|
||||
// CentOS has an intermediate /device directory:
|
||||
// https://github.com/giampaolo/psutil/issues/971
|
||||
files, err = filepath.Glob(syspath + "/class/hwmon/hwmon*/device/temp*_*")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
require.NoError(t, os.Setenv("HOST_SYS", filepath.Join("testdata", "with_name", "sys")))
|
||||
plugin := &Temperature{
|
||||
DeviceTag: true,
|
||||
Log: &testutil.Logger{},
|
||||
}
|
||||
require.NoError(t, plugin.Init())
|
||||
if len(files) == 0 { // handle distributions without hwmon, like raspbian #391, parse legacy thermal_zone files
|
||||
files, err = filepath.Glob(syspath + "/class/thermal/thermal_zone*/")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var acc testutil.Accumulator
|
||||
require.NoError(t, plugin.Gather(&acc))
|
||||
actual := acc.GetTelegrafMetrics()
|
||||
testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime(), testutil.SortMetrics())
|
||||
temperatures := make([]host.TemperatureStat, 0, len(files))
|
||||
for _, file := range files {
|
||||
// Get the name of the temperature you are reading
|
||||
name, err := os.ReadFile(filepath.Join(file, "type"))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
// Get the temperature reading
|
||||
current, err := os.ReadFile(filepath.Join(file, "temp"))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
temperature, err := strconv.ParseInt(strings.TrimSpace(string(current)), 10, 64)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
temperatures = append(temperatures, host.TemperatureStat{
|
||||
SensorKey: strings.TrimSpace(string(name)),
|
||||
Temperature: float64(temperature) / 1000.0,
|
||||
})
|
||||
}
|
||||
return temperatures, nil
|
||||
}
|
||||
|
||||
// example directory
|
||||
// device/ temp1_crit_alarm temp2_crit_alarm temp3_crit_alarm temp4_crit_alarm temp5_crit_alarm temp6_crit_alarm temp7_crit_alarm
|
||||
// name temp1_input temp2_input temp3_input temp4_input temp5_input temp6_input temp7_input
|
||||
// power/ temp1_label temp2_label temp3_label temp4_label temp5_label temp6_label temp7_label
|
||||
// subsystem/ temp1_max temp2_max temp3_max temp4_max temp5_max temp6_max temp7_max
|
||||
// temp1_crit temp2_crit temp3_crit temp4_crit temp5_crit temp6_crit temp7_crit uevent
|
||||
temperatures := make([]host.TemperatureStat, 0, len(files))
|
||||
for _, file := range files {
|
||||
filename := strings.Split(filepath.Base(file), "_")
|
||||
if filename[1] == "label" {
|
||||
// Do not try to read the temperature of the label file
|
||||
continue
|
||||
}
|
||||
|
||||
// Get the label of the temperature you are reading
|
||||
var label string
|
||||
c, _ := os.ReadFile(filepath.Join(filepath.Dir(file), filename[0]+"_label"))
|
||||
if c != nil {
|
||||
//format the label from "Core 0" to "core0_"
|
||||
label = fmt.Sprintf("%s_", strings.Join(strings.Split(strings.TrimSpace(strings.ToLower(string(c))), " "), ""))
|
||||
}
|
||||
|
||||
// Get the name of the temperature you are reading
|
||||
name, err := os.ReadFile(filepath.Join(filepath.Dir(file), "name"))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Get the temperature reading
|
||||
current, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
temperature, err := strconv.ParseFloat(strings.TrimSpace(string(current)), 64)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
tempName := strings.TrimSpace(strings.ToLower(strings.Join(filename[1:], "")))
|
||||
temperatures = append(temperatures, host.TemperatureStat{
|
||||
SensorKey: fmt.Sprintf("%s_%s%s", strings.TrimSpace(string(name)), label, tempName),
|
||||
Temperature: temperature / 1000.0,
|
||||
})
|
||||
}
|
||||
return temperatures, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
temp,sensor=nvme_composite_input temp=35.85
|
||||
temp,sensor=nvme_composite_alarm temp=0
|
||||
temp,sensor=nvme_composite_crit temp=84.85
|
||||
temp,sensor=nvme_composite_max temp=81.85
|
||||
temp,sensor=nvme_composite_min temp=-273.15
|
||||
temp,sensor=nvme_sensor1_input temp=35.85
|
||||
temp,sensor=nvme_sensor1_min temp=-273.15
|
||||
temp,sensor=nvme_sensor1_max temp=65261.85
|
||||
temp,sensor=nvme_sensor2_input temp=38.85
|
||||
temp,sensor=nvme_sensor2_max temp=65261.85
|
||||
temp,sensor=nvme_sensor2_min temp=-273.15
|
||||
temp,sensor=k10temp_tctl_input temp=33.25
|
||||
temp,sensor=k10temp_tccd1_input temp=33.25
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
temp,sensor=nvme_composite temp=35.85
|
||||
temp,sensor=nvme_sensor_1 temp=35.85
|
||||
temp,sensor=nvme_sensor_2 temp=38.85
|
||||
temp,sensor=k10temp_tctl temp=33.25
|
||||
temp,sensor=k10temp_tccd1 temp=33.25
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
[[inputs.temp]]
|
||||
# Metric format will be set in the code
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
temp,device=nvme0,sensor=nvme_composite_input temp=32.85
|
||||
temp,device=nvme0,sensor=nvme_composite_alarm temp=0
|
||||
temp,device=nvme0,sensor=nvme_composite_crit temp=84.85
|
||||
temp,device=nvme0,sensor=nvme_composite_max temp=81.85
|
||||
temp,device=nvme0,sensor=nvme_composite_min temp=-273.15
|
||||
temp,device=nvme0,sensor=nvme_sensor1_input temp=32.85
|
||||
temp,device=nvme0,sensor=nvme_sensor1_max temp=65261.85
|
||||
temp,device=nvme0,sensor=nvme_sensor1_min temp=-273.15
|
||||
temp,device=nvme0,sensor=nvme_sensor2_input temp=36.85
|
||||
temp,device=nvme0,sensor=nvme_sensor2_max temp=65261.85
|
||||
temp,device=nvme0,sensor=nvme_sensor2_min temp=-273.15
|
||||
temp,device=nvme1,sensor=nvme_composite_input temp=35.85
|
||||
temp,device=nvme1,sensor=nvme_composite_alarm temp=0
|
||||
temp,device=nvme1,sensor=nvme_composite_crit temp=84.85
|
||||
temp,device=nvme1,sensor=nvme_composite_max temp=81.85
|
||||
temp,device=nvme1,sensor=nvme_composite_min temp=-273.15
|
||||
temp,device=nvme1,sensor=nvme_sensor1_input temp=35.85
|
||||
temp,device=nvme1,sensor=nvme_sensor1_max temp=65261.85
|
||||
temp,device=nvme1,sensor=nvme_sensor1_min temp=-273.15
|
||||
temp,device=nvme1,sensor=nvme_sensor2_input temp=37.85
|
||||
temp,device=nvme1,sensor=nvme_sensor2_max temp=65261.85
|
||||
temp,device=nvme1,sensor=nvme_sensor2_min temp=-273.15
|
||||
temp,device=0000:00:18.3,sensor=k10temp_tctl_input temp=31.875
|
||||
temp,device=0000:00:18.3,sensor=k10temp_tccd1_input temp=30.75
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
temp,device=nvme0,sensor=nvme_composite temp=32.85
|
||||
temp,device=nvme0,sensor=nvme_sensor_1 temp=32.85
|
||||
temp,device=nvme0,sensor=nvme_sensor_2 temp=36.85
|
||||
temp,device=nvme1,sensor=nvme_composite temp=35.85
|
||||
temp,device=nvme1,sensor=nvme_sensor_1 temp=35.85
|
||||
temp,device=nvme1,sensor=nvme_sensor_2 temp=37.85
|
||||
temp,device=0000:00:18.3,sensor=k10temp_tctl temp=31.875
|
||||
temp,device=0000:00:18.3,sensor=k10temp_tccd1 temp=30.75
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
[[inputs.temp]]
|
||||
add_device_tag = true
|
||||
# Metric format will be set in the code
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
temp,sensor=nvme_composite_input temp=32.85
|
||||
temp,sensor=nvme_composite_alarm temp=0
|
||||
temp,sensor=nvme_composite_crit temp=84.85
|
||||
temp,sensor=nvme_composite_max temp=81.85
|
||||
temp,sensor=nvme_composite_min temp=-273.15
|
||||
temp,sensor=nvme_sensor1_input temp=32.85
|
||||
temp,sensor=nvme_sensor1_max temp=65261.85
|
||||
temp,sensor=nvme_sensor1_min temp=-273.15
|
||||
temp,sensor=nvme_sensor2_input temp=36.85
|
||||
temp,sensor=nvme_sensor2_max temp=65261.85
|
||||
temp,sensor=nvme_sensor2_min temp=-273.15
|
||||
temp,sensor=nvme_composite_input temp=35.85
|
||||
temp,sensor=nvme_composite_alarm temp=0
|
||||
temp,sensor=nvme_composite_crit temp=84.85
|
||||
temp,sensor=nvme_composite_max temp=81.85
|
||||
temp,sensor=nvme_composite_min temp=-273.15
|
||||
temp,sensor=nvme_sensor1_input temp=35.85
|
||||
temp,sensor=nvme_sensor1_max temp=65261.85
|
||||
temp,sensor=nvme_sensor1_min temp=-273.15
|
||||
temp,sensor=nvme_sensor2_input temp=37.85
|
||||
temp,sensor=nvme_sensor2_max temp=65261.85
|
||||
temp,sensor=nvme_sensor2_min temp=-273.15
|
||||
temp,sensor=k10temp_tctl_input temp=31.875
|
||||
temp,sensor=k10temp_tccd1_input temp=30.75
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
temp,sensor=nvme_composite temp=32.85
|
||||
temp,sensor=nvme_sensor_1 temp=32.85
|
||||
temp,sensor=nvme_sensor_2 temp=36.85
|
||||
temp,sensor=nvme_composite temp=35.85
|
||||
temp,sensor=nvme_sensor_1 temp=35.85
|
||||
temp,sensor=nvme_sensor_2 temp=37.85
|
||||
temp,sensor=k10temp_tctl temp=31.875
|
||||
temp,sensor=k10temp_tccd1 temp=30.75
|
||||
|
|
@ -0,0 +1 @@
|
|||
../../nvme0/
|
||||
|
|
@ -0,0 +1 @@
|
|||
nvme
|
||||
|
|
@ -0,0 +1 @@
|
|||
0
|
||||
|
|
@ -0,0 +1 @@
|
|||
84850
|
||||
|
|
@ -0,0 +1 @@
|
|||
32850
|
||||
|
|
@ -0,0 +1 @@
|
|||
Composite
|
||||
|
|
@ -0,0 +1 @@
|
|||
81850
|
||||
|
|
@ -0,0 +1 @@
|
|||
-273150
|
||||
|
|
@ -0,0 +1 @@
|
|||
32850
|
||||
|
|
@ -0,0 +1 @@
|
|||
Sensor 1
|
||||
|
|
@ -0,0 +1 @@
|
|||
65261850
|
||||
|
|
@ -0,0 +1 @@
|
|||
-273150
|
||||
|
|
@ -0,0 +1 @@
|
|||
36850
|
||||
|
|
@ -0,0 +1 @@
|
|||
Sensor 2
|
||||
|
|
@ -0,0 +1 @@
|
|||
65261850
|
||||
|
|
@ -0,0 +1 @@
|
|||
-273150
|
||||
|
|
@ -0,0 +1 @@
|
|||
../../nvme1/
|
||||
|
|
@ -0,0 +1 @@
|
|||
nvme
|
||||
|
|
@ -0,0 +1 @@
|
|||
0
|
||||
|
|
@ -0,0 +1 @@
|
|||
84850
|
||||
|
|
@ -0,0 +1 @@
|
|||
35850
|
||||
|
|
@ -0,0 +1 @@
|
|||
Composite
|
||||
|
|
@ -0,0 +1 @@
|
|||
81850
|
||||
|
|
@ -0,0 +1 @@
|
|||
-273150
|
||||
|
|
@ -0,0 +1 @@
|
|||
35850
|
||||
|
|
@ -0,0 +1 @@
|
|||
Sensor 1
|
||||
|
|
@ -0,0 +1 @@
|
|||
65261850
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue