2021-11-25 18:19:09 +01:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
2021-11-29 15:32:58 +01:00
|
|
|
"encoding/json"
|
2021-11-25 18:19:09 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-02-11 17:17:10 +01:00
|
|
|
|
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
|
2022-01-25 15:37:43 +01:00
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2021-11-25 18:19:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const HWMON_PATH = `/sys/class/hwmon`
|
|
|
|
|
|
|
|
type TempCollectorConfig struct {
|
2021-11-29 15:32:58 +01:00
|
|
|
ExcludeMetrics []string `json:"exclude_metrics"`
|
|
|
|
TagOverride map[string]map[string]string `json:"tag_override"`
|
2021-11-25 18:19:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type TempCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
2022-02-11 17:17:10 +01:00
|
|
|
config TempCollectorConfig
|
|
|
|
sensors map[string]string
|
2021-11-25 18:19:09 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *TempCollector) Init(config json.RawMessage) error {
|
2021-11-25 18:19:09 +01:00
|
|
|
m.name = "TempCollector"
|
|
|
|
m.setup()
|
2022-01-25 15:37:43 +01:00
|
|
|
m.meta = map[string]string{"source": m.name, "group": "IPMI", "unit": "degC"}
|
2021-11-25 18:19:09 +01:00
|
|
|
if len(config) > 0 {
|
|
|
|
err := json.Unmarshal(config, &m.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 17:17:10 +01:00
|
|
|
// Find all temperature sensor files
|
|
|
|
m.sensors = make(map[string]string)
|
|
|
|
globPattern := filepath.Join(HWMON_PATH, "*", "temp*_input")
|
|
|
|
inputFiles, err := filepath.Glob(globPattern)
|
2021-11-25 18:19:09 +01:00
|
|
|
if err != nil {
|
2022-02-11 17:17:10 +01:00
|
|
|
return fmt.Errorf("Unable to glob files with pattern '%s': %v", globPattern, err)
|
|
|
|
}
|
|
|
|
if inputFiles == nil {
|
|
|
|
return fmt.Errorf("Unable to find any files with pattern '%s'", globPattern)
|
2021-11-25 18:19:09 +01:00
|
|
|
}
|
|
|
|
|
2022-02-11 17:17:10 +01:00
|
|
|
// Get sensor name for each temperature sensor file
|
|
|
|
for _, file := range inputFiles {
|
2022-02-12 10:13:26 +01:00
|
|
|
nameFile := filepath.Join(filepath.Dir(file), "name")
|
|
|
|
name := ""
|
|
|
|
n, err := ioutil.ReadFile(nameFile)
|
|
|
|
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:
|
2021-11-25 18:19:09 +01:00
|
|
|
continue
|
2022-02-12 10:13:26 +01:00
|
|
|
case len(name) != 0 && len(label) != 0:
|
|
|
|
metricName = name + "_" + label
|
|
|
|
case len(name) != 0:
|
|
|
|
metricName = name
|
|
|
|
case len(label) != 0:
|
|
|
|
metricName = label
|
2021-11-25 18:19:09 +01:00
|
|
|
}
|
2022-02-11 17:17:10 +01:00
|
|
|
metricName = strings.Replace(metricName, " ", "_", -1)
|
|
|
|
if !strings.Contains(metricName, "temp") {
|
|
|
|
metricName = "temp_" + metricName
|
|
|
|
}
|
|
|
|
metricName = strings.ToLower(metricName)
|
|
|
|
m.sensors[metricName] = file
|
2021-11-25 18:19:09 +01:00
|
|
|
}
|
2022-02-11 17:17:10 +01:00
|
|
|
|
2022-02-12 10:13:26 +01:00
|
|
|
// Empty sensors map
|
|
|
|
if len(m.sensors) == 0 {
|
|
|
|
return fmt.Errorf("No temperature sensors found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finished initialization
|
2022-02-11 17:17:10 +01:00
|
|
|
m.init = true
|
|
|
|
return nil
|
2021-11-25 18:19:09 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *TempCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
2021-11-25 18:19:09 +01:00
|
|
|
|
2022-02-11 17:17:10 +01:00
|
|
|
for metricName, file := range m.sensors {
|
|
|
|
tags := map[string]string{"type": "node"}
|
|
|
|
for key, newtags := range m.config.TagOverride {
|
|
|
|
if strings.Contains(file, key) {
|
|
|
|
tags = newtags
|
|
|
|
break
|
2021-11-25 18:19:09 +01:00
|
|
|
}
|
2022-02-11 17:17:10 +01:00
|
|
|
}
|
|
|
|
buffer, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
x, err := strconv.ParseInt(strings.TrimSpace(string(buffer)), 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
y, err := lp.New(metricName, tags, m.meta, map[string]interface{}{"value": int(float64(x) / 1000)}, time.Now())
|
2021-11-25 18:19:09 +01:00
|
|
|
if err == nil {
|
2022-02-11 17:17:10 +01:00
|
|
|
cclog.ComponentDebug(m.name, y)
|
|
|
|
output <- y
|
2021-11-25 18:19:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 17:17:10 +01:00
|
|
|
|
2021-11-25 18:19:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TempCollector) Close() {
|
|
|
|
m.init = false
|
|
|
|
}
|