Use sensor name and sensor label as metric name

This commit is contained in:
Holger Obermaier 2022-02-12 10:13:26 +01:00
parent bd246bdacf
commit 6b12baff6e

View File

@ -50,12 +50,29 @@ func (m *TempCollector) Init(config json.RawMessage) error {
// Get sensor name for each temperature sensor file // Get sensor name for each temperature sensor file
for _, file := range inputFiles { for _, file := range inputFiles {
nameFile := strings.TrimSuffix(file, "_input") + "_label" nameFile := filepath.Join(filepath.Dir(file), "name")
name, err := ioutil.ReadFile(nameFile) name := ""
if err != nil { n, err := ioutil.ReadFile(nameFile)
continue if err == nil {
name = strings.TrimSpace(string(n))
}
labelFile := strings.TrimSuffix(file, "_input") + "_label"
label := ""
l, err := ioutil.ReadFile(labelFile)
if err == nil {
label = strings.TrimSpace(string(l))
}
metricName := ""
switch {
case len(name) == 0 && len(label) == 0:
continue
case len(name) != 0 && len(label) != 0:
metricName = name + "_" + label
case len(name) != 0:
metricName = name
case len(label) != 0:
metricName = label
} }
metricName := strings.TrimSpace(string(name))
metricName = strings.Replace(metricName, " ", "_", -1) metricName = strings.Replace(metricName, " ", "_", -1)
if !strings.Contains(metricName, "temp") { if !strings.Contains(metricName, "temp") {
metricName = "temp_" + metricName metricName = "temp_" + metricName
@ -64,6 +81,12 @@ func (m *TempCollector) Init(config json.RawMessage) error {
m.sensors[metricName] = file m.sensors[metricName] = file
} }
// Empty sensors map
if len(m.sensors) == 0 {
return fmt.Errorf("No temperature sensors found")
}
// Finished initialization
m.init = true m.init = true
return nil return nil
} }