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(inputs.temp): Fix regression in metric formats #14649

Merged
merged 9 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 44 additions & 42 deletions plugins/inputs/temp/temp_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,55 @@ func (t *Temperature) Gather(acc telegraf.Accumulator) error {
}
}

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 {
acc.AddFields(
"temp",
map[string]interface{}{"temp": temp.Temperature},
t.getTagsForTemperature(temp, "_input"),
)
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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Loading
Loading